Commit 0f693003 authored by Chris Jewell's avatar Chris Jewell
Browse files

parse_gemlang.py PEP8 compliant.

parent bc51c9f9
......@@ -21,5 +21,5 @@
from gem.gemlang.ast.utils import serialize
from gem.gemlang.ast_walker import ASTWalker
from gem.gemlang.parse_gemlang import ParseTree2AST
from gem.gemlang.parse_gemlang import GEMParser
from gem.gemlang.parse_gemlang import gemparse
......@@ -17,7 +17,7 @@
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Parse GEM Language to AST
"""Parse GEM Language to AST"""
import os
......@@ -25,15 +25,15 @@ from lark import Lark, Transformer, v_args
from gem.gemlang.ast.ast_base import GEMProgram, Block, FArgList, FArg, KwRef, Statement
from gem.gemlang.ast.ast_statement import EpiDecl, AssignExpr, AssignTransition, StochasticAssignExpr
from gem.gemlang.ast.ast_expression import * # pylint: disable=unused-wildcard-import
from gem.gemlang.ast.ast_expression import * # pylint: disable=unused-wildcard-import,wildcard-import
class GrammarError(Exception):
pass
"""Raised in response to errors in gemlang grammar"""
@v_args(meta=True)
class GEMParser(Transformer):
class ParseTree2AST(Transformer):
"""GEMParser parse-tree to AST transformer."""
# pylint: disable=missing-function-docstring,no-self-use,unused-argument,undefined-variable,invalid-name
......@@ -79,7 +79,7 @@ class GEMParser(Transformer):
if len(ex) > 1:
raise ValueError("Number of arguments > 1 in expression")
return ex[0]
def argdef(self, args, meta):
return FArg(args[0], meta=meta)
......@@ -161,18 +161,19 @@ class GEMParser(Transformer):
raise GrammarError(f"No translation of production rule '{data}' in GEM grammar")
def make_tree_parser():
glpath = os.path.dirname(__file__)
with open(os.path.join(glpath, 'gem_grammar.cfgr'), 'r') as f:
grammar = f.read()
return Lark(grammar, start='gem', parser='earley',
propagate_positions=True)
tree_parser = make_tree_parser()
class GEMParser:
"""Instantiates a gemlang 1st stage parser."""
__glpath = os.path.dirname(__file__)
with open(os.path.join(__glpath, 'gem_grammar.cfgr'), 'r') as f:
__grammar = f.read()
__parser = Lark(__grammar, start='gem', parser='earley',
propagate_positions=True)
def __new__(cls, program):
parse_tree = cls.parse(program)
return ParseTree2AST().transform(parse_tree)
def gemparse(program):
"""Takes a GEM program and returns an AST representation."""
parse_tree = tree_parser.parse(program)
return GEMParser().transform(parse_tree)
@classmethod
def parse(cls, program):
"""Returns a Lark parse tree representation of a gem program."""
return cls.__parser.parse(program)
......@@ -28,7 +28,7 @@ import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow_probability import edward2 as ed
from gem.gemlang import gemparse
from gem.gemlang import GEMParser
from gem.gemlang.model_generator import CodeGenerator
from gem.gemlang.semantics.symbol_table import SymbolTable
from gem.gemlang.semantics.symbol_resolve import SymbolResolver, \
......@@ -106,7 +106,7 @@ class GEM:
def __parse__(self, gemprog):
try:
ast = gemparse(gemprog)
ast = GEMParser(gemprog)
self.__create_symbol_table()
SymbolDeclarer().visit(ast, self.__symtab)
SymbolResolver().visit(ast, self.__symtab)
......
......@@ -24,7 +24,7 @@ from lark import Lark
from gem import gemlang
from gem.gemlang.ast.utils import serialize
from gem.gemlang.parse_gemlang import GEMParser
from gem.gemlang.parse_gemlang import ParseTree2AST
from gem.gemlang.util import expr2infix
from gem.gemlang.ast_walker import ASTWalker
......@@ -45,7 +45,7 @@ class TestExpr2Infix(unittest.TestCase):
def test_ast2infix(self):
prog = "beta * sizeof(S) * sizeof(I) + 3. / (3. - 1.)"
gem_tree = self.gem_lp.parse(prog)
ast = GEMParser().transform(gem_tree)
ast = ParseTree2AST().transform(gem_tree)
ast_tree = self.ast_lp.parse(serialize(ast))
infix = expr2infix(ast_tree)
self.assertEqual("(((beta * sizeof(S)) * sizeof(I)) + (3.0 / (3.0 - 1.0)))",
......
......@@ -36,7 +36,7 @@ from lark import Lark
from gem import gemlang
from gem.gemlang.ast.utils import serialize
from gem.gemlang.parse_gemlang import GEMParser
from gem.gemlang.parse_gemlang import ParseTree2AST
from gem import GEM
......@@ -89,7 +89,7 @@ class TestExpressionParse(unittest.TestCase):
def test_arith(self):
program = """beta * pow(I, 3.) * S / (3.0 - 2.) * 5.0"""
tree = self.parser.parse(program)
ep = GEMParser()
ep = ParseTree2AST()
ast = ep.transform(tree)
self.assertEqual(
"""(MulExpr (MulExpr (MulExpr (IdRef beta) (Call (IdRef pow) (ArgList (IdRef I) (Float 3.0)))) (TruedivExpr (IdRef S) (SubExpr (Float 3.0) (Float 2.0)))) (Float 5.0))""",
......@@ -112,14 +112,14 @@ class TestDeclParse(unittest.TestCase):
def test_2dArray(self):
stmt = """[[1,2],[3,4],[5,6]]"""
tree = self.parser.parse(stmt)
sp = GEMParser()
sp = ParseTree2AST()
ast = sp.transform(tree)
print(serialize(ast))
def test_varassign(self):
stmt = """x = 1 + 3"""
tree = self.parser.parse(stmt)
sp = GEMParser()
sp = ParseTree2AST()
ast = sp.transform(tree)
self.assertEqual(
"""(GEMProgram (AssignExpr (IdRef x) (AddExpr (Integer 1) (Integer 3))))""",
......@@ -128,7 +128,7 @@ class TestDeclParse(unittest.TestCase):
def test_stochassign(self):
stmt = """beta ~ Gamma(0.1, 0.1)"""
tree = self.parser.parse(stmt)
sp = GEMParser()
sp = ParseTree2AST()
ast = sp.transform(tree)
self.assertEqual(
"""(GEMProgram (StochasticAssignExpr (IdRef beta) (Call (IdRef Gamma) (ArgList (Float 0.1) (Float 0.1)))))""",
......@@ -137,7 +137,7 @@ class TestDeclParse(unittest.TestCase):
def test_transition_assign(self):
stmt = """[S -> I] = gamma"""
tree = self.parser.parse(stmt)
sp = GEMParser()
sp = ParseTree2AST()
ast = sp.transform(tree)
self.assertEqual(
"""(GEMProgram (AssignTransition (TransitionDef (IdRef S) (IdRef I)) (IdRef gamma)))""",
......@@ -161,7 +161,7 @@ class TestDeclParse(unittest.TestCase):
"""
tree = self.parser.parse(stmt)
sp = GEMParser()
sp = ParseTree2AST()
ast = sp.transform(tree)
self.maxDiff = None
self.assertEqual(
......@@ -200,7 +200,7 @@ class TestGEMLang(unittest.TestCase):
"""
tree = self.parser.parse(program)
sp = GEMParser()
sp = ParseTree2AST()
ast = sp.transform(tree)
self.assertEqual(
"""(GEMProgram (AssignExpr (IdRef popsize) (Integer 100)) (StochasticAssignExpr (IdRef epsilon) (Call (IdRef Gamma) (ArgList (Float 1.0) (Float 1.0)))) (StochasticAssignExpr (IdRef beta) (Call (IdRef Gamma) (ArgList (Float 1.0) (Float 1.0)))) (StochasticAssignExpr (IdRef gamma) (Call (IdRef Gamma) (ArgList (Float 1.0) (Float 0.1)))) (StochasticAssignExpr (IdRef I0) (Call (IdRef Multinomial) (ArgList (KwArg (KwRef n) (Integer 1)) (KwArg (KwRef size) (IdRef popsize))))) (EpiDecl MyModel (ArgList (KwArg (KwRef type) (String "ILM")) (KwArg (KwRef time_origin) (Float 0.0)) (KwArg (KwRef time_step) (Float 1.0))) (FArgList (FArg lemon)) (Block (AssignExpr (IdRef S) (Call (IdRef State) (ArgList (KwArg (KwRef init) (SubExpr (Integer 1) (IdRef I0)))))) (AssignExpr (IdRef I) (Call (IdRef State) (ArgList (KwArg (KwRef init) (IdRef I0))))) (AssignExpr (IdRef R) (Call (IdRef State) (ArgList (KwArg (KwRef init) (Integer 0))))) (AssignExpr (IdRef siRate) (AddExpr (IdRef epsilon) (MulExpr (IdRef beta) (Call (IdRef sizeof) (ArgList (IdRef I)))))) (AssignTransition (TransitionDef (IdRef S) (IdRef I)) (IdRef siRate)) (AssignTransition (TransitionDef (IdRef I) (IdRef R)) (IdRef gamma)))) (StochasticAssignExpr (IdRef x) (Call (IdRef MyModel) (ArgList))))""",
......
......@@ -20,7 +20,7 @@
import unittest
from gem.gemlang.parse_gemlang import gemparse
from gem.gemlang.parse_gemlang import GEMParser
from gem.gemlang.ast.ast_base import GEMProgram
from gem.gemlang.semantics.symbol_table import SymbolTable
from gem.gemlang.semantics.symbol_resolve import SymbolResolver
......@@ -48,7 +48,7 @@ class TestInterpreter(unittest.TestCase):
myEpi ~ MyEpi(color="yellow")
"""
ast = gemparse(model_str)
ast = GEMParser(model_str)
self.assertIsInstance(ast, GEMProgram)
declarer = SymbolDeclarer()
symtab = SymbolTable()
......
......@@ -27,7 +27,7 @@ import os
import inspect
from gem import gemlang
from gem.gemlang.parse_gemlang import GEMParser
from gem.gemlang.parse_gemlang import ParseTree2AST
#@unittest.skip("TestParseCompleteness to be implemented")
class TestParseCompleteness(unittest.TestCase):
......@@ -44,6 +44,6 @@ class TestParseCompleteness(unittest.TestCase):
self.prod_rules.append(prod_rule.groups()[0])
def test_parse_completeness(self):
prod_rule_methods = tuple(GEMParser.__dict__)
prod_rule_methods = tuple(ParseTree2AST.__dict__)
for rule in self.prod_rules:
self.assertIn(rule, prod_rule_methods, f"Production rule '{rule}' not implemented in GEMParser")
......@@ -19,7 +19,7 @@
#
import unittest
from gem.gemlang import gemparse
from gem.gemlang import GEMParser
from gem.gemlang.semantics.symbol_table import SymbolTable
from gem.gemlang.semantics.symbol_resolve import SymbolResolver
from gem.gemlang.semantics.symbol_declare import SymbolDeclarer
......@@ -31,7 +31,7 @@ class TestParseDeclarations(unittest.TestCase):
prog = """
pi = 3.14157
"""
ast = gemparse(prog)
ast = GEMParser.__call__(prog)
symtab = SymbolTable()
SymbolDeclarer().visit(ast, symtab)
assign = ast.children[0]
......@@ -45,7 +45,7 @@ class TestParseDeclarations(unittest.TestCase):
prog = """
rv ~ Gamma(0.1, 0.1)
"""
ast = gemparse(prog)
ast = GEMParser.__call__(prog)
symtab = SymbolTable()
SymbolDeclarer().visit(ast, symtab)
assign = ast.children[0]
......@@ -62,7 +62,7 @@ class TestResolveSymbols(unittest.TestCase):
pi = 3.14157
pi * 2
"""
ast = gemparse(prog)
ast = GEMParser.__call__(prog)
symtab = SymbolTable()
SymbolDeclarer().visit(ast, symtab)
SymbolResolver().visit(ast, symtab)
......@@ -75,7 +75,7 @@ class TestResolveSymbols(unittest.TestCase):
gamma ~ Gamma(0.1, 0.1)
gamma * 2
"""
ast = gemparse(prog)
ast = GEMParser.__call__(prog)
symtab = SymbolTable()
declarer = SymbolDeclarer()
declarer.visit(ast, symtab)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment