Skip to content

Commit

Permalink
#146 : alternate non terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Jan 2, 2020
1 parent 3236506 commit aa1130a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
53 changes: 50 additions & 3 deletions ParserTests/EBNFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public int TestTerminal(List<Token<GroupTestToken>> options, Token<GroupTestToke
}


public class AlternateChoiceTest
public class AlternateChoiceTestTerminal
{
[Production("choice : [ a | b | c]")]
public string Choice(Token<OptionTestToken> c)
Expand All @@ -228,6 +228,34 @@ public string C(Token<OptionTestToken> c)

}

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

[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 @@ -729,10 +757,10 @@ public void TestBug104()
}

[Fact]
public void TestAlternateChoice()
public void TestAlternateChoiceTerminal()
{
var startingRule = $"choice";
var parserInstance = new AlternateChoiceTest();
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);
Expand All @@ -746,6 +774,25 @@ public void TestAlternateChoice()
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);
parseResult = builtParser.Result.Parse("b", "choice");
Assert.True(parseResult.IsOk);
parseResult = builtParser.Result.Parse("c", "choice");
Assert.True(parseResult.IsOk);
parseResult = builtParser.Result.Parse("d", "choice");
Assert.False(parseResult.IsOk);
}

[Fact]
public void TestAlternateChoiceErrorMixedTerminalAndNonTerminal()
Expand Down
7 changes: 4 additions & 3 deletions sly/parser/generator/ParserBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,12 @@ private static BuildResult<Parser<IN, OUT>> CheckAlternates(BuildResult<Parser<I
{
if (clause is ChoiceClause<IN> choice)
{
bool allTheSame = choice.Choices.Select(c => c is TerminalClause<IN>).Aggregate((x, y) => x && y);
if (!allTheSame)
bool allTerminals = choice.Choices.Select(c => c is TerminalClause<IN>).Aggregate((x, y) => x && y);
bool allNonTerminals = choice.Choices.Select(c => c is NonTerminalClause<IN>).Aggregate((x, y) => x && y);
if (!allNonTerminals && !allTerminals)
{
result.AddError(new ParserInitializationError(ErrorLevel.ERROR,
$"{rule.RuleString} contains {choice.ToString()} with mixed terminal and nonterminal."));
$"{rule.ToString()} contains {choice.ToString()} with mixed terminal and nonterminal."));
}
}
}
Expand Down

0 comments on commit aa1130a

Please sign in to comment.