From 002a9c1fee19539ce33c71614f1d44ba18d43b94 Mon Sep 17 00:00:00 2001 From: rameel Date: Tue, 24 Nov 2026 20:31:40 +0500 Subject: [PATCH] parser: report errors inside argument lists at their position The arguments of a call and an indexer were labeled with As, which discards the diagnostics from inside the labeled parser. A list stops before a broken argument, so the error pointed at the comma before it or at the start of the list rather than where an expression is missing: "test".ToString(1,) (1:18) Expected ')' Math.Max(1, (2 + )) (1:11) Expected ')' "test"[(] (1:8) Expected expression Remove both labels, so the error points where the expression is missing, as in C#, and lists the tokens that can start an expression, as for 1 +. An empty indexer, as in "test"[], gets the same list instead of "Expected expression", and an unfinished list, as in "test"[0, lists the operators, ',' and ']', as 1 + (2 * 3 does. --- .../ExpressionParser.Parser.cs | 4 +-- .../Data/Errors.txt | 31 ++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs index 45995cd..2191476 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs @@ -130,7 +130,7 @@ private static Parser CreateParser() var expression_list = expression .Separated( - Seq(L(','), S)).As("expression_list"); + Seq(L(','), S)); // Member access, calls, and indexers share one precedence level, so each of them // is parsed as a postfix operator that wraps the expression to its left. @@ -146,7 +146,7 @@ private static Parser CreateParser() expression .Separated( Seq(L(','), S), - min: 1).As("expression") + min: 1) .Between( Seq(L('['), S), Seq(L(']'), S)) diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index d1dda0a..c6a332a 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -59,7 +59,7 @@ Non-invocable member 'maxvalue' cannot be used like a method. (1:11) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or ')' 1 + 2 * 3( -(1:11) Expected ')' +(1:11) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, '(', or ')' 1 + 2 * 3(2) Method name expected (expression: 3). @@ -68,7 +68,7 @@ Method name expected (expression: 3). Cannot apply indexing with [] to an expression of type 'System.Int32'. 1 + 2 * 3[ -(1:11) Expected expression +(1:11) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' 1 + 2 * 3] (1:10) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input @@ -101,17 +101,38 @@ Cannot apply indexing with [] to an expression of type 'System.Int32'. (1:2) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input "test"[] -(1:8) Expected expression +(1:8) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' "test"[ ] -(1:9) Expected expression +(1:9) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' "test".Split(',')[] -(1:19) Expected expression +(1:19) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' "test"["a"] Ambiguous match found:\n Char Chars [Int32] (in System.String) +"test".ToString(1,) +(1:19) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' + +Math.Max(1, (2 + )) +(1:18) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' + +"test"[0,] +(1:10) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' + +"test"[(] +(1:9) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' + +"test"[1 +] +(1:11) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' + +"test"[0 +(1:9) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', ',', or ']' + +Math.Max(1, 2 +(1:14) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', ',', or ')' + StringSplitOptions.None | StringComparison.Ordinal Operator '|' cannot be applied to operands of type 'System.StringSplitOptions' and 'System.StringComparison'.