diff --git a/tree-sitter-tako/grammar.js b/tree-sitter-tako/grammar.js index 9945808a..fb654737 100644 --- a/tree-sitter-tako/grammar.js +++ b/tree-sitter-tako/grammar.js @@ -33,7 +33,6 @@ const PREC = { assign: 0, closure: -2, sequence: -3, - postfix_sequence: -3, }; const RIGHT_OPERATORS = [ @@ -43,7 +42,6 @@ const RIGHT_OPERATORS = [ const OPERATORS = [ ['field', '.'], ['has_type', ':'], - ['sequence', ';'], ['and', '&&'], ['or', '||'], ['bitand', '&'], @@ -66,6 +64,9 @@ const OPERATORS = [ const POSTFIX_OPERATORS = [ ['try', '?'], +]; + +const OPTIONALLY_POSTFIX_OPERATORS = [ ['sequence', ';'], ]; @@ -79,6 +80,7 @@ const ALL_OPERATORS = [ ...OPERATORS, ...RIGHT_OPERATORS, ...POSTFIX_OPERATORS, + ...OPTIONALLY_POSTFIX_OPERATORS, ...UNARY_OPERATORS, ]; @@ -108,6 +110,14 @@ function operators_gen() { field('right', $._expression), )); } + for (const [name, operator] of OPTIONALLY_POSTFIX_OPERATORS) { + const precedence = PREC[name]; + operators[name] = ($) => right(precedence, seq( + field('left', $._expression), + field('operator', operator), + field('right', optional($._expression)), + )); + } for (const [name, operator] of POSTFIX_OPERATORS) { const precedence = PREC[name]; operators[name] = ($) => left(precedence, seq( @@ -131,11 +141,10 @@ module.exports = grammar({ rules: { // TODO: add the actual grammar rules source_file: ($) => seq(optional($.shebang), separated_one(optional($._expression), $.heading)), - block: ($) => seq('{', optional($._expression), '}'), _expression: ($) => choice( $._operator_expression, // Consider keeping this name to support editing? $.call, - seq('(', $._expression ,')'), + $.parens, $.block, $.string_literal, $._number, @@ -143,6 +152,8 @@ module.exports = grammar({ $.color, $.ident, ), + block: ($) => seq('{', optional($._expression), '}'), + parens: ($) => seq('(', $._expression ,')'), call: ($) => left(PREC.call, seq($._expression, '(', separated($._expression, ','), optional(','), ')')), _operator_expression: ($) => { return choice(...ALL_OPERATORS.map(([name, _operator_parser]) => { diff --git a/tree-sitter-tako/test/corpus/definitions.tk b/tree-sitter-tako/test/corpus/definitions.tk index 8988fae7..af9f2a87 100644 --- a/tree-sitter-tako/test/corpus/definitions.tk +++ b/tree-sitter-tako/test/corpus/definitions.tk @@ -79,7 +79,7 @@ test(a=3,b=2) = 3 ) ) ================== -Constant Definition using empty block +Constant Definition using empty Block ================== test = { } @@ -91,7 +91,7 @@ test = { ) ) ================== -Constant Definition using block +Constant Definition using Block ================== test = { 3 diff --git a/tree-sitter-tako/test/corpus/number.tk b/tree-sitter-tako/test/corpus/number.tk index 44ae0260..e52a521e 100644 --- a/tree-sitter-tako/test/corpus/number.tk +++ b/tree-sitter-tako/test/corpus/number.tk @@ -64,4 +64,4 @@ Color wrong number of digits ================== #ff00aaa --- -(source_file (color) (ERROR)) +(source_file (color) (ERROR (ident))) diff --git a/tree-sitter-tako/test/corpus/simple_exprs.tk b/tree-sitter-tako/test/corpus/simple_exprs.tk index c5f085a5..952943c3 100644 --- a/tree-sitter-tako/test/corpus/simple_exprs.tk +++ b/tree-sitter-tako/test/corpus/simple_exprs.tk @@ -1,3 +1,30 @@ +================== +Simple value with postfix sequence +================== + +0; + +--- +(source_file + (sequence + (int_literal) + ) +) + +================== +Simple values with infix sequence +================== + +0;1 + +--- +(source_file + (sequence + (int_literal) + (int_literal) + ) +) + ================== Simple Expressions ==================