Skip to content

Commit b82ab80

Browse files
committed
refactor!: rework type parameters and interface type elements
1 parent e395081 commit b82ab80

File tree

5 files changed

+186
-148
lines changed

5 files changed

+186
-148
lines changed

grammar.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ module.exports = grammar({
8282
$._package_identifier,
8383
$._top_level_declaration,
8484
$._string_literal,
85+
$._interface_elem,
8586
],
8687

8788
word: $ => $.identifier,
@@ -91,10 +92,10 @@ module.exports = grammar({
9192
[$._simple_type, $.generic_type, $._expression],
9293
[$.qualified_type, $._expression],
9394
[$.generic_type, $._simple_type],
94-
[$.parameter_declaration, $._simple_type, $.generic_type, $._expression],
95-
[$.parameter_declaration, $._simple_type, $._expression],
96-
[$.parameter_declaration, $._expression],
9795
[$.parameter_declaration, $._simple_type],
96+
[$.type_parameter_declaration, $._simple_type, $._expression],
97+
[$.type_parameter_declaration, $._expression],
98+
[$.type_parameter_declaration, $._simple_type, $.generic_type, $._expression],
9899
],
99100

100101
supertypes: $ => [
@@ -227,11 +228,16 @@ module.exports = grammar({
227228

228229
type_parameter_list: $ => seq(
229230
'[',
230-
commaSep1($.parameter_declaration),
231+
commaSep1($.type_parameter_declaration),
231232
optional(','),
232233
']',
233234
),
234235

236+
type_parameter_declaration: $ => seq(
237+
commaSep1(field('name', $.identifier)),
238+
field('type', alias($.type_elem, $.type_constraint)),
239+
),
240+
235241
parameter_list: $ => seq(
236242
'(',
237243
optional(seq(
@@ -300,18 +306,17 @@ module.exports = grammar({
300306
$.map_type,
301307
$.channel_type,
302308
$.function_type,
303-
$.union_type,
304309
$.negated_type,
305310
),
306311

307312
generic_type: $ => prec.dynamic(1, seq(
308-
field('type', choice($._type_identifier, $.qualified_type, $.union_type, $.negated_type)),
313+
field('type', choice($._type_identifier, $.qualified_type, $.negated_type)),
309314
field('type_arguments', $.type_arguments),
310315
)),
311316

312317
type_arguments: $ => prec.dynamic(2, seq(
313318
'[',
314-
commaSep1($._type),
319+
commaSep1($.type_elem),
315320
optional(','),
316321
']',
317322
)),
@@ -343,12 +348,6 @@ module.exports = grammar({
343348
$.field_declaration_list,
344349
),
345350

346-
union_type: $ => prec.left(seq(
347-
$._type,
348-
'|',
349-
$._type,
350-
)),
351-
352351
negated_type: $ => prec.left(seq(
353352
'~',
354353
$._type,
@@ -386,35 +385,26 @@ module.exports = grammar({
386385
'interface',
387386
'{',
388387
optional(seq(
389-
$._interface_body,
390-
repeat(seq(terminator, $._interface_body)),
388+
$._interface_elem,
389+
repeat(seq(terminator, $._interface_elem)),
391390
optional(terminator),
392391
)),
393392
'}',
394393
),
395394

396-
_interface_body: $ => choice(
397-
$.method_spec,
398-
$.struct_elem,
399-
alias($._simple_type, $.constraint_elem),
400-
),
401-
402-
struct_elem: $ => seq(
403-
$.struct_term,
404-
repeat(seq('|', $.struct_term)),
395+
_interface_elem: $ => choice(
396+
$.method_elem,
397+
$.type_elem,
405398
),
406399

407-
struct_term: $ => prec(1, seq(
408-
optional(choice('~', '*')),
409-
$.struct_type,
410-
)),
411-
412-
method_spec: $ => seq(
400+
method_elem: $ => seq(
413401
field('name', $._field_identifier),
414402
field('parameters', $.parameter_list),
415403
field('result', optional(choice($.parameter_list, $._simple_type))),
416404
),
417405

406+
type_elem: $ => sep1($._type, '|'),
407+
418408
map_type: $ => prec.right(seq(
419409
'map',
420410
'[',
@@ -929,6 +919,20 @@ module.exports = grammar({
929919
},
930920
});
931921

922+
/**
923+
* Creates a rule to match one or more occurrences of `rule` separated by `sep`
924+
*
925+
* @param {RuleOrLiteral} rule
926+
*
927+
* @param {RuleOrLiteral} separator
928+
*
929+
* @return {SeqRule}
930+
*
931+
*/
932+
function sep1(rule, separator) {
933+
return seq(rule, repeat(seq(separator, rule)));
934+
}
935+
932936
/**
933937
* Creates a rule to match one or more of the rules separated by a comma
934938
*

test/corpus/declarations.txt

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -304,56 +304,68 @@ func f2(a File, b, c, d Thing) int {}
304304
(function_declaration
305305
(identifier)
306306
(type_parameter_list
307-
(parameter_declaration
308-
(identifier)
307+
(type_parameter_declaration
309308
(identifier)
310-
(type_identifier))
311-
(parameter_declaration
312309
(identifier)
313-
(interface_type))
314-
(parameter_declaration
315-
(identifier)
316-
(generic_type
317-
(type_identifier)
318-
(type_arguments
319-
(generic_type
320-
(type_identifier)
321-
(type_arguments
322-
(type_identifier)))))))
310+
(type_constraint
311+
(type_identifier)))
312+
(type_parameter_declaration
313+
(identifier)
314+
(type_constraint
315+
(interface_type)))
316+
(type_parameter_declaration
317+
(identifier)
318+
(type_constraint
319+
(generic_type
320+
(type_identifier)
321+
(type_arguments
322+
(type_elem
323+
(generic_type
324+
(type_identifier)
325+
(type_arguments
326+
(type_elem
327+
(type_identifier))))))))))
323328
(parameter_list
324329
(parameter_declaration
325330
(identifier)
326331
(generic_type
327332
(type_identifier)
328333
(type_arguments
329-
(type_identifier)))))
334+
(type_elem
335+
(type_identifier))))))
330336
(block))
331337
(function_declaration
332338
(identifier)
333339
(type_parameter_list
334-
(parameter_declaration
335-
(identifier)
336-
(identifier)
337-
(type_identifier))
338-
(parameter_declaration
340+
(type_parameter_declaration
339341
(identifier)
340-
(interface_type))
341-
(parameter_declaration
342342
(identifier)
343-
(generic_type
344-
(type_identifier)
345-
(type_arguments
346-
(generic_type
347-
(type_identifier)
348-
(type_arguments
349-
(type_identifier)))))))
343+
(type_constraint
344+
(type_identifier)))
345+
(type_parameter_declaration
346+
(identifier)
347+
(type_constraint
348+
(interface_type)))
349+
(type_parameter_declaration
350+
(identifier)
351+
(type_constraint
352+
(generic_type
353+
(type_identifier)
354+
(type_arguments
355+
(type_elem
356+
(generic_type
357+
(type_identifier)
358+
(type_arguments
359+
(type_elem
360+
(type_identifier))))))))))
350361
(parameter_list
351362
(parameter_declaration
352363
(identifier)
353364
(generic_type
354365
(type_identifier)
355366
(type_arguments
356-
(type_identifier)))))
367+
(type_elem
368+
(type_identifier))))))
357369
(block))
358370
(function_declaration
359371
(identifier)
@@ -365,23 +377,25 @@ func f2(a File, b, c, d Thing) int {}
365377
(package_identifier)
366378
(type_identifier))
367379
(type_arguments
368-
(type_identifier)))))
380+
(type_elem
381+
(type_identifier))))))
369382
(block))
370383
(function_declaration
371384
(identifier)
372385
(type_parameter_list
373-
(parameter_declaration
386+
(type_parameter_declaration
374387
(identifier)
375-
(union_type
388+
(type_constraint
376389
(type_identifier)
377390
(type_identifier)))
378-
(parameter_declaration
391+
(type_parameter_declaration
379392
(identifier)
380-
(negated_type
381-
(type_identifier)))
382-
(parameter_declaration
393+
(type_constraint
394+
(negated_type
395+
(type_identifier))))
396+
(type_parameter_declaration
383397
(identifier)
384-
(union_type
398+
(type_constraint
385399
(negated_type
386400
(type_identifier))
387401
(negated_type

test/corpus/expressions.txt

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,39 @@ func main() {
101101
(parameter_list)
102102
(block
103103
(expression_statement
104-
(call_expression
105-
(identifier)
106-
(type_arguments
107-
(type_identifier))
108-
(argument_list
109-
(identifier))))
104+
(type_conversion_expression
105+
(generic_type
106+
(type_identifier)
107+
(type_arguments
108+
(type_elem
109+
(type_identifier))))
110+
(identifier)))
110111
(expression_statement
111-
(call_expression
112-
(identifier)
113-
(type_arguments
112+
(type_conversion_expression
113+
(generic_type
114114
(type_identifier)
115-
(type_identifier))
116-
(argument_list
117-
(identifier))))
115+
(type_arguments
116+
(type_elem
117+
(type_identifier))
118+
(type_elem
119+
(type_identifier))))
120+
(identifier)))
118121
(expression_statement
119-
(call_expression
120-
(identifier)
121-
(type_arguments
122-
(generic_type
123-
(type_identifier)
124-
(type_arguments
125-
(type_identifier)))
126-
(type_identifier))
127-
(argument_list
128-
(index_expression
129-
(identifier)
130-
(identifier))))))))
122+
(type_conversion_expression
123+
(generic_type
124+
(type_identifier)
125+
(type_arguments
126+
(type_elem
127+
(generic_type
128+
(type_identifier)
129+
(type_arguments
130+
(type_elem
131+
(type_identifier)))))
132+
(type_elem
133+
(type_identifier))))
134+
(index_expression
135+
(identifier)
136+
(identifier)))))))
131137

132138
================================================================================
133139
Calls to 'make' and 'new'

test/corpus/literals.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,14 @@ const g1 = Foo[float64, Bar[int]] { }
511511
(generic_type
512512
(type_identifier)
513513
(type_arguments
514-
(type_identifier)
515-
(generic_type
516-
(type_identifier)
517-
(type_arguments
518-
(type_identifier)))))
514+
(type_elem
515+
(type_identifier))
516+
(type_elem
517+
(generic_type
518+
(type_identifier)
519+
(type_arguments
520+
(type_elem
521+
(type_identifier)))))))
519522
(literal_value))))))
520523

521524
================================================================================

0 commit comments

Comments
 (0)