Skip to content

Commit

Permalink
[optional]
Browse files Browse the repository at this point in the history
Problem:
Using _word() to define an optional arg leads to a lot of syntax errors in
real-world :help files because [brackets] are commonly used for non-args.

Solution:
Define (optional) with a regex, like (keycode) instead of using the more
"formal" mechanism. Regex has "best-effort" behavior, while seq() behaves more
strictly.

Don't treat `[{arg}]` as (optional), else it clobbers the nested `{arg}`.

fix #1
  • Loading branch information
justinmk committed Jun 26, 2023
1 parent 72fe166 commit a6a7780
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
6 changes: 4 additions & 2 deletions corpus/argument.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ nvim_buf_detach_event[{buf}]
(word))
(line
(word)
(optional_arg
(word)))))
(word)
(argument
(word))
(word))))

================================================================================
NOT an argument
Expand Down
24 changes: 8 additions & 16 deletions corpus/argument_optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,22 @@ optional [argument]
(block
(line
(word)
(optional_arg
(word))
(optional)
(word)
(optional_arg
(word))
(optional_arg
(word))
(optional)
(optional)
(argument
(word))
(tag
(word)))
(line
(word)
(optional_arg
(word))
(optional)
(word)
(optional_arg
(word))
(optional_arg
(word))
(optional_arg
(word))
(optional_arg
(word))
(optional)
(optional)
(optional)
(optional)
(argument
(word))
(word))))
3 changes: 1 addition & 2 deletions corpus/codeblock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ codeblock stop and start on same line
(line
(word)
(word)
(optional_arg
(word)))
(optional))
(line
(argument
(word))))
Expand Down
3 changes: 1 addition & 2 deletions corpus/taglink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ Hello |world| hello
(taglink
(word))
(word)
(optional_arg
(word))
(optional)
(word))
(line
(taglink
Expand Down
3 changes: 1 addition & 2 deletions corpus/url.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ markdown: [https://neovim.io/doc/user/#yay](https://neovim.io/doc/user/#yay).
(word))
(line
(word)
(optional_arg
(word))
(optional)
(word)
(url
(word))
Expand Down
7 changes: 4 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = grammar({
$.taglink,
$.codespan,
$.argument,
$.optional_arg,
$.optional,
$.keycode,
),

Expand Down Expand Up @@ -96,6 +96,9 @@ module.exports = grammar({
/ALT-./,
),

// Optional argument: [arg] (no whitespace allowed)
optional: () => /\[[^\]{\n\t ]+\]/,

// First part (minus tags) of h3 or column_heading.
uppercase_name: () => seq(
token.immediate(_uppercase_word), // No whitespace before heading.
Expand Down Expand Up @@ -211,8 +214,6 @@ module.exports = grammar({
codespan: ($) => _word($, /[^``\n]+/, '`', '`'),
// Argument: {arg} (no whitespace allowed)
argument: ($) => _word($, /[^}\n\t ]+/, '{', '}'),
// Optional argument: [arg] (no whitespace allowed)
optional_arg: ($) => _word($, /[^\]\n\t ]+/, '[', ']'),
},
});

Expand Down

0 comments on commit a6a7780

Please sign in to comment.