-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
233 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using sly.lexer; | ||
|
||
namespace CslyNullIssue | ||
{ | ||
public enum Issue259ExpressionToken | ||
{ | ||
[Lexeme("L:[A-Za-z0-9_]+")] | ||
LVAR = 50, | ||
|
||
[Lexeme("A:[:A-Za-z0-9 ]+,\\s*([A-Za-z0-9 ]+)")] | ||
SIMVAR = 52, | ||
|
||
// [Lexeme("\\[[A-Z_0-9 ]+:[A-Z_0-9 ]+\\]")] | ||
[Lexeme("\\[[A-Za-z_0-9 ]+:[A-Za-z_0-9 ]+\\]")] | ||
DCS_VAR = 53, | ||
|
||
[Lexeme("OFF")] | ||
OFF = 0, | ||
|
||
[Lexeme("ON")] | ||
ON = 1, | ||
|
||
// Hex/decimal prefix? | ||
[Lexeme("0[xX][0-9a-fA-F]+")] | ||
HEX_NUMBER = 2, | ||
|
||
[Lexeme("[0-9]+(\\.[0-9]+)?")] | ||
DECIMAL_NUMBER = 3, | ||
|
||
[Lexeme("\\+")] | ||
PLUS = 4, | ||
|
||
[Lexeme("-")] | ||
MINUS = 5, | ||
|
||
[Lexeme("\\*")] | ||
TIMES = 6, | ||
|
||
[Lexeme("/")] | ||
DIVIDE = 7, | ||
|
||
[Lexeme("\\|")] | ||
BITWISE_OR = 8, | ||
|
||
[Lexeme("&")] | ||
BITWISE_AND = 9, | ||
|
||
[Lexeme("[ \\t]+", isSkippable: true)] | ||
WHITESPACE = 20, | ||
|
||
[Lexeme("OR")] | ||
LOGICAL_OR = 10, | ||
[Lexeme("AND")] | ||
LOGICAL_AND = 11, | ||
|
||
[Lexeme("NOT")] | ||
NOT = 12, | ||
|
||
[Lexeme("(<=?)|(==)|(!=)|(>=?)")] | ||
COMPARISON = 13, | ||
|
||
[Lexeme("\\(")] | ||
LPAREN = 30, | ||
|
||
[Lexeme("\\)")] | ||
RPAREN = 31, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System.Globalization; | ||
using sly.lexer; | ||
using sly.parser.generator; | ||
|
||
namespace CslyNullIssue | ||
{ | ||
public class Issue259Parser | ||
{ | ||
[Operand] | ||
[Production("logical_literal: OFF")] | ||
[Production("logical_literal: ON")] | ||
public string LiteralBool(Token<Issue259ExpressionToken> token) | ||
{ | ||
return token.Value; | ||
} | ||
|
||
[Operand] | ||
[Production("primary: HEX_NUMBER")] | ||
public string NumericExpressionFromLiteralNumber(Token<Issue259ExpressionToken> offsetToken) | ||
{ | ||
return offsetToken.Value; | ||
} | ||
|
||
[Operand] | ||
[Production("primary: DECIMAL_NUMBER")] | ||
public string NumericExpressionFromDecimalNumber(Token<Issue259ExpressionToken> offsetToken) | ||
{ | ||
var text = offsetToken.Value; | ||
var value = double.Parse(text, CultureInfo.InvariantCulture); | ||
return value.ToString(CultureInfo.InvariantCulture); | ||
} | ||
|
||
[Infix((int)Issue259ExpressionToken.PLUS, Associativity.Left, 14)] | ||
[Infix((int)Issue259ExpressionToken.MINUS, Associativity.Left, 14)] | ||
[Infix((int)Issue259ExpressionToken.TIMES, Associativity.Left, 15)] | ||
[Infix((int)Issue259ExpressionToken.DIVIDE, Associativity.Left, 15)] | ||
[Infix((int)Issue259ExpressionToken.BITWISE_AND, Associativity.Left, 10)] | ||
[Infix((int)Issue259ExpressionToken.BITWISE_OR, Associativity.Left, 8)] | ||
public string NumberExpression(string lhs, Token<Issue259ExpressionToken> token, string rhs) | ||
{ | ||
return $"({lhs} {token.Value} {rhs})"; | ||
} | ||
|
||
[Infix((int)Issue259ExpressionToken.LOGICAL_AND, Associativity.Left, 7)] | ||
[Infix((int)Issue259ExpressionToken.LOGICAL_OR, Associativity.Left, 6)] | ||
public string LogicalExpression(string lhs, Token<Issue259ExpressionToken> token, string rhs) | ||
{ | ||
return $"({lhs} {token.Value} {rhs})"; | ||
} | ||
|
||
[Prefix((int)Issue259ExpressionToken.MINUS, Associativity.Right, 17)] | ||
public string NumericExpression(Token<Issue259ExpressionToken> _, string child) | ||
{ | ||
return $"-{child}"; | ||
} | ||
|
||
// We want NOT to to bind tighter than AND/OR but looser than numeric comparison operations | ||
[Prefix((int)Issue259ExpressionToken.NOT, Associativity.Right, 11)] | ||
public string LogicalExpression(Token<Issue259ExpressionToken> _, string child) | ||
{ | ||
return $"(NOT {child})"; | ||
} | ||
|
||
[Infix((int)Issue259ExpressionToken.COMPARISON, Associativity.Left, 12)] | ||
public string Comparison(string lhs, Token<Issue259ExpressionToken> token, string rhs) | ||
{ | ||
return $"({lhs} {token.Value} {rhs})"; | ||
} | ||
|
||
|
||
[Operand] | ||
[Production("numeric_literal: LVAR")] | ||
public string Lvar(Token<Issue259ExpressionToken> token) | ||
{ | ||
return token.Value; | ||
} | ||
|
||
[Operand] | ||
[Production("numeric_literal: SIMVAR")] | ||
public string SimVarExpression(Token<Issue259ExpressionToken> simvarToken) | ||
{ | ||
var text = simvarToken.Value[2..]; | ||
var bits = text.Split(","); | ||
var varName = bits[0]; | ||
var type = bits[1].Trim(); | ||
return $"A:{varName}, {type}"; | ||
} | ||
|
||
[Operand] | ||
[Production("group : LPAREN Issue259Parser_expressions RPAREN")] | ||
public string Group(Token<Issue259ExpressionToken> _1, string child, Token<Issue259ExpressionToken> _2) | ||
{ | ||
return child; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using sly.parser; | ||
using sly.parser.generator; | ||
using Xunit; | ||
|
||
namespace CslyNullIssue | ||
{ | ||
public class Issue259Tests | ||
{ | ||
[Fact] | ||
public static void Issue259Test() | ||
{ | ||
var expression = "1 < 2 AND 3 <= 4 AND 5 == 6 AND 7 >= 8 AND 9 > 10 AND 11 != 12 AND 13 <> 14"; | ||
|
||
|
||
var startingRule = $"{typeof(Issue259Parser).Name}_expressions"; | ||
var parserInstance = new Issue259Parser(); | ||
var builder = new ParserBuilder<Issue259ExpressionToken, string>("en"); | ||
var parser = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, startingRule); | ||
Assert.True(parser.IsOk); | ||
Assert.NotNull(parser.Result); | ||
|
||
Console.WriteLine(parser.Result.Configuration.Dump()); | ||
|
||
var parseResult = parser.Result.Parse(expression); | ||
|
||
Assert.True(parseResult.IsError); | ||
Assert.Single(parseResult.Errors); | ||
|
||
var error = parseResult.Errors[0]; | ||
|
||
var unexpectedTokenError = error as UnexpectedTokenSyntaxError<Issue259ExpressionToken>; | ||
Assert.NotNull(unexpectedTokenError); | ||
Assert.Equal(Issue259ExpressionToken.COMPARISON, unexpectedTokenError.UnexpectedToken.TokenID); | ||
Assert.Equal(">",unexpectedTokenError.UnexpectedToken.Value); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.ON); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.OFF); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.MINUS); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.HEX_NUMBER); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.DECIMAL_NUMBER); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.LVAR); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.SIMVAR); | ||
Assert.Contains(unexpectedTokenError.ExpectedTokens, x => x == Issue259ExpressionToken.LPAREN); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters