当前位置: 首页>>代码示例>>Python>>正文


Python ParserPython.metamodel方法代码示例

本文整理汇总了Python中arpeggio.ParserPython.metamodel方法的典型用法代码示例。如果您正苦于以下问题:Python ParserPython.metamodel方法的具体用法?Python ParserPython.metamodel怎么用?Python ParserPython.metamodel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在arpeggio.ParserPython的用法示例。


在下文中一共展示了ParserPython.metamodel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: language_from_str

# 需要导入模块: from arpeggio import ParserPython [as 别名]
# 或者: from arpeggio.ParserPython import metamodel [as 别名]
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
开发者ID:zeph,项目名称:textX,代码行数:70,代码来源:textx.py


注:本文中的arpeggio.ParserPython.metamodel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。