Skip to content

Commit

Permalink
Merge pull request #147 from b3b00/feature/#146-alternate-operator
Browse files Browse the repository at this point in the history
Feature/#146 alternate operator
  • Loading branch information
b3b00 authored Jan 2, 2020
2 parents 9906162 + 21c9bdc commit 9e5b551
Show file tree
Hide file tree
Showing 13 changed files with 646 additions and 67 deletions.
112 changes: 112 additions & 0 deletions ParserTests/EBNFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,59 @@ public int TestTerminal(List<Token<GroupTestToken>> options, Token<GroupTestToke
}


public class AlternateChoiceTestTerminal
{
[Production("choice : [ a | b | c]")]
public string Choice(Token<OptionTestToken> c)
{
return c.Value;
}
}

public class AlternateChoiceTestError
{
[Production("choice : [ a | b | C]")]
public string Choice(Token<OptionTestToken> c)
{
return c.Value;
}

[Production("C : c")]
public string C(Token<OptionTestToken> c)
{
return c.Value;
}

}

public class AlternateChoiceTestNonTerminal
{
[Production("choice : [ A | B | C]")]
public string Choice(string c)
{
return c;
}

[Production("C : c")]
public string C(Token<OptionTestToken> t)
{
return $"C({t.Value})";
}

[Production("B : b")]
public string B(Token<OptionTestToken> t)
{
return $"B({t.Value})";
}

[Production("A : a")]
public string A(Token<OptionTestToken> t)
{
return $"A({t.Value})";
}

}

public class Bugfix104Test
{
[Production("testNonTerm : sub (COMMA[d] unreachable)? ")]
Expand Down Expand Up @@ -295,6 +348,8 @@ public string B(List<Token<TokenType>> bstr)
return "B()";
}




private Parser<TokenType, string> Parser;

Expand Down Expand Up @@ -700,5 +755,62 @@ public void TestBug104()
Assert.False(builtParser.IsError);
Assert.False(builtParser.Errors.Any());
}

[Fact]
public void TestAlternateChoiceTerminal()
{
var startingRule = $"choice";
var parserInstance = new AlternateChoiceTestTerminal();
var builder = new ParserBuilder<OptionTestToken, string>();
var builtParser = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, startingRule);
Assert.False(builtParser.IsError);
Assert.False(builtParser.Errors.Any());
var parseResult = builtParser.Result.Parse("a", "choice");
Assert.True(parseResult.IsOk);
Assert.Equal("a",parseResult.Result);
parseResult = builtParser.Result.Parse("b", "choice");
Assert.True(parseResult.IsOk);
Assert.Equal("b",parseResult.Result);
parseResult = builtParser.Result.Parse("c", "choice");
Assert.True(parseResult.IsOk);
Assert.Equal("c",parseResult.Result);
parseResult = builtParser.Result.Parse("d", "choice");
Assert.False(parseResult.IsOk);
}

[Fact]
public void TestAlternateChoiceNonTerminal()
{
var startingRule = $"choice";
var parserInstance = new AlternateChoiceTestNonTerminal();
var builder = new ParserBuilder<OptionTestToken, string>();
var builtParser = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, startingRule);
Assert.False(builtParser.IsError);
Assert.False(builtParser.Errors.Any());
var parseResult = builtParser.Result.Parse("a", "choice");
Assert.True(parseResult.IsOk);
Assert.Equal("A(a)",parseResult.Result);
parseResult = builtParser.Result.Parse("b", "choice");
Assert.True(parseResult.IsOk);
Assert.Equal("B(b)",parseResult.Result);
parseResult = builtParser.Result.Parse("c", "choice");
Assert.True(parseResult.IsOk);
Assert.Equal("C(c)",parseResult.Result);
parseResult = builtParser.Result.Parse("d", "choice");
Assert.False(parseResult.IsOk);
}

[Fact]
public void TestAlternateChoiceErrorMixedTerminalAndNonTerminal()
{
var startingRule = $"choice";
var parserInstance = new AlternateChoiceTestError();
var builder = new ParserBuilder<OptionTestToken, string>();
var builtParser = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, startingRule);
Assert.True(builtParser.IsError);
Assert.Single(builtParser.Errors);
Assert.Contains("mixed", builtParser.Errors.First().Message);

}
}
}
90 changes: 87 additions & 3 deletions samples/ParserExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,17 +520,101 @@ private static void TestGrammarParser()

}


public static void TestScript()
{
var parserInstance = new ScriptParser();
var builder = new ParserBuilder<ScriptToken, object>();
var parserBuild = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "test");
if (parserBuild.IsOk)
{
var parser = parserBuild.Result;
string ok1 = @"|B|study(""Name or something"", overlay)|E|";
string ok2 = "|B|test(close, 123, open)|E|";
string kw = "|B|test(kw=123)|E|";
string ko1 = "|B|test2(a, b, c=100)|E|";
string ko2 = "|B|plotshape(data, style=shapexcross)|E|";

// var r = parser.Parse(ok1);
// r = parser.Parse(ok2);
// r = parser.Parse(kw);
// var r = parser.Parse("a, b, c=100", "fun_actual_args2");
// var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken>();

var r = parser.Parse(ko1);
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken>();
var root = graphviz.VisitTree(r.SyntaxTree);
var graph = graphviz.Graph.Compile();
r = parser.Parse(ko2);
}
else
{
foreach (var e in parserBuild.Errors)
{
Console.WriteLine(e.Level+ " - " + e.Message);
}
}
}

public static void TestScriptRight()
{
var parserInstance = new ScriptParserRight();
var builder = new ParserBuilder<ScriptToken, object>();
var parserBuild = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "test");
if (parserBuild.IsOk)
{
var parser = parserBuild.Result;
string ok1 = @"|B|study(""Name or something"", overlay)|E|";
string ok2 = "|B|test(close, 123, open)|E|";
string kw = "|B|test(kw=123)|E|";
string ko1 = "|B|test2(a, b, c=100, d=200)|E|";
string ko2 = "|B|plotshape(data, style=shapexcross)|E|";
string ko3 = "|B|plotshape(data = default, style=shapexcross)|E|";
string badmixko = "|B|plotshape(data = default, t, y=20)|E|";

// var r = parser.Parse(ok1);
// r = parser.Parse(ok2);
// r = parser.Parse(kw);
var r = parser.Parse("a, b, c=100, d= 200", "fun_actual_args");
var graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken>();
var root1 = graphviz.VisitTree(r.SyntaxTree);
var graph1 = graphviz.Graph.Compile();

r = parser.Parse(ko1);
graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken>();
var root = graphviz.VisitTree(r.SyntaxTree);
var graph = graphviz.Graph.Compile();
r = parser.Parse(ko3);
graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken>();
root = graphviz.VisitTree(r.SyntaxTree);
graph = graphviz.Graph.Compile();
r = parser.Parse(badmixko);
graphviz = new GraphVizEBNFSyntaxTreeVisitor<ScriptToken>();
root = graphviz.VisitTree(r.SyntaxTree);
graph = graphviz.Graph.Compile();
}
else
{
foreach (var e in parserBuild.Errors)
{
Console.WriteLine(e.Level+ " - " + e.Message);
}
}
}

private static void Main(string[] args)
{
//TestContextualParser();
//TestTokenCallBacks();
//test104();
//testJSON();
TestGrammarParser();
//TestGrammarParser();
// TestGraphViz();
//TestGraphViz();
//TestChars();

// TestGraphViz();
// TestChars();
TestScript();
TestScriptRight();
}
}

Expand Down
Loading

0 comments on commit 9e5b551

Please sign in to comment.