def test_sequence(self): x = t.Exactly("x") y = t.Exactly("y") z = t.And([x, y]) self.assertEqual(writeBytecode(z), [t.Match('x'), t.Match('y')])
def test_singleOr(self): """ Test code generation for a sequence of alternatives. """ x1 = t.Or([t.Exactly("x")]) x = t.Exactly("x") self.assertEqual(writePython(x, ""), writePython(x1, ""))
def test_doubleOr(self): xy = t.Or([t.Exactly("x"), t.Exactly("y")]) self.assertEqual(writeBytecode(xy), [t.Choice(3), t.Match('x'), t.Commit(2), t.Match('y')])
def test_grammar(self): r1 = t.Rule("foo", t.Exactly("x")) r2 = t.Rule("baz", t.Exactly("y")) x = t.Grammar("BuilderTest", False, [r1, r2]) g = writeBytecodeGrammar(x) self.assertEqual(sorted(g.keys()), ['baz', 'foo']) self.assertEqual(g['foo'], [t.Match('x')]) self.assertEqual(g['baz'], [t.Match('y')])
def test_not(self): x = t.Not(t.Exactly("x")) self.assertEqual(writeBytecode(x), [t.Choice(4), t.Match('x'), t.Commit(1), t.Fail()])
def test_optional(self): x = t.Optional(t.Exactly("x")) self.assertEqual(writeBytecode(x), [t.Choice(3), t.Match('x'), t.Commit(2), t.Python("None")])
def test_many1(self): xs = t.Many1(t.Exactly("x")) self.assertEqual(writeBytecode(xs), [t.Match('x'), t.Choice(3), t.Match('x'), t.Commit(-2)])
def test_sequence(self): """ Test generation of code for sequence patterns. """ x = t.Exactly("x") y = t.Exactly("y") z = t.And([x, y]) self.assertEqual( writePython(z, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _G_exactly_2, lastError = self.exactly('y') self.considerError(lastError, None) _G_exactly_2 """))
def test_markAsTree(self): """ Grammars containing list patterns are marked as taking tree-shaped input rather than character streams. """ x = t.Rule("foo", t.List(t.Exactly("x"))) g = t.Grammar("TestGrammar", True, [x]) self.assert_("\n tree = True\n" in writePython(g, ""))
def test_repeat(self): x = t.Repeat(3, 4, t.Exactly('x')) self.assertEqual(writeBytecode(x), [t.Python("3"), t.Push(), t.Python("4"), t.Push(), t.RepeatChoice(3), t.Match('x'), t.Commit(-2)])
def test_lookahead(self): x = t.Lookahead(t.Exactly("x")) self.assertEqual(writeBytecode(x), [t.Choice(7), t.Choice(4), t.Match('x'), t.Commit(1), t.Fail(), t.Commit(1), t.Fail()])
def test_or(self): """ Test code generation for a sequence of alternatives. """ xy = t.Or([t.Exactly("x"), t.Exactly("y")]) self.assertEqual( writePython(xy, ""), dd(""" def _G_or_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) def _G_or_3(): _G_exactly_4, lastError = self.exactly('y') self.considerError(lastError, None) return (_G_exactly_4, self.currentError) _G_or_5, lastError = self._or([_G_or_1, _G_or_3]) self.considerError(lastError, None) _G_or_5 """))
def test_exactly(self): """ Test generation of code for the 'exactly' pattern. """ x = t.Exactly("x") self.assertEqual( writePython(x, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _G_exactly_1 """))
def test_bind(self): """ Test code generation for variable assignment. """ x = t.Exactly("x") b = t.Bind("var", x) self.assertEqual( writePython(b, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _locals['var'] = _G_exactly_1 _G_exactly_1 """))
def test_grammar(self): """ Test generation of an entire grammar. """ r1 = t.Rule("foo", t.Exactly("x")) r2 = t.Rule("baz", t.Exactly("y")) x = t.Grammar("BuilderTest", False, [r1, r2]) self.assertEqual( writePython(x, ""), dd(""" def createParserClass(GrammarBase, ruleGlobals): if ruleGlobals is None: ruleGlobals = {} class BuilderTest(GrammarBase): def rule_foo(self): _locals = {'self': self} self.locals['foo'] = _locals _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, 'foo') return (_G_exactly_1, self.currentError) def rule_baz(self): _locals = {'self': self} self.locals['baz'] = _locals _G_exactly_2, lastError = self.exactly('y') self.considerError(lastError, 'baz') return (_G_exactly_2, self.currentError) if BuilderTest.globals is not None: BuilderTest.globals = BuilderTest.globals.copy() BuilderTest.globals.update(ruleGlobals) else: BuilderTest.globals = ruleGlobals return BuilderTest """))
def test_rule(self): """ Test generation of entire rules. """ x = t.Rule("foo", t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def rule_foo(self): _locals = {'self': self} self.locals['foo'] = _locals _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, 'foo') return (_G_exactly_1, self.currentError) """))
def test_listpattern(self): """ Test code generation for list patterns. """ x = t.List(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_listpattern_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_listpattern_3, lastError = self.listpattern(_G_listpattern_1) self.considerError(lastError, None) _G_listpattern_3 """))
def test_label(self): """ Test code generation for custom labels. """ xs = t.Label(t.Exactly("x"), 'CustomLabel') self.assertEqual( writePython(xs, ""), dd(""" def _G_label_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_label_3, lastError = self.label(_G_label_1, "CustomLabel") self.considerError(lastError, None) _G_label_3 """))
def test_pred(self): """ Test code generation for predicate expressions. """ x = t.Predicate(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_pred_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_pred_3, lastError = self.pred(_G_pred_1) self.considerError(lastError, None) _G_pred_3 """))
def test_not(self): """ Test code generation for negated terms. """ x = t.Not(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_not_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_not_3, lastError = self._not(_G_not_1) self.considerError(lastError, None) _G_not_3 """))
def test_lookahead(self): """ Test code generation for lookahead expressions. """ x = t.Lookahead(t.Exactly("x")) self.assertEqual( writePython(x), dd(""" def _G_lookahead_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_lookahead_3, lastError = self.lookahead(_G_lookahead_1) self.considerError(lastError, None) _G_lookahead_3 """))
def test_optional(self): """ Test code generation for optional terms. """ x = t.Optional(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_optional_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) def _G_optional_3(): return (None, self.input.nullError()) _G_or_4, lastError = self._or([_G_optional_1, _G_optional_3]) self.considerError(lastError, None) _G_or_4 """))
def test_many(self): """ Test generation of code for matching zero or more instances of a pattern. """ xs = t.Many(t.Exactly("x")) self.assertEqual( writePython(xs, ""), dd(""" def _G_many_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_many_3, lastError = self.many(_G_many_1) self.considerError(lastError, None) _G_many_3 """))
def test_singleOr(self): x1 = t.Or([t.Exactly("x")]) x = t.Exactly("x") self.assertEqual(writeBytecode(x1), writeBytecode(x))
def test_exactly(self): x = t.Exactly("a") self.assertEqual(writeBytecode(x), [t.Match("a")])
def test_consumedby(self): x = t.ConsumedBy(t.Exactly('x')) self.assertEqual( writeBytecode(x), [t.StartSlice(), t.Match('x'), t.EndSlice()])
def test_rule(self): x = t.Rule("foo", t.Exactly("x")) k, v = writeBytecodeRule(x) self.assertEqual(k, "foo") self.assertEqual(v, [t.Match('x')])
def test_listpattern(self): x = t.List(t.Exactly("x")) self.assertEqual( writeBytecode(x), [t.Descend(), t.Match('x'), t.Ascend()])
def test_bind(self): x = t.Exactly("x") b = t.Bind("var", x) self.assertEqual(writeBytecode(b), [t.Match('x'), t.Bind('var')])