Skip to content

Commit

Permalink
Merge pull request #489 from b3b00/fix/explicit-groups
Browse files Browse the repository at this point in the history
- groups with explicit tokens.
  • Loading branch information
b3b00 authored Oct 14, 2024
2 parents 632bb1f + ac88eb0 commit e32365d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/sly/parser/generator/RuleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public GroupClause<IN> GroupClause(Token<EbnfTokenGeneric> id)
[Production("groupclause : STRING ")]
public GroupClause<IN> GroupClauseExplicit(Token<EbnfTokenGeneric> explicitToken)
{
var clause = BuildTerminalOrNonTerimal(explicitToken.Value,discard:true);
var clause = BuildTerminalOrNonTerimal(explicitToken.Value,discard:false);
return new GroupClause<IN>(clause);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, string no
while (i < rules.Count)
{
var innerrule = rules[i];
if (startPosition < tokens.Count && !tokens[startPosition].IsEOS &&
innerrule.Match(tokens, startPosition, Configuration))
if (startPosition < tokens.Count
&& (!tokens[startPosition].IsEOS || (tokens[startPosition].IsEOS && innerrule.MayBeEmpty))
&& innerrule.Match(tokens, startPosition, Configuration))
{
var innerRuleRes = Parse(tokens, innerrule, startPosition, nonTerminalName, parsingContext);
rulesResults.Add(innerRuleRes);
Expand All @@ -49,9 +50,10 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, string no
innerRuleRes.GetErrors().Count == 0 || other)
{
greaterIndex = innerRuleRes.EndingPosition;
if (innerRuleRes.GetErrors() != null)
if (innerRuleRes.GetErrors() != null)
innerRuleErrors.AddRange(innerRuleRes.GetErrors());
}

if (innerRuleRes.GetErrors() != null)
innerRuleErrors.AddRange(innerRuleRes.GetErrors());
}
Expand Down
58 changes: 58 additions & 0 deletions tests/ParserTests/ExplicitTokensTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using sly.parser;
using sly.parser.generator;
using sly.parser.generator.visitor;
using sly.parser.parser;
using Xunit;

namespace ParserTests
Expand All @@ -24,6 +25,31 @@ public enum Lex
Dbl = 2
}

public class ExplicitTokenGroupParser
{
[Production("root : ('a' 'b')* discard")]
public string Root(List<Group<Lex, string>> groups, string discard)
{

return string.Join(" ",groups.Select(group =>
{
var r = group.Token(0).Value + "-" + group.Token(1).Value;
return r;
}).ToList())+"_"+discard;
}

[Production("discard : ('c' 'd'[d])?")]
public string Discard(ValueOption<Group<Lex, string>> option)
{
return option.Match<string>(group => group.Token(0).Value + (group.Count == 1),
() => "empty");
}
}

public class list<T>

Check warning on line 49 in tests/ParserTests/ExplicitTokensTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type name 'list' only contains lower-cased ascii characters. Such names may become reserved for the language.

Check warning on line 49 in tests/ParserTests/ExplicitTokensTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The type name 'list' only contains lower-cased ascii characters. Such names may become reserved for the language.
{
}

public class Parse
{
[Production("program : statement*")]
Expand Down Expand Up @@ -185,6 +211,38 @@ public void Test()
//"(condition:(a == 1.0,(b = ( 1.0 + ( 2.0 * 3.0 ) )),(b = ( 2.0 + a ))))(c = 3.0)"
Check.That(r.Result).IsEqualTo("((condition:(a == 1.0,(b = ( 1.0 + ( 2.0 * 3.0 ) )),(b = ( 2.0 + a )))),(c = 3.0))");
}

[Fact]
public void TestExplicitGroups()
{
var instance = new ExplicitTokenGroupParser();
var builder = new ParserBuilder<Lex, string>();
var build = builder.BuildParser(instance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");
Check.That(build).IsOk();
var parser = build.Result;
Check.That(parser).IsNotNull();
var parsed = parser.Parse("a b a b c d");
Check.That(parsed).IsOkParsing();
var result = parsed.Result;
Check.That(result).IsNotNull();
Check.That(result).IsEqualTo("a-b a-b_cTrue");
}

[Fact]
public void TestExplicitGroups2()
{
var instance = new ExplicitTokenGroupParser();
var builder = new ParserBuilder<Lex, string>();
var build = builder.BuildParser(instance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");
Check.That(build).IsOk();
var parser = build.Result;
Check.That(parser).IsNotNull();
var parsed = parser.Parse("a b a b");
Check.That(parsed).IsOkParsing();
var result = parsed.Result;
Check.That(result).IsNotNull();
Check.That(result).IsEqualTo("a-b a-b_empty");
}

}

Expand Down

0 comments on commit e32365d

Please sign in to comment.