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


Python lark.Lark方法代码示例

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


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

示例1: __init__

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def __init__(self, version=None, variant="default"):

        version = version if version else max(available_parsers.keys())

        if version not in available_parsers:
            raise ParserError(f"Unknown parser grammar version: {version}")

        if variant not in available_parsers[version]:
            raise ParserError(f"Unknown variant of the parser: {variant}")

        self.version = version
        self.variant = variant

        with open(available_parsers[version][variant]) as file:
            self.lark = Lark(file)

        self.tree = None
        self.filter = None 
开发者ID:Materials-Consortia,项目名称:optimade-python-tools,代码行数:20,代码来源:lark_parser.py

示例2: main

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def main(s, out_fn):
    graphviz_setup()
    project_root = os.path.normpath(os.path.join(os.path.dirname(__file__), "../../"))
    fld = os.path.normpath(project_root + "./mappyfile")
    gf = os.path.join(fld, "mapfile.lalr.g")
    grammar_text = open(gf).read()

    g = Lark(grammar_text, parser="lalr", lexer="contextual")
    t = g.parse(s)
    print(t)
    pydot__tree_to_png(t, os.path.join(project_root, "docs/images", out_fn))
    print(t.pretty()) 
开发者ID:geographika,项目名称:mappyfile,代码行数:14,代码来源:grammar.py

示例3: _create_lalr_parser

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def _create_lalr_parser(self):
        grammar_text = self.load_grammar("mapfile.lalr.g")
        if self.include_comments:
            callbacks = {'COMMENT': self._comments.append, 'CCOMMENT': self._comments.append}
            extra_args = dict(propagate_positions=True, lexer_callbacks=callbacks)
        else:
            extra_args = {}
        return Lark(grammar_text, parser="lalr", lexer="contextual", **extra_args) 
开发者ID:geographika,项目名称:mappyfile,代码行数:10,代码来源:parser.py

示例4: run

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def run(playlist):
    global parser
    parser = Lark(G.grammar, parser='lalr')
    dirname = os.path.dirname(playlist)

    setFilename(playlist)
    pushDir(dirname)

    with open(playlist) as f:
        tree = parser.parse(f.read())
        # print(tree)
        # print("="*16)
        for t in tree.children:
            do(t) 
开发者ID:hofstadter-io,项目名称:self-driving-desktop,代码行数:16,代码来源:parser.py

示例5: onKeyUp

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def onKeyUp(self, e=None):
		l = Lark(self.grammar["value"], parser=self.parser)

		try:
			ast = l.parse(self.input["value"])
		except Exception as e:
			self.ast.appendChild(
				html5.Li(str(e)), replace=True
			)

		print(ast)
		traverse = lambda node: html5.Li([node.data, html5.Ul([traverse(c) for c in node.children])] if isinstance(node, Tree) else node)
		self.ast.appendChild(traverse(ast), replace=True) 
开发者ID:lark-parser,项目名称:lark,代码行数:15,代码来源:app.py

示例6: create_parser

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def create_parser(self):
        grammar = '''
            anons: ":" "{" "}" "," "[" "]"
            TRUE: "true"
            FALSE: "false"
            NULL: "NULL"
            %import common.ESCAPED_STRING -> STRING
            %import common.SIGNED_NUMBER  -> NUMBER
            %import common.WS
            %ignore WS
        '''

        self.lark = Lark(grammar, parser=None, lexer='standard')
        # All tokens: print([t.name for t in self.lark.parser.lexer.tokens]) 
开发者ID:lark-parser,项目名称:lark,代码行数:16,代码来源:qscintilla_json.py

示例7: test_earley

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def test_earley():

    json_parser = Lark(json_grammar, maybe_placeholders=False)
    tree = json_parser.parse(test_json)

    new_json = Reconstructor(json_parser).reconstruct(tree)
    print (new_json)
    print (json.loads(new_json) == json.loads(test_json)) 
开发者ID:lark-parser,项目名称:lark,代码行数:10,代码来源:reconstruct_json.py

示例8: test_lalr

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def test_lalr():

    json_parser = Lark(json_grammar, parser='lalr', maybe_placeholders=False)
    tree = json_parser.parse(test_json)

    new_json = Reconstructor(json_parser).reconstruct(tree)
    print (new_json)
    print (json.loads(new_json) == json.loads(test_json)) 
开发者ID:lark-parser,项目名称:lark,代码行数:10,代码来源:reconstruct_json.py

示例9: main

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def main():
    if len(sys.argv) == 1 or '-h' in sys.argv or '--help' in sys.argv:
        print("Lark Serialization Tool - Stores Lark's internal state & LALR analysis as a JSON file")
        print("")
        argparser.print_help()
    else:
        args = argparser.parse_args()
        serialize(args.grammar_file, args.out, args.lexer, args.start) 
