@@ -50,18 +50,6 @@ interface CreateConfigContext extends CreateConfigOptions {
50
50
* The config being created
51
51
*/
52
52
config : PureConfig ;
53
-
54
- /**
55
- * The name of the rule currently being parsed.
56
- * Used for group names.
57
- */
58
- currentRule ?: string ;
59
-
60
- /**
61
- * The number of groups in the current rule.
62
- * Used for group names.
63
- */
64
- groups : number ;
65
53
}
66
54
67
55
/**
@@ -96,7 +84,7 @@ function config_process_directive(text: string, $: CreateConfigContext) {
96
84
}
97
85
log ( 1 , 'Including: ' + contents ) ;
98
86
for ( const node of $ . include ( contents ) ) {
99
- config_process_node ( node , child_context ( $ ) ) ;
87
+ config_process_node ( child_context ( $ ) , node ) ;
100
88
}
101
89
break ;
102
90
// ##flags <rule> <flags>
@@ -147,8 +135,31 @@ function config_process_directive(text: string, $: CreateConfigContext) {
147
135
}
148
136
}
149
137
150
- function config_process_expression ( expression : Node , $ : CreateConfigContext ) : [ DefinitionPart [ ] , boolean ] {
151
- const _sub = child_context ( $ ) ;
138
+ /**
139
+ * Info about a rule that is used when parsing an expression
140
+ */
141
+ interface RuleInfo {
142
+ /**
143
+ * The name of the rule being parsed.
144
+ * Used for group names.
145
+ */
146
+ name ?: string ;
147
+
148
+ /**
149
+ * The number of groups in the rule.
150
+ * Used for group names.
151
+ */
152
+ groups : number ;
153
+ }
154
+
155
+ /**
156
+ * An expression parsed by `config_process_expression`.
157
+ * This might be change to an object later.
158
+ */
159
+ type ParsedExpression = [ pattern : DefinitionPart [ ] , isAlternation : boolean ] ;
160
+
161
+ function config_process_expression ( $ : CreateConfigContext , expression : Node , rule : RuleInfo ) : ParsedExpression {
162
+ const sub_context = child_context ( $ ) ;
152
163
153
164
let isAlternation = false ;
154
165
@@ -166,7 +177,7 @@ function config_process_expression(expression: Node, $: CreateConfigContext): [D
166
177
if ( term . kind == 'expression_continue' || term . kind == 'expression#0' ) {
167
178
_log ( 2 , 'Found expression_continue' ) ;
168
179
let next ;
169
- [ next , isAlternation ] = config_process_expression ( term , _sub ) ;
180
+ [ next , isAlternation ] = config_process_expression ( sub_context , term , rule ) ;
170
181
pattern . push ( ...next ) ;
171
182
continue ;
172
183
}
@@ -221,7 +232,7 @@ function config_process_expression(expression: Node, $: CreateConfigContext): [D
221
232
222
233
const type = typeForGroup [ node . kind ] ;
223
234
224
- const [ subPattern , isAlternation ] = config_process_expression ( inner , _sub ) ;
235
+ const [ subPattern , isAlternation ] = config_process_expression ( sub_context , inner , rule ) ;
225
236
226
237
// Check if subPattern contains another rule name, if so, no need to create a new group
227
238
const existing = subPattern . length == 1 && subPattern [ 0 ] . kind !== 'string' ? subPattern [ 0 ] . kind : null ;
@@ -230,7 +241,7 @@ function config_process_expression(expression: Node, $: CreateConfigContext): [D
230
241
break ;
231
242
}
232
243
233
- const subName = `${ $ . currentRule } #${ $ . groups ++ } ` ;
244
+ const subName = `${ rule . name } #${ rule . groups ++ } ` ;
234
245
235
246
$ . config . definitions . push ( {
236
247
name : subName ,
@@ -253,22 +264,22 @@ function config_process_expression(expression: Node, $: CreateConfigContext): [D
253
264
return [ pattern , isAlternation ] ;
254
265
}
255
266
256
- function config_process_node ( node : Node , $ : CreateConfigContext ) {
257
- const _sub = child_context ( $ ) ;
267
+ function config_process_node ( $ : CreateConfigContext , node : Node ) {
268
+ const sub_context = child_context ( $ ) ;
258
269
259
270
const log = logger ( $ . log , { kind : node . kind , depth : $ . depth } ) ;
260
271
261
272
log ( 3 , `Processing ${ node . kind } at ${ node . line } :${ node . column } ` ) ;
262
273
263
274
switch ( node . kind ) {
264
275
case 'directive' :
265
- config_process_directive ( node . text , _sub ) ;
276
+ config_process_directive ( node . text , sub_context ) ;
266
277
}
267
278
268
279
if ( node . kind != 'rule' ) {
269
280
// Recursively process child nodes
270
281
for ( const child of node . children || [ ] ) {
271
- config_process_node ( child , _sub ) ;
282
+ config_process_node ( sub_context , child ) ;
272
283
}
273
284
return ;
274
285
}
@@ -283,10 +294,7 @@ function config_process_node(node: Node, $: CreateConfigContext) {
283
294
return ;
284
295
}
285
296
286
- $ . currentRule = name ;
287
- $ . groups = 0 ;
288
-
289
- const [ pattern , isAlternation ] = config_process_expression ( expression , _sub ) ;
297
+ const [ pattern , isAlternation ] = config_process_expression ( sub_context , expression , { name, groups : 0 } ) ;
290
298
291
299
/*
292
300
Inline single-use literals
@@ -341,7 +349,7 @@ export function create_config(ast: Node[], options: CreateConfigOptions): Config
341
349
342
350
// Start processing from the root node
343
351
for ( const node of ast ) {
344
- config_process_node ( node , context ) ;
352
+ config_process_node ( context , node ) ;
345
353
}
346
354
347
355
if ( ! config . root_nodes ) {
0 commit comments