@@ -14,6 +14,9 @@ function commaSep1(rule) {
1414function commaSep2 ( rule ) {
1515 return seq ( rule , repeat1 ( seq ( "," , rule ) ) )
1616}
17+ function commaSep1Trailing ( rule ) {
18+ return seq ( commaSep1 ( rule ) , optional ( "," ) )
19+ }
1720
1821const FUNC_GRAMMAR = {
1922 source_file : $ => repeat ( $ . _top_level_item ) ,
@@ -72,7 +75,7 @@ const FUNC_GRAMMAR = {
7275 seq (
7376 field ( "type_parameters" , optional ( $ . type_parameters ) ) ,
7477 field ( "return_type" , $ . _type_hint ) ,
75- field ( "name" , $ . identifier ) ,
78+ field ( "name" , $ . function_name ) ,
7679 choice (
7780 seq (
7881 field ( "parameters" , $ . parameter_list ) ,
@@ -302,16 +305,65 @@ const FUNC_GRAMMAR = {
302305 ) ,
303306 ) ,
304307 ) ,
305- local_vars_declaration : $ => prec . dynamic ( 90 , field ( "lhs" , $ . _var_declaration_lhs ) ) ,
306308
307- tuple_vars_declaration : $ =>
308- prec ( 100 , seq ( "[" , field ( "vars" , commaSep1 ( $ . _var_declaration_lhs ) ) , optional ( "," ) , "]" ) ) ,
309+ // ------------------------------------------------------------------
310+ // local vars
311+ var_declaration : $ => seq ( field ( "type" , optional ( $ . _type_hint ) ) , field ( "name" , $ . identifier ) ) ,
312+
313+ nested_tensor_declaration : $ =>
314+ prec (
315+ 101 ,
316+ seq (
317+ "(" ,
318+ field (
319+ "vars" ,
320+ commaSep1Trailing (
321+ choice (
322+ $ . nested_tensor_declaration ,
323+ $ . var_declaration ,
324+ $ . tuple_vars_declaration ,
325+ $ . underscore ,
326+ ) ,
327+ ) ,
328+ ) ,
329+ ")" ,
330+ ) ,
331+ ) ,
309332 tensor_vars_declaration : $ =>
310- prec ( 100 , seq ( "(" , field ( "vars" , commaSep1 ( $ . _var_declaration_lhs ) ) , optional ( "," ) , ")" ) ) ,
311- var_declaration : $ => seq ( field ( "type" , $ . _type_hint ) , field ( "name" , $ . identifier ) ) ,
333+ prec (
334+ 101 ,
335+ seq (
336+ field ( "type" , $ . _type_hint ) , // e.g. `var`
337+ "(" ,
338+ field (
339+ "vars" ,
340+ commaSep1Trailing (
341+ choice (
342+ $ . nested_tensor_declaration ,
343+ $ . var_declaration ,
344+ $ . tuple_vars_declaration ,
345+ $ . underscore ,
346+ ) ,
347+ ) ,
348+ ) ,
349+ ")" ,
350+ ) ,
351+ ) ,
352+
353+ _multiple_vars_declaration : $ =>
354+ prec . left ( 90 , seq ( choice ( $ . tensor_vars_declaration , $ . tuple_vars_declaration ) ) ) ,
355+ local_vars_declaration : $ =>
356+ field ( "lhs" , choice ( $ . _multiple_vars_declaration , $ . var_declaration ) ) ,
357+ //local_vars_declaration: $ => prec.dynamic(90, field("lhs", $._var_declaration_lhs)),
358+
359+ tuple_vars_declaration : $ =>
360+ prec ( 101 , seq ( "[" , field ( "vars" , commaSep1Trailing ( $ . var_declaration ) ) , "]" ) ) ,
361+ // tensor_vars_declaration: $ =>
362+ // prec(100, seq("(", field("vars", commaSep1Trailing($._var_declaration_lhs)), optional(","), ")")),
363+ // var_declaration: $ => seq(field("type", $._type_hint), field("name", $.identifier)),
312364
313- _var_declaration_lhs : $ =>
314- choice ( $ . tuple_vars_declaration , $ . tensor_vars_declaration , $ . var_declaration ) ,
365+ // _var_declaration_lhs: $ =>
366+ // choice($.tuple_vars_declaration, $.tensor_vars_declaration, $.var_declaration),
315367
316368 type_expression : $ =>
317369 prec (
@@ -367,15 +419,15 @@ const FUNC_GRAMMAR = {
367419 $ . type_identifier ,
368420 $ . tensor_type ,
369421 $ . tuple_type ,
370- $ . _parenthesized_type ,
422+ // $._parenthesized_type,
371423 ) ,
372424
373- _parenthesized_type : $ => seq ( "(" , $ . _type_hint , ")" ) ,
425+ // _parenthesized_type: $ => alias( seq("(", $._type_hint, ")"), $.tensor_type ),
374426
375427 primitive_type : $ => choice ( "int" , "cell" , "slice" , "builder" , "cont" , "tuple" ) ,
376428 // constant_type: $ => choice("int", "slice"),
377429
378- tensor_type : $ => choice ( seq ( "(" , ")" ) , seq ( "(" , field ( "types" , commaSep2 ( $ . _type_hint ) ) , ")" ) ) ,
430+ tensor_type : $ => choice ( seq ( "(" , ")" ) , seq ( "(" , field ( "types" , commaSep1 ( $ . _type_hint ) ) , ")" ) ) ,
379431
380432 tuple_type : $ => seq ( "[" , field ( "types" , commaSep ( $ . _type_hint ) ) , "]" ) ,
381433
@@ -400,6 +452,7 @@ const FUNC_GRAMMAR = {
400452 // actually, FunC identifiers are much more flexible
401453 identifier : _ => / ` [ ^ ` ] + ` | [ a - z A - Z 0 - 9 _ \$ % ] [ ^ \s \+ \- \* \/ % , \. ; \( \) \{ \} \[ \] = \| \^ \~ ] * / ,
402454 underscore : _ => "_" ,
455+ function_name : $ => / ( ` .* ` ) | ( ( \. | ~ ) ? ( ( [ $ % a - z A - Z 0 - 9 _ ] ( \w | [ ' ? : $ % ! ] ) + ) | ( [ a - z A - Z % $ ] ) ) ) / ,
403456
404457 // multiline_comment: $ => seq('{-', repeat(choice(/./, $.multiline_comment)), '-}'),
405458 // unfortunately getting panic while generating parser with support for nested comments
@@ -429,5 +482,6 @@ module.exports = grammar({
429482 [ $ . parameter_list_relaxed , $ . parameter_list ] ,
430483 [ $ . tensor_expression , $ . tensor_type ] ,
431484 [ $ . typed_tuple , $ . tuple_type ] ,
485+ [ $ . var_declaration , $ . type_identifier ] ,
432486 ] ,
433487} )
0 commit comments