Skip to content

Commit

Permalink
Fix zero or more end pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jul 22, 2018
1 parent 4247582 commit 9956bb0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
53 changes: 53 additions & 0 deletions tests/test_textparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ def test_zero_or_more(self):
tree = grammar.parse(tokens)
self.assertEqual(tree, expected_tree)

def test_zero_or_more_end(self):
grammar = Grammar(
Sequence(ZeroOrMore('WORD', Sequence('WORD', 'NUMBER')),
Sequence('WORD', 'NUMBER')))

datas = [
(
[('WORD', 'bar'), ('NUMBER', '1')],
[[], ['bar', '1']]
),
(
[('WORD', 'foo'), ('WORD', 'bar'), ('NUMBER', '1')],
[['foo'], ['bar', '1']]
)
]

for tokens, expected_tree in datas:
tokens = tokenize(tokens + [('__EOF__', '')])
tree = grammar.parse(tokens)
self.assertEqual(tree, expected_tree)

def test_one_or_more(self):
grammar = Grammar(OneOrMore('WORD'))

Expand Down Expand Up @@ -157,6 +178,38 @@ def test_one_or_more_mismatch(self):

self.assertEqual(str(cm.exception), '')

def test_one_or_more_end(self):
grammar = Grammar(
Sequence(OneOrMore('WORD', Sequence('WORD', 'NUMBER')),
Sequence('WORD', 'NUMBER')))

datas = [
(
[('WORD', 'foo'), ('WORD', 'bar'), ('NUMBER', '1')],
[['foo'], ['bar', '1']]
)
]

for tokens, expected_tree in datas:
tokens = tokenize(tokens + [('__EOF__', '')])
tree = grammar.parse(tokens)
self.assertEqual(tree, expected_tree)

def test_one_or_more_end_mismatch(self):
grammar = Grammar(OneOrMore('WORD', Sequence('WORD', 'NUMBER')))

datas = [
[('WORD', 'bar'), ('NUMBER', '1')]
]

for tokens in datas:
tokens = tokenize(tokens + [('__EOF__', '')])

with self.assertRaises(textparser.Error) as cm:
grammar.parse(tokens)

self.assertEqual(str(cm.exception), '')

def test_tokenizer_error(self):
datas = [
(2, 'hej', 'he>>!<<j'),
Expand Down
4 changes: 3 additions & 1 deletion textparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ def match(self, tokens):
try:
while True:
if self._end is not None:
tokens.save()
mo = _match_item(self._end, tokens)
tokens.restore()

if mo is None:
if mo is not None:
break

mo = _match_item(self._element, tokens)
Expand Down

0 comments on commit 9956bb0

Please sign in to comment.