Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'''
class Override(Named):
pass
class OverrideList(NamedList):
pass
class Special(Base):
pass
class RuleRef(Base):
template = "self._{name}_()"
class RuleInclude(_Decorator):
def render_fields(self, fields):
super().render_fields(fields)
fields.update(exp=self.rend(self.node.rule.exp))
template = '''
{exp}
'''
class Rule(_Decorator):
@staticmethod
def param_repr(p):
def codegen(model):
return PythonCodeGenerator().render(model)
class Base(ModelRenderer):
def defines(self):
return self.node.defines()
class Void(Base):
template = 'self._void()'
class Any(Base):
template = 'self._any()'
class Fail(Base):
template = 'self._fail()'
class Comment(Base):
def render_fields(self, fields):
lines = '\n'.join(
'# %s' % str(c) for c in self.node.comment.splitlines()
)
fields.update(lines=lines)
template = '\n{lines}\n'
class SkipTo(Closure):
template = '''\
def block{n}():
{exp:1::}
self._skip_to(block{n})\
'''
class Optional(_Decorator):
template = '''\
with self._optional():
{exp:1::}\
'''
class Cut(Base):
template = 'self._cut()'
class Named(_Decorator):
def __str__(self):
return '%s:%s' % (self.name, self.rend(self.exp))
def render_fields(self, fields):
fields.update(n=self.counter(),
name=safe_name(self.node.name)
)
template = '''
{exp}
self.name_last_node('{name}')\
'''
class Token(Base):
def render_fields(self, fields):
fields.update(token=repr(self.node.token))
template = "self._token({token})"
class Constant(Base):
def render_fields(self, fields):
fields.update(literal=repr(self.node.literal))
template = "self._constant({literal})"
class Pattern(Base):
def render_fields(self, fields):
raw_repr = repr(self.node.pattern)
fields.update(pattern=raw_repr)
template = 'self._pattern({pattern})'
class Lookahead(_Decorator):
template = '''\
with self._if():
{exp:1::}\
'''
class NegativeLookahead(_Decorator):
template = '''\
def _find_renderer_class(self, node):
if not isinstance(node, Node):
return None
name = node.__class__.__name__
renderer = globals().get(name)
if not renderer or not issubclass(renderer, Base):
raise CodegenError('Renderer for %s not found' % name)
return renderer
self._left_join(block{n}, sep{n})\
'''
class RightJoin(PositiveJoin):
template = '''\
def sep{n}():
{sep:1::}
def block{n}():
{exp:1::}
self._right_join(block{n}, sep{n})\
'''
class EmptyClosure(Base):
template = 'self._empty_closure()'
class SkipTo(Closure):
template = '''\
def block{n}():
{exp:1::}
self._skip_to(block{n})\
'''
class Optional(_Decorator):
template = '''\
with self._optional():
{exp:1::}\
'''
class EOF(Base):
template = 'self._check_eof()'
class _Decorator(Base):
template = '{exp}'
class Group(_Decorator):
template = '''\
with self._group():
{exp:1::}\
'''
class Token(Base):
def render_fields(self, fields):
fields.update(token=repr(self.node.token))
template = "self._token({token})"
class Constant(Base):
def render_fields(self, fields):
fields.update(literal=repr(self.node.literal))
template = "self._constant({literal})"
class Pattern(Base):
def render_fields(self, fields):
raw_repr = repr(self.node.pattern)
%s,
%s
)\
'''
class BasedRule(Rule):
def defines(self):
return self.rhs.defines()
def render_fields(self, fields):
super().render_fields(fields)
fields.update(exp=self.rhs)
class Grammar(Base):
def render_fields(self, fields):
abstract_template = trim(self.abstract_rule_template)
abstract_rules = [
abstract_template.format(name=safe_name(rule.name))
for rule in self.node.rules
]
abstract_rules = indent('\n'.join(abstract_rules))
whitespace = self.node.whitespace or self.node.directives.get('whitespace')
if not whitespace:
whitespace = 'None'
elif isinstance(whitespace, RETYPE):
whitespace = repr(whitespace)
else:
whitespace = 're.compile({0})'.format(repr(whitespace))
class Base(ModelRenderer):
def defines(self):
return self.node.defines()
class Void(Base):
template = 'self._void()'
class Any(Base):
template = 'self._any()'
class Fail(Base):
template = 'self._fail()'
class Comment(Base):
def render_fields(self, fields):
lines = '\n'.join(
'# %s' % str(c) for c in self.node.comment.splitlines()
)
fields.update(lines=lines)
template = '\n{lines}\n'
class EOLComment(Comment):
pass
class NamedList(Named):
template = '''
{exp}
self.add_last_node_to_name('{name}')\
'''
class Override(Named):
pass
class OverrideList(NamedList):
pass
class Special(Base):
pass
class RuleRef(Base):
template = "self._{name}_()"
class RuleInclude(_Decorator):
def render_fields(self, fields):
super().render_fields(fields)
fields.update(exp=self.rend(self.node.rule.exp))
template = '''
{exp}
'''