def test_3_words(self): defs = GSequence([GWord("def"), GWord("def"), GWord("def")]) lines = ["def def", " def"] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) self.assertEqual((1, len(lines[1]) - 1), defs.get_last_pos().tuple(), "Position should be in last line")
def test_optional_invalid(self): defs = GSequence([ GWord("def"), GName(), GOptional(GWord(":")), GName(), GWord("{") ]) lines = ["def test {"] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertFalse(defs.match(text_iterator))
def test_bracket_exp_fun2(self): bracket_exp = BracketExp("(", ")") rdefs = GWord("a, b, c") bracket_exp.set_recursive_definitions([rdefs]) defs = GSequence([GWord("def"), GName(), bracket_exp]) # defs.set_recursive_definitions([defs]) lines = ["def TestFun_123(a, b, c) "] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) pos = defs.get_last_pos().tuple() self.assertEqual((0, len(lines[0]) - 2), pos) self.assertNotEqual(None, bracket_exp.get_body())
def test_optional_type(self): defs = GSequence([ GWord("def"), GName(), GOptional(GWord(":")), GName(), GWord("{") ]) lines = ["def test: test_name {"] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) pos = defs.get_last_pos().tuple() self.assertEqual((0, 20), pos)
def test_def_name(self): defs = GSequence([GWord("def"), GName()]) lines = ["def Test_Fun123", ""] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) pos = defs.get_last_pos() self.assertEqual((0, 14), pos.tuple()) self.assertEqual("Test_Fun123", defs.get_definitions()[1].get_name())
def test_bracket_exp4(self): defs = GSequence([ GWord("def"), GName(), BracketExp("(", ")"), GType(), GWord("="), BracketExp("{", "}") ]) lines = [ "def TestFun_123(a, b, c) :Unit = {", " print(123) ", "print(456)", "}" ] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) pos = defs.get_last_pos() self.assertEqual(len(lines), pos[0])
def test_def_fun_incomplete(self): defs = GSequence([GWord("def"), GName()]) lines = ["def Test_Fun123(a: Integer)", ""] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) pos = defs.get_last_pos().tuple() self.assertEqual(0, pos[0]) self.assertEqual(len("def Test_Fun123") - 1, pos[1]) self.assertEqual("Test_Fun123", defs.get_definitions()[1].get_name())
def test_bracket_exp_fun(self): bracket_exp = BracketExp("(", ")") gname = GName() defs = GSequence([GWord("def"), gname, bracket_exp]) # defs.set_recursive_definitions([defs]) lines = ["def TestFun(a, b, c)"] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) pos = defs.get_last_pos().tuple() self.assertEqual((0, len(lines[0]) - 1), pos) self.assertEqual("a, b, c", bracket_exp.get_body().get_text()) self.assertEqual("TestFun", gname.get_name())
def test_nested_bracket_exps(self): defs = GSequence([ GWord("def"), GName(), BracketExp("(", ")"), BracketExp("{", "}") ]) nestedFun = "def hello() { print(\"hello\") }" lines = ["def TestFun_123(a, b, c) {", nestedFun, "}"] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertTrue(defs.match(text_iterator)) pos = defs.get_last_pos() self.assertEqual(len(lines), pos[0])
from object_map import ObjectMap from grammar.gword import GWord from grammar.gname import GName from grammar.bracket_exp import BracketExp from grammar.gsequence import GSequence from grammar.gany import GAny from grammar.goptional import GOptional import re SCALA_TYPE = GSequence([GWord(":"), GName]) DEFINITIONS = [ GSequence( [GWord("class"), GName(), BracketExp("(", ")"), BracketExp("{", "}")]), GSequence([ GWord("def"), GName(), BracketExp("(", ")"), GOptional(SCALA_TYPE), BracketExp("{", "}") ]), GSequence([GAny()]) ] class ScalaFileParser(object): def __init__(self, lines): self._lines = lines
def __init__(self): super().__init__([GWord("class"), GName(), BracketExp("{", "}")])
from .code_model import CodeModel from grammar.gword import GWord from grammar.gname import GName from grammar.bracket_exp import BracketExp from grammar.gsequence import GSequence from grammar.gany import GAny from grammar.goptional import GOptional from iterator.text_iterator import TextIterator SCALA_TYPE = GSequence([GWord(":"), GName]) class ClassDefinition(GSequence): def __init__(self): super().__init__([GWord("class"), GName(), BracketExp("{", "}")]) class CppParser(object): def __init__(self, text): self._text_iterator = TextIterator(text) self._definitions = [ClassDefinition(), GSequence([GAny()])] for df in self._definitions: df.set_recursive_definitions(self._definitions) def parse_objects(self): for definition in self._definitions: print(definition) if definition.match(self._text_iterator): pos = definition.get_last_pos() print("matches: pos {}", pos) print("parsed definition: {}".format(definition))
def test_3_words_invalid(self): defs = GSequence([GWord("def"), GWord("def"), GWord("def")]) lines = ["def def", " abc"] text = "\n".join(lines) text_iterator = TextIterator(text) self.assertFalse(defs.match(text_iterator))