From 16631811cf81e04f2075e174fa5ae7a29c949fc4 Mon Sep 17 00:00:00 2001 From: rameel Date: Tue, 24 Nov 2026 15:06:01 +0500 Subject: [PATCH 1/2] parser: support verbatim identifiers The keywords true, false, and null could not be used as names. A member named after one of them was reachable only through another case, as in x.True, which works only because DefaultBinder ignores case; x.@true, as written in C#, was a syntax error. As in C#, a name prefixed with @ is now an identifier even if it is a keyword, wherever an identifier is expected, as in @true, x.@null, and 1 :> @long: identifier = "@"? identifier_start identifier_body* ; The prefix is not part of the name, so error messages show the name without it, as the C# compiler does. Expr.ToString prefixes a keyword name with @, so x.@null prints as x.@null and parses back to the same tree. --- .../ExpressionParser.Parser.cs | 14 ++++++++-- .../Expressions/Expr.Reference.cs | 2 +- .../Expressions/Expr.Unary.cs | 2 +- .../Expressions/Identifier.cs | 13 +++++++++ .../Data/Errors.txt | 12 ++++++++ .../ExpressionParserTest.cs | 28 +++++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs index 55cc148..901dc07 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs @@ -49,11 +49,21 @@ private static Parser CreateParser() Character.LetterOrDigit, L('_')).Void(); - var identifier = + var identifier_name = Seq( identifier_start, identifier_body.Many() - ).Map(m => new Identifier(m.ToString())).As("identifier"); + ).Map(m => new Identifier(m.ToString())); + + // + // As in C#, the '@' prefix makes a verbatim identifier, which can be a keyword, as in 'x.@null'. + // The prefix is not part of the name. The keyword check does not match the prefix, so a verbatim + // identifier passes Not(keyword) wherever an identifier is expected. + // + var identifier = + L('@').Optional() + .Then(identifier_name) + .As("identifier"); var keyword = Seq( diff --git a/src/Ramstack.ExpressionParser/Expressions/Expr.Reference.cs b/src/Ramstack.ExpressionParser/Expressions/Expr.Reference.cs index aee6782..d608c53 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Expr.Reference.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Expr.Reference.cs @@ -26,6 +26,6 @@ public override T Accept(ExprVisitor visitor) => /// public override string ToString() => - Value.ToString(); + Value.ToSourceString(); } } diff --git a/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs b/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs index 441574c..8922615 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs @@ -79,7 +79,7 @@ public override T Accept(ExprVisitor visitor) => /// public override string ToString() => UnaryType switch { - UnaryType.Convert => $"({Operand} :> {Operator})", + UnaryType.Convert => $"({Operand} :> {Operator.ToSourceString()})", _ => $"{Operator}({Operand})" }; } diff --git a/src/Ramstack.ExpressionParser/Expressions/Identifier.cs b/src/Ramstack.ExpressionParser/Expressions/Identifier.cs index 47c6f08..2ac6315 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Identifier.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Identifier.cs @@ -22,4 +22,17 @@ public void Deconstruct(out string name) => /// public override string ToString() => Name; + + /// + /// Returns the name as it is written in an expression. + /// + /// + /// The name, prefixed with @ if it is a keyword, such as true, + /// so that the parser reads it back as an identifier. + /// + /// + /// The keywords are those of the keyword rule in . + /// + internal string ToSourceString() => + Name is "true" or "false" or "null" ? $"@{Name}" : Name; } diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index fe4625b..ebe34d4 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -28,6 +28,18 @@ int.true:>text double.false:>text (1:8) Expected identifier +@true +Cannot resolve symbol 'true'. + +"abc".@null +Cannot resolve symbol 'null'. + +"abc".@ +(1:7) Expected identifier + +@ true +(1:1) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' + Math.int:>text Cannot resolve symbol 'int'. diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 6fcd15a..f4f622a 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -862,6 +862,25 @@ public void Subparser_DotWithoutMember_StopsBeforeDot(string source, string expe Assert.That(source[result.Length..], Is.EqualTo(rest)); } + [TestCase("@true", 1)] + [TestCase("@false + @null", 5)] + [TestCase("Self.@true", 1)] + [TestCase("Self.@null", 3)] + [TestCase("@Value", 4)] + [TestCase("Self.@Value", 4)] + public void Evaluate_VerbatimIdentifier_ResolvesMemberByName(string expression, int expected) => + Assert.That(ExpressionParser.Evaluate(expression, new KeywordContext()), Is.EqualTo(expected)); + + [TestCase("@true", "@true")] + [TestCase("x.@null", "x.@null")] + [TestCase("@false(1)[2]", "@false(1)[2]")] + [TestCase("x :> @null", "(x :> @null)")] + [TestCase("@trueValue", "trueValue")] + [TestCase("@x.@y", "x.y")] + [TestCase("1 :> @long", "(1 :> long)")] + public void ToString_VerbatimIdentifier_PrefixesOnlyKeywords(string source, string expected) => + Assert.That(ExpressionParser.Parser.Parse(source).Value?.ToString(), Is.EqualTo(expected)); + [Test] public void MaxDepthTest() { @@ -1474,4 +1493,13 @@ public class ArrayContext public int[][] Jagged { get; } = [[1], [1, 2, 3]]; public Array Untyped { get; } = new[] { "a", "b" }; } + + public class KeywordContext + { + public int @true { get; } = 1; + public int @false { get; } = 2; + public int @null { get; } = 3; + public int Value { get; } = 4; + public KeywordContext Self => this; + } } From 2715fd04f05721e9a71de61f4e67332991f595c8 Mon Sep 17 00:00:00 2001 From: rameel Date: Tue, 24 Nov 2026 15:13:32 +0500 Subject: [PATCH 2/2] parser: document verbatim identifiers --- README.md | 3 +++ src/Ramstack.ExpressionParser/Expressions/Identifier.cs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index a4a9f40..c7a9174 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,9 @@ If the expression is invalid, `Success` is `false`, and `ErrorMessage` describes Identifiers are resolved in a case-insensitive manner, so `math.min`, `Math.Min`, and `MATH.MIN` refer to the same method. +`true`, `false`, and `null` are keywords. As in C#, prefix a name with `@` to use it as an identifier, +for example `item.@null` for a member named `null`. + ### Evaluating expressions `Evaluate` parses, compiles, and runs an expression in a single call: diff --git a/src/Ramstack.ExpressionParser/Expressions/Identifier.cs b/src/Ramstack.ExpressionParser/Expressions/Identifier.cs index 2ac6315..2af4bd3 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Identifier.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Identifier.cs @@ -10,6 +10,9 @@ public sealed class Identifier(string name) /// /// Gets the name of the identifier or the symbol of the operator. /// + /// + /// The name of a verbatim identifier, such as @null, does not include the @ prefix. + /// public string Name { get; } = name; ///