Skip to content

Commit

Permalink
Merge pull request #29 from b3b00/bugfix/various
Browse files Browse the repository at this point in the history
Bugfix/various
  • Loading branch information
b3b00 committed Jun 6, 2024
2 parents 6e71145 + d752900 commit 1da648d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
61 changes: 60 additions & 1 deletion Tester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public static List<string> GetLines(this string content)

public static void Main(string[] args)
{
Compile("C:\\Users\\olduh\\dev\\BlazorCslyViz\\BlazorVizView\\samples\\grammar\\indented-while.txt");
//Compile("C:\\Users\\olduh\\dev\\BlazorCslyViz\\BlazorVizView\\samples\\grammar\\indented-while.txt");
// Extract(@"C:\Users\olduh\dev\csly\src\samples\jsonparser\EbnfJsonGenericParser.cs","C:\\Users\\olduh\\dev\\csly\\src\\samples\\jsonparser\\JsonTokenGeneric.cs", @"C:\Users\olduh\dev\BlazorCslyViz\BlazorVizView\samples\grammar\json.txt");
Parse(@"C:\Users\olduh\dev\BlazorCslyViz\BlazorVizView\samples\grammar\json.txt", @"C:\Users\olduh\dev\BlazorCslyViz\BlazorVizView\samples\source\json.txt");
}


Expand Down Expand Up @@ -93,5 +95,62 @@ private static void Compile(string path)
}

}

private static void Extract(string parserPath, string lexrerPath, string outputPath = null)
{
var e = CslyProcessor.ExtractGrammar(File.ReadAllText(parserPath), File.ReadAllText(lexrerPath));
if (e.IsOK)
{
if (outputPath != null)
{
File.WriteAllText(outputPath, e.Result);
}

Console.WriteLine("extraction ok");
var r = CslyProcessor.Compile(e.Result);
if (r.IsOK)
{
Console.WriteLine("compilation ok");
}
else
{
foreach (var error in r.Errors)
{
Console.WriteLine(error);
}
}
}
else
{
foreach (var error in e.Errors)
{
Console.WriteLine(error);
}
}

}

private static void Parse(string grammarPath, string sourcePath, string outputPath = null)
{
var e = CslyProcessor.GetDot(File.ReadAllText(grammarPath), File.ReadAllText(sourcePath));
if (e.IsOK)
{
if (outputPath != null)
{
File.WriteAllText(outputPath, e.Result);
}

Console.WriteLine("parse ok");

}
else
{
foreach (var error in e.Errors)
{
Console.WriteLine(error);
}
}

}
}
}
21 changes: 19 additions & 2 deletions csly-cli-extractor/LexerSpecificationExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ private string Lexeme(string name, GenericToken type, params string[] args)
}
case GenericToken.KeyWord:
{
return $"[KeyWord] {name} : \"{args[0].Replace("\"","\\\"")}\";";
string definitions = string.Join(" ", args.Select(x => "\"" + x.Replace("\"", "\\\"") + "\""));

return $"[KeyWord] {name} : {definitions};";
}
case GenericToken.SugarToken:
{
Expand Down Expand Up @@ -315,7 +317,22 @@ public string ExtractFromSource(string lexerSource)
string[] pstrings = new string[] { };
if (attr?.ArgumentList?.Arguments != null && attr.ArgumentList.Arguments.Any())
{
pstrings = attr.ArgumentList.Arguments.Select(x => x.Expression.ExprToString())
Predicate<AttributeArgumentSyntax> filter = e =>
{
if (e.NameColon != null && e.NameColon.Name.Identifier.Text == "channel")
{
return false;
}
if (e.NameColon != null && e.NameEquals.Name.Identifier.Text == "channel")
{
return false;
}
return true;
};

pstrings = attr.ArgumentList.Arguments.Where(x => filter(x)).Select(x => x.Expression.ExprToString())
.ToArray();
}

Expand Down
12 changes: 11 additions & 1 deletion csly-cli-parser/CLIParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public ICLIModel ModeDefault(ParserContext context)


[Production(
"token : attribute* LEFTBRACKET[d] [KEYWORDTOKEN|SUGARTOKEN|SINGLELINECOMMENT] RIGHTBRACKET[d] ID COLON[d] STRING SEMICOLON[d]")]
"token : attribute* LEFTBRACKET[d] [SUGARTOKEN|SINGLELINECOMMENT] RIGHTBRACKET[d] ID COLON[d] STRING SEMICOLON[d]")]
public ICLIModel OneArgToken(List<ICLIModel> attributes, Token<CLIToken> type, Token<CLIToken> id, Token<CLIToken> value, ParserContext context)
{
var tokenType = type.TokenID switch
Expand Down Expand Up @@ -161,6 +161,16 @@ public ICLIModel TwoArgToken(List<ICLIModel> attributes, Token<CLIToken> type, T
return new TokenModel(attributes.Cast<AttributeModel>().ToList(), tokenType,id.Value,arg1.StringWithoutQuotes.Replace("\\\\","\\"), arg2.StringWithoutQuotes.Replace("\\\\","\\")) {Position = type.Position};
}

[Production(
"token : attribute* LEFTBRACKET[d] [KEYWORDTOKEN] RIGHTBRACKET[d] ID COLON[d] STRING* SEMICOLON[d]")]
public ICLIModel ManyArgToken(List<ICLIModel> attributes, Token<CLIToken> type, Token<CLIToken> id, List<Token<CLIToken>> args, ParserContext context)
{
var tokenType = GenericToken.KeyWord;
context.AddEnumName(id.Value);
var aargs = args.Select(arg => arg.StringWithoutQuotes.Replace("\\\\", "\\")).ToArray();
return new TokenModel(attributes.Cast<AttributeModel>().ToList(), tokenType,id.Value,aargs) {Position = type.Position};
}

[Production(
"token :attribute* LEFTBRACKET[d] DATETOKEN[d] RIGHTBRACKET[d] ID COLON[d] [DDMMYYYY|YYYYMMDD] CHAR SEMICOLON[d]")]
public ICLIModel DateToken(List<ICLIModel> attributes, Token<CLIToken> id, Token<CLIToken> dateType, Token<CLIToken> separator, ParserContext context)
Expand Down

0 comments on commit 1da648d

Please sign in to comment.