-
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.
generator : explicit tokens unit tests
- Loading branch information
Showing
15 changed files
with
372 additions
and
100 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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\sly\sly.csproj" OutputItemType="Analyzer"/> | ||
</ItemGroup> | ||
</Project> |
89 changes: 89 additions & 0 deletions
89
src/samples/ExplicitTokens/ExplicitTokensExpressionParser.cs
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,89 @@ | ||
using sly.lexer; | ||
using sly.parser.generator; | ||
|
||
namespace ExplicitTokens; | ||
|
||
[ParserRoot("ExplicitTokensExpressionParser_expressions")] | ||
public class ExplicitTokensExpressionParser | ||
{ | ||
[Production("primary: DOUBLE")] | ||
[Operand] | ||
public double Primary(Token<ExplicitTokensTokens> doubleToken) | ||
{ | ||
return doubleToken.DoubleValue; | ||
} | ||
|
||
[Operand] | ||
[Production("primary : 'bozzo'[d]")] | ||
public double Bozzo() | ||
{ | ||
return 42.0; | ||
} | ||
|
||
[Operand] | ||
[Production("primary : TEST[d]")] | ||
public double Test() | ||
{ | ||
return 0.0; | ||
} | ||
|
||
|
||
[Infix("'+'",Associativity.Left, 10)] | ||
[Infix("'-'", Associativity.Left, 10)] | ||
public double BinaryTermExpression(double left, Token<ExplicitTokensTokens> operation, double right) | ||
{ | ||
switch (operation.Value) | ||
{ | ||
case "+" : return left + right; | ||
case "-" : return left - right; | ||
default : throw new InvalidOperationException($"that is not possible ! {operation.Value} is not a valid operation"); | ||
} | ||
} | ||
|
||
[Operation((int) ExplicitTokensTokens.TIMES, Affix.InFix, Associativity.Left, 50)] | ||
[Operation("DIVIDE", Affix.InFix, Associativity.Left, 50)] | ||
public double BinaryFactorExpression(double left, Token<ExplicitTokensTokens> operation, double right) | ||
{ | ||
double result = 0; | ||
switch (operation.TokenID) | ||
{ | ||
case ExplicitTokensTokens.TIMES: | ||
{ | ||
result = left * right; | ||
break; | ||
} | ||
case ExplicitTokensTokens.DIVIDE: | ||
{ | ||
result = left / right; | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
[Prefix("'-'", Associativity.Right, 100)] | ||
public double PreFixExpression(Token<ExplicitTokensTokens> operation, double value) | ||
{ | ||
return -value; | ||
} | ||
|
||
[Postfix("'!'", Associativity.Left, 110)] | ||
public double PostFixExpression(double value, Token<ExplicitTokensTokens> operation) | ||
{ | ||
double factorial = 1; | ||
for (int i = 0; i < value; i++) | ||
{ | ||
factorial *= i; | ||
} | ||
|
||
return factorial; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/samples/ExplicitTokens/ExplicitTokensExpressionParserGenerator.cs
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,10 @@ | ||
using sly.sourceGenerator; | ||
|
||
namespace ExplicitTokens; | ||
|
||
|
||
[ParserGenerator(typeof(ExplicitTokensTokens), typeof(ExplicitTokensExpressionParser),typeof(double))] | ||
public partial class ExplicitTokensExpressionParserGenerator : AbstractParserGenerator<ExplicitTokensTokens> | ||
{ | ||
|
||
} |
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,63 @@ | ||
using sly.lexer; | ||
using sly.parser.generator; | ||
|
||
namespace ExplicitTokens; | ||
|
||
[ParserRoot("expression")] | ||
public class ExplicitTokensParser | ||
{ | ||
[Production("primary: DOUBLE")] | ||
public double Primary(Token<ExplicitTokensTokens> doubleToken) | ||
{ | ||
return doubleToken.DoubleValue; | ||
} | ||
|
||
[Production("primary : 'bozzo'[d]")] | ||
public double Bozzo() | ||
{ | ||
return 42.0; | ||
} | ||
|
||
[Production("primary : TEST[d]")] | ||
public double Test() | ||
{ | ||
return 0.0; | ||
} | ||
|
||
|
||
[Production("expression : primary ['+' | '-'] expression")] | ||
|
||
|
||
public double Expression(double left, Token<ExplicitTokensTokens> operatorToken, double right) | ||
{ | ||
double result = 0.0; | ||
|
||
|
||
switch (operatorToken.StringWithoutQuotes) | ||
{ | ||
case "+": | ||
{ | ||
result = left + right; | ||
break; | ||
} | ||
case "-": | ||
{ | ||
result = left - right; | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
[Production("expression : primary ")] | ||
public double Simple(double value) | ||
{ | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using sly.sourceGenerator; | ||
|
||
namespace ExplicitTokens; | ||
|
||
[ParserGenerator(typeof(ExplicitTokensTokens), typeof(ExplicitTokensParser), typeof(double))] | ||
public partial class ExplicitTokensParserGenerator : AbstractParserGenerator<ExplicitTokensTokens> | ||
{ | ||
|
||
} |
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,27 @@ | ||
using sly.lexer; | ||
|
||
namespace ExplicitTokens; | ||
|
||
[Lexer(IgnoreWS = true, IgnoreEOL = true)] | ||
public enum ExplicitTokensTokens | ||
{ | ||
[MultiLineComment("/*","*/")] | ||
MULTILINECOMMENT = 1, | ||
|
||
[SingleLineComment("//")] | ||
SINGLELINECOMMENT = 2, | ||
|
||
[Lexeme(GenericToken.Identifier, IdentifierType.AlphaNumeric)] | ||
ID = 3, | ||
|
||
[Lexeme(GenericToken.Double, channel:101)] | ||
DOUBLE = 4, | ||
|
||
[Keyword("Test")] | ||
TEST = 5, | ||
[Sugar("*")] | ||
TIMES = 6, | ||
|
||
[Sugar("/")] | ||
DIVIDE = 7 | ||
} |
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
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.