Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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}
Sequence,
List)
class JsonGrammar(Grammar):
START = Ref()
# JSON strings should be enclosed in double quotes.
# A backslash can be used as escape character.
r_string = Regex(r'(")(?:(?=(\\?))\2.)*?\1')
# JSON does not support floats or integers prefixed with a + sign
# and floats must start with a number, for example .5 is not allowed
# but should be written like 0.5
r_float = Regex(r'-?[0-9]+\.?[0-9]+')
r_integer = Regex('-?[0-9]+')
k_true = Keyword('true')
k_false = Keyword('false')
k_null = Keyword('null')
json_map_item = Sequence(r_string, ':', START)
json_map = Sequence('{', List(json_map_item), '}')
json_array = Sequence('[', List(START), ']')
START = Choice(
r_string,
r_float,
r_integer,
k_true,
k_false,
pos_guards = {k: expand_dontcares(v) for k, v in pos_guards.items()}
neg_guards = {k: expand_dontcares(v) for k, v in neg_guards.items()}
pos_sail_guards = ' & '.join(['(' + ' | '.join('{} == 0b{}'.format(k, v) for v in vs) + ')' for k, vs in pos_guards.items()])
neg_sail_guards = ' & '.join(['(' + ' & '.join('{} != 0b{}'.format(k, v) for v in vs) + ')' for k, vs in neg_guards.items()])
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()
SiriDB grammar.
Note: choices can be optimized using most_greedy=False when there
is a preferable order in choices.
This only should be used when there's no conflict in making a
decision by the parser. (e.g. two choices should start with the
same keyword because in that case we should usually take the most
greedy one)
'''
RE_KEYWORDS = re.compile('[a-z_]+')
# Regular expressions
r_float = Regex('[-+]?[0-9]*\.?[0-9]+')
r_integer = Regex('[-+]?[0-9]+')
r_uinteger = Regex('[0-9]+')
r_time_str = Regex('[0-9]+[smhdw]')
r_singleq_str = Regex('(?:\'(?:[^\']*)\')+')
r_doubleq_str = Regex('(?:"(?:[^"]*)")+')
r_grave_str = Regex('(?:`(?:[^`]*)`)+')
r_uuid_str = Regex(
'[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}')
# we only allow an optional 'i' for case-insensitive regex
r_regex = Regex('(/[^/\\\\]*(?:\\\\.[^/\\\\]*)*/i?)')
r_comment = Regex('#.*')
# Keywords
k_access = Keyword('access')
k_active_handles = Keyword('active_handles')
k_active_tasks = Keyword('active_tasks')
k_address = Keyword('address')
k_after = Keyword('after')
'''
RE_KEYWORDS = re.compile('[a-z_]+')
# Regular expressions
r_float = Regex('[-+]?[0-9]*\.?[0-9]+')
r_integer = Regex('[-+]?[0-9]+')
r_uinteger = Regex('[0-9]+')
r_time_str = Regex('[0-9]+[smhdw]')
r_singleq_str = Regex('(?:\'(?:[^\']*)\')+')
r_doubleq_str = Regex('(?:"(?:[^"]*)")+')
r_grave_str = Regex('(?:`(?:[^`]*)`)+')
r_uuid_str = Regex(
'[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}')
# we only allow an optional 'i' for case-insensitive regex
r_regex = Regex('(/[^/\\\\]*(?:\\\\.[^/\\\\]*)*/i?)')
r_comment = Regex('#.*')
# Keywords
k_access = Keyword('access')
k_active_handles = Keyword('active_handles')
k_active_tasks = Keyword('active_tasks')
k_address = Keyword('address')
k_after = Keyword('after')
k_all = Keyword('all')
k_alter = Keyword('alter')
k_and = Keyword('and')
k_as = Keyword('as')
k_backup_mode = Keyword('backup_mode')
k_before = Keyword('before')
k_buffer_size = Keyword('buffer_size')
k_buffer_path = Keyword('buffer_path')
class MultipleBIMIRecords(BIMIError):
"""Raised when multiple BIMI records are found"""
class _SPFGrammar(Grammar):
"""Defines Pyleri grammar for SPF records"""
version_tag = Regex(SPF_VERSION_TAG_REGEX_STRING)
mechanism = Regex(SPF_MECHANISM_REGEX_STRING, IGNORECASE)
START = Sequence(version_tag, Repeat(mechanism))
class _DMARCGrammar(Grammar):
"""Defines Pyleri grammar for DMARC records"""
version_tag = Regex(DMARC_VERSION_REGEX_STRING)
tag_value = Regex(DMARC_TAG_VALUE_REGEX_STRING)
START = Sequence(version_tag, List(tag_value, delimiter=";", opt=True))
class _BIMIGrammar(Grammar):
"""Defines Pyleri grammar for BIMI records"""
version_tag = Regex(BIMI_VERSION_REGEX_STRING)
tag_value = Regex(BIMI_TAG_VALUE_REGEX_STRING)
START = Sequence(version_tag, List(tag_value, delimiter=";", opt=True))
tag_values = OrderedDict(adkim=OrderedDict(name="DKIM Alignment Mode",
default="r",
description='In relaxed mode, '
'the Organizational '
'Domains of both the '
Keyword,
Sequence,
List)
class JsonGrammar(Grammar):
START = Ref()
# JSON strings should be enclosed in double quotes.
# A backslash can be used as escape character.
r_string = Regex(r'(")(?:(?=(\\?))\2.)*?\1')
# JSON does not support floats or integers prefixed with a + sign
# and floats must start with a number, for example .5 is not allowed
# but should be written like 0.5
r_float = Regex(r'-?[0-9]+\.?[0-9]+')
r_integer = Regex('-?[0-9]+')
k_true = Keyword('true')
k_false = Keyword('false')
k_null = Keyword('null')
json_map_item = Sequence(r_string, ':', START)
json_map = Sequence('{', List(json_map_item), '}')
json_array = Sequence('[', List(START), ']')
START = Choice(
r_string,
r_float,
r_integer,
k_true,
Keyword('IMMEDIATE'),
Keyword('SIGNED_IMMEDIATE'),
Keyword('BITMASK_IMMEDIATE_32'),
Keyword('BITMASK_IMMEDIATE_64'),
Keyword('CONDITION'),
Keyword('INVERTED_CONDITION'),
Keyword('SYSREG'),
Keyword('PREFETCH_OP'),
Keyword('AT_INSTRUCTION'),
Keyword('TLBI_INSTRUCTION'),
Keyword('IC_INSTRUCTION'),
Keyword('DC_INSTRUCTION'),
Keyword('CONSTANT'),
Keyword('BARRIER_SCOPE'))
type_property = Sequence(Keyword('TYPE'), types)
bits = Regex('\'[0-9]+\'')
integer = Regex('[0-9]+')
number = Choice(bits, integer)
multiple = Sequence(name, Token('*'), number)
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,
lhs = '{}({})'.format(sanitize(enc_name), ', '.join(fields))
pos_guards = {k: expand_dontcares(v) for k, v in pos_guards.items()}
neg_guards = {k: expand_dontcares(v) for k, v in neg_guards.items()}
pos_sail_guards = ' & '.join(['(' + ' | '.join('{} == 0b{}'.format(k, v) for v in vs) + ')' for k, vs in pos_guards.items()])
neg_sail_guards = ' & '.join(['(' + ' & '.join('{} != 0b{}'.format(k, v) for v in vs) + ')' for k, vs in neg_guards.items()])
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)
import copy
import itertools
import math
import re
import string
from collections import defaultdict, OrderedDict
from pyleri import Grammar, Regex, Choice, Sequence, Token, Keyword, Repeat, Ref
import explanation_rewrites
class ExplanationGrammar(Grammar):
name_inner = Regex('[A-Za-z0-9:\'_#]+')
quoted_name = Regex('"[A-Za-z0-9:\'_ #]+"')
angle_name = Regex('<[A-Za-z0-9:\'_ #]+>')
imm_name = Regex('#[0-9]+')
name = Choice(quoted_name, angle_name, imm_name, name_inner)
types = Choice(Keyword('WREG_ZR'),
Keyword('XREG_ZR'),
Keyword('WREG_SP'),
Keyword('XREG_SP'),
Keyword('FPREG'),
Keyword('FPREG_128'),
Keyword('FPREG_64'),
Keyword('FPREG_32'),
Keyword('FPREG_16'),
Keyword('FPREG_8'),
Keyword('IMMEDIATE'),
Keyword('SIGNED_IMMEDIATE'),
Keyword('BITMASK_IMMEDIATE_32'),
Keyword('BITMASK_IMMEDIATE_64'),