diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index 6be30d0..ce03a28 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -32,7 +32,7 @@ jobs: - name: Build with dotnet run: dotnet build --configuration Release - name: Test with dotnet - uses: b3b00/coverlet-action@1.2.4 + uses: b3b00/coverlet-action@1.3.0 id: 'coverlet' if: env.RUN_TESTS with: @@ -48,9 +48,9 @@ jobs: github-token: ${{secrets.GITHUB_TOKEN }} path-to-lcov: ${{steps.coverlet.outputs.coverageFile}} #base-path: ${{github.workspace}} - - name: publish nuget + - name: publish dotnet tool nuget if: ${{success() && matrix.os == 'windows-latest' && env.PUBLISH_NUGET}} - id: publish_nuget + id: publish__dotnet_tool_nuget uses: alirezanet/publish-nuget@v3.0.0 with: VERSION_REGEX: (.*)<\/version> @@ -58,6 +58,16 @@ jobs: NUGET_KEY: ${{secrets.NUGET_KEY}} VERSION_FILE_PATH: ${{env.MAIN_CSPROJ}} INCLUDE_SYMBOLS: true + - name: publish api nuget + if: ${{success() && matrix.os == 'windows-latest' && env.PUBLISH_NUGET}} + id: publish_nuget + uses: alirezanet/publish-nuget@v3.0.0 + with: + VERSION_REGEX: (.*)<\/version> + PROJECT_FILE_PATH: csly-cli-api/csly-cli-api.csproj + NUGET_KEY: ${{secrets.NUGET_KEY}} + VERSION_FILE_PATH: csly-cli-api/csly-cli-api.csproj + INCLUDE_SYMBOLS: true - name: Create Release if: ${{ success() && matrix.os == 'windows-latest' && steps.publish_nuget.outputs.VERSION != '' && steps.publish_nuget.outputs.VERSION != null }} id: create_release diff --git a/CslyCliApiTester/CslyCliApiTester.csproj b/CslyCliApiTester/CslyCliApiTester.csproj new file mode 100644 index 0000000..21e289e --- /dev/null +++ b/CslyCliApiTester/CslyCliApiTester.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + ConsoleApp2 + + + + + + + diff --git a/CslyCliApiTester/Program.cs b/CslyCliApiTester/Program.cs new file mode 100644 index 0000000..d881524 --- /dev/null +++ b/CslyCliApiTester/Program.cs @@ -0,0 +1,45 @@ +// See https://aka.ms/new-console-template for more information + + + +using csly_cli_api; + +namespace csly_cli_api +{ + + public class Program + { + + public static void Main(string[] args) + { + var grammar = @" +genericLexer MinimalLexer; +[Date] DATE : YYYYMMDD '-'; + +parser MinimalParser; + +-> root : DATE ; +"; + + string source = @" +2024-05-07 +"; + + + var r = CslyProcessor.GetDot(grammar, source); + if (r.IsOK) + { + Console.WriteLine(r.Result); + } + + else + { + foreach (var error in r.Errors) + { + Console.WriteLine(error); + } + } + + } + } +} \ No newline at end of file diff --git a/LexerOptions.cs b/LexerOptions.cs new file mode 100644 index 0000000..797f69e --- /dev/null +++ b/LexerOptions.cs @@ -0,0 +1,12 @@ +namespace csly.cli.model.parser; + +public class LexerOptions : ICLIModel +{ + public bool IgnoreWS { get; set; } + + public bool IgnoreEOL { get; set; } + + public bool IgnoreKeyWordCase { get; set; } + + public bool IndentationAware { get; set; } +} \ No newline at end of file diff --git a/ParserOPtimization.cs b/ParserOPtimization.cs new file mode 100644 index 0000000..0c718bd --- /dev/null +++ b/ParserOPtimization.cs @@ -0,0 +1,10 @@ +using sly.parser.generator; + +namespace csly.cli.model.parser; + +public class ParserOptimization : ICLIModel +{ + public bool UseMemoization { get; set; } + public bool BroadenTokenWindow { get; set; } + +} \ No newline at end of file diff --git a/Tester/Program.cs b/Tester/Program.cs index 8f5419b..7a4a1d9 100644 --- a/Tester/Program.cs +++ b/Tester/Program.cs @@ -7,8 +7,6 @@ using csly.cli.parser; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using sly.lexer; -using sly.parser.generator; namespace cslyCliTester { @@ -33,7 +31,13 @@ public static List GetLines(this string content) public static void Main(string[] args) { + TestDate(); + } + + + private static void TestDate() + { var grammar = @" genericLexer MinimalLexer; [Date] DATE : YYYYMMDD '.'; @@ -72,7 +76,6 @@ public static void Main(string[] args) Console.WriteLine($"token is bad {dateTime} - expected 2024.04.23"); } } - } } } \ No newline at end of file diff --git a/Tester/Tester.csproj b/Tester/Tester.csproj index 069d95e..6b1181d 100644 --- a/Tester/Tester.csproj +++ b/Tester/Tester.csproj @@ -15,7 +15,7 @@ - + diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 73e9572..8e28140 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -8,11 +8,11 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Tests/data/minimalJSON.json b/Tests/data/minimalJSON.json index f04d127..ad9d606 100644 --- a/Tests/data/minimalJSON.json +++ b/Tests/data/minimalJSON.json @@ -1,6 +1,8 @@ { + "IsEpsilon": false, "Children": [ { + "IsEpsilon": false, "Token": { "Channel": 0, "Position": { @@ -21,6 +23,7 @@ "IsEOS": false, "IsIndent": false, "IsUnIndent": false, + "IsIndentation": false, "IndentationLevel": 0, "IsWhiteSpace": false, "IsEOL": false, diff --git a/csly-cli-api/CslyProcessor.cs b/csly-cli-api/CslyProcessor.cs new file mode 100644 index 0000000..38bd5a6 --- /dev/null +++ b/csly-cli-api/CslyProcessor.cs @@ -0,0 +1,83 @@ +using clsy.cli.builder.parser; + +namespace csly_cli_api; + + + +public class CliResult +{ + private bool _isOk; + + public bool IsOK => _isOk; + + private string _result; + + public string Result => _result; + + private List _errors; + + public List Errors => _errors; + + public CliResult(List errors) + { + _isOk = false; + _errors = errors; + } + + public CliResult(string result) + { + _isOk = true; + _result = result; + } +} + +public class CslyProcessor +{ + public static CliResult GetDot(string grammar, string source) + { + var builder = new ParserBuilder(); + var model = builder.CompileModel(grammar, "MinimalParser"); + if (model.IsOk) + { + var r = builder.Getz(grammar, source, "TestParser", + new List<(string format, SyntaxTreeProcessor processor)>() + { ("DOT", (SyntaxTreeProcessor)ParserBuilder.SyntaxTreeToDotGraph) }); + if (r.IsError) + { + return new CliResult(r.Error.Select(x => $"parse error : {x}").ToList()); + } + else + { + return new CliResult(r.Value[0].content); + } + } + else + { + return new CliResult(model.Error.Select(x => $"grammar error : {x}").ToList()); + } + } + + public static CliResult GetJson(string grammar, string source) + { + var builder = new ParserBuilder(); + var model = builder.CompileModel(grammar, "MinimalParser"); + if (model.IsOk) + { + var r = builder.Getz(grammar, source, "TestParser", + new List<(string format, SyntaxTreeProcessor processor)>() + { ("JSON", (SyntaxTreeProcessor)ParserBuilder.SyntaxTreeToDotGraph) }); + if (r.IsError) + { + return new CliResult(r.Error.Select(x => $"parse error : {x}").ToList()); + } + else + { + return new CliResult(r.Value[0].content); + } + } + else + { + return new CliResult(model.Error.Select(x => $"grammar error : {x}").ToList()); + } + } +} \ No newline at end of file diff --git a/csly-cli-api/csly-cli-api.csproj b/csly-cli-api/csly-cli-api.csproj new file mode 100644 index 0000000..770ca4d --- /dev/null +++ b/csly-cli-api/csly-cli-api.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + csly_cli_api + enable + enable + + + + + + + + + + + diff --git a/csly-cli-builder/ParserBuilder.cs b/csly-cli-builder/ParserBuilder.cs index 2e6700f..f079ba9 100644 --- a/csly-cli-builder/ParserBuilder.cs +++ b/csly-cli-builder/ParserBuilder.cs @@ -124,6 +124,17 @@ public void AddParserOptimizations(TypeBuilder builder, ParserModel model) ConstructorInfo constructorInfo = attributeType.GetConstructor(new Type[] { }); + CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder( + constructorInfo, new object[] { }); + + builder.SetCustomAttribute(customAttributeBuilder); + } + if (model.AutoCloseIndentations) + { + Type attributeType = typeof(AutoCloseIndentationsAttribute); + + ConstructorInfo constructorInfo = attributeType.GetConstructor(new Type[] { }); + CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder( constructorInfo, new object[] { }); @@ -427,6 +438,14 @@ private Type BuildTypeParameter(IClause clause) { switch (clause) { + case IndentClause indentClause: + { + return TokenType; + } + case UIndentClause uIndentClause: + { + return TokenType; + } case TerminalClause terminal: { return TokenType; diff --git a/csly-cli-builder/csly-cli-builder.csproj b/csly-cli-builder/csly-cli-builder.csproj index a566dc2..f5f178d 100644 --- a/csly-cli-builder/csly-cli-builder.csproj +++ b/csly-cli-builder/csly-cli-builder.csproj @@ -13,9 +13,9 @@ - + diff --git a/csly-cli-extractor/csly-cli-extractor.csproj b/csly-cli-extractor/csly-cli-extractor.csproj index c2a60c2..1150e75 100644 --- a/csly-cli-extractor/csly-cli-extractor.csproj +++ b/csly-cli-extractor/csly-cli-extractor.csproj @@ -9,13 +9,8 @@ + - - - - - - diff --git a/csly-cli-model/csly-cli-model.csproj b/csly-cli-model/csly-cli-model.csproj index a436683..273584b 100644 --- a/csly-cli-model/csly-cli-model.csproj +++ b/csly-cli-model/csly-cli-model.csproj @@ -8,7 +8,7 @@ - + diff --git a/csly-cli-model/parser/IndentClause.cs b/csly-cli-model/parser/IndentClause.cs new file mode 100644 index 0000000..73fe996 --- /dev/null +++ b/csly-cli-model/parser/IndentClause.cs @@ -0,0 +1,9 @@ +namespace csly.cli.model.parser; + +public class IndentClause : IClause +{ + public override string ToString() + { + return "INDENT"; + } +} \ No newline at end of file diff --git a/csly-cli-model/parser/ParserModel.cs b/csly-cli-model/parser/ParserModel.cs index c099309..47926ce 100644 --- a/csly-cli-model/parser/ParserModel.cs +++ b/csly-cli-model/parser/ParserModel.cs @@ -8,6 +8,8 @@ public class ParserModel : ICLIModel public bool BroadenTokenWindow { get; set; } + public bool AutoCloseIndentations { get; set; } + public string Root => Rules.FirstOrDefault(x => x.IsRoot)?.NonTerminalName; public string Name { get; set; } } \ No newline at end of file diff --git a/csly-cli-model/parser/PrefixRule.cs b/csly-cli-model/parser/PrefixRule.cs index 30192c9..3ca1c5a 100644 --- a/csly-cli-model/parser/PrefixRule.cs +++ b/csly-cli-model/parser/PrefixRule.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using sly.parser.generator; namespace csly.cli.model.parser { diff --git a/csly-cli-model/parser/Rule.cs b/csly-cli-model/parser/Rule.cs index c342b8c..ea047b0 100644 --- a/csly-cli-model/parser/Rule.cs +++ b/csly-cli-model/parser/Rule.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using sly.parser.generator; namespace csly.cli.model.parser { @@ -42,5 +41,10 @@ public string Key public string NonTerminalName { get; set; } public bool IsRoot { get; set; } + + public override string ToString() + { + return $"{NonTerminalName} : {string.Join(" ", Clauses.Select(x => x.ToString()))}"; + } } } \ No newline at end of file diff --git a/csly-cli-model/parser/TerminalClause.cs b/csly-cli-model/parser/TerminalClause.cs index f597b4d..443d47e 100644 --- a/csly-cli-model/parser/TerminalClause.cs +++ b/csly-cli-model/parser/TerminalClause.cs @@ -1,7 +1,6 @@ using System.Diagnostics; using System.Text; using System.Diagnostics.CodeAnalysis; -using sly.lexer; namespace csly.cli.model.parser { diff --git a/csly-cli-model/parser/UIndentClause.cs b/csly-cli-model/parser/UIndentClause.cs new file mode 100644 index 0000000..9aa7284 --- /dev/null +++ b/csly-cli-model/parser/UIndentClause.cs @@ -0,0 +1,9 @@ +namespace csly.cli.model.parser; + +public class UIndentClause : IClause +{ + public override string ToString() + { + return "UINDENT"; + } +} \ No newline at end of file diff --git a/csly-cli-parser/csly-cli-parser.csproj b/csly-cli-parser/csly-cli-parser.csproj index 9a7f9a6..c779c4b 100644 --- a/csly-cli-parser/csly-cli-parser.csproj +++ b/csly-cli-parser/csly-cli-parser.csproj @@ -6,13 +6,10 @@ enable csly.cli.parser - - - - + - + diff --git a/csly-cli.sln b/csly-cli.sln index 0a3fdeb..eccb905 100644 --- a/csly-cli.sln +++ b/csly-cli.sln @@ -16,6 +16,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csly-cli-decompiler", "csly EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tester", "Tester\Tester.csproj", "{CECA9DD8-5BC9-4196-A8A1-C6D2D27AC3CC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csly-cli-api", "csly-cli-api\csly-cli-api.csproj", "{68EB1250-C4D1-4617-96A0-ECCEB8B2DEE3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CslyCliApiTester", "CslyCliApiTester\CslyCliApiTester.csproj", "{0769976A-12E8-4BEB-90CF-A6E9ED0FBDB2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,5 +62,13 @@ Global {CECA9DD8-5BC9-4196-A8A1-C6D2D27AC3CC}.Debug|Any CPU.Build.0 = Debug|Any CPU {CECA9DD8-5BC9-4196-A8A1-C6D2D27AC3CC}.Release|Any CPU.ActiveCfg = Release|Any CPU {CECA9DD8-5BC9-4196-A8A1-C6D2D27AC3CC}.Release|Any CPU.Build.0 = Release|Any CPU + {68EB1250-C4D1-4617-96A0-ECCEB8B2DEE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {68EB1250-C4D1-4617-96A0-ECCEB8B2DEE3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {68EB1250-C4D1-4617-96A0-ECCEB8B2DEE3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {68EB1250-C4D1-4617-96A0-ECCEB8B2DEE3}.Release|Any CPU.Build.0 = Release|Any CPU + {0769976A-12E8-4BEB-90CF-A6E9ED0FBDB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0769976A-12E8-4BEB-90CF-A6E9ED0FBDB2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0769976A-12E8-4BEB-90CF-A6E9ED0FBDB2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0769976A-12E8-4BEB-90CF-A6E9ED0FBDB2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/csly-cli/Program.cs b/csly-cli/Program.cs index 9bd72bc..4abe71d 100644 --- a/csly-cli/Program.cs +++ b/csly-cli/Program.cs @@ -7,7 +7,6 @@ using CommandLine; using decompiler; using sly.cli.options; -using sly.lexer; using specificationExtractor; @@ -144,7 +143,7 @@ private static int GenerateProject(GenerateProjectOptions generate) - + "; diff --git a/csly-cli/csly-cli.csproj b/csly-cli/csly-cli.csproj index 8346d0e..8f2b6d8 100644 --- a/csly-cli/csly-cli.csproj +++ b/csly-cli/csly-cli.csproj @@ -26,9 +26,7 @@ - - - +