def test_stringConsumedBy(self): called = [] grammarSource = "rule = <'x'+>:y -> y" grammar = OMeta(grammarSource).parseGrammar("Parser") def interp(result, error): called.append(result) trampoline = TrampolinedGrammarInterpreter(grammar, "rule", interp) trampoline.receive("xxxxx") trampoline.end() self.assertEqual(called, ["xxxxx"])
def _setupInterp(self): """ Resets the parser. The parser will begin parsing with the rule named 'initial'. """ self._interp = TrampolinedGrammarInterpreter( grammar=self.grammar, ruleName=self.receiver.currentRule, callback=None, globals=self.bindings)
def test_failure(self): g = OMeta(""" foo = 'a':one baz:two 'd'+ 'e' -> (one, two) baz = 'b' | 'c' """, {}) tree = g.parseGrammar('TestGrammar') i = TrampolinedGrammarInterpreter( tree, 'foo', callback=lambda x: setattr(self, 'result', x)) e = self.assertRaises(ParseError, i.receive, 'foobar') self.assertEqual(str(e), "\nfoobar\n^\nParse error at line 2, column 0:" " expected the character 'a'. trail: []\n")
def doIt(s): """ @param s: The string to be parsed by the wrapped grammar. """ tree = not isinstance(s, basestring) if tree: raise unittest.SkipTest("Not applicable for push parsing") results = [] def whenDone(val, err): results.append(val) parser = TrampolinedGrammarInterpreter(self._tree, name, whenDone, self._globals) for i, c in enumerate(s): assert len(results) == 0 parser.receive(c) parser.end() if results and parser.input.position == len(parser.input.data): try: return ''.join(results[0]) except TypeError: return results[0] else: raise parser.currentError