Skip to content

Commit

Permalink
Test that mutual recursion works
Browse files Browse the repository at this point in the history
Working on issue 57
  • Loading branch information
rbtcollins committed Nov 17, 2015
1 parent 4c84184 commit 8fc17f6
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ometa/test/test_pymeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ def test_leftrecursion(self):
self.assertEqual(g.num("32767"), 32767)


def test_mutual_leftrecursion(self):
g = self.compile("""
num ::= (<num>:n <digit>:d => n * 10 + d
| <digit>)
digit ::= :x ?(x.isdigit()) => int(x)
x ::= <expr>
expr ::= <x>:e '-' <num>:n => e - n
| <num>
""")
self.assertEqual(g.expr("3"), 3)
self.assertEqual(g.expr("3-2"), 1)
self.assertEqual(g.expr("3-2-1"), 0)
self.assertEqual(g.x("3"), 3)
self.assertEqual(g.x("3-2"), 1)
self.assertEqual(g.x("3-2-1"), 0)


def test_characterVsSequence(self):
"""
Characters (in single-quotes) are not regarded as sequences.
Expand Down Expand Up @@ -806,6 +823,23 @@ def test_leftrecursion(self):
self.assertEqual(g.num("32767"), 32767)


def test_mutual_leftrecursion(self):
g = self.compile("""
num = (num:n digit:d -> n * 10 + d
| digit)
digit = :x ?(x.isdigit()) -> int(x)
x = expr
expr = x:e '-' num:n -> e - n
| num
""")
self.assertEqual(g.expr("3"), 3)
self.assertEqual(g.expr("3-2"), 1)
self.assertEqual(g.expr("3-2-1"), 0)
self.assertEqual(g.x("3"), 3)
self.assertEqual(g.x("3-2"), 1)
self.assertEqual(g.x("3-2-1"), 0)


def test_characterVsSequence(self):
"""
Characters (in single-quotes) are not regarded as sequences.
Expand Down Expand Up @@ -1011,6 +1045,25 @@ def test_leftrecursion(self):
self.assertEqual(g.num("3"), 3)
self.assertEqual(g.num("32767"), 32767)

def test_mutual_leftrecursion(self):
g = self.compile("""
num = (num:n digit:d -> makeInt(n, d)
| digit)
digit = :x ?(isdigit(x)) -> int(x)
x = expr
expr = x:e '-' num:n -> subtract(e, n)
| num
""", {"makeInt": lambda x, y: x * 10 + y,
"isdigit": lambda x: x.isdigit(),
"subtract": lambda x, y: x-y})
self.assertEqual(g.expr("3"), 3)
self.assertEqual(g.expr("3-2"), 1)
self.assertEqual(g.expr("3-2-1"), 0)
self.assertEqual(g.x("3"), 3)
self.assertEqual(g.x("3-2"), 1)
self.assertEqual(g.x("3-2-1"), 0)


def test_characterVsSequence(self):
"""
Characters (in single-quotes) are not regarded as sequences.
Expand Down

0 comments on commit 8fc17f6

Please sign in to comment.