Skip to content

Commit

Permalink
generator : explicit tokens unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Sep 10, 2024
1 parent 997fb28 commit 97c8e1b
Show file tree
Hide file tree
Showing 15 changed files with 372 additions and 100 deletions.
7 changes: 7 additions & 0 deletions sly.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CslyGenerator.Tests", "test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneratedXML", "src\samples\GeneratedXML\GeneratedXML.csproj", "{7FEF0D32-FFC7-4886-A4B7-71314C25627E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExplicitTokens", "src\samples\ExplicitTokens\ExplicitTokens.csproj", "{94B9B654-82CB-4969-8DCA-250916E53724}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
Expand Down Expand Up @@ -138,6 +140,10 @@ Global
{7FEF0D32-FFC7-4886-A4B7-71314C25627E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FEF0D32-FFC7-4886-A4B7-71314C25627E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FEF0D32-FFC7-4886-A4B7-71314C25627E}.Release|Any CPU.Build.0 = Release|Any CPU
{94B9B654-82CB-4969-8DCA-250916E53724}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{94B9B654-82CB-4969-8DCA-250916E53724}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94B9B654-82CB-4969-8DCA-250916E53724}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94B9B654-82CB-4969-8DCA-250916E53724}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -155,6 +161,7 @@ Global
{81E72CFA-A6D6-4DB4-B3B5-E167064E7858} = {36ED7F1A-2E81-4A71-81FE-A357E5840A33}
{BC5020CA-6BFC-4895-9A1E-99B4A0AEDB11} = {36ED7F1A-2E81-4A71-81FE-A357E5840A33}
{7FEF0D32-FFC7-4886-A4B7-71314C25627E} = {36ED7F1A-2E81-4A71-81FE-A357E5840A33}
{94B9B654-82CB-4969-8DCA-250916E53724} = {36ED7F1A-2E81-4A71-81FE-A357E5840A33}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {43254130-CF3E-480E-952F-E50CA5D2E417}
Expand Down
11 changes: 11 additions & 0 deletions src/samples/ExplicitTokens/ExplicitTokens.csproj
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 src/samples/ExplicitTokens/ExplicitTokensExpressionParser.cs
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;
}






}
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>
{

}
63 changes: 63 additions & 0 deletions src/samples/ExplicitTokens/ExplicitTokensParser.cs
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;
}




}
9 changes: 9 additions & 0 deletions src/samples/ExplicitTokens/ExplicitTokensParserGenerator.cs
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>
{

}
27 changes: 27 additions & 0 deletions src/samples/ExplicitTokens/ExplicitTokensTokens.cs
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
}
1 change: 1 addition & 0 deletions tests/CslyGenerator.Tests/CslyGenerator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<EmbeddedResource Include="data\callbacks.txt" />
<EmbeddedResource Include="data\explicits.txt" />
<EmbeddedResource Include="data\expression.txt" />
<EmbeddedResource Include="data\extended.txt" />
<EmbeddedResource Include="data\labels.txt" />
Expand Down
1 change: 1 addition & 0 deletions tests/CslyGenerator.Tests/SourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private ImmutableArray<SyntaxTree> generateSource(string source, string classNam
[InlineData("/data/callbacks.txt", "ParserCallbacksGenerator")]
[InlineData("/data/labels.txt", "DuplicateLabelLexerGenerator")]
[InlineData("/data/expression.txt", "ExpressionParserGenerator")]
[InlineData("/data/explicits.txt", "ExplicitTokensExpressionParserGenerator")]
public void TestGenerator(string source, string className)
{
var code = _embeddedResourceFileSystem.ReadAllText(source);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
using System;
using expressionparser;
using sly.lexer;
using sly.parser.generator;
[ParserGenerator(typeof(ExplicitTokensTokens), typeof(ExplicitTokensExpressionParser),typeof(double))]
public class ExplicitTokensExpressionParserGenerator : AbstractParserGenerator<ExplicitTokensTokens>
{

}

namespace ParserTests
[Lexer(IgnoreWS = true, IgnoreEOL = true)]
public enum ExplicitTokensTokens
{
public class ExplicitTokensExpressionParser
[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
}


public class ExplicitTokensExpressionParser
{
[Production("primary: DOUBLE")]
[Operand]
Expand Down Expand Up @@ -87,5 +111,4 @@ public double PostFixExpression(double value, Token<ExplicitTokensTokens> operat



}
}
}
Loading

0 comments on commit 97c8e1b

Please sign in to comment.