Add tagged model patterns

This commit is contained in:
Kodi Arfer 2018-05-29 14:36:10 -07:00
parent 5ffbb4b0eb
commit 7a40561db8

View File

@ -9,6 +9,7 @@ from funcparserlib.parser import (
some, skip, many, finished, a, Parser, NoParseError, State) some, skip, many, finished, a, Parser, NoParseError, State)
from functools import reduce from functools import reduce
from itertools import repeat from itertools import repeat
from collections import namedtuple
from operator import add from operator import add
from math import isinf from math import isinf
@ -74,3 +75,11 @@ def times(lo, hi, parser):
end = e.state.max end = e.state.max
return result, State(s.pos, end) return result, State(s.pos, end)
return f return f
Tag = namedtuple('Tag', ['tag', 'value'])
def tag(tag_name, parser):
"""Matches the given parser and produces a named tuple `(Tag tag value)`
with `tag` set to the given tag name and `value` set to the parser's
value."""
return parser >> (lambda x: Tag(tag_name, x))