From 33d191739dfa5e2f98e866162fb4fd3c60f32094 Mon Sep 17 00:00:00 2001 From: rameel Date: Tue, 24 Nov 2026 13:45:06 +0500 Subject: [PATCH 1/2] parser: parse member access as a postfix operator Member access was parsed one level above calls and indexers: member_expression = call_or_indexer_expression ("." S !keyword call_or_indexer_expression)* ; call_or_indexer_expression = primary_expression (call_expression / indexer_expression)* ; The part after a dot arrived as a finished chain such as b(1)[2], and HijackMemberExpression rebuilt it recursively to put the member access at its bottom. Parse member access, calls, and indexers as postfix operators of one level, each applied to the expression on its left, as in the C# grammar: postfix_expression = primary_expression (call / indexer / member_access)* ; member_access = "." S !keyword identifier S ; The chain is built in a loop, so the depth check before the rebuild is no longer needed; the final check still limits the depth. Only an identifier is accepted after a dot now. single.10 and "abc".(Length) fail with "Expected identifier" at the position of the member instead of "Identifier expected" when the expression is built. The checks in ExpressionBuilder stay for trees built directly. --- .../ExpressionParser.Parser.cs | 60 ++++++++----------- .../Data/Errors.txt | 7 ++- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs index 179f843..eda41df 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs @@ -124,53 +124,45 @@ private static Parser CreateParser() .Separated( Seq(L(','), S)).As("expression_list"); - var call_expression = + // + // 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. + // + var call = expression_list .Between( Seq(L('('), S), - Seq(L(')'), S)).Do(list => (list, indexer: false)); + Seq(L(')'), S)) + .Do(Func (parameters) => expr => new Expr.Call(expr, parameters)); - var indexer_expression = + var indexer = expression_list .Between( Seq(L('['), S), - Seq(L(']'), S)).Do(list => (list, indexer: true)); + Seq(L(']'), S)) + .Do(Func (parameters) => expr => new Expr.Indexer(expr, parameters)); - var call_or_indexer_expression = + var member_access = + identifier + .Between( + Seq(L('.'), S, Not(keyword)), + S) + .Do(Func (name) => expr => new Expr.MemberAccess(expr, new Expr.Reference(name))); + + var postfix_expression = Seq( primary_expression, Choice( - call_expression, - indexer_expression + call, + indexer, + member_access ).Many() - ).Do((expr, list) => - { - foreach (var (parameters, indexer) in list) - expr = indexer - ? new Expr.Indexer(expr, parameters) - : new Expr.Call(expr, parameters); - - // - // HijackMemberExpression processes this chain recursively during parsing, - // so check the depth before the chain reaches it. - // - return EnsureDepth(expr); - }); - - var member_expression = - call_or_indexer_expression.Fold(Seq(L('.'), S, Not(keyword)), (lhs, rhs, _) => + ).Do((expr, operators) => { - return HijackMemberExpression(lhs, rhs); + foreach (var apply in operators) + expr = apply(expr); - static Expr HijackMemberExpression(Expr result, Expr value) - { - return value switch - { - Expr.Call call => new Expr.Call(HijackMemberExpression(result, call.Expression), call.Parameters), - Expr.Indexer indexer => new Expr.Indexer(HijackMemberExpression(result, indexer.Expression), indexer.Parameters), - _ => new Expr.MemberAccess(result, value) - }; - } + return expr; }); // @@ -179,7 +171,7 @@ static Expr HijackMemberExpression(Expr result, Expr value) // var cast_expression = Seq( - member_expression, + postfix_expression, identifier.Between( Seq(L(":>"), S), S).Many() diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index 95a44da..ad2bb49 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -14,10 +14,13 @@ string.Empty:>text Cannot resolve symbol 'text'. single.10:>text -Identifier expected (expression: 10). +(1:8) Expected identifier float."10":>text -Identifier expected (expression: "10"). +(1:7) Expected identifier + +"abc".(Length) +(1:7) Expected identifier int.true:>text (1:4) Expected '(', '[', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input From 3fa26e40be3ec9ccc67870f748f6ade8b3c1840d Mon Sep 17 00:00:00 2001 From: rameel Date: Tue, 24 Nov 2026 14:14:49 +0500 Subject: [PATCH 2/2] parser: report a keyword after a dot as a missing identifier A keyword after a dot, as in int.true, was rejected by !keyword, which reports no expected token, so the error pointed at the dot and listed the operators that can follow int: (1:4) Expected '(', '[', ':>', [%*/], [+-], '<<', '>>', ... Label the member name "identifier" with As, so the error points past the dot and reads "(1:5) Expected identifier", as for a.10 or a dot at the end. Unlike Fail, a labeled parser fails without stopping the parse, so Subparser still stops before a dot that starts no member access, as in a..b. --- .../ExpressionParser.Parser.cs | 10 ++++++++-- .../Ramstack.ExpressionParser.Tests/Data/Errors.txt | 4 ++-- .../ExpressionParserTest.cs | 12 ++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs index eda41df..55cc148 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs @@ -142,10 +142,16 @@ private static Parser CreateParser() Seq(L(']'), S)) .Do(Func (parameters) => expr => new Expr.Indexer(expr, parameters)); + // + // Not(keyword) reports no expected token when it fails, so the member name is labeled + // to report "Expected identifier" for a keyword after the dot. Unlike Fail, a labeled parser + // fails without stopping the parse, so Subparser still stops before a dot that an enclosing + // grammar uses, as in 'a..b'. + // var member_access = - identifier + Not(keyword).Then(identifier).As("identifier") .Between( - Seq(L('.'), S, Not(keyword)), + Seq(L('.'), S), S) .Do(Func (name) => expr => new Expr.MemberAccess(expr, new Expr.Reference(name))); diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index ad2bb49..fe4625b 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -23,10 +23,10 @@ float."10":>text (1:7) Expected identifier int.true:>text -(1:4) Expected '(', '[', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input +(1:5) Expected identifier double.false:>text -(1:7) Expected '(', '[', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input +(1:8) Expected identifier Math.int:>text Cannot resolve symbol 'int'. diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index b9bb7ad..6fcd15a 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -850,6 +850,18 @@ public void ParseErrorExpression(string expression, string expected) Assert.That(result.ToString(), Is.EqualTo(expected)); } + [TestCase("a..b", "a", "..b")] + [TestCase("1..5", "1", "..5")] + [TestCase("x.y..z", "x.y", "..z")] + public void Subparser_DotWithoutMember_StopsBeforeDot(string source, string expected, string rest) + { + var result = ExpressionParser.Subparser.Parse(source); + + Assert.That(result.Success, Is.True); + Assert.That(result.Value!.ToString(), Is.EqualTo(expected)); + Assert.That(source[result.Length..], Is.EqualTo(rest)); + } + [Test] public void MaxDepthTest() {