Skip to content

Commit cea2935

Browse files
authored
Merge pull request #520 from b3b00/feature/api-fluent
Feature/api fluent
2 parents 2c99852 + 448bd48 commit cea2935

File tree

68 files changed

+2549
-742
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2549
-742
lines changed

src/samples/ParserExample/Program.cs

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -478,17 +478,6 @@ private static void testErrors()
478478
var column = error.Column; // 12
479479
}
480480

481-
private static void TestRuleParser()
482-
{
483-
Console.WriteLine("hum hum...");
484-
var parserInstance = new RuleParser<EbnfToken>();
485-
var builder = new ParserBuilder<EbnfToken, IClause<EbnfToken>>();
486-
var r = builder.BuildParser(parserInstance, ParserType.LL_RECURSIVE_DESCENT, "rule");
487-
488-
var parser = r.Result;
489-
var rule = parser.Parse("a ( b ) ", "clauses");
490-
}
491-
492481

493482
public static BuildResult<Parser<ExpressionToken, int>> buildSimpleExpressionParserWithContext()
494483
{
@@ -737,7 +726,7 @@ private static void TestGraphViz()
737726
var parser = builder.BuildParser(parserInstance, ParserType.LL_RECURSIVE_DESCENT, StartingRule);
738727
var result = parser.Result.Parse("2 + 2 * 3");
739728
var tree = result.SyntaxTree;
740-
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ExpressionToken>();
729+
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ExpressionToken, int>();
741730
var root = graphviz.VisitTree(tree);
742731
string graph = graphviz.Graph.Compile();
743732
// File.Delete("c:\\temp\\tree.dot");
@@ -783,32 +772,6 @@ private static void TestChars()
783772
}
784773
}
785774

786-
private static void TestGrammarParser()
787-
{
788-
string productionRule = "clauses : clause (COMMA [D] clause)*";
789-
var ruleparser = new RuleParser<TestGrammarToken>();
790-
var builder = new ParserBuilder<EbnfTokenGeneric, GrammarNode<TestGrammarToken>>();
791-
var grammarParser = builder.BuildParser(ruleparser, ParserType.LL_RECURSIVE_DESCENT, "rule").Result;
792-
var result = grammarParser.Parse(productionRule);
793-
//(grammarParser.Lexer as GenericLexer<TestGrammarToken>).ResetLexer();
794-
Console.WriteLine($"alors ? {string.Join('\n',result.Errors.Select(e => e.ErrorMessage))}");
795-
result = grammarParser.Parse(productionRule);
796-
Console.WriteLine($"alors ? {string.Join('\n',result.Errors.Select(e => e.ErrorMessage))}");
797-
;
798-
799-
Console.WriteLine("starting");
800-
ErroneousGrammar parserInstance = new ErroneousGrammar();
801-
Console.WriteLine("new instance");
802-
803-
var builder2 = new ParserBuilder<TestGrammarToken, object>();
804-
Console.WriteLine("builder");
805-
806-
var Parser = builder.BuildParser(parserInstance,ParserType.EBNF_LL_RECURSIVE_DESCENT,"rule");
807-
Console.WriteLine($"built : {Parser.IsOk}");
808-
809-
810-
}
811-
812775
public static void TestRecursion()
813776
{
814777
var builder = new ParserBuilder<TestGrammarToken, object>();
@@ -861,7 +824,7 @@ public static void TestScript()
861824
string ko2 = "|B|plotshape(data, style=shapexcross)|E|";
862825

863826
var r = parser.Parse(ko1);
864-
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken>();
827+
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken, object>();
865828
var root = graphviz.VisitTree(r.SyntaxTree);
866829
var graph = graphviz.Graph.Compile();
867830
r = parser.Parse(ko2);
@@ -1195,8 +1158,8 @@ public static void TestIndentedParserNeverEnding()
11951158

11961159
var parseResult = parser.Parse(source);
11971160

1198-
GraphVizEBNFSyntaxTreeVisitor<IndentedLangLexer> grapher =
1199-
new GraphVizEBNFSyntaxTreeVisitor<IndentedLangLexer>();
1161+
GraphVizEBNFSyntaxTreeVisitor<IndentedLangLexer, Ast> grapher =
1162+
new GraphVizEBNFSyntaxTreeVisitor<IndentedLangLexer, Ast>();
12001163
grapher.VisitTree(parseResult.SyntaxTree);
12011164
var graph = grapher.Graph.Compile();
12021165
//File.WriteAllText(@"c:\tmp\graph.dot", graph);
@@ -1624,7 +1587,7 @@ private static void NodeNames()
16241587
else
16251588
{
16261589
;
1627-
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ExpressionToken>();
1590+
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ExpressionToken, int>();
16281591
var root = graphviz.VisitTree(r.SyntaxTree);
16291592
string graph = graphviz.Graph.Compile();
16301593
File.Delete("c:\\temp\\tree.dot");

src/sly/lexer/CallBacksBuilder.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45

@@ -41,5 +42,42 @@ public static void AddCallback<IN>(GenericLexer<IN> lexer, MethodInfo method, IN
4142
var callbackDelegate = (Func<Token<IN>,Token<IN>>)Delegate.CreateDelegate(typeof(Func<Token<IN>,Token<IN>>), method);
4243
lexer.AddCallBack(token,callbackDelegate);
4344
}
45+
46+
public static List<(IN tokenId,Func<Token<IN>, Token<IN>> callback)> GetCallbacks<IN>(GenericLexer<IN> lexer) where IN : struct
47+
{
48+
List<(IN,Func<Token<IN>, Token<IN>>)> callbacks = new List<(IN,Func<Token<IN>, Token<IN>>)>();
49+
var classAttributes =
50+
(CallBacksAttribute[]) typeof(IN).GetCustomAttributes(typeof(CallBacksAttribute), true);
51+
if (classAttributes.Any())
52+
{
53+
Type callbackClass = classAttributes[0].CallBacksClass;
54+
if (callbackClass != null)
55+
{
56+
57+
var methods = callbackClass.GetMethods().ToList();
58+
methods = methods.Where<MethodInfo>(m =>
59+
{
60+
var attributes = m.GetCustomAttributes().ToList();
61+
var attr = attributes.Find(a => a.GetType() == typeof(TokenCallbackAttribute));
62+
return m.IsStatic && attr != null;
63+
}).ToList<MethodInfo>();
64+
65+
foreach (var method in methods)
66+
{
67+
var attributes = method.GetCustomAttributes(typeof(TokenCallbackAttribute), false)
68+
.Cast<TokenCallbackAttribute>().ToList();
69+
70+
foreach (var attr in attributes)
71+
{
72+
IN tokenId = EnumConverter.ConvertIntToEnum<IN>(attr.EnumValue);
73+
var callback = (Func<Token<IN>,Token<IN>>)Delegate.CreateDelegate(typeof(Func<Token<IN>,Token<IN>>), method);
74+
callbacks.Add((tokenId,callback));
75+
}
76+
}
77+
}
78+
}
79+
80+
return callbacks;
81+
}
4482
}
4583
}

0 commit comments

Comments
 (0)