def _from_peg(self, language_def): parser = ParserPython(peggrammar, comment, reduce_tree=False, debug=self.debug) parser.root_rule_name = self.root_rule_name parse_tree = parser.parse(language_def) return visit_parse_tree(parse_tree, PEGVisitor(self.root_rule_name, self.comment_rule_name, self.ignore_case, debug=self.debug))
def _from_peg(self, language_def): parser = ParserPython(peggrammar, comment, reduce_tree=False, debug=self.debug) parser.root_rule_name = self.root_rule_name parse_tree = parser.parse(language_def) return visit_parse_tree(parse_tree, PEGVisitor(self.root_rule_name, self.comment_rule_name, self.ignore_case, debug=self.debug))
def language_from_str(language_def, metamodel): """ Constructs parser and initializes metamodel from language description given in textX language. Args: language_def (str): A language description in textX. metamodel (TextXMetaModel): A metamodel to initialize. Returns: Parser for the new language. """ if metamodel.debug: metamodel.dprint("*** PARSING LANGUAGE DEFINITION ***") # Check the cache for already conctructed textX parser if metamodel.debug in textX_parsers: parser = textX_parsers[metamodel.debug] else: # Create parser for TextX descriptions from # the textX grammar specified in this module parser = ParserPython(textx_model, comment_def=comment, ignore_case=False, reduce_tree=False, debug=metamodel.debug) # Prepare regex used in keyword-like strmatch detection. # See str_match_SA flags = 0 if metamodel.ignore_case: flags = re.IGNORECASE parser.keyword_regex = re.compile(r'[^\d\W]\w*', flags) # Cache it for subsequent calls textX_parsers[metamodel.debug] = parser # This is used during parser construction phase. # Metamodel is filled in. Classes are created based on grammar rules. parser.metamodel = metamodel # Builtin rules representing primitive types parser.root_rule_name = None # Parse language description with textX parser try: parser.parse(language_def) except NoMatch as e: line, col = parser.pos_to_linecol(e.position) raise TextXSyntaxError(text(e), line, col) # Construct new parser based on the given language description. lang_parser = parser.getASG() # Meta-model is constructed. Validate semantics of metamodel. parser.metamodel.validate() # Meta-model is constructed. Here we connect meta-model and language # parser for convenience. lang_parser.metamodel = parser.metamodel metamodel.parser = lang_parser if metamodel.debug: # Create dot file for debuging purposes PMDOTExporter().exportFile( lang_parser.parser_model, "{}_parser_model.dot".format(metamodel.rootcls.__name__)) return lang_parser
def language_from_str(language_def, metamodel): """ Constructs parser and initializes metamodel from language description given in textX language. Args: language_def (str): A language description in textX. metamodel (TextXMetaModel): A metamodel to initialize. Returns: Parser for the new language. """ if metamodel.debug: metamodel.dprint("*** PARSING LANGUAGE DEFINITION ***") # Check the cache for already conctructed textX parser if metamodel.debug in textX_parsers: parser = textX_parsers[metamodel.debug] else: # Create parser for TextX descriptions from # the textX grammar specified in this module parser = ParserPython(textx_model, comment_def=comment, ignore_case=False, reduce_tree=False, debug=metamodel.debug) # Prepare regex used in keyword-like strmatch detection. # See str_match_SA flags = 0 if metamodel.ignore_case: flags = re.IGNORECASE parser.keyword_regex = re.compile(r'[^\d\W]\w*', flags) # Cache it for subsequent calls textX_parsers[metamodel.debug] = parser # This is used during parser construction phase. # Metamodel is filled in. Classes are created based on grammar rules. parser.metamodel = metamodel # Builtin rules representing primitive types parser.root_rule_name = None # Parse language description with textX parser try: parser.parse(language_def) except NoMatch as e: line, col = parser.pos_to_linecol(e.position) raise TextXSyntaxError(text(e), line, col) # Construct new parser based on the given language description. lang_parser = parser.getASG() # Meta-model is constructed. Validate semantics of metamodel. parser.metamodel.validate() # Meta-model is constructed. Here we connect meta-model and language # parser for convenience. lang_parser.metamodel = parser.metamodel metamodel.parser = lang_parser if metamodel.debug: # Create dot file for debuging purposes PMDOTExporter().exportFile( lang_parser.parser_model, "{}_parser_model.dot".format(metamodel.rootcls.__name__)) return lang_parser