How to use the tatsu.leftrec.Nullable 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 / leftrec.py View on Github external
Nullable.no = staticmethod(lambda: Nullable(None, True, False))  # Not nullable
Nullable.yes = staticmethod(lambda: Nullable(None, True, True))  # Nullable
github neogeny / TatSu / tatsu / grammars.py View on Github external
def _nullable(self):
        return Nullable.of(self.exp)
github neogeny / TatSu / tatsu / grammars.py View on Github external
def _nullable(self):
        return Nullable.all(self.sequence)
github neogeny / TatSu / tatsu / leftrec.py View on Github external
if n.resolved:
                if not n.nullable:
                    # Not nullable if any is not nullable
                    self.resolve_with(False)
                    return
            else:
                unresolved.append(c)
        if not unresolved:
            # Nullable if all are nullable
            self.resolve_with(True)
        else:
            # Otherwise still unresolved
            self.chilren = unresolved


class _Any(Nullable):
    def resolve(self, node, rule_dict):
        # Inverse of All
        unresolved = []
        for c in self.children:
            c = follow(c, rule_dict)
            n = c._nullability
            if n.resolved:
                if n.nullable:
                    self.resolve_with(True)
                    return
            else:
                unresolved.append(c)
        if not unresolved:
            self.resolve_with(False)
        else:
            self.children = unresolved
github neogeny / TatSu / tatsu / grammars.py View on Github external
def _nullable(self):
        return Nullable.any(self.options)
github neogeny / TatSu / tatsu / leftrec.py View on Github external
self.resolve_with(False)
        else:
            self.children = unresolved


class _Single(Nullable):
    def resolve(self, node, rule_dict):
        n = follow(self.children[0], rule_dict)._nullability
        if not n.resolved:
            return
        self.resolve_with(n.nullable)


Nullable.all = _All     # Nullable if all children are nullable
Nullable.any = _Any     # Nullable if one child is nullable
Nullable.of = staticmethod(lambda child: _Single([child]))       # Nullable if the only child is nullable
Nullable.no = staticmethod(lambda: Nullable(None, True, False))  # Not nullable
Nullable.yes = staticmethod(lambda: Nullable(None, True, True))  # Nullable


def resolve_nullability(grammar, rule_dict):
    dependants = defaultdict(list)
    visited = set()     # To prevent infinite recursion

    def walk(model):    # TODO Write a walker for this?
        if model in visited:
            return
        visited.add(model)

        for child in model.children_list():
            child = follow(child, rule_dict)
            walk(child)
github neogeny / TatSu / tatsu / leftrec.py View on Github external
def resolve(self, node, rule_dict):
        pass

    def resolve_with(self, n):
        self.resolved = True
        self.nullable = n
        self.children = None  # No longer needed

    all = None  # type: Type[Nullable]
    any = None  # type: Type[Nullable]
    of = None  # type: staticmethod
    no = None  # type: staticmethod
    yes = None  # type: staticmethod


class _All(Nullable):
    def resolve(self, node, rule_dict):
        unresolved = []
        for c in self.children:
            c = follow(c, rule_dict)
            n = c._nullability
            if n.resolved:
                if not n.nullable:
                    # Not nullable if any is not nullable
                    self.resolve_with(False)
                    return
            else:
                unresolved.append(c)
        if not unresolved:
            # Nullable if all are nullable
            self.resolve_with(True)
        else:
github neogeny / TatSu / tatsu / grammars.py View on Github external
def __init__(self, ast=None, ctx=None):
        super().__init__(ast=ast, ctx=ctx)
        self._lookahead = None
        self._firstset = None
        self._follow_set = set()
        self.value = None
        self._nullability = self._nullable()
        if isinstance(self._nullability, int):  # Allow simple boolean values
            if self._nullability:
                self._nullability = Nullable.yes()
            else:
                self._nullability = Nullable.no()
github neogeny / TatSu / tatsu / leftrec.py View on Github external
unresolved.append(c)
        if not unresolved:
            self.resolve_with(False)
        else:
            self.children = unresolved


class _Single(Nullable):
    def resolve(self, node, rule_dict):
        n = follow(self.children[0], rule_dict)._nullability
        if not n.resolved:
            return
        self.resolve_with(n.nullable)


Nullable.all = _All     # Nullable if all children are nullable
Nullable.any = _Any     # Nullable if one child is nullable
Nullable.of = staticmethod(lambda child: _Single([child]))       # Nullable if the only child is nullable
Nullable.no = staticmethod(lambda: Nullable(None, True, False))  # Not nullable
Nullable.yes = staticmethod(lambda: Nullable(None, True, True))  # Nullable


def resolve_nullability(grammar, rule_dict):
    dependants = defaultdict(list)
    visited = set()     # To prevent infinite recursion

    def walk(model):    # TODO Write a walker for this?
        if model in visited:
            return
        visited.add(model)

        for child in model.children_list():