def run_test(filename): with open(filename, 'r') as program: try: # Parse the tree raw_text = program.read() tree = parser.parse(raw_text) # Insert attributes in the tree for index, inst in enumerate(tree): line, col = parser.pos_to_linecol(inst.position) visit_parse_tree(inst, SetInstructionVisitor(index, line)) # Prepare the simulation (Preprocess what we can) simulator = Simulator(tree, MAX_REG) # Execute the simulation simulator.simulate(5) # Maximum of 5 seconds for the simulation print("Test of «" + filename + "» Succeeded") except NoMatch as error: print("Test of «" + filename + "» failed during the parsing: " + str(error)) except SimulationError as error: print("Test of «" + filename + "» Failed during the execution: " + repr(error))
def parse(self, source, file_name=None): """Parse the given input and returns the generated data. Args: source (str): Source to be parsed """ return visit_parse_tree(super().parse(source), Visitor())
def parse(input_string, prefix=''): """Parses the given DSL string and returns parsed results. Args: input_string (str): DSL string prefix (str): Optional prefix to add to every element name, useful to namespace things Returns: dict: Parsed content """ tree = parser.parse(input_string) visitor = ChatlVisitor(prefix) visit_parse_tree(tree, visitor) return visitor.parsed
def runGrammar(program): parser = ParserPEG(grammar, "program", "comment", debug=False) tree = parser.parse(program) ast = visit_parse_tree(tree, VisitorClass(debug=False)) ast.accept(Interpreter()) # runGrammar(""" # let f = fn (a) { a + 1 } # print(f(3)) # """)
def parse(input_string): """Parses the given DSL string and returns parsed results. Args: input_string (str): DSL string Returns: dict: Parsed content """ tree = PARSER.parse(input_string) visitor = ChatlVisitor() return visit_parse_tree(tree, visitor)
def canonify_music_code(line, text_string_decoder=None): parse_tree = parser.parse(line) return visit_parse_tree( parse_tree, ABCVisitor(text_string_decoder=text_string_decoder))
text = self.text_string_decoder(text) return text def visit_WSP(self, node, children): if self.abc_debug: self.print_debug(node, children) return ' ' def canonify_music_code(line, text_string_decoder=None): parse_tree = parser.parse(line) return visit_parse_tree( parse_tree, ABCVisitor(text_string_decoder=text_string_decoder)) if __name__ == '__main__': # pragma: no cover import pprint import sys pp = pprint.PrettyPrinter(indent=2) result = parser.parse(sys.argv[1]) print(result) # not useful if one is interested in literal terminals pp.pprint(result) print('==================================================') v = visit_parse_tree(result, ABCVisitor(abc_debug=True)) print('==================================================') pp.pprint(v) for c in v: print(hex(ord(c)))
def runGrammar(program): parser = ParserPEG(grammar, "program", "comment", debug=False) tree = parser.parse(program) solution = visit_parse_tree(tree, Visitor(debug=False)) solution.evaluate(Interpreter())