Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
 def __init__(self):
     characters = string.letters + string.digits + "_-.'"
     word = parser_.CharacterSeries(characters)
     whitespace = parser_.Whitespace()
     elements = (
         word,
         parser_.Repetition(parser_.Sequence((whitespace, word)), min=0),
     )
     parser_.Sequence.__init__(self, elements)
Ejemplo n.º 3
0
 def __init__(self):
     # Use a pattern to allow ascii and Unicode alphanumeric characters plus a
     # few special characters.
     pattern = re.compile(r"[\w_\-.',]", re.UNICODE)
     word = parser_.CharacterSeries(None, pattern=pattern)
     whitespace = parser_.Whitespace()
     elements = (
         word,
         parser_.Repetition(parser_.Sequence((whitespace, word)), min=0),
     )
     parser_.Sequence.__init__(self, elements)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 def initialize(self):
     self._element_identifier = _ElementRef()
     self._action_identifier = _ActionRef()
     alternatives = parser_.Alternative((
         _Literal(),
         _Optional(),
         _Group(),
         self._element_identifier,
     ))
     elements = [
         parser_.Whitespace(optional=True),
         alternatives,
         parser_.Optional(
             parser_.Sequence((
                 parser_.Whitespace(optional=True),
                 self._action_identifier,
             ))),
         parser_.Whitespace(optional=True),
     ]
     parser_.Sequence.__init__(self, elements)