Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that mutual recursion works for Ometa V1 syntax #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"

install:
- "bash .travis.install"
Expand Down
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