Skip to content

Commit 35f6c62

Browse files
committed
[optional]
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
1 parent ffa9407 commit 35f6c62

6 files changed

+19
-27
lines changed

corpus/argument.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ nvim_buf_detach_event[{buf}]
117117
(word))
118118
(line
119119
(word)
120-
(optional_arg
121-
(word)))))
120+
(word)
121+
(argument
122+
(word))
123+
(word))))
122124

123125
================================================================================
124126
NOT an argument

corpus/argument_optional.txt

+8-16
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,22 @@ optional [argument]
1111
(block
1212
(line
1313
(word)
14-
(optional_arg
15-
(word))
14+
(optional)
1615
(word)
17-
(optional_arg
18-
(word))
19-
(optional_arg
20-
(word))
16+
(optional)
17+
(optional)
2118
(argument
2219
(word))
2320
(tag
2421
(word)))
2522
(line
2623
(word)
27-
(optional_arg
28-
(word))
24+
(optional)
2925
(word)
30-
(optional_arg
31-
(word))
32-
(optional_arg
33-
(word))
34-
(optional_arg
35-
(word))
36-
(optional_arg
37-
(word))
26+
(optional)
27+
(optional)
28+
(optional)
29+
(optional)
3830
(argument
3931
(word))
4032
(word))))

corpus/codeblock.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ codeblock stop and start on same line
398398
(line
399399
(word)
400400
(word)
401-
(optional_arg
402-
(word)))
401+
(optional))
403402
(line
404403
(argument
405404
(word))))

corpus/taglink.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ Hello |world| hello
5656
(taglink
5757
(word))
5858
(word)
59-
(optional_arg
60-
(word))
59+
(optional)
6160
(word))
6261
(line
6362
(taglink

corpus/url.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ markdown: [https://neovim.io/doc/user/#yay](https://neovim.io/doc/user/#yay).
5454
(word))
5555
(line
5656
(word)
57-
(optional_arg
58-
(word))
57+
(optional)
5958
(word)
6059
(url
6160
(word))

grammar.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = grammar({
5353
$.taglink,
5454
$.codespan,
5555
$.argument,
56-
$.optional_arg,
56+
$.optional,
5757
$.keycode,
5858
),
5959

@@ -96,6 +96,9 @@ module.exports = grammar({
9696
/ALT-./,
9797
),
9898

99+
// Optional argument: [arg] (no whitespace allowed)
100+
optional: () => /\[[^\]{\n\t ]+\]/,
101+
99102
// First part (minus tags) of h3 or column_heading.
100103
uppercase_name: () => seq(
101104
token.immediate(_uppercase_word), // No whitespace before heading.
@@ -211,8 +214,6 @@ module.exports = grammar({
211214
codespan: ($) => _word($, /[^``\n]+/, '`', '`'),
212215
// Argument: {arg} (no whitespace allowed)
213216
argument: ($) => _word($, /[^}\n\t ]+/, '{', '}'),
214-
// Optional argument: [arg] (no whitespace allowed)
215-
optional_arg: ($) => _word($, /[^\]\n\t ]+/, '[', ']'),
216217
},
217218
});
218219

0 commit comments

Comments
 (0)