Minor tweaks on Parser

This commit is contained in:
Ruediger Ludwig 2022-12-08 07:42:26 +01:00
parent 4ddef44cc2
commit 64e3eb063c
4 changed files with 86 additions and 69 deletions

View file

@ -112,7 +112,7 @@ def test_choice2():
def test_seq():
input = '1234'
parser = P.seq(P.any_char(), P.any_char(), P.any_char(), P.any_char())
parser = P.seq(P.one_char(), P.one_char(), P.one_char(), P.one_char())
expected = ('1', '2', '3', '4')
result = parser.parse(input).get()
assert result == expected
@ -120,7 +120,7 @@ def test_seq():
def test_seq_seq():
input = '1,2,3,4'
digit = P.char_by_func(lambda c: c.isdigit(), "")
digit = P.char_func(lambda c: c.isdigit(), )
parser = P.sep_seq(digit, digit, digit, digit, sep=P.is_char(','))
expected = ('1', '2', '3', '4')
@ -165,3 +165,19 @@ def test_seq_eof():
expected = [(['a', 'a'], ())]
result = list(parser.parse_multi(input))
assert result == expected
def test_optional():
input = '12'
parser = P.seq(P.is_char('1').optional(), P.unsigned())
expected = [('1', 2), (None, 12), (None, 1)]
result = list(parser.parse_multi(input))
assert result == expected
def test_choice():
input = '1'
parser = P.choice(P.is_char('1'), P.is_char('b'), P.unsigned())
expected = ['1', 1]
result = list(parser.parse_multi(input))
assert result == expected