-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
186 changed files
with
225,463 additions
and
1,120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ jobs: | |
output: 'lcov.net7.0.info' | ||
threshold: 80 | ||
outputFormat: 'lcov' | ||
excludes: '[program]*,[expressionParser]*,[jsonparser]*,[while]*,[indentedWhile]*,[SimpleExpressionParser]*,[GenericLexerWithCallbacks]*,[indented]*,[postProcessedLexerParser]*,[XML]*,[SimpleTemplate]*' | ||
excludes: '[program]*,[expressionParser]*,[jsonparser]*,[while]*,[indentedWhile]*,[SimpleExpressionParser]*,[GenericLexerWithCallbacks]*,[indented]*,[postProcessedLexerParser]*,[XML]*,[SimpleTemplate]*,[GeneratedXML]*,[ExplicitTokens]*,[postProcessedLexerParser]*,[GenericLexerWithCallbacks]*' | ||
|
||
- name: coveralls | ||
uses: coverallsapp/[email protected] | ||
|
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<RootNamespace>CslyGenerator.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2"/> | ||
<PackageReference Include="xunit" Version="2.4.2"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\sly\sly.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
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,255 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using sly.sourceGenerator; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Xunit; | ||
|
||
namespace CslyGenerator.Tests; | ||
|
||
public class SourceGeneratorTests | ||
{ | ||
private const string parserClassTest = $@" | ||
using aot.lexer; | ||
using sly.lexer; | ||
using sly.parser.generator; | ||
namespace TestNamespace; | ||
public enum AotTestLexer | ||
{{ | ||
[Double] | ||
DOUBLE, | ||
[AlphaId] | ||
IDENTIFIER, | ||
[Sugar(""+"")] | ||
PLUS, | ||
[Sugar(""++"")] | ||
INCREMENT, | ||
[Sugar(""-"")] | ||
MINUS, | ||
[Sugar(""*"")] | ||
TIMES, | ||
[Sugar(""/"")] | ||
DIVIDE, | ||
[Sugar(""("")] | ||
LPAREN, | ||
[Sugar("")"")] | ||
RPAREN, | ||
[Sugar(""!"")] | ||
FACTORIAL, | ||
[Push(""carré"")] | ||
[Sugar(""²"")] | ||
SQUARE, | ||
[Mode(""carré"")] | ||
[Lexeme(""$-$"")] | ||
REGEX, | ||
[Lexeme(GenericToken.KeyWord,""hello"")] | ||
HELLO, | ||
[Lexeme(GenericToken.String)] | ||
STR, | ||
[Lexeme(GenericToken.Int)] | ||
I, | ||
[MultiLineComment(""<!--"",""-->"",channel:Channels.Main)] | ||
[Mode] | ||
COMMENT, | ||
}} | ||
[ParserRoot(""root"")] | ||
[UseMemoization] | ||
[UseBroadentokenWindow] | ||
[AutoCloseIndentationsAttribute] | ||
public class AotTestParser | ||
{{ | ||
[Production(""root : SimpleExpressionParser_expressions"")] | ||
public double Root(double value) => value; | ||
[Operation((int) AotTestLexer.PLUS, Affix.InFix, Associativity.Right, 10)] | ||
[Operation(""MINUS"", Affix.InFix, Associativity.Left, 10)] | ||
public double BinaryTermExpression(double left, Token<AotTestLexer> operation, double right) | ||
{{ | ||
double result = 0; | ||
switch (operation.TokenID) | ||
{{ | ||
case AotTestLexer.PLUS: | ||
{{ | ||
result = left + right; | ||
break; | ||
}} | ||
case AotTestLexer.MINUS: | ||
{{ | ||
result = left - right; | ||
break; | ||
}} | ||
}} | ||
return result; | ||
}} | ||
[Operation((int) AotTestLexer.TIMES, Affix.InFix, Associativity.Right, 50)] | ||
[Operation(""DIVIDE"", Affix.InFix, Associativity.Left, 50)] | ||
[NodeName(""multiplication_or_division"")] | ||
public double BinaryFactorExpression(double left, Token<AotTestLexer> operation, double right) | ||
{{ | ||
double result = 0; | ||
switch (operation.TokenID) | ||
{{ | ||
case AotTestLexer.TIMES: | ||
{{ | ||
result = left * right; | ||
break; | ||
}} | ||
case AotTestLexer.DIVIDE: | ||
{{ | ||
result = left / right; | ||
break; | ||
}} | ||
}} | ||
return result; | ||
}} | ||
[Prefix((int) AotTestLexer.MINUS, Associativity.Right, 100)] | ||
public double PreFixExpression(Token<AotTestLexer> operation, double value) | ||
{{ | ||
return -value; | ||
}} | ||
[Postfix((int) AotTestLexer.FACTORIAL, Associativity.Right, 100)] | ||
public double PostFixExpression(double value, Token<AotTestLexer> operation) | ||
{{ | ||
if (operation.TokenID == AotTestLexer.SQUARE) | ||
{{ | ||
return value * value; | ||
}} | ||
if(operation.TokenID == AotTestLexer.FACTORIAL || operation.Value == ""!"") | ||
{{ | ||
var factorial = 1; | ||
for (var i = 1; i <= value; i++) factorial *= i; | ||
return factorial; | ||
}} | ||
return value; | ||
}} | ||
[Operand] | ||
[Production(""operand : primary_value"")] | ||
[NodeName(""double"")] | ||
public double OperandValue(double value) | ||
{{ | ||
return value; | ||
}} | ||
[Production(""primary_value : DOUBLE"")] | ||
[NodeName(""double"")] | ||
public double OperandDouble(Token<AotTestLexer> value) | ||
{{ | ||
return value.DoubleValue; | ||
}} | ||
[Production(""primary_value : INT"")] | ||
[NodeName(""integer"")] | ||
public double OperandInt(Token<AotTestLexer> value) | ||
{{ | ||
return value.DoubleValue; | ||
}} | ||
[Production(""primary_value : LPAREN SimpleExpressionParser_expressions RPAREN"")] | ||
[NodeName(""group"")] | ||
public double OperandGroup(Token<AotTestLexer> lparen, double value, Token<AotTestLexer> rparen) | ||
{{ | ||
return value; | ||
}} | ||
}} | ||
[ParserGenerator(typeof(AotTestLexer), typeof(AotTestParser), typeof(double))] | ||
public partial class TestGenerator | ||
{{ | ||
}} | ||
public enum ExtendedLexer | ||
{{ | ||
[Extension] | ||
EXT, | ||
[Sugar(""-"")] | ||
DASH, | ||
[AlphaNumId] | ||
ID | ||
}} | ||
public class ExtendedLexerParser {{ }} | ||
[ParserGenerator(typeof(ExtendedLexer),typeof(ExtendedLexerParser),typeof(string))] | ||
public partial class ExtendedLexerGenerator : AbstractParserGenerator<ExtendedLexer> | ||
{{ | ||
public override Action<ExtendedLexer, LexemeAttribute, GenericLexer<ExtendedLexer>> UseTokenExtensions() | ||
{{ | ||
var e = (ExtendedLexer token, LexemeAttribute lexem, GenericLexer<ExtendedLexer> lexer) => | ||
{{ | ||
if (token == ExtendedLexer.EXT) | ||
{{ | ||
NodeCallback<GenericToken> callback = (FSMMatch<GenericToken> match) => | ||
{{ | ||
match.Properties[GenericLexer<ExtendedLexer>.DerivedToken] = ExtendedLexer.EXT; | ||
return match; | ||
}}; | ||
var fsmBuilder = lexer.FSMBuilder; | ||
fsmBuilder.GoTo(GenericLexer<ExtendedLexer>.start) | ||
.Transition('$') | ||
.Transition('_') | ||
.Transition('$') | ||
.End(GenericToken.Extension) // mark as ending node | ||
.CallBack(callback); // set the ending callback | ||
}} | ||
}}; | ||
return e; | ||
}} | ||
}} | ||
"; | ||
|
||
[Fact] | ||
public void ParserGeneratorTest() | ||
{ | ||
// Create an instance of the source generator. | ||
var generator = new CslyParserGenerator(); | ||
|
||
// Source generators should be tested using 'GeneratorDriver'. | ||
var driver = CSharpGeneratorDriver.Create(new[] { generator }); | ||
|
||
// To run generators, we can use an empty compilation. | ||
var compilation = CSharpCompilation.Create(nameof(ParserGeneratorTest), | ||
new[] { CSharpSyntaxTree.ParseText(parserClassTest) }, | ||
new[] | ||
{ | ||
// To support 'System.Attribute' inheritance, add reference to 'System.Private.CoreLib'. | ||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location) | ||
}); | ||
|
||
// Run generators. Don't forget to use the new compilation rather than the previous one. | ||
var runResult = driver.RunGenerators(compilation).GetRunResult(); | ||
|
||
var generatedFiles = runResult.GeneratedTrees.Select(x => new FileInfo(x.FilePath).Name).ToArray(); | ||
|
||
Assert.Equivalent(new[] | ||
{ | ||
"TestGenerator.g.cs", | ||
"ExtendedLexerGenerator.g.cs" | ||
}, generatedFiles); | ||
} | ||
} |
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,20 @@ | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace CslyGenerator.Tests.Utils; | ||
|
||
public class TestAdditionalFile : AdditionalText | ||
{ | ||
private readonly SourceText _text; | ||
|
||
public TestAdditionalFile(string path, string text) | ||
{ | ||
Path = path; | ||
_text = SourceText.From(text); | ||
} | ||
|
||
public override SourceText GetText(CancellationToken cancellationToken = new()) => _text; | ||
|
||
public override string Path { get; } | ||
} |
Oops, something went wrong.