Skip to content

Commit

Permalink
Merge e720d63 into 7c19df1
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Jun 9, 2022
2 parents 7c19df1 + e720d63 commit a2e6a36
Show file tree
Hide file tree
Showing 38 changed files with 872 additions and 197 deletions.
Binary file added .github/workflows/.dotnetcore.yml.un~
Binary file not shown.
1 change: 1 addition & 0 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- dev
- tech/actions
- master
- feature/implicit-tokens
pull_request:
branches:
- dev
Expand Down
103 changes: 103 additions & 0 deletions .github/workflows/dotnetcore.yml~
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: .NET Core

on:
push:
branches:
- dev
- tech/actions
- master
pull_request:
branches:
- dev
permissions:
pull-requests: write
jobs:
build:
env:
TESTS_PROJECT: 'ParserTests/ParserTests.csproj' # path to test project or solution
PUBLISH_NUGET: true # if true a nuget will be published on version change
RUN_TESTS: true # if true tests are run and coverage data is published to coveralls and a coverage report is produced.
MAIN_CSPROJ: 'sly/sly.csproj' # main project (for nuget packaging)
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101
- name: Clean artifacts and nugets
run: dotnet clean --configuration Release && dotnet nuget locals all --clear
- name: Build with dotnet
run: dotnet build --configuration Release
- name: Test with dotnet
uses: b3b00/coverlet-action@1.2.2
id: 'coverlet'
if: env.RUN_TESTS
with:
testProject: ${{env.TESTS_PROJECT}}
output: 'lcov.info'
threshold: 80
outputFormat: 'lcov'
excludes: '[program]*,[expressionParser]*,[jsonparser]*,[while]*,[indentedWhile]*,[SimpleExpressionParser]*,[GenericLexerWithCallbacks]*,[indented]*'
- name: coveralls
uses: coverallsapp/github-action@v1.1.1
if: matrix.os == 'windows-latest' && env.RUN_TESTS
with:
github-token: ${{secrets.GITHUB_TOKEN }}
path-to-lcov: ${{steps.coverlet.outputs.coverageFile}}
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@4.8.12
with:
reports: ${{steps.coverlet.outputs.coverageFile}}
targetdir: 'coveragereport'
reporttypes: 'HtmlInline;MarkdownSummary'
verbosity: 'Info' # The verbosity level of the log messages. Values: Verbose, Info, Warning, Error, Off
tag: '${{ github.run_number }}_${{ github.run_id }}'
# - name: Publish coverage summary
# uses: marocchino/sticky-pull-request-comment@v2
# with:
# path: coveragereport/Summary.md
- name: publish nuget
if: matrix.os == 'windows-latest' && env.PUBLISH_NUGET
id: publish_nuget
uses: brandedoutcast/publish-nuget@v2.5.2
with:
VERSION_REGEX: <version>(.*)<\/version>
PROJECT_FILE_PATH: ${{env.MAIN_CSPROJ}}
NUGET_KEY: ${{secrets.NUGET_KEY}}
VERSION_FILE_PATH: ${{env.MAIN_CSPROJ}}
- name: Create Release
if: ${{ success() && matrix.os == 'windows-latest' && steps.publish_nuget.outputs.VERSION != '' && steps.publish_nuget.outputs.VERSION != null }}
id: create_release
uses: actions/create-release@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.publish_nuget.outputs.VERSION }}
release_name: Release ${{ steps.publish_nuget.outputs.VERSION }}
draft: false
prerelease: false
- name: Upload Release Asset
if: ${{ success() && matrix.os == 'windows-latest' && steps.create_release.outputs.upload_url != '' && steps.create_release.outputs.upload_url != null }}
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ steps.publish_nuget.outputs.PACKAGE_PATH }}
asset_name: ${{ steps.publish_nuget.outputs.PACKAGE_NAME }}
asset_content_type: application/zip
- name: refresh coverage badge
uses: fjogeleit/http-request-action@master
with:
url: https://camo.githubusercontent.com/12c4fcb3b21fbb2a725fc61449fb1b91e972c4c8a2baaf5904936d8e334bdbe8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f62336230302f63736c792f62616467652e7376673f6272616e63683d64657626736572766963653d676974687562
method: PURGE
- name: refresh nuget badge
uses: fjogeleit/http-request-action@master
with:
url: https://camo.githubusercontent.com/b0c8ccfcb3256380ae37c312e789e6282143295fa495361ea5c2dbe2169bff8c/68747470733a2f2f696d672e736869656c64732e696f2f6e756765742f762f736c792e737667
method: PURGE
108 changes: 108 additions & 0 deletions ParserTests/ImplicitTokensExpressionParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using expressionparser;
using sly.lexer;
using sly.parser.generator;

