Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'''This class and THIS instance.
:copyright: 2018, Jeroen van der Heijden (Transceptor Technology)
'''
from .elements import Element
class This(Element):
def _get_node_result(self, root, tree, rule, _s, node):
if node.start not in rule._tested:
rule._tested[node.start] = root._walk(
rule._element,
node.start,
node.children,
rule,
True)
rule._tree[node.start] = node.children
if rule._tested[node.start][0]:
node.children = rule._tree[node.start]
root._append_tree(tree, node, rule._tested[node.start][1])
return rule._tested[node.start]
def _validate_element(element):
if isinstance(element, str):
return Token(element)
if isinstance(element, Element):
return element
raise TypeError(
'Expecting an element or string but received type: {}'.format(
type(element)))
@staticmethod
def _validate_element(element):
if isinstance(element, str):
return Token(element)
if isinstance(element, Element):
return element
raise TypeError(
'Expecting an element or string but received type: {}'.format(
type(element)))
@classmethod
def _validate_elements(cls, elements):
return [cls._validate_element(elem) for elem in elements]
class NamedElement(Element):
__slots__ = ('name',)
def _export_js(self, js_indent, indent, classes, cname):
classes.add(self.__class__.__name__.lstrip('_'))
if hasattr(self, 'name') and indent > 0:
return '{}.{}'.format(cname, self.name) if cname else self.name
indent = 0 if indent < 0 else 1 if indent == 0 else indent
return self._run_export_js(js_indent, indent, classes, cname)
def _export_js_elements(self, js_indent, indent, classes, cname):
new_indent = indent + 1
value = ',\n'.join(['{indent}{elem}'.format(
indent=js_indent * new_indent,
elem=elem._export_js(
js_indent,
self,
py_module_name=PY_MODULE_NAME,
py_template=PY_TEMPLATE,
py_indent=PY_INDENTATION):
'''Export the grammar to a python file which can be
used with the pyleri module. This can be useful when python code
if used to auto-create a grammar and an export of the final result is
required.'''
language = []
classes = {'Grammar'}
indent = 0
for name in self._order:
elem = getattr(self, name, None)
if not isinstance(elem, Element):
continue
if not hasattr(elem, '_export_py'):
continue
language.append('{indent}{name} = {value}'.format(
indent=py_indent,
name=name,
value=elem._export_py(py_indent, indent, classes)))
for name, ref in self._refs.items():
language.append(
'{indent}{name} = {value}'
.format(
indent=py_indent,
name=name,
value=ref._element._export_py(
py_indent,
def __new__(mcs, name, bases, attrs, **kwargs):
for n, ref in attrs['_refs'].items():
if ref.element is None:
raise MissingRefError(
'Forward reference {!r} is createad but the '
'actual reference is missing.'.format(n))
if bases and not isinstance(attrs.get('START'), Element):
raise MissingStartError(
'Grammar is missing the required START element entry point.')
if bases:
used = set()
_used_checker(attrs['START'], used)
elems = {
elem for elem in attrs['_order']
if isinstance(attrs[elem], Element)}
if used != elems:
raise UnusedElementError(
'Unused element(s) found: {}'.format(
', '.join(elems - used)))
return super().__new__(mcs, name, bases, attrs)