Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self._refs[key].element = value
if isinstance(value, NamedElement):
value.name = key
return
if isinstance(value, Ref):
self._refs[key] = value
if key == 'RE_KEYWORDS':
if self._has_keywords:
raise ReKeywordsChangedError(
'RE_KEYWORDS must be set on top of Grammar before '
'keywords are set.')
self._re_keywords = value
if isinstance(value, NamedElement):
if hasattr(value, 'name'):
raise NameAssignedError(
'Element name is set to {0!r} and therefore cannot be '
'set to {1!r}. Use Repeat({0}, 1, 1) as a workaround.'
.format(value.name, key))
self._check_keywords(value)
value.name = key
super().__setitem__(key, value)
def __setitem__(self, key, value):
if key not in self:
for k in self._order:
if k.upper() == key.upper():
raise NameAssignedError(
'Element names must be unique in a case in-sensitive '
'way. Cannot set both "{}" and "{}".'.format(
k, key))
self._order.append(key)
elif key in self._refs:
self._refs[key].element = value
if isinstance(value, NamedElement):
value.name = key
return
if isinstance(value, Ref):
self._refs[key] = value
if key == 'RE_KEYWORDS':
if self._has_keywords:
raise ReKeywordsChangedError(
'RE_KEYWORDS must be set on top of Grammar before '
'keywords are set.')
self._re_keywords = value
if isinstance(value, NamedElement):
if hasattr(value, 'name'):
raise NameAssignedError(
'''Optional class.
:copyright: 2018, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement, c_export, go_export, java_export
class Optional(NamedElement):
__slots__ = ('_element',)
def __init__(self, element):
self._element = self._validate_element(element)
@property
def _elements(self):
yield self._element
def _get_node_result(self, root, tree, rule, _s, node):
is_valid, pos = root._walk(
self._element,
node.start,
node.children,
rule,
'''Prio class.
:copyright: 2015, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement
from .rule import Rule
class _Prio(NamedElement):
__slots__ = ('_elements', '_name')
def __init__(self, *elements):
self._elements = self._validate_elements(elements)
def _get_node_result(self, root, tree, rule, s, node):
if node.start not in rule._tested:
rule._tested[node.start] = False, node.start
for elem in self._elements:
children = []
is_valid, pos = root._walk(elem, node.start, children, rule, True)
if is_valid and \
pos > rule._tested[node.start][1]:
node.children = rule._tree[node.start] = children
'''Token class.
:copyright: 2018, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement, c_export, go_export, java_export
class Token(NamedElement):
__slots__ = ('_token',)
def __init__(self, token):
if not isinstance(token, str):
raise TypeError('Token(): first argument must be a string '
'value, got {}'.format(token))
self._token = token
def __repr__(self):
return self._token
def _get_node_result(self, root, tree, rule, s, node):
is_valid = s.startswith(self._token)
if is_valid:
'''Rule class.
:copyright: 2015, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement
class Rule(NamedElement):
__slots__ = ('_element', '_tested', '_tree')
def __init__(self, element):
self._element = element
def _get_node_result(self, root, tree, rule, _s, node):
self._tested = {}
self._tree = {}
is_valid, pos = root._walk(
self._element,
node.start,
node.children,
self,
True)
if is_valid:
class MyGrammar(Grammar):
# needed because the default ^\w+ will not match keyword: my-keyword
RE_KEYWORDS = re.compile('^[\w-]+')
my_keyword = Keyword('my-keyword')
...
:copyright: 2015, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement, c_export, go_export, java_export
class Keyword(NamedElement):
__slots__ = ('_keyword', '_ign_case')
def __init__(self, keyword, ign_case=False):
if not isinstance(keyword, str):
raise TypeError(
'Keyword(): first positional argument must be a string value')
self._keyword = keyword
self._ign_case = bool(ign_case)
def __repr__(self):
return self._keyword
def _get_node_result(self, root, tree, rule, s, node):
'''pyleri.Ref Class.
When a forward reference is used the grammar must contain a final
reference with the same name. The Grammar class is used to validate
a valid grammer and set element.
:copyright: 2018, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement
class Ref(NamedElement):
'''Ref class.'''
__slots__ = ('_element', '_get_node_result')
@property
def element(self):
return getattr(self, '_element', None)
@element.setter
def element(self, element):
self._element = self._validate_element(element)
self._get_node_result = self._element._get_node_result
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Ref({})'.format(self._element.__class__.__name__)
'''pyleri.Choice Class.
Choose one of the given elements. When most_greedy is True we will choose
the 'longest' element when multiple elements are valid. If most_greedy is
False we will return the first match.
:copyright: 2015, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement, c_export, go_export, java_export
class Choice(NamedElement):
__slots__ = ('_elements', '_get_node_result')
def __init__(self, *elements, most_greedy=True):
self._elements = self._validate_elements(elements)
self._get_node_result = \
self._most_greedy_result if most_greedy else \
self._stop_at_first_match
def _most_greedy_result(self, root, tree, rule, s, node):
mg_is_valid, mg_pos = False, node.start
for elem in self._elements:
children = []
is_valid, pos = root._walk(elem, node.start, children, rule, True)
The grammar must find given elements separated by a delimiter. When no
delimiter is given we split using a comma. All arguments:
element: List searches for this element
delimiter: Elements must be separated by this delimiter. (default: ',')
mi: Minimal elements the parser will search for. (default: 0)
ma: Maximum elements the parser will search for. (default: None, unlimited)
opt: When set to True the list may end with a delimiter. (default: False)
:copyright: 2015, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import NamedElement, c_export, go_export, java_export
class List(NamedElement):
__slots__ = ('_element', '_delimiter', '_min', '_max', '_opt')
def __init__(self, element, delimiter=',', mi=0, ma=None, opt=False):
self._element, self._delimiter = \
map(self._validate_element, (element, delimiter))
if not isinstance(mi, int) or mi < 0:
raise TypeError('List(): "mi" must be an integer value larger '
'than or equal to 0, got: {}'.format(mi))
self._min = mi
if not isinstance(ma, (int, type(None))) or \
(ma is not None and ma < mi):
raise TypeError('List(): "ma" must be an integer or None value. '
'If an integer is specified it must be larger '