本文整理汇总了Python中antlr4.ParseTreeWalker方法的典型用法代码示例。如果您正苦于以下问题:Python antlr4.ParseTreeWalker方法的具体用法?Python antlr4.ParseTreeWalker怎么用?Python antlr4.ParseTreeWalker使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类antlr4
的用法示例。
在下文中一共展示了antlr4.ParseTreeWalker方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_parser
# 需要导入模块: import antlr4 [as 别名]
# 或者: from antlr4 import ParseTreeWalker [as 别名]
def run_parser(quil):
# type: (str) -> List[AbstractInstruction]
"""
Run the ANTLR parser.
:param str quil: a single or multiline Quil program
:return: list of instructions that were parsed
"""
# Step 1: Run the Lexer
input_stream = InputStream(quil)
lexer = QuilLexer(input_stream)
stream = CommonTokenStream(lexer)
# Step 2: Run the Parser
parser = QuilParser(stream)
parser.removeErrorListeners()
parser.addErrorListener(CustomErrorListener())
tree = parser.quil()
# Step 3: Run the Listener
pyquil_listener = PyQuilListener()
walker = ParseTreeWalker()
walker.walk(pyquil_listener, tree)
return pyquil_listener.result
示例2: _parse_input_stream
# 需要导入模块: import antlr4 [as 别名]
# 或者: from antlr4 import ParseTreeWalker [as 别名]
def _parse_input_stream(input_stream: antlr4.InputStream) -> RootNode:
error_listener = _ConsoleErrorListener()
lexer = JSONPathLexer(input_stream)
lexer.addErrorListener(error_listener)
token_stream = antlr4.CommonTokenStream(lexer)
parser = _JSONPathParser(token_stream)
parser.addErrorListener(error_listener)
tree = parser.jsonpath()
listener = _JSONPathListener(_stack=[])
walker = antlr4.ParseTreeWalker()
walker.walk(listener, tree)
# pylint: disable=protected-access
return listener._stack.pop()
# pylint: enable=protected-access
示例3: _parse_boolexpr
# 需要导入模块: import antlr4 [as 别名]
# 或者: from antlr4 import ParseTreeWalker [as 别名]
def _parse_boolexpr(self, s):
lexer = BoolExprLexer(
antlr4.InputStream(s)
)
stream = antlr4.CommonTokenStream(lexer)
parser = BoolExprParser(stream)
tree = parser.booleanExpression()
eb = _BECompiler()
walker = antlr4.ParseTreeWalker()
walker.walk(eb, tree)
return eb.expression, eb.comparator, eb.value
示例4: _parse_string
# 需要导入模块: import antlr4 [as 别名]
# 或者: from antlr4 import ParseTreeWalker [as 别名]
def _parse_string(s):
lexer = minemeld.ft.condition.BoolExprLexer(
antlr4.InputStream(s)
)
stream = antlr4.CommonTokenStream(lexer)
parser = minemeld.ft.condition.BoolExprParser(stream)
tree = parser.booleanExpression()
eb = ExprBuilder()
walker = antlr4.ParseTreeWalker()
walker.walk(eb, tree)
return eb
示例5: parse
# 需要导入模块: import antlr4 [as 别名]
# 或者: from antlr4 import ParseTreeWalker [as 别名]
def parse(self, equel):
lexer = equelLexer(equel)
tokenstream = CommonTokenStream(lexer)
parser = equelParser(tokenstream)
parsetree = parser.equel()
walker = ParseTreeWalker()
listener = EQUELParserListener(self.engine)
walker.walk(listener, parsetree)
return parsetree
示例6: parse
# 需要导入模块: import antlr4 [as 别名]
# 或者: from antlr4 import ParseTreeWalker [as 别名]
def parse(self, input, trace = False):
if self.useDatabase:
self.db = model.A2LDatabase(self.fnbase, debug = self.debug)
lexer = self.lexerClass(input)
lexer.removeErrorListeners()
lexer.addErrorListener(MyErrorListener())
tokenStream = antlr4.CommonTokenStream(lexer)
# tokenStream = BufferedTokenStream(lexer)
parser = self.parserClass(tokenStream)
parser.setTrace(trace)
parser.removeErrorListeners()
parser.addErrorListener(MyErrorListener())
meth = getattr(parser, self.startSymbol)
self._syntaxErrors = parser._syntaxErrors
tree = meth()
if self.listener:
if self.useDatabase:
self.listener.db = self.db
listener = self.listener()
walker = antlr4.ParseTreeWalker()
result = walker.walk(listener, tree)
if self.useDatabase:
self.db.session.commit()
return self.db.session
else:
return listener
示例7: parse
# 需要导入模块: import antlr4 [as 别名]
# 或者: from antlr4 import ParseTreeWalker [as 别名]
def parse(text):
input_stream = antlr4.InputStream(text)
lexer = ModelicaLexer(input_stream)
stream = antlr4.CommonTokenStream(lexer)
parser = ModelicaParser(stream)
# parser.buildParseTrees = False
parse_tree = parser.stored_definition()
ast_listener = ASTListener()
parse_walker = antlr4.ParseTreeWalker()
parse_walker.walk(ast_listener, parse_tree)
modelica_file = ast_listener.ast_result
return file_to_tree(modelica_file)