예제 #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
예제 #2
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)