Skip to content

Commit 577e6ce

Browse files
committed
add copy of lark
1 parent f374a0b commit 577e6ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+8187
-0
lines changed

lark/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .utils import logger
2+
from .tree import Tree
3+
from .visitors import Transformer, Visitor, v_args, Discard, Transformer_NonRecursive
4+
from .visitors import InlineTransformer, inline_args # XXX Deprecated
5+
from .exceptions import (ParseError, LexError, GrammarError, UnexpectedToken,
6+
UnexpectedInput, UnexpectedCharacters, UnexpectedEOF, LarkError)
7+
from .lexer import Token
8+
from .lark import Lark
9+
10+
__version__ = "0.11.4"

lark/__pyinstaller/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# For usage of lark with PyInstaller. See https://pyinstaller-sample-hook.readthedocs.io/en/latest/index.html
2+
3+
import os
4+
5+
def get_hook_dirs():
6+
return [os.path.dirname(__file__)]

lark/__pyinstaller/hook-lark.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#-----------------------------------------------------------------------------
2+
# Copyright (c) 2017-2020, PyInstaller Development Team.
3+
#
4+
# Distributed under the terms of the GNU General Public License (version 2
5+
# or later) with exception for distributing the bootloader.
6+
#
7+
# The full license is in the file COPYING.txt, distributed with this software.
8+
#
9+
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
10+
#-----------------------------------------------------------------------------
11+
12+
from PyInstaller.utils.hooks import collect_data_files
13+
14+
datas = collect_data_files('lark')

lark/ast_utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Module of utilities for transforming a lark.Tree into a custom Abstract Syntax Tree
3+
"""
4+
5+
import inspect, re
6+
7+
from lark import Transformer, v_args
8+
9+
class Ast(object):
10+
"""Abstract class
11+
12+
Subclasses will be collected by `create_transformer()`
13+
"""
14+
pass
15+
16+
class AsList(object):
17+
"""Abstract class
18+
19+
Subclasses will be instanciated with the parse results as a single list, instead of as arguments.
20+
"""
21+
22+
def camel_to_snake(name):
23+
return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
24+
25+
def _call(func, _data, children, _meta):
26+
return func(*children)
27+
28+
inline = v_args(wrapper=_call)
29+
30+
def create_transformer(ast_module, transformer=None):
31+
"""Collects `Ast` subclasses from the given module, and creates a Lark transformer that builds the AST.
32+
33+
For each class, we create a corresponding rule in the transformer, with a matching name.
34+
CamelCase names will be converted into snake_case. Example: "CodeBlock" -> "code_block".
35+
36+
Classes starting with an underscore (`_`) will be skipped.
37+
38+
Parameters:
39+
ast_module: A Python module containing all the subclasses of ``ast_utils.Ast``
40+
transformer (Optional[Transformer]): An initial transformer. Its attributes may be overwritten.
41+
"""
42+
t = transformer or Transformer()
43+
44+
for name, obj in inspect.getmembers(ast_module):
45+
if not name.startswith('_') and inspect.isclass(obj):
46+
if issubclass(obj, Ast):
47+
if not issubclass(obj, AsList):
48+
obj = inline(obj).__get__(t)
49+
50+
setattr(t, camel_to_snake(name), obj)
51+
52+
return t

lark/common.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from warnings import warn
2+
from copy import deepcopy
3+
4+
from .utils import Serialize
5+
from .lexer import TerminalDef
6+
7+
###{standalone
8+
9+
10+
class LexerConf(Serialize):
11+
__serialize_fields__ = 'terminals', 'ignore', 'g_regex_flags', 'use_bytes', 'lexer_type'
12+
__serialize_namespace__ = TerminalDef,
13+
14+
def __init__(self, terminals, re_module, ignore=(), postlex=None, callbacks=None, g_regex_flags=0, skip_validation=False, use_bytes=False):
15+
self.terminals = terminals
16+
self.terminals_by_name = {t.name: t for t in self.terminals}
17+
assert len(self.terminals) == len(self.terminals_by_name)
18+
self.ignore = ignore
19+
self.postlex = postlex
20+
self.callbacks = callbacks or {}
21+
self.g_regex_flags = g_regex_flags
22+
self.re_module = re_module
23+
self.skip_validation = skip_validation
24+
self.use_bytes = use_bytes
25+
self.lexer_type = None
26+
27+
@property
28+
def tokens(self):
29+
warn("LexerConf.tokens is deprecated. Use LexerConf.terminals instead", DeprecationWarning)
30+
return self.terminals
31+
32+
def _deserialize(self):
33+
self.terminals_by_name = {t.name: t for t in self.terminals}
34+
35+
def __deepcopy__(self, memo=None):
36+
return type(self)(
37+
deepcopy(self.terminals, memo),
38+
self.re_module,
39+
deepcopy(self.ignore, memo),
40+
deepcopy(self.postlex, memo),
41+
deepcopy(self.callbacks, memo),
42+
deepcopy(self.g_regex_flags, memo),
43+
deepcopy(self.skip_validation, memo),
44+
deepcopy(self.use_bytes, memo),
45+
)
46+
47+
48+
class ParserConf(Serialize):
49+
__serialize_fields__ = 'rules', 'start', 'parser_type'
50+
51+
def __init__(self, rules, callbacks, start):
52+
assert isinstance(start, list)
53+
self.rules = rules
54+
self.callbacks = callbacks
55+
self.start = start
56+
57+
self.parser_type = None
58+
59+
###}

0 commit comments

Comments
 (0)