Skip to content

Commit

Permalink
Merge pull request #252 from b3b00/feature/short-operation-attributes
Browse files Browse the repository at this point in the history
shorter attributes for operations
  • Loading branch information
b3b00 authored Nov 5, 2021
2 parents c7472b4 + c3b7c1d commit 0c456a0
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 6 deletions.
104 changes: 103 additions & 1 deletion ParserTests/ExpressionGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,93 @@ public double BinaryTermExpression(double left, Token<ExpressionToken> operation
}

}



public class ShortOperationAttributesParser
{
[Infix((int) ExpressionToken.PLUS, Associativity.Right, 10)]
[Infix("MINUS", Associativity.Left, 10)]
public double BinaryTermExpression(double left, Token<ExpressionToken> operation, double right)
{
double result = 0;
switch (operation.TokenID)
{
case ExpressionToken.PLUS:
{
result = left + right;
break;
}
case ExpressionToken.MINUS:
{
result = left - right;
break;
}
}

return result;
}


[Infix((int) ExpressionToken.TIMES, Associativity.Right, 50)]
[Infix("DIVIDE", Associativity.Left, 50)]
public double BinaryFactorExpression(double left, Token<ExpressionToken> operation, double right)
{
double result = 0;
switch (operation.TokenID)
{
case ExpressionToken.TIMES:
{
result = left * right;
break;
}
case ExpressionToken.DIVIDE:
{
result = left / right;
break;
}
}

return result;
}


[Prefix((int) ExpressionToken.MINUS, Associativity.Right, 100)]
public double PreFixExpression(Token<ExpressionToken> operation, double value)
{
return -value;
}

[Postfix((int) ExpressionToken.FACTORIAL, Associativity.Right, 100)]
public double PostFixExpression(double value, Token<ExpressionToken> operation)
{
var factorial = 1;
for (var i = 1; i <= value; i++) factorial = factorial * i;
return factorial;
}



[Operand]
[Production("double_value : DOUBLE")]
public double OperandDouble(Token<ExpressionToken> value)
{
return value.DoubleValue;
}

[Operand]
[Production("int_value : INT")]
public double OperandInt(Token<ExpressionToken> value)
{
return value.DoubleValue;
}

[Operand]
[Production("group : LPAREN ShortOperationAttributesParser_expressions RPAREN")]
public double OperandGroup(Token<ExpressionToken> lparen, double value, Token<ExpressionToken> rparen)
{
return value;
}
}

public class ExpressionGeneratorTests
{
Expand Down Expand Up @@ -249,5 +335,21 @@ public void TestBadOperatorString()
builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, StartingRule));
Assert.Contains("bad enum name MINUSCULE",exception.Message);
}

[Fact]
public void TestShortOperationAttributes()
{
StartingRule = $"{typeof(ShortOperationAttributesParser).Name}_expressions";
var parserInstance = new ShortOperationAttributesParser();
var builder = new ParserBuilder<ExpressionToken, double>();
var buildResult = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, StartingRule);
Assert.True(buildResult.IsOk);
var parser = buildResult.Result;
Assert.NotNull(parser);
var result = parser.Parse("-1 +2 * (5 + 6) - 4 ");
Assert.True(result.IsOk);
Assert.Equal(-1+2*(5+6)-4, result.Result);

}
}
}
16 changes: 15 additions & 1 deletion samples/ParserExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ private static void Main(string[] args)
// TestI18N();
// Console.ReadLine();
// TestShortGeneric();
TestIssue239();
//TestIssue239();
TestShortOperations();
}


Expand Down Expand Up @@ -967,6 +968,19 @@ private static void TestShortGeneric()
;
}

private static void TestShortOperations()
{
var startingRule = $"{typeof(ShortOperationAttributesParser).Name}_expressions";
var parserInstance = new ShortOperationAttributesParser();
var builder = new ParserBuilder<ExpressionToken, double>();
var buildResult = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, startingRule);
Assert.True(buildResult.IsOk);
var parser = buildResult.Result;
Assert.NotNull(parser);
var result = parser.Parse("-1 +2 * (5 + 6) - 4 ");
Assert.Equal(-1+2*(5+6)-4, result.Result);
}

private static void TestIssue239()
{
Issue239Tests.TestOk();
Expand Down
8 changes: 4 additions & 4 deletions sly/parser/generator/ExpressionRulesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ public BuildResult<ParserConfiguration<IN, OUT>> BuildExpressionRules(
ParserConfiguration<IN, OUT> configuration, Type parserClass,
BuildResult<ParserConfiguration<IN, OUT>> result)
{

var methods = parserClass.GetMethods().ToList();
methods = methods.Where(m =>
{
var attributes = m.GetCustomAttributes().ToList();
var attr = attributes.Find(a => a.GetType() == typeof(OperationAttribute));
return attr != null;
var attributes = m.GetCustomAttributes(typeof(OperationAttribute),true).ToList();

return attributes.Any();
}).ToList();

var operationsByPrecedence = new Dictionary<int, List<OperationMetaData<IN>>>();

methods.ForEach(m =>
{
var attributes =
Expand Down
13 changes: 13 additions & 0 deletions sly/parser/generator/InfixAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace sly.parser.generator
{
public class InfixAttribute : OperationAttribute
{
public InfixAttribute(int intToken, Associativity assoc, int precedence) : base(intToken,Affix.InFix,assoc,precedence)
{
}

public InfixAttribute(string stringToken, Associativity assoc, int precedence) : base(stringToken,Affix.InFix, assoc,precedence)
{
}
}
}
13 changes: 13 additions & 0 deletions sly/parser/generator/PostfixAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace sly.parser.generator
{
public class PostfixAttribute : OperationAttribute
{
public PostfixAttribute(int intToken, Associativity assoc, int precedence) : base(intToken,Affix.PostFix,assoc,precedence)
{
}

public PostfixAttribute(string stringToken, Associativity assoc, int precedence) : base(stringToken,Affix.PostFix, assoc,precedence)
{
}
}
}
13 changes: 13 additions & 0 deletions sly/parser/generator/PrefixAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace sly.parser.generator
{
public class PrefixAttribute : OperationAttribute
{
public PrefixAttribute(int intToken, Associativity assoc, int precedence) : base(intToken,Affix.PreFix,assoc,precedence)
{
}

public PrefixAttribute(string stringToken, Associativity assoc, int precedence) : base(stringToken,Affix.PreFix, assoc,precedence)
{
}
}
}

0 comments on commit 0c456a0

Please sign in to comment.