Permalink
Browse files

ExpressionSample - disallow reserved words as identifiers

  • Loading branch information...
bobbymcr committed Apr 9, 2018
1 parent 8589723 commit e055b227f600d216502be726d7e7cb2fdb4fdf2f
@@ -54,5 +54,13 @@ public void WithOtherOperations(string input, string output)
{
Test.Good(input, output);
}
[InlineData("1+AND")]
[InlineData("AND(1,X)")]
[Theory]
public void FailedReserved(string input)
{
Test.Bad(input);
}
}
}
@@ -45,5 +45,13 @@ public void WithOtherOperations(string input, string output)
{
Test.Good(input, output);
}
[InlineData("1+NOT")]
[InlineData("NOT(1,X)")]
[Theory]
public void FailedReserved(string input)
{
Test.Bad(input);
}
}
}
@@ -54,5 +54,13 @@ public void WithOtherOperations(string input, string output)
{
Test.Good(input, output);
}
[InlineData("1+OR")]
[InlineData("OR(1,X)")]
[Theory]
public void FailedReserved(string input)
{
Test.Bad(input);
}
}
}
@@ -44,9 +44,20 @@ private static class Ch
public static readonly Parser<char> Star = Parse.Char('*');
public static readonly Parser<char> Slash = Parse.Char('/');
public static readonly Parser<char> Caret = Parse.Char('^');
public static readonly Parser<char> Space = Parse.Char(' ');
public static readonly Parser<char> NonQuote = Parse.AnyChar.Except(Quote);
}
private static class Kw
{
public static readonly Parser<IEnumerable<char>> And = Parse.String("AND");
public static readonly Parser<IEnumerable<char>> Not = Parse.String("NOT");
public static readonly Parser<IEnumerable<char>> Or = Parse.String("OR");
public static readonly Parser<IEnumerable<char>> Any =
And.Or(Not).Or(Or);
}
private static class Lit
{
public static readonly Parser<BasicExpression> Num =
@@ -88,7 +99,9 @@ public StringLiteral(string s)
private static class Var
{
public static readonly Parser<string> Id = Parse.Identifier(Parse.Letter, Parse.LetterOrDigit);
public static readonly Parser<string> Id =
Parse.Identifier(Parse.Letter, Parse.LetterOrDigit)
.Except(Kw.Any);
public static readonly Parser<BasicVariable> StrScalar =
from v in Id
@@ -215,7 +228,8 @@ private static class Num
Parse.ChainOperator(Op.Additive, Mult, Op.Apply);
private static readonly Parser<BasicExpression> Not =
from k in Parse.String("NOT ")
from k in Kw.Not
from s in Ch.Space
from x in Add
select new NotExpression(x);
@@ -257,11 +271,15 @@ public NotExpression(BasicExpression x)
private static class Op
{
public static readonly Parser<IOperator> And =
from k in Parse.String(" AND ")
from s1 in Ch.Space
from k in Kw.And
from s2 in Ch.Space
select AndOperator.Value;
public static readonly Parser<IOperator> Or =
from k in Parse.String(" OR ")
from s1 in Ch.Space
from k in Kw.Or
from s2 in Ch.Space
select OrOperator.Value;
public static readonly Parser<IOperator> Add =

0 comments on commit e055b22

Please sign in to comment.