File: node.py

package info (click to toggle)
python-pegen 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,980 kB
  • sloc: python: 15,064; makefile: 89
file content (32 lines) | stat: -rw-r--r-- 737 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from token import tok_name
from tokenize import TokenInfo


def short_token(tok: TokenInfo) -> str:
    s = tok.string
    if s == '' or s.isspace():
        return tok_name[tok.type]
    else:
        return repr(s)


def alt_repr(x) -> str:
    if isinstance(x, TokenInfo):
        return short_token(x)
    else:
        return repr(x)


class Node:

    def __init__(self, type, children):
        self.type = type
        self.children = children

    def __repr__(self):
        return f"Node({self.type}, [{', '.join(map(alt_repr, self.children))}])"

    def __eq__(self, other):
        if not isinstance(other, Node):
            return NotImplemented
        return self.type == other.type and self.children == other.children