def expect_match(self, pattern, on_error=None):
"""
Require @item to match at the current `position`.
Raises a ValueError if @item does not match.
@pattern
A regular expression.
@on_error
A function that returns an error. The error returned overrides the
default ValueError.
"""
m = re.compile(pattern).match(self.source, self.position)
if m:
self.position += m.end() - m.start()
return m
if not on_error:
raise ValueError('expected match with \'{0}\', at \'{1}\''.format(pattern, self.source[self.position:]))
raise on_error()
评论列表
文章目录