Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import random
from pyleri import Choice
from pyleri import Grammar
from pyleri import Keyword
from pyleri import Repeat
from pyleri import Sequence
from pyleri import end_of_statement
# Create a Grammar Class to define your language.
class MyGrammar(Grammar):
RE_KEYWORDS = re.compile('\S+')
r_name = Keyword('"pyleri"')
k_hi = Keyword('hi')
k_bye = Keyword('bye')
START = Repeat(Sequence(Choice(k_hi, k_bye), r_name), mi=2)
# Print the expected elements as a indented and numbered list.
def print_expecting(node_expecting, string_expecting):
for loop, e in enumerate(node_expecting):
string_expecting = '{}\n\t({}) {}'.format(string_expecting, loop, e)
print(string_expecting)
# Complete a string until it is valid according to the grammar.
def auto_correction(string, my_grammar):
node = my_grammar.parse(string)
print('\nParsed string: {}'.format(node.tree.string))
if node.is_valid:
string_expecting = 'String is valid. \nExpected: '
import json
from pyleri import Choice
from pyleri import Grammar
from pyleri import Keyword
from pyleri import Regex
from pyleri import Repeat
from pyleri import Sequence
# Create a Grammar Class to define your language
class MyGrammar(Grammar):
r_name = Regex('(?:"(?:[^"]*)")+')
k_hi = Keyword('hi')
k_bye = Keyword('bye')
START = Repeat(Sequence(Choice(k_hi, k_bye), r_name))
# Returns properties of a node object as a dictionary:
def node_props(node, children):
return {
'start': node.start,
'end': node.end,
'name': node.element.name if hasattr(node.element, 'name') else None,
'element': node.element.__class__.__name__,
'string': node.string,
'children': children}
# Recursive method to get the children of a node object:
def get_children(children):
return [node_props(c, get_children(c.children)) for c in children]
Sequence(THIS, k_and, THIS),
Sequence(THIS, k_or, THIS)))
series_setopr = Choice(
k_union,
c_difference,
k_intersection,
k_symmetric_difference,
most_greedy=False)
series_parentheses = Sequence('(', THIS, ')')
series_all = Choice(Token('*'), k_all, most_greedy=False)
series_name = Repeat(string, 1, 1)
group_name = Repeat(r_grave_str, 1, 1)
series_re = Repeat(r_regex, 1, 1)
uuid = Choice(r_uuid_str, string, most_greedy=False)
group_match = Repeat(r_grave_str, 1, 1)
series_match = Prio(
List(Choice(
series_all,
series_name,
group_match,
series_re,
most_greedy=False), series_setopr, 1),
Choice(
series_all,
series_name,
group_match,
series_re,
most_greedy=False),
series_parentheses,
division = Sequence(name, Token('/'), number)
addition = Sequence(name, Token('+'), number)
subtraction = Sequence(name, Token('-'), number)
subtraction_from = Sequence(number, Token('-'), name)
encoded_property = Sequence(Keyword('ENCODED'), Choice(name, multiple, division, addition, subtraction, subtraction_from))
default_property = Sequence(Keyword('DEFAULT'), Choice(name, number))
multiple_of_property = Sequence(Keyword('MULTIPLE_OF'), number)
constant_value_property = Sequence(Keyword('CONSTANT_VALUE'), imm_name)
expr_property = Sequence(Keyword('EXPR'), Choice(name, multiple, division, addition, subtraction, subtraction_from, Keyword('PRESENCE')))
prop = Choice(type_property,
encoded_property,
default_property,
multiple_of_property,
expr_property,
constant_value_property)
START = Repeat(prop, mi=1)
exp_grammar = ExplanationGrammar()
class Explanation:
def __init__(self, type, props, values=None):
self.type = type
self.props = props
if values == None:
self.values = []
else:
self.values = values
def __str__(self):
return 'Explanation(type={!r}, props={!r}, values={!r})'.format(self.type, self.props, self.values)
def unquote_name(name):
Sequence('(', THIS, ')'),
Sequence(THIS, k_and, THIS),
Sequence(THIS, k_or, THIS)))
series_setopr = Choice(
k_union,
c_difference,
k_intersection,
k_symmetric_difference,
most_greedy=False)
series_parentheses = Sequence('(', THIS, ')')
series_all = Choice(Token('*'), k_all, most_greedy=False)
series_name = Repeat(string, 1, 1)
group_name = Repeat(r_grave_str, 1, 1)
series_re = Repeat(r_regex, 1, 1)
uuid = Choice(r_uuid_str, string, most_greedy=False)
group_match = Repeat(r_grave_str, 1, 1)
series_match = Prio(
List(Choice(
series_all,
series_name,
group_match,
series_re,
most_greedy=False), series_setopr, 1),
Choice(
series_all,
series_name,
group_match,
series_re,
most_greedy=False),
k_selected_points,
k_select_points_limit,
k_server,
k_startup_time,
k_status,
k_sync_progress,
k_tee_pipe_name,
k_time_precision,
k_timezone,
k_uptime,
k_uuid,
k_version,
k_who_am_i,
most_greedy=False), ',', 0))
timeit_stmt = Repeat(k_timeit, 1, 1)
help_stmt = Ref()
START = Sequence(
Optional(timeit_stmt),
Optional(Choice(
select_stmt,
list_stmt,
count_stmt,
alter_stmt,
create_stmt,
drop_stmt,
grant_stmt,
revoke_stmt,
show_stmt,
calc_stmt,
clause = 'mapping clause assembly = {}{}{} <-> {}'.format(lhs,
' if ' if neg_sail_guards else '',
neg_sail_guards,
rhs.replace(':', '@'))
print(clause, file=file)
class ASMTemplateGrammar(Grammar):
doublespace = Regex('\s\s+')
space = Regex('\s')
link = Regex('<[A-Za-z0-9_|()+]+>')
text = Regex('[A-Za-z0-9_[\]!,#.]+')
optional = Ref()
optional = Sequence('{', Repeat(Choice(link, text, optional, space), mi=1), '}')
bracket_alternative = Sequence('(', Repeat(Choice(link, text, space), mi=1), '|', Repeat(Choice(link, text, space), mi=1), ')')
# unbracket_alternative = Sequence(Choice(link, text), mi=1), '|', Repeat(Choice(link, text), mi=1))
optional_alternative = Sequence('{', Repeat(Choice(link, text, space), mi=1), '|', Repeat(Choice(link, text, space), mi=1), '}')
START = Repeat(Choice(doublespace, space, link, text, optional_alternative, bracket_alternative, optional), mi=1)
def _walk(self, element, pos, tree, rule, is_required):
if self._pos != pos:
self._s = self._string[pos:] #.lstrip() # don't strip whitespace
self._pos = self._len_string - len(self._s)
node = Node(element, self._string, self._pos)
self._expecting.set_mode_required(node.start, is_required)
return element._get_node_result(self, tree, rule, self._s, node)
asm_grammar = ASMTemplateGrammar()
class BitConcatsGrammar(Grammar):
START = Ref()
arg = Regex('[A-Za-z][A-Za-z0-9]*')
brackets = Sequence('(', START, ')')
series_setopr = Choice(
k_union,
c_difference,
k_intersection,
k_symmetric_difference,
most_greedy=False)
series_parentheses = Sequence('(', THIS, ')')
series_all = Choice(Token('*'), k_all, most_greedy=False)
series_name = Repeat(string, 1, 1)
group_name = Repeat(r_grave_str, 1, 1)
series_re = Repeat(r_regex, 1, 1)
uuid = Choice(r_uuid_str, string, most_greedy=False)
group_match = Repeat(r_grave_str, 1, 1)
series_match = Prio(
List(Choice(
series_all,
series_name,
group_match,
series_re,
most_greedy=False), series_setopr, 1),
Choice(
series_all,
series_name,
group_match,
series_re,
most_greedy=False),
series_parentheses,
Sequence(THIS, series_setopr, THIS),
)
Sequence(THIS, series_setopr, THIS),
)
limit_expr = Sequence(k_limit, int_expr)
before_expr = Sequence(k_before, time_expr)
after_expr = Sequence(k_after, time_expr)
between_expr = Sequence(k_between, time_expr, k_and, time_expr)
access_expr = List(access_keywords, ',', 1)
prefix_expr = Sequence(k_prefix, string)
suffix_expr = Sequence(k_suffix, string)
f_all = Choice(Token('*'), k_all, most_greedy=False)
f_points = Repeat(k_points, 1, 1) # DEPRECATED
f_difference = Sequence(
k_difference,
'(',
Optional(time_expr),
')')
f_derivative = Sequence(
k_derivative,
'(',
List(time_expr, ',', 0, 2),
')')
f_mean = Sequence(
k_mean,
'(', Optional(time_expr), ')')
f_median = Sequence(
k_median,
list_shards = Sequence(
k_shards, Optional(shard_columns), Optional(where_shard))
list_users = Sequence(
k_users, Optional(user_columns), Optional(where_user))
revoke_user = Sequence(k_user, string)
alter_stmt = Sequence(k_alter, Choice(
alter_user,
alter_group,
alter_server,
alter_servers,
alter_database,
most_greedy=False))
calc_stmt = Repeat(time_expr, 1, 1)
count_stmt = Sequence(k_count, Choice(
count_groups,
count_pools,
count_series,
count_servers,
count_servers_received,
count_servers_selected,
count_shards,
count_shards_size,
count_users,
count_series_length,
most_greedy=True))
create_stmt = Sequence(k_create, Choice(
create_group,