def test_optional_greedy(self): """ Test greedy setting of optional parser class. """ input = "abc" p = parser.Sequence([ parser.Sequence([ parser.String("a"), parser.Optional(parser.String("b"), greedy=False) ]), parser.Sequence( [parser.Optional(parser.String("b")), parser.String("c")]), ]) expected_output_1 = [["a", None], ["b", "c"]] expected_output_2 = [["a", "b"], [None, "c"]] state = parser.State(input) print "Parser:", p print "State:", state generator = p.parse(state) generator.next() root = state.build_parse_tree() self.assertEqual(root.value(), expected_output_1) print "Output 1:", expected_output_1 generator.next() root = state.build_parse_tree() self.assertEqual(root.value(), expected_output_2) print "Output 2:", expected_output_2
def __init__(self): # Use a pattern to allow ascii and Unicode alphanumeric characters plus # underscores. pattern = re.compile(r"\w", re.UNICODE) name = parser_.CharacterSeries(None, pattern=pattern) elements = (parser_.String("{"), name, parser_.String("}")) parser_.Sequence.__init__(self, elements) self._identifiers = None
def initialize(self): self._single = _Single() self._single.initialize() repetition = parser_.Repetition(self._single) elements = ( repetition, parser_.Repetition(parser_.Sequence(( parser_.String("|"), repetition, )), min=0), ) parser_.Sequence.__init__(self, elements)
def __init__(self): elements = (parser_.String("+"), stuff, parser_.String("+")) parser_.Sequence.__init__(self, elements)
def __init__(self): characters = string.letters + string.digits + "_" name = parser_.CharacterSeries(characters) elements = (parser_.String("{"), name, parser_.String("}")) parser_.Sequence.__init__(self, elements) self._identifiers = None