tree-sitter-nu icon indicating copy to clipboard operation
tree-sitter-nu copied to clipboard

Fail to parse multi-line binary expression

Open blindFS opened this issue 4 months ago • 0 comments

Failed case:

('foo'

+

'bar')

I can manage to make it work by adding additional expr_binary_parenthesized rule like this:

diff --git a/grammar.js b/grammar.js
index 5ad1222..08a5d00 100644
--- a/grammar.js
+++ b/grammar.js
@@ -26,6 +26,7 @@ module.exports = grammar({
     [$._val_number_decimal],
     [$._immediate_decimal],
     [$.expr_parenthesized],
+    [$.expr_binary_parenthesized],
     [$.val_variable],
     [$._expression, $._expr_binary_expression],
     [$._match_pattern_value, $._value],
@@ -577,6 +578,7 @@ module.exports = grammar({
       seq(
         choice(
           prec.right(69, $._expression),
+          alias($.expr_binary_parenthesized, $.expr_binary),
           $._ctrl_expression_parenthesized,
           $.where_command,
           alias($._command_parenthesized_body, $.command),
@@ -592,6 +594,7 @@ module.exports = grammar({
     pipe_element_parenthesized_last: ($) =>
       choice(
         $._expression,
+        alias($.expr_binary_parenthesized, $.expr_binary),
         $._ctrl_expression_parenthesized,
         $.where_command,
         alias($._command_parenthesized_body, $.command),
@@ -818,6 +821,30 @@ module.exports = grammar({
         ),
       ),
 
+    expr_binary_parenthesized: ($) =>
+      choice(
+        ...TABLE().map(([precedence, opr]) =>
+          prec.right(
+            // @ts-ignore
+            precedence,
+            seq(
+              field("lhs", $._expr_binary_expression),
+              "\n",
+              // @ts-ignore
+              field("opr", opr),
+              field(
+                "rhs",
+                choice(
+                  $._expr_binary_expression,
+                  alias($.expr_binary_parenthesized, $.expr_binary),
+                  alias($.unquoted, $.val_string),
+                ),
+              ),
+            ),
+          ),
+        ),
+      ),
+
     _expr_binary_expression: ($) =>
       choice($._value, $.expr_binary, $.expr_unary, $.expr_parenthesized),

But there are probably better ways to do it.

blindFS avatar Oct 01 '24 13:10 blindFS