-
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.
Merge pull request #266 from b3b00/experiment/lexer-post-processing
Experiment/lexer post processing
- Loading branch information
Showing
29 changed files
with
881 additions
and
17 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
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,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
using postProcessedLexerParser; | ||
using postProcessedLexerParser.expressionModel; | ||
|
||
namespace ParserTests | ||
{ | ||
public class PostProcessedLexerTests | ||
{ | ||
[Fact] | ||
public void TestPostLexerProcessing() | ||
{ | ||
var Parser = postProcessedLexerParser.PostProcessedLexerParserBuilder.buildPostProcessedLexerParser(); | ||
|
||
var r = Parser.Parse("2 * x"); | ||
Assert.False(r.IsError); | ||
var res = r.Result.Evaluate(new ExpressionContext(new Dictionary<string, double>() | ||
{ { "x", 2 } })); | ||
Assert.NotNull(res); | ||
Assert.Equal(4,res.Value); | ||
|
||
|
||
r = Parser.Parse("2 x"); | ||
Assert.False(r.IsError); | ||
res = r.Result.Evaluate(new ExpressionContext(new Dictionary<string, double>() | ||
{ { "x", 2 } })); | ||
Assert.NotNull(res); | ||
Assert.Equal(4,res.Value); | ||
|
||
|
||
r = Parser.Parse("2 ( x ) "); | ||
Assert.False(r.IsError); | ||
res = r.Result.Evaluate(new ExpressionContext(new Dictionary<string, double>() | ||
{ { "x", 2 } })); | ||
Assert.NotNull(res); | ||
Assert.Equal(4,res.Value); | ||
|
||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using System; | ||
using expressionparser; | ||
using simpleExpressionParser; | ||
using sly.lexer; | ||
using sly.parser.generator; | ||
|
||
namespace Issue244 | ||
{ | ||
public class Issue244Parser | ||
{ | ||
[Operation((int) SimpleExpressionToken.PLUS, Affix.InFix, Associativity.Right, 10)] | ||
[Operation("MINUS", Affix.InFix, Associativity.Left, 10)] | ||
public Result BinaryTermExpression(Result left, Token<SimpleExpressionToken> operation, Result right) | ||
{ | ||
// Get the length of the expression : | ||
int length = right.EndIndex - left.StartIndex; | ||
|
||
|
||
Result result = null; | ||
switch (operation.TokenID) | ||
{ | ||
case SimpleExpressionToken.PLUS: | ||
{ | ||
result = new Result() | ||
{ | ||
Value = left.Value + right.Value, | ||
StartColumn = left.StartColumn, | ||
EndColumn = right.EndColumn, | ||
StartIndex = left.StartIndex, | ||
EndIndex = right.EndIndex | ||
} | ||
; | ||
break; | ||
} | ||
case SimpleExpressionToken.MINUS: | ||
{ | ||
result = new Result() | ||
{ | ||
Value = left.Value - right.Value, | ||
StartColumn = left.StartColumn, | ||
EndColumn = right.EndColumn, | ||
StartIndex = left.StartIndex, | ||
EndIndex = right.EndIndex | ||
}; | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
[Operation((int) SimpleExpressionToken.TIMES, Affix.InFix, Associativity.Right, 50)] | ||
[Operation("DIVIDE", Affix.InFix, Associativity.Left, 50)] | ||
public Result BinaryFactorExpression(Result left, Token<SimpleExpressionToken> operation, Result right) | ||
{ | ||
Result result = null; | ||
switch (operation.TokenID) | ||
{ | ||
case SimpleExpressionToken.TIMES: | ||
{ | ||
result = new Result() | ||
{ | ||
Value = left.Value * right.Value, | ||
StartColumn = left.StartColumn, | ||
EndColumn = right.EndColumn, | ||
StartIndex = left.StartIndex, | ||
EndIndex = right.EndIndex | ||
}; | ||
break; | ||
} | ||
case SimpleExpressionToken.DIVIDE: | ||
{ | ||
result = new Result() | ||
{ | ||
Value = left.Value / right.Value, | ||
StartColumn = left.StartColumn, | ||
EndColumn = right.EndColumn, | ||
StartIndex = left.StartIndex, | ||
EndIndex = right.EndIndex | ||
}; | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
[Operation((int) SimpleExpressionToken.MINUS, Affix.PreFix, Associativity.Right, 100)] | ||
public Result PreFixExpression(Token<SimpleExpressionToken> operation, Result value) | ||
{ | ||
return new Result() | ||
{ | ||
Value = -value.Value, | ||
StartColumn = operation.Position.Column, | ||
EndColumn = value.EndColumn, | ||
StartIndex = operation.Position.Index, | ||
EndIndex = value.EndIndex | ||
}; | ||
} | ||
|
||
|
||
|
||
[Operand] | ||
[Production("operand : primary_value")] | ||
public Result OperandValue(Result value) | ||
{ | ||
return value; | ||
} | ||
|
||
|
||
[Production("primary_value : DOUBLE")] | ||
public Result OperandDouble(Token<SimpleExpressionToken> value) | ||
{ | ||
return new Result() | ||
{ | ||
Value = value.DoubleValue, | ||
StartColumn = value.Position.Column, | ||
EndColumn = value.Position.Column + value.Value.Length, | ||
StartIndex = value.Position.Index, | ||
EndIndex = value.Position.Index + value.Value.Length | ||
}; | ||
} | ||
|
||
[Production("primary_value : INT")] | ||
public Result OperandInt(Token<SimpleExpressionToken> value) | ||
{ | ||
return new Result() | ||
{ | ||
Value = value.DoubleValue, | ||
StartColumn = value.Position.Column, | ||
EndColumn = value.Position.Column + value.Value.Length, | ||
StartIndex = value.Position.Index, | ||
EndIndex = value.Position.Index + value.Value.Length | ||
}; | ||
} | ||
|
||
[Production("primary_value : LPAREN Issue244Parser_expressions RPAREN")] | ||
public Result OperandParens(Token<SimpleExpressionToken> lparen, Result value, Token<SimpleExpressionToken> rparen) | ||
{ | ||
return new Result() | ||
{ | ||
Value = value.Value, | ||
StartColumn = lparen.Position.Column, | ||
EndColumn = rparen.Position.Column + rparen.Value.Length, | ||
StartIndex = rparen.Position.Index, | ||
EndIndex = rparen.Position.Index + rparen.Value.Length, | ||
}; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.