Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if isinstance(obj, int):
yield Token('INT', obj)
elif isinstance(obj, (type(''), type(u''))):
yield Token('STR', obj)
else:
raise TypeError(obj)
parser = Lark("""
start: data_item+
data_item: STR INT*
%declare STR INT
""", parser='lalr', lexer=TypeLexer)
class ParseToDict(Transformer):
@v_args(inline=True)
def data_item(self, name, *numbers):
return name.value, [n.value for n in numbers]
start = dict
def test():
data = ['alice', 1, 27, 3, 'bob', 4, 'carrie', 'dan', 8, 6]
print(data)
tree = parser.parse(data)
res = ParseToDict().transform(tree)
print('-->')
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
"""
class TreeToJson(Transformer):
@v_args(inline=True)
def string(self, s):
return s[1:-1].replace('\\"', '"')
array = list
pair = tuple
object = dict
number = v_args(inline=True)(float)
null = lambda self, _: None
true = lambda self, _: True
false = lambda self, _: False
### Create the JSON parser with Lark, using the Earley algorithm
# json_parser = Lark(json_grammar, parser='earley', lexer='standard')
class SanitizeError(Exception):
"""
Raised when something goes wrong during sanitizing.
"""
pass
class InconsistentField(SanitizeError):
"""
Raised when a field contains an error which can't be
corrected automatically. Inherits from SanitizeError.
"""
pass
class SanitizerTransformer(_lark.Transformer):
def _call_userfunc(self, tree, new_children=None):
# Comes from "lark/visitors.py", to avoid raising of a "VisitError".
children = new_children if new_children is not None else tree.children
try:
f = getattr(self, tree.data)
except AttributeError:
return self.__default__(tree.data, children, tree.meta)
else:
if getattr(f, 'meta', False):
return f(children, tree.meta)
elif getattr(f, 'inline', False):
return f(*children)
elif getattr(f, 'whole_tree', False):
if new_children is not None:
raise NotImplementedError("Doesn't work with the base Transformer class")
return f(tree)
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.INT
%import common.DIGIT
%import common.CNAME
%import common.WS
%ignore WS
"""
def identity(x):
return x
@v_args(inline=True)
class TreeToSearchExprs(Transformer):
# Convert the expressions
def equals_expr(self, k, v):
return {str(k): v}
def field_in_expr(self, field, lower, upper):
return {str(field): Range(lower, upper)}
def time_in_expr(self, time_field, date_range):
return {str(time_field): date_range}
# Convert the literals
def string(self, val):
return str(val[1:-1])
simple_string = str
url_string = str
from lark import Transformer
from lark.exceptions import ParseError
from arkhe.lang.parser import get_parser
from arkhe.utils import create_instr
from arkhe.vm import Operation
b16 = partial(int, base=16)
class InvalidInstrSize(Exception):
pass
class Base10(Transformer):
def start(self, instrs):
return list(chain.from_iterable(instrs))
def instr(self, tokens):
op = tokens.pop(0)
operands = list(map(b16, tokens))
return create_instr(op, *operands)
class Parser:
def __init__(self):
self.parser = get_parser()
self.transformer = Base10()
def __call__(self, code):
super().__init__(*args, **kwargs)
self.uri = uri
self.abspath = abspath
def _sp(self, meta):
return SourcePosition(
uri=self.uri,
abspath=self.abspath,
line=meta.line,
column=meta.column,
end_line=meta.end_line,
end_column=meta.end_column,
)
class _ExprTransformer(_SourcePositionTransformerMixin, lark.Transformer):
# pylint: disable=no-self-use,unused-argument
def boolean_true(self, items, meta) -> Expr.Base:
return Expr.Boolean(self._sp(meta), True)
def boolean_false(self, items, meta) -> Expr.Base:
return Expr.Boolean(self._sp(meta), False)
def int(self, items, meta) -> Expr.Base:
assert len(items) == 1
return Expr.Int(self._sp(meta), to_int(items[0]))
def float(self, items, meta) -> Expr.Base:
assert len(items) == 1
return Expr.Float(self._sp(meta), to_float(items[0]))
if ref.ros_type in ROS_BUILTIN_TYPES:
terminal = True
else:
msg_fields = self.fields[msg_type]
msg_constants = self.constants[msg_type]
assert not ref.field_type is None
fields[i] = ref
if not terminal:
raise TypeError("'{}' is not a built-in type".format(token))
###############################################################################
# Transformer
###############################################################################
class PropertyTransformer(Transformer):
def __init__(self, type_checker=None):
self.type_checker = type_checker or NullTypeChecker()
def transform(self, tree):
self.type_checker.reset()
return Transformer.transform(self, tree)
def property(self, children):
if len(children) == 2:
assert isinstance(children[0], HplReceiveStatement)
assert isinstance(children[1], HplPublishStatement)
return HplProperty(children[1], receive_stmt=children[0])
else:
assert len(children) == 1
assert isinstance(children[0], HplPublishStatement)
return HplProperty(children[0], receive_stmt=None)
%import common.ESCAPED_STRING -> ESCAPED_STRING
_SP: " "
NILVALUE: "-"
ZULU: "Z"
'''
Header = collections.namedtuple('Header', ['pri', 'version', 'timestamp', 'hostname', 'appname', 'procid', 'msgid'])
SDElement = collections.namedtuple('SDElement', ['sd_id', 'sd_params'])
ParsedMessage = collections.namedtuple('ParsedMessage', ['header', 'structured_data', 'message'])
class TreeTransformer(Transformer):
def NILVALUE(self, inp):
return '-'
def pri(self, inp):
return int(inp[0])
def version(self, inp):
return int(inp[0])
def timestamp(self, inp):
if len(inp) == 1:
return inp[0]
else:
datetime = str(inp[0])
rest = [str(i.children[0]) for i in inp[1:]]
return datetime + ''.join(rest)
TagFragGetItem,
TagFragFilter,
TagFragOutput,
TagFragOpComparison,
)
from .tag import Tag, TagLiteral
from .tagmgr import get_tag, load_all_tags
from .config import LIQUID_LOG_INDENT
from .grammar import BASE_GRAMMAR
from .exceptions import (
TagUnclosed, EndTagUnexpected,
TagWrongPosition
)
@v_args(inline=True)
class TagFactory(Transformer):
"""Transformer for the parser"""
# Last literal tag or last compact mode
LAST_LITERAL_OR_COMPACT = None
def __init__(self, parser, base_level=0):
"""Construct. base_level is used for tags like liquid"""
super().__init__()
self.parser = parser
self.config = parser.config
self.stack = deque()
self.base_level = base_level
self.root = get_tag(
'ROOT', None, self._context(Diot(line=1, column=1))
)
if base_level == 0:
self.config.logger.info('PARSING %s ...',