namespace ParserTests
{
public class ImplicitTokensExpressionParser
{
[Production("primary: DOUBLE")]
[Operand]
public double Primary(Token<ImplicitTokensTokens> 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;
}


[Operation("'+'", Affix.InFix, Associativity.Left, 10)]
[Operation("'-'", Affix.InFix, Associativity.Left, 10)]
public double BinaryTermExpression(double left, Token<ImplicitTokensTokens> 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");
}
return 0;
}

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

return result;
}


[Operation("'-'", Affix.PreFix, Associativity.Right, 100)]
public double PreFixExpression(Token<ImplicitTokensTokens> operation, double value)
{
return -value;
}


public double Expression(double left, Token<ImplicitTokensTokens> 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;
}




}
}
63 changes: 63 additions & 0 deletions ParserTests/ImplicitTokensParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using sly.lexer;
using sly.parser.generator;

namespace ParserTests
{
public class ImplicitTokensParser
{
[Production("primary: DOUBLE")]
public double Primary(Token<ImplicitTokensTokens> 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<ImplicitTokensTokens> 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;
}




}
}
73 changes: 73 additions & 0 deletions ParserTests/ImplicitTokensTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.IO;
using sly.buildresult;
using sly.parser;
using sly.parser.generator;
using sly.parser.generator.visitor;
using Xunit;

namespace ParserTests
{
public class ImplicitTokensTests
{
private Parser<ImplicitTokensTokens, double> Parser;

private BuildResult<Parser<ImplicitTokensTokens, double>> BuildParser()
{
var parserInstance = new ImplicitTokensParser();
var builder = new ParserBuilder<ImplicitTokensTokens, double>();
var result = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "expression");
return result;
}

private BuildResult<Parser<ImplicitTokensTokens, double>> BuildExpressionParser()
{
var parserInstance = new ImplicitTokensExpressionParser();
var builder = new ParserBuilder<ImplicitTokensTokens, double>();
var result = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, nameof(ImplicitTokensExpressionParser)+"_expressions");
return result;
}

[Fact]
public void BuildParserTest()
{
var parser = BuildParser();
Assert.True(parser.IsOk);
Assert.NotNull(parser.Result);
var r = parser.Result.Parse("2.0 - 2.0 + bozzo + Test");
Assert.True(r.IsOk);
// grammar is left associative so expression really is
// (2.0 - (2.0 + (bozzo + Test))) = 2 - ( 2 + (42 + 0)) = 2 - (2 + 42) = 2 - 44 = -42
Assert.Equal(-42.0,r.Result);
}

[Fact]
public void BuildExpressionParserTest()
{
var parser = BuildExpressionParser();
Assert.True(parser.IsOk);
Assert.NotNull(parser.Result);
var r = parser.Result.Parse("2.0 - 2.0 + bozzo + Test");
var tree = r.SyntaxTree;
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ImplicitTokensTokens>();
var dump = tree.Dump("\t");
// File.Delete(@"c:\temp\tree.txt");
// File.WriteAllText(@"c:\temp\tree.txt",dump);
//
var json = $@"{{
{tree.ToJson()}
}}";
// File.Delete(@"c:\temp\tree.json");
// File.WriteAllText(@"c:\temp\tree.json",json);
//
var root = graphviz.VisitTree(tree);
string graph = graphviz.Graph.Compile();
// File.Delete("c:\\temp\\tree.dot");
// File.AppendAllText("c:\\temp\\tree.dot", graph);
Assert.True(r.IsOk);


Assert.Equal(2 - 2 + 42 + 0,r.Result);
}
}

}
28 changes: 28 additions & 0 deletions ParserTests/ImplicitTokensTokens.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using sly.lexer;

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

0 comments on commit a2e6a36

Please sign in to comment.