Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cli api #19

Merged
merged 7 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -48,16 +48,26 @@ 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>(.*)<\/version>
PROJECT_FILE_PATH: ${{env.MAIN_CSPROJ}}
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>(.*)<\/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
Expand Down
15 changes: 15 additions & 0 deletions CslyCliApiTester/CslyCliApiTester.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>ConsoleApp2</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\csly-cli-api\csly-cli-api.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions CslyCliApiTester/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
}
}
12 changes: 12 additions & 0 deletions LexerOptions.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
10 changes: 10 additions & 0 deletions ParserOPtimization.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
9 changes: 6 additions & 3 deletions Tester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using csly.cli.parser;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using sly.lexer;
using sly.parser.generator;

namespace cslyCliTester {

Expand All @@ -33,7 +31,13 @@ public static List<string> GetLines(this string content)

public static void Main(string[] args)
{
TestDate();
}



private static void TestDate()
{
var grammar = @"
genericLexer MinimalLexer;
[Date] DATE : YYYYMMDD '.';
Expand Down Expand Up @@ -72,7 +76,6 @@ public static void Main(string[] args)
Console.WriteLine($"token is bad {dateTime} - expected 2024.04.23");
}
}

}
}
}
2 changes: 1 addition & 1 deletion Tester/Tester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="sly" Version="3.1.0" />
<PackageReference Include="sly" Version="3.1.4" />
<ProjectReference Include="..\csly-cli-builder\csly-cli-builder.csproj" />
<ProjectReference Include="..\csly-cli-model\csly-cli-model.csproj" />
<ProjectReference Include="..\csly-cli-parser\csly-cli-parser.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="sly" Version="3.1.0" />
<PackageReference Include="SharpCoreFileSystem" Version="1.0.4" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NFluent" Version="3.0.4" />
<PackageReference Include="sly" Version="3.1.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
3 changes: 3 additions & 0 deletions Tests/data/minimalJSON.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"IsEpsilon": false,
"Children": [
{
"IsEpsilon": false,
"Token": {
"Channel": 0,
"Position": {
Expand All @@ -21,6 +23,7 @@
"IsEOS": false,
"IsIndent": false,
"IsUnIndent": false,
"IsIndentation": false,
"IndentationLevel": 0,
"IsWhiteSpace": false,
"IsEOL": false,
Expand Down
83 changes: 83 additions & 0 deletions csly-cli-api/CslyProcessor.cs
Original file line number Diff line number Diff line change
@@ -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<string> _errors;

public List<string> Errors => _errors;

public CliResult(List<string> 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());
}
}
}
18 changes: 18 additions & 0 deletions csly-cli-api/csly-cli-api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>csly_cli_api</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\csly-cli-builder\csly-cli-builder.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="sly" Version="3.1.4" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions csly-cli-builder/ParserBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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[] { });

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion csly-cli-builder/csly-cli-builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="sly" Version="3.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0-4.final" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="sly" Version="3.1.4" />
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
</ItemGroup>

Expand Down
7 changes: 1 addition & 6 deletions csly-cli-extractor/csly-cli-extractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0-4.final" />
<PackageReference Include="sly" Version="3.1.4" />

</ItemGroup>



<ItemGroup>
<PackageReference Include="sly" Version="3.1.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion csly-cli-model/csly-cli-model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="sly" Version="3.1.0" />
<PackageReference Include="sly" Version="3.1.4" />
</ItemGroup>


Expand Down
9 changes: 9 additions & 0 deletions csly-cli-model/parser/IndentClause.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace csly.cli.model.parser;

public class IndentClause : IClause
{
public override string ToString()
{
return "INDENT";
}
}
2 changes: 2 additions & 0 deletions csly-cli-model/parser/ParserModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
1 change: 0 additions & 1 deletion csly-cli-model/parser/PrefixRule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using sly.parser.generator;

namespace csly.cli.model.parser
{
Expand Down
Loading
Loading