Commit 41c3a2e8 authored by Igor Dejanovic's avatar Igor Dejanovic

Support for skipws and ws params for Sequence.

parent d220578f
......@@ -280,10 +280,25 @@ class Sequence(ParsingExpression):
"""
Will match sequence of parser expressions in exact order they are defined.
"""
def __init__(self, *elements, **kwargs):
super(Sequence, self).__init__(*elements, **kwargs)
self.ws = kwargs.pop('ws', None)
self.skipws = kwargs.pop('skipws', None)
def _parse(self, parser):
results = []
c_pos = parser.position
seq_len = len(self.nodes)
if self.ws is not None:
old_ws = parser.ws
parser.ws = self.ws
if self.skipws is not None:
old_skipws = parser.skipws
parser.skipws = self.skipws
try:
for e in self.nodes:
result = None
......@@ -307,6 +322,12 @@ class Sequence(ParsingExpression):
self._nm_change_rule(m, parser)
raise
finally:
if self.ws is not None:
parser.ws = old_ws
if self.skipws is not None:
parser.skipws = old_skipws
return results
......@@ -1504,7 +1525,8 @@ class ParserPython(Parser):
elif isinstance(expression, Match):
retval = expression
elif isinstance(expression, Repetition) or \
elif isinstance(expression, Sequence) or \
isinstance(expression, Repetition) or \
isinstance(expression, SyntaxPredicate) or \
isinstance(expression, Decorator):
retval = expression
......
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_sequence_params
# Purpose: Test Sequence expression parameters.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
from __future__ import unicode_literals
import pytest
from arpeggio import ParserPython, NoMatch, Sequence
from arpeggio import RegExMatch as _
def test_skipws():
"""
skipws may be defined per Sequence.
"""
def grammar(): return Sequence("one", "two", "three"), "four"
parser = ParserPython(grammar)
# By default, skipws is True and whitespaces will be skipped.
parser.parse("one two three four")
def grammar(): return Sequence("one", "two", "three",
skipws=False), "four"
parser = ParserPython(grammar)
# If we disable skipws for sequence only then whitespace
# skipping should not be done inside sequence.
with pytest.raises(NoMatch):
parser.parse("one two three four")
# But it will be done outside of it
parser.parse("onetwothree four")
def test_ws():
"""
ws can be changed per Sequence.
"""
def grammar(): return Sequence("one", "two", "three"), "four"
parser = ParserPython(grammar)
# By default, ws consists of space, tab and newline
# So this should parse.
parser.parse("""one
two three four""")
def grammar(): return Sequence("one", "two", "three",
ws=' '), "four"
parser = ParserPython(grammar)
# If we change ws per sequence and set it to space only
# given input will raise exception
with pytest.raises(NoMatch):
parser.parse("""one
two three four""")
# But ws will be default outside of sequence
parser.parse("""one two three
four""")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment