Exemplo n.º 1
0
    def test_backwards_lexing(self):
        lexer = IncrementalLexer("""
"::=":doublecolon
"=":equal
":":singlecolon
        """)
        ast = AST()
        ast.init()
        bos = ast.parent.children[0]
        eos = ast.parent.children[1]
        text = TextNode(Terminal(":"))
        bos.insert_after(text)
        lexer.relex(text)

        assert bos.next_term.symbol.name == ":"
        assert bos.next_term.lookup == "singlecolon"
        assert text.lookahead == 1

        text2 = TextNode(Terminal(":"))
        text.insert_after(text2)
        lexer.relex(text2)
        assert text2.lookahead == 1

        assert bos.next_term.symbol.name == ":"
        assert bos.next_term.next_term.symbol.name == ":"

        text3 = TextNode(Terminal("="))
        text2.insert_after(text3)
        lexer.relex(text3)

        assert bos.next_term.symbol.name == "::="
        assert isinstance(bos.next_term.next_term, EOS)
Exemplo n.º 2
0
    def test_backwards_lexing(self):
        lexer = IncrementalLexer("""
"::=":doublecolon
"=":equal
":":singlecolon
        """)
        ast = AST()
        ast.init()
        bos = ast.parent.children[0]
        eos = ast.parent.children[1]
        text = TextNode(Terminal(":"))
        bos.insert_after(text)
        lexer.relex(text)

        assert bos.next_term.symbol.name == ":"
        assert bos.next_term.lookup == "singlecolon"
        assert text.lookahead == 1

        text2 = TextNode(Terminal(":"))
        text.insert_after(text2)
        lexer.relex(text2)
        assert text2.lookahead == 1

        assert bos.next_term.symbol.name == ":"
        assert bos.next_term.next_term.symbol.name == ":"

        text3 = TextNode(Terminal("="))
        text2.insert_after(text3)
        lexer.relex(text3)

        assert bos.next_term.symbol.name == "::="
        assert isinstance(bos.next_term.next_term, EOS)
Exemplo n.º 3
0
    def test_lookahead(self):
        lexer = IncrementalLexer("""
"aaa":aaa
"a":a
"b":b
        """)
        ast = AST()
        ast.init()
        bos = ast.parent.children[0]
        eos = ast.parent.children[1]
        text = TextNode(Terminal("baab"))
        bos.insert_after(text)
        lexer.relex(text)
        assert ast.parent.children[1].symbol.name == "b"
        assert ast.parent.children[2].symbol.name == "a"
        assert ast.parent.children[3].symbol.name == "a"
        assert ast.parent.children[4].symbol.name == "b"
        ast.parent.children[1].symbol = None
        ast.parent.children[3].symbol.name = "aa"
        lexer.relex(ast.parent.children[3])

        assert ast.parent.children[2].symbol.name == "aaa"
        assert ast.parent.children[3].symbol.name == "b"
Exemplo n.º 4
0
 def set_language(self, lang, whitespace):
     if isinstance(lang, Language):
         lrp = IncParser(str(lang.grammar), 1, whitespace)
         lrp.init_ast()
         lexer = IncrementalLexer(str(lang.priorities))
         self.editor.set_mainlanguage(lrp, lexer, lang.name)
     elif isinstance(lang, EcoGrammar):
         bootstrap = BootstrapParser(lr_type=1, whitespaces=whitespace)
         bootstrap.parse(lang.grammar)
         self.editor.set_mainlanguage(bootstrap.incparser,
                                      bootstrap.inclexer, lang.name)
     elif isinstance(lang, EcoFile):
         incparser, inclexer = lang.load()
         self.editor.set_mainlanguage(incparser, inclexer, lang.name)
Exemplo n.º 5
0
 def parse(self, ecogrammar):
     # this is only called for grammars based on Eco Grammar (not Eco Grammar (Eco))
     from grammars.eco_grammar import eco_grammar as grammar
     self.lexer = IncrementalLexer(grammar.priorities)
     self.parser = IncParser(grammar.grammar, 1, True)
     self.parser.init_ast()
     self.ast = self.parser.previous_version.parent
     self.treemanager = TreeManager()
     self.treemanager.add_parser(self.parser, self.lexer, grammar.name)
     self.treemanager.import_file(ecogrammar)
     if self.parser.last_status == False:
         raise Exception("Invalid input grammar due to syntax errors")
     self.read_options()
     self.parse_both()
     self.create_parser()
     self.create_lexer()
Exemplo n.º 6
0
    def test_lookahead(self):
        lexer = IncrementalLexer("""
"aaa":aaa
"a":a
"b":b
        """)
        ast = AST()
        ast.init()
        bos = ast.parent.children[0]
        eos = ast.parent.children[1]
        text = TextNode(Terminal("baab"))
        bos.insert_after(text)
        lexer.relex(text)
        assert ast.parent.children[1].symbol.name == "b"
        assert ast.parent.children[2].symbol.name == "a"
        assert ast.parent.children[3].symbol.name == "a"
        assert ast.parent.children[4].symbol.name == "b"
        ast.parent.children[1].symbol = None
        ast.parent.children[3].symbol.name = "aa"
        lexer.relex(ast.parent.children[3])

        assert ast.parent.children[2].symbol.name == "aaa"
        assert ast.parent.children[3].symbol.name == "b"
Exemplo n.º 7
0
 def setup_class(cls):
     cls.lexer = IncrementalLexer(calc1.priorities)
     cls.parser = IncParser(calc1.grammar, 1, True)
     cls.parser.init_ast()
     cls.ast = cls.parser.previous_version