How to use the tatsu.grammars.Model function in TatSu

To help you get started, we’ve selected a few TatSu examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github neogeny / TatSu / tatsu / grammars.py View on Github external
def __init__(self, ast=None, exp=None, **kwargs):
        if exp is not None:
            self.exp = exp
        elif not isinstance(ast, AST):
            # Patch to avoid bad interactions with attribute setting in Model.
            # Also a shortcut for subexpressions that are not ASTs.
            ast = AST(exp=ast)
        super().__init__(ast)
        assert isinstance(self.exp, Model)
github neogeny / TatSu / tatsu / grammars.py View on Github external
class Comment(Model):
    def __init__(self, ast=None, **kwargs):
        self.comment = None
        super().__init__(ast=AST(comment=ast))

    def _to_str(self, lean=False):
        return '(* %s *)' % self.comment


class EOLComment(Comment):
    def _to_str(self, lean=False):
        return '  # %s\n' % self.comment


class EOF(Model):
    def parse(self, ctx):
        ctx._check_eof()

    def _to_str(self, lean=False):
        return '$'


class Decorator(Model):
    def __init__(self, ast=None, exp=None, **kwargs):
        if exp is not None:
            self.exp = exp
        elif not isinstance(ast, AST):
            # Patch to avoid bad interactions with attribute setting in Model.
            # Also a shortcut for subexpressions that are not ASTs.
            ast = AST(exp=ast)
        super().__init__(ast)
github neogeny / TatSu / tatsu / grammars.py View on Github external
class Token(Model):
    def __postinit__(self, ast):
        super().__postinit__(ast)
        self.token = ast

    def parse(self, ctx):
        return ctx._token(self.token)

    def _first(self, k, f):
        return {(self.token,)}

    def _to_str(self, lean=False):
        return repr(self.token)


class Constant(Model):
    def __postinit__(self, ast):
        super().__postinit__(ast)
        self.literal = ast

    def parse(self, ctx):
        return self.literal

    def _first(self, k, f):
        return {()}

    def _to_str(self, lean=False):
        return '`%s`' % repr(self.literal)

    def _nullable(self):
        return True
github neogeny / TatSu / tatsu / grammars.py View on Github external
def classes():
        return [
            c for c in globals().values()
            if isinstance(c, type) and issubclass(c, Model)
        ]
github neogeny / TatSu / tatsu / grammars.py View on Github external
def __init__(self, ast=None, **kwargs):
        super().__init__(ast=AST(name='@', exp=ast))

    def defines(self):
        return []


class OverrideList(NamedList):
    def __init__(self, ast=None, **kwargs):
        super().__init__(ast=AST(name='@', exp=ast))

    def defines(self):
        return []


class Special(Model):
    def _first(self, k, f):
        return {(self.value,)}

    def _to_str(self, lean=False):
        return '?%s?' % self.value

    def _nullable(self):
        return True


class RuleRef(Model):
    def __postinit__(self, ast):
        super().__postinit__(ast)
        self.name = ast

    def parse(self, ctx):
github neogeny / TatSu / tatsu / parser_semantics.py View on Github external
def __init__(self, grammar_name):
        super(EBNFGrammarSemantics, self).__init__(
            base_type=grammars.Model,
            types=grammars.Model.classes()
        )
        self.grammar_name = grammar_name
        self.rules = OrderedDict()
github neogeny / TatSu / tatsu / grammars.py View on Github external
return 1

    def pretty(self):
        return self._to_str()

    def pretty_lean(self):
        return self._to_str(lean=True)

    def _to_str(self, lean=False):
        return '%s:%d' % (type(self).__name__, id(self))

    def __str__(self):
        return self._to_str()


class Void(Model):
    def parse(self, ctx):
        return ctx._void()

    def _to_str(self, lean=False):
        return '()'

    def _nullable(self):
        return True


class Any(Model):
    def parse(self, ctx):
        return ctx._any()

    def _to_str(self, lean=False):
        return '/./'
github neogeny / TatSu / tatsu / grammars.py View on Github external
def defines(self):
        return []


class Special(Model):
    def _first(self, k, f):
        return {(self.value,)}

    def _to_str(self, lean=False):
        return '?%s?' % self.value

    def _nullable(self):
        return True


class RuleRef(Model):
    def __postinit__(self, ast):
        super().__postinit__(ast)
        self.name = ast

    def parse(self, ctx):
        try:
            rule = ctx._find_rule(self.name)
        except KeyError:
            ctx._error(self.name, exclass=FailedRef)
        else:
            return rule()

    def missing_rules(self, rules):
        if self.name not in rules:
            return {self.name}
        return set()
github neogeny / TatSu / tatsu / grammars.py View on Github external
def __str__(self):
        return self._to_str()


class Void(Model):
    def parse(self, ctx):
        return ctx._void()

    def _to_str(self, lean=False):
        return '()'

    def _nullable(self):
        return True


class Any(Model):
    def parse(self, ctx):
        return ctx._any()

    def _to_str(self, lean=False):
        return '/./'


class Fail(Model):
    def parse(self, ctx):
        return ctx._fail()

    def _to_str(self, lean=False):
        return '!()'


class Comment(Model):
github neogeny / TatSu / tatsu / parser_semantics.py View on Github external
def __init__(self, grammar_name):
        super(EBNFGrammarSemantics, self).__init__(
            base_type=grammars.Model,
            types=grammars.Model.classes()
        )
        self.grammar_name = grammar_name
        self.rules = OrderedDict()