From 13b9d7e1b771b8421aefe1f2836fbb5aad4adee4 Mon Sep 17 00:00:00 2001 From: Jay Pratt Date: Fri, 4 Oct 2024 22:06:25 +1000 Subject: [PATCH] Add notation for containers list array/list/set/vector as '[' ']' --- tree-sitter-tako/grammar.js | 15 +- tree-sitter-tako/test/corpus/simple_exprs.tk | 140 +++++++++++++++++++ 2 files changed, 150 insertions(+), 5 deletions(-) diff --git a/tree-sitter-tako/grammar.js b/tree-sitter-tako/grammar.js index fb654737..31e40d07 100644 --- a/tree-sitter-tako/grammar.js +++ b/tree-sitter-tako/grammar.js @@ -142,19 +142,24 @@ module.exports = grammar({ // TODO: add the actual grammar rules source_file: ($) => seq(optional($.shebang), separated_one(optional($._expression), $.heading)), _expression: ($) => choice( - $._operator_expression, // Consider keeping this name to support editing? - $.call, - $.parens, - $.block, + $._expression_not_literal, $.string_literal, $._number, $.hex_literal, $.color, + ), + _expression_not_literal: ($) => choice( + $._operator_expression, // Consider keeping this name to support editing? + $.parens, + $.container, + $.call, + $.block, $.ident, ), block: ($) => seq('{', optional($._expression), '}'), parens: ($) => seq('(', $._expression ,')'), - call: ($) => left(PREC.call, seq($._expression, '(', separated($._expression, ','), optional(','), ')')), + container: ($) => seq('[', separated($._expression, ','), optional(',') ,']'), + call: ($) => left(PREC.call, seq($._expression_not_literal, '(', separated($._expression, ','), optional(','), ')')), _operator_expression: ($) => { return choice(...ALL_OPERATORS.map(([name, _operator_parser]) => { try { diff --git a/tree-sitter-tako/test/corpus/simple_exprs.tk b/tree-sitter-tako/test/corpus/simple_exprs.tk index 952943c3..10edf464 100644 --- a/tree-sitter-tako/test/corpus/simple_exprs.tk +++ b/tree-sitter-tako/test/corpus/simple_exprs.tk @@ -25,6 +25,146 @@ Simple values with infix sequence ) ) +================== +Simple values with parens +================== + +(0) + +--- +(source_file + (parens + (int_literal) + ) +) + +================== +Simple values with more parens +================== + +((0)) + +--- +(source_file + (parens + (parens + (int_literal) + ) + ) +) + +================== +Simple values with mismatched parens +================== + +((0() + +--- +(source_file + (parens + (parens + (int_literal) + (ERROR)) + (MISSING ")") + ) +) + +================== +Empty Block +================== + +{} + +--- +(source_file + (block) +) + +================== +Empty Block with suffix sequence +================== + +{}; + +--- +(source_file + (sequence (block)) +) + +================== +Empty Container notation +================== + +[] + +--- +(source_file + (container) +) + + +================== +Container notation with a single value +================== + +[1] + +--- +(source_file + (container + (int_literal) + ) +) + +================== +Container notation with multiple values +================== + +[1,2,3,4,] + +--- +(source_file + (container + (int_literal) + (int_literal) + (int_literal) + (int_literal) + ) +) + +================== +Container notation containing multiple identifiers +================== + +[a,b,c,d,] + +--- +(source_file + (container + (ident) + (ident) + (ident) + (ident) + ) +) + + +================== +Container notation containing multiple subexpressions +================== + +[3+a,b*c,c=0,"Wow",] + +--- +(source_file + (container + (add (int_literal) (ident)) + (mul (ident) (ident)) + (assign (ident) (int_literal)) + (string_literal) + ) +) + ================== Simple Expressions ==================