Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hex numbers support #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/NReco.LambdaParser.Tests/LambdaParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public void Eval() {

Assert.Equal(3M, lambdaParser.Eval("1+2", varContext) );
Assert.Equal(6M, lambdaParser.Eval("1+2+3", varContext));
Assert.Equal(6M, lambdaParser.Eval("0x1+0x02+0x003+0", varContext));
Assert.Equal(276M, lambdaParser.Eval("0xFF+0x0A+0x0B", varContext));

Assert.Equal("b{0}_", lambdaParser.Eval("\"b{0}_\"", varContext));

Assert.Equal(3M, lambdaParser.Eval("(1+(3-1)*4)/3", varContext));
Expand Down
27 changes: 23 additions & 4 deletions src/NReco.LambdaParser/Linq/LambdaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,15 @@ protected Lexem ReadLexem(string s, int startIdx) {
if (lexem.Type == LexemType.Unknown)
lexem.Type = LexemType.Name;
} else if (Char.IsDigit(s[lexem.End])) {
if (lexem.Type == LexemType.Unknown)
lexem.Type = LexemType.NumberConstant;
if (lexem.Type == LexemType.Unknown) {
if ((lexem.End + 1) < s.Length && s[lexem.End] == '0' && s[lexem.End + 1] == 'x') {
lexem.End++;
lexem.Type = LexemType.HexNumberConstant;
}
else {
lexem.Type = LexemType.NumberConstant;
}
}
} else if (Array.IndexOf(specialNameChars, s[lexem.End]) >= 0) {
if (lexem.Type == LexemType.Unknown || lexem.Type==LexemType.Name) {
lexem.Type = LexemType.Name;
Expand Down Expand Up @@ -559,7 +566,16 @@ protected ParseResult ParseValue(string expr, int start) {
return new ParseResult() {
End = lexem.End,
Expr = Expression.Constant(new LambdaParameterWrapper( numConst, Comparer) ) };
} else if (lexem.Type == LexemType.StringConstant) {
} else if (lexem.Type == LexemType.HexNumberConstant) {
int numConst;
if (!Int32.TryParse(lexem.GetValue(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out numConst))
{
throw new Exception(String.Format("Invalid number: {0}", lexem.GetValue()));
}
return new ParseResult() {
End = lexem.End,
Expr = Expression.Constant(new LambdaParameterWrapper( numConst, Comparer) ) };
} else if (lexem.Type == LexemType.StringConstant) {
return new ParseResult() {
End = lexem.End,
Expr = Expression.Constant( new LambdaParameterWrapper( lexem.GetValue(), Comparer) ) };
Expand Down Expand Up @@ -650,6 +666,7 @@ protected enum LexemType {
Delimiter,
StringConstant,
NumberConstant,
HexNumberConstant,
Stop
}

Expand All @@ -666,7 +683,9 @@ public string GetValue() {
rawValue = Expr.Substring(Start, End-Start).Trim();
if (Type==LexemType.StringConstant) {
rawValue = rawValue.Substring(1, rawValue.Length-2).Replace( "\"\"", "\"" );
}
} else if (Type == LexemType.HexNumberConstant) {
rawValue = rawValue.Replace("0x", "");
}
}
return rawValue;
}
Expand Down