Skip to content

Commit 589affd

Browse files
GrayJackmaxbrunsfeld
authored andcommitted
Macro repetition pattern '?' (#62)
* Allow zero-or-one repetition pattern * Update test to include new repetition pattern
1 parent f2be62e commit 589affd

File tree

5 files changed

+9570
-9429
lines changed

5 files changed

+9570
-9429
lines changed

corpus/macros.txt

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,80 @@ macro_rules! o_O {
6565
}
6666
}
6767

68+
macro_rules! zero_or_one {
69+
($($e:expr),?) => {
70+
$($e),?
71+
};
72+
}
73+
6874
----
6975

7076
(source_file
71-
(macro_definition (identifier)
77+
(macro_definition
78+
name: (identifier)
7279
(macro_rule
73-
(token_tree_pattern)
74-
(token_tree (identifier) (token_tree (string_literal)))))
75-
(macro_definition (identifier)
80+
left: (token_tree_pattern)
81+
right: (token_tree
82+
(identifier)
83+
(token_tree
84+
(string_literal)))))
85+
(macro_definition
86+
name: (identifier)
7687
(macro_rule
77-
(token_tree_pattern)
78-
(token_tree (integer_literal) (integer_literal))))
79-
(macro_definition (identifier)
88+
left: (token_tree_pattern)
89+
right: (token_tree
90+
(integer_literal)
91+
(integer_literal))))
92+
(macro_definition
93+
name: (identifier)
8094
(macro_rule
81-
(token_tree_pattern (identifier) (token_binding_pattern (metavariable) (fragment_specifier)))
82-
(token_tree
83-
(identifier) (token_tree (string_literal) (metavariable))))
95+
left: (token_tree_pattern
96+
(identifier)
97+
(token_binding_pattern
98+
name: (metavariable)
99+
type: (fragment_specifier)))
100+
right: (token_tree
101+
(identifier)
102+
(token_tree
103+
(string_literal)
104+
(metavariable))))
84105
(macro_rule
85-
(token_tree_pattern
106+
left: (token_tree_pattern
107+
(identifier)
108+
(token_binding_pattern
109+
name: (metavariable)
110+
type: (fragment_specifier)))
111+
right: (token_tree
86112
(identifier)
87-
(token_binding_pattern (metavariable) (fragment_specifier)))
88-
(token_tree
89-
(identifier) (token_tree (string_literal) (metavariable)))))
90-
(macro_definition (identifier)
113+
(token_tree
114+
(string_literal)
115+
(metavariable)))))
116+
(macro_definition
117+
name: (identifier)
118+
(macro_rule
119+
left: (token_tree_pattern
120+
(token_repetition_pattern
121+
(token_binding_pattern
122+
name: (metavariable)
123+
type: (fragment_specifier))
124+
(token_tree_pattern
125+
(token_repetition_pattern
126+
(token_binding_pattern
127+
name: (metavariable)
128+
type: (fragment_specifier))))))
129+
right: (token_tree
130+
(token_repetition
131+
(token_repetition
132+
(metavariable)
133+
(metavariable))))))
134+
(macro_definition
135+
name: (identifier)
91136
(macro_rule
92-
(token_tree_pattern (token_repetition_pattern
93-
(token_binding_pattern (metavariable) (fragment_specifier))
94-
(token_tree_pattern (token_repetition_pattern
95-
(token_binding_pattern (metavariable) (fragment_specifier))))))
96-
(token_tree (token_repetition (token_repetition (metavariable) (metavariable)))))))
137+
left: (token_tree_pattern
138+
(token_repetition_pattern
139+
(token_binding_pattern
140+
name: (metavariable)
141+
type: (fragment_specifier))))
142+
right: (token_tree
143+
(token_repetition
144+
(metavariable))))))

grammar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ module.exports = grammar({
163163
)),
164164

165165
token_repetition_pattern: $ => seq(
166-
'$', '(', repeat($._token_pattern), ')', optional(/[^+*]+/), choice('+', '*')
166+
'$', '(', repeat($._token_pattern), ')', optional(/[^+*?]+/), choice('+', '*', '?')
167167
),
168168

169169
fragment_specifier: $ => choice(
@@ -183,7 +183,7 @@ module.exports = grammar({
183183
),
184184

185185
token_repetition: $ => seq(
186-
'$', '(', repeat($._tokens), ')', optional(/[^+*]+/), choice('+', '*')
186+
'$', '(', repeat($._tokens), ')', optional(/[^+*?]+/), choice('+', '*', '?')
187187
),
188188

189189
_non_special_token: $ => choice(

src/grammar.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@
426426
"members": [
427427
{
428428
"type": "PATTERN",
429-
"value": "[^+*]+"
429+
"value": "[^+*?]+"
430430
},
431431
{
432432
"type": "BLANK"
@@ -443,6 +443,10 @@
443443
{
444444
"type": "STRING",
445445
"value": "*"
446+
},
447+
{
448+
"type": "STRING",
449+
"value": "?"
446450
}
447451
]
448452
}
@@ -602,7 +606,7 @@
602606
"members": [
603607
{
604608
"type": "PATTERN",
605-
"value": "[^+*]+"
609+
"value": "[^+*?]+"
606610
},
607611
{
608612
"type": "BLANK"
@@ -619,6 +623,10 @@
619623
{
620624
"type": "STRING",
621625
"value": "*"
626+
},
627+
{
628+
"type": "STRING",
629+
"value": "?"
622630
}
623631
]
624632
}
@@ -735,7 +743,7 @@
735743
},
736744
{
737745
"type": "PATTERN",
738-
"value": "[\\/_\\-=->,;:::!=?.@*=\\/='&=#%=^=+<>|~]+"
746+
"value": "[/_\\-=->,;:::!=?.@*=/='&=#%=^=+<>|~]+"
739747
},
740748
{
741749
"type": "STRING",

0 commit comments

Comments
 (0)