Skip to content

Commit e8ccc8e

Browse files
committed
more repeat test
1 parent e098de2 commit e8ccc8e

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

tests/ParserTests/EBNFTests.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,35 @@
2020

2121
namespace ParserTests
2222
{
23-
23+
public enum BasicToken
24+
{
25+
[AlphaId]
26+
ID,
27+
28+
[Int]
29+
INT,
30+
31+
[Sugar(".")]
32+
PERIOD
33+
}
34+
35+
public class RepeatParser
36+
{
37+
[Production("root : thing{0-6} PERIOD[d]")]
38+
public string Root(List<string> things)
39+
{
40+
return string.Join(",", things);
41+
}
42+
43+
[Production("thing : ID INT")]
44+
public string Thing(Token<BasicToken> id, Token<BasicToken> integer)
45+
{
46+
return $"{id.Value}({integer.IntValue})";
47+
}
48+
}
49+
50+
51+
2452
[Lexer(IgnoreWS = true, IgnoreEOL = true)]
2553
public enum DoNotIgnoreCommentsTokenWithChannels
2654
{
@@ -1818,6 +1846,33 @@ public void TestNotClosingIndents()
18181846
Check.That(error.ErrorType).IsEqualTo(ErrorType.UnexpectedEOS);
18191847
}
18201848

1821-
1849+
1850+
[Fact]
1851+
public void TestRepeat()
1852+
{
1853+
ParserBuilder<BasicToken, string> builder = new ParserBuilder<BasicToken, string>();
1854+
var instance = new RepeatParser();
1855+
var parserRes = builder.BuildParser(instance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");
1856+
Check.That(parserRes).IsOk();
1857+
1858+
var parser = parserRes.Result;
1859+
Check.That(parser).IsNotNull();
1860+
var parseResult = parser.Parse("a1b2c3d4e5.");
1861+
Check.That(parseResult).IsOkParsing();
1862+
Check.That(parseResult.Result).IsEqualTo("a(1),b(2),c(3),d(4),e(5)");
1863+
parseResult = parser.Parse(".");
1864+
Check.That(parseResult).IsOkParsing();
1865+
Check.That(parseResult.Result).IsEqualTo("");
1866+
parseResult = parser.Parse("a1b2c3d4e5f6g7");
1867+
Check.That(parseResult).Not.IsOkParsing();
1868+
Check.That(parseResult.Errors).CountIs(1);
1869+
var error = parseResult.Errors[0] as UnexpectedTokenSyntaxError<BasicToken>;
1870+
Check.That(error).IsNotNull();
1871+
Check.That(error.ErrorType).IsEqualTo(ErrorType.UnexpectedToken);
1872+
1873+
Check.That(error.UnexpectedToken.TokenID).IsEqualTo(BasicToken.ID);
1874+
Check.That(error.UnexpectedToken.Value).IsEqualTo("g");
1875+
1876+
}
18221877
}
18231878
}

0 commit comments

Comments
 (0)