[parser] one_or_more

Finally making lists easier
This commit is contained in:
John Doty 2024-11-10 18:47:24 -08:00
parent 682ff4e5fc
commit 3bffe98df0
2 changed files with 85 additions and 3 deletions

View file

@ -2,7 +2,7 @@ import pytest
import parser.runtime as runtime
from parser import Grammar, seq, rule, Terminal
from parser import Grammar, seq, rule, Terminal, one_or_more
class Tokens:
@ -169,3 +169,40 @@ def test_grammar_ignore_trivia():
),
),
)
def test_one_or_more():
@rule
def sentence():
return one_or_more(WORD)
WORD = Terminal("WORD", "blah")
BLANK = Terminal("BLANK", " ")
table = Grammar(start=sentence, trivia=[BLANK]).build_table()
assert "BLANK" in table.trivia
tree, errors = runtime.Parser(table).parse(Tokens(WORD, BLANK, WORD, BLANK))
assert errors == []
assert tree == runtime.Tree(
"sentence",
0,
3,
(
runtime.TokenValue(
"WORD",
0,
1,
[],
[runtime.TokenValue("BLANK", 1, 2, [], [])],
),
runtime.TokenValue(
"WORD",
2,
3,
[runtime.TokenValue("BLANK", 1, 2, [], [])],
[runtime.TokenValue("BLANK", 3, 4, [], [])],
),
),
)