A = 1 B = 2 nfa = ["A: A A A | A A | B", (A,0,0,A), {(A,0,0,A): [(A,1,0,A),(A,2,0,A),(B,3,0,A)], (A,1,0,A): [(A,5,0,A)], (A,5,0,A): [(A,6,0,A)], (B,3,0,A): [(FIN,FEX,0,A)], (A,6,0,A): [(FIN,FEX,0,A)], (A,2,0,A): [(A,4,0,A)], (A,4,0,A): [(FIN,FEX,0,A)]}] # A: B (A | A A)* bnftranslator.eliminate_left_recursion(nfa) pprint.pprint(nfa[2]) class TestBNFTranslator(unittest.TestCase): def test_c_bnf_to_ebnf(self): c_grammar_pth = path(__file__).dirname().joinpath("c.g") c_token_pth = path(__file__).dirname().joinpath("token.g") grammar = bnftranslator.convertbnf(c_grammar_pth, c_token_pth) # The 'and_expression' rule is an example of a BNF -> EBNF rule conversion self.assertTrue(grammar.find("and_expression: equality_expression ('&' equality_expression)*")>0) unittest.run_unittest(TestLeftRecElimination) unittest.run_unittest(TestBNFTranslator)
import langscape.util.unittest as unittest class Test_cstrep(unittest.TestCase): def setUp(self): import langscape self.python = langscape.load_langlet("python") def test_pprint_simple(self): cst = self.python.parse("1+2\n") self.python.pprint(cst) def test_pprint_simple2(self): cst = self.python.parse("1+3\n") unittest.run_unittest(Test_cstrep)
"A: A A A | A A | B", (A, 0, 0, A), { (A, 0, 0, A): [(A, 1, 0, A), (A, 2, 0, A), (B, 3, 0, A)], (A, 1, 0, A): [(A, 5, 0, A)], (A, 5, 0, A): [(A, 6, 0, A)], (B, 3, 0, A): [(FIN, FEX, 0, A)], (A, 6, 0, A): [(FIN, FEX, 0, A)], (A, 2, 0, A): [(A, 4, 0, A)], (A, 4, 0, A): [(FIN, FEX, 0, A)] } ] # A: B (A | A A)* bnftranslator.eliminate_left_recursion(nfa) pprint.pprint(nfa[2]) class TestBNFTranslator(unittest.TestCase): def test_c_bnf_to_ebnf(self): c_grammar_pth = path(__file__).dirname().joinpath("c.g") c_token_pth = path(__file__).dirname().joinpath("token.g") grammar = bnftranslator.convertbnf(c_grammar_pth, c_token_pth) # The 'and_expression' rule is an example of a BNF -> EBNF rule conversion self.assertTrue( grammar.find( "and_expression: equality_expression ('&' equality_expression)*" ) > 0) unittest.run_unittest(TestLeftRecElimination) unittest.run_unittest(TestBNFTranslator)
self.assertTrue(len(find_all(nd, kwd_or)) == 2) def test_import(self): nd = self.p4d.fn.import_name("x") self.p4d.check_node(nd) class TestCSTInterpolationGallery(unittest.TestCase): def setUp(self): import langscape self.langlet = langscape.load_langlet("gallery") self.cstbuilder = CSTBuilder(self.langlet) self.symbol = self.langlet.parse_symbol self.token = self.langlet.parse_token def test_def_func(self): suite = self.langlet.fn.suite(self.langlet.fn.print_stmt("a")) cst = self.langlet.fn.funcdef("def", "foo", self.langlet.fn.parameters("(", ")"), suite) self.langlet.check_node(cst) def test_return(self): self.langlet.fn.return_stmt("a") unittest.run_unittest(TestCSTBuilderTestlist) unittest.run_unittest(TestCSTBuilderFileInput) unittest.run_unittest(TestCSTInterpolation) unittest.run_unittest(TestCSTInterpolationGallery)
self.langlet = langscape.load_langlet("python") self.fn = LangletCSTFunction(self.langlet) def test_is_atomic(self): cst = self.langlet.parse("1+2\n") self.assertFalse(self.fn.is_atomic(cst)) self.assertTrue(find_node(cst, self.langlet.parse_symbol.atom)) self.assertTrue(self.fn.is_atomic(self.langlet.fn.Number("4"))) def test_pprint_simple2(self): cst = self.langlet.parse("1+3\n") class TestCSTFunctionCoverage(unittest.TestCase): def setUp(self): import langscape from langscape.langlets.python.cstfunction import LangletCSTFunction self.langlet = langscape.load_langlet("coverage") self.fn = LangletCSTFunction(self.langlet) def test_pprint_simple(self): cst = self.langlet.parse("1+2\n") def test_pprint_simple2(self): cst = self.langlet.parse("1+3\n") unittest.run_unittest(TestCSTFunctionBase) unittest.run_unittest(TestCSTFunctionPython) unittest.run_unittest(TestCSTFunctionCoverage)
class TestCodeTemplateWithCapture(unittest.TestCase): def test_with_capture(self): ct = CodeTemplate(python, swap) ct.bind(x = "a", y = "b") cst = ct.substitute(x = "temp", y="y") code = python.unparse(cst) D = {"temp":7, "y":8} exec code in D self.assertTrue(D["temp"] == D["y"] == 8) def test_without_capture(self): ct = CodeTemplate(python, swap) ct.bind(x = "a", y = "b") ct.local_names("temp") cst = ct.substitute(x = "temp", y="y") code = python.unparse(cst) D = {"temp":7, "y":8} exec code in D self.assertTrue(D["temp"] == 8) self.assertTrue(D["y"] == 7) unittest.run_unittest(TestBasicSplicing) unittest.run_unittest(TestCodeTemplate) unittest.run_unittest(TestCodeTemplateWithCapture)
return ct.source loop(5) class TestCodeTemplateWithCapture(unittest.TestCase): def test_with_capture(self): ct = CodeTemplate(python, swap) ct.bind(x="a", y="b") cst = ct.substitute(x="temp", y="y") code = python.unparse(cst) D = {"temp": 7, "y": 8} exec code in D self.assertTrue(D["temp"] == D["y"] == 8) def test_without_capture(self): ct = CodeTemplate(python, swap) ct.bind(x="a", y="b") ct.local_names("temp") cst = ct.substitute(x="temp", y="y") code = python.unparse(cst) D = {"temp": 7, "y": 8} exec code in D self.assertTrue(D["temp"] == 8) self.assertTrue(D["y"] == 7) unittest.run_unittest(TestBasicSplicing) unittest.run_unittest(TestCodeTemplate) unittest.run_unittest(TestCodeTemplateWithCapture)