diff --git a/src/NReco.LambdaParser.Tests/LambdaParserTests.cs b/src/NReco.LambdaParser.Tests/LambdaParserTests.cs index dff729b..a35bd69 100644 --- a/src/NReco.LambdaParser.Tests/LambdaParserTests.cs +++ b/src/NReco.LambdaParser.Tests/LambdaParserTests.cs @@ -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)); diff --git a/src/NReco.LambdaParser/Linq/LambdaParser.cs b/src/NReco.LambdaParser/Linq/LambdaParser.cs index 8061f10..e7b4a10 100644 --- a/src/NReco.LambdaParser/Linq/LambdaParser.cs +++ b/src/NReco.LambdaParser/Linq/LambdaParser.cs @@ -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; @@ -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) ) }; @@ -650,6 +666,7 @@ protected enum LexemType { Delimiter, StringConstant, NumberConstant, + HexNumberConstant, Stop } @@ -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; }