开发者ID:lark-parser,项目名称:lark,代码行数:10,代码来源:serialize.py

示例10: create_code_for_nearley_grammar

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def create_code_for_nearley_grammar(g, start, builtin_path, folder_path):
    import js2py

    emit_code = []
    def emit(x=None):
        if x:
            emit_code.append(x)
        emit_code.append('\n')

    js_code = ['function id(x) {return x[0];}']
    n2l = NearleyToLark()
    rule_defs = _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path, set())
    lark_g = '\n'.join(rule_defs)
    lark_g += '\n'+'\n'.join('!%s: %s' % item for item in n2l.extra_rules.items())

    emit('from lark import Lark, Transformer')
    emit()
    emit('grammar = ' + repr(lark_g))
    emit()

    for alias, code in n2l.alias_js_code.items():
        js_code.append('%s = (%s);' % (alias, code))

    emit(js2py.translate_js('\n'.join(js_code)))
    emit('class TransformNearley(Transformer):')
    for alias in n2l.alias_js_code:
        emit("    %s = var.get('%s').to_python()" % (alias, alias))
    emit("    __default__ = lambda self, n, c, m: c if c else None")

    emit()
    emit('parser = Lark(grammar, start="n_%s", maybe_placeholders=False)' % start)
    emit('def parse(text):')
    emit('    return TransformNearley().transform(parser.parse(text))')

    return ''.join(emit_code) 
开发者ID:lark-parser,项目名称:lark,代码行数:37,代码来源:nearley.py

示例11: assert_reconstruct

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def assert_reconstruct(self, grammar, code):
        parser = Lark(grammar, parser='lalr', maybe_placeholders=False)
        tree = parser.parse(code)
        new = Reconstructor(parser).reconstruct(tree)
        self.assertEqual(_remove_ws(code), _remove_ws(new)) 
开发者ID:lark-parser,项目名称:lark,代码行数:7,代码来源:test_reconstructor.py

示例12: test_json_example

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def test_json_example(self):
        test_json = '''
            {
                "empty_object" : {},
                "empty_array"  : [],
                "booleans"     : { "YES" : true, "NO" : false },
                "numbers"      : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
                "strings"      : [ "This", [ "And" , "That", "And a \\"b" ] ],
                "nothing"      : null
            }
        '''

        json_grammar = r"""
            ?start: value

            ?value: object
                  | array
                  | string
                  | SIGNED_NUMBER      -> number
                  | "true"             -> true
                  | "false"            -> false
                  | "null"             -> null

            array  : "[" [value ("," value)*] "]"
            object : "{" [pair ("," pair)*] "}"
            pair   : string ":" value

            string : ESCAPED_STRING

            %import common.ESCAPED_STRING
            %import common.SIGNED_NUMBER
            %import common.WS

            %ignore WS
        """

        json_parser = Lark(json_grammar, parser='lalr', maybe_placeholders=False)
        tree = json_parser.parse(test_json)

        new_json = Reconstructor(json_parser).reconstruct(tree)
        self.assertEqual(json.loads(new_json), json.loads(test_json)) 
开发者ID:lark-parser,项目名称:lark,代码行数:43,代码来源:test_reconstructor.py

示例13: test_simple

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def test_simple(self):
        g = '''start: "a"'''

        fn = "bla"

        fs = lark_module.FS
        mock_fs = MockFS()
        try:
            lark_module.FS = mock_fs
            Lark(g, parser='lalr', cache=fn)
            assert fn in mock_fs.files
            parser = Lark(g, parser='lalr', cache=fn)
            assert parser.parse('a') == Tree('start', [])

            mock_fs.files = {}
            assert len(mock_fs.files) == 0
            Lark(g, parser='lalr', cache=True)
            assert len(mock_fs.files) == 1
            parser = Lark(g, parser='lalr', cache=True)
            assert parser.parse('a') == Tree('start', [])

            parser = Lark(g+' "b"', parser='lalr', cache=True)
            assert len(mock_fs.files) == 2
            assert parser.parse('ab') == Tree('start', [])

            parser = Lark(g, parser='lalr', cache=True)
            assert parser.parse('a') == Tree('start', [])

        finally:
            lark_module.FS = fs 
开发者ID:lark-parser,项目名称:lark,代码行数:32,代码来源:test_cache.py

示例14: lark_parser

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def lark_parser():
        return Lark(grammar, parser='lalr', debug=True) 
开发者ID:simon816,项目名称:Command-Block-Assembly,代码行数:4,代码来源:reader.py

示例15: lark_parser

# 需要导入模块: import lark [as 别名]
# 或者: from lark import Lark [as 别名]
def lark_parser():
        return Lark(grammar, parser='lalr', debug=True,
                    lexer_callbacks=lexer_callbacks)#, propagate_positions=True) 
开发者ID:simon816,项目名称:Command-Block-Assembly,代码行数:5,代码来源:parser_.py


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