|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import dataclasses |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +from .._logging import rich_print |
| 9 | +from .tokenizer import Token, TokenType, tokenize |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from collections.abc import Generator |
| 13 | + |
| 14 | +__all__ = ["Node", "Block", "parse"] |
| 15 | + |
| 16 | + |
| 17 | +def __dir__() -> list[str]: |
| 18 | + return __all__ |
| 19 | + |
| 20 | + |
| 21 | +@dataclasses.dataclass(frozen=True) |
| 22 | +class Node: |
| 23 | + __slots__ = ("name", "value", "start", "stop") |
| 24 | + |
| 25 | + name: str |
| 26 | + value: str |
| 27 | + start: int |
| 28 | + stop: int |
| 29 | + |
| 30 | + def __str__(self) -> str: |
| 31 | + return f"{self.name}({self.value})" |
| 32 | + |
| 33 | + |
| 34 | +@dataclasses.dataclass(frozen=True) |
| 35 | +class Block(Node): |
| 36 | + __slots__ = ("contents",) |
| 37 | + |
| 38 | + contents: list[Node] |
| 39 | + |
| 40 | + def __str__(self) -> str: |
| 41 | + return f"{super().__str__()} ... {len(self.contents)} children" |
| 42 | + |
| 43 | + |
| 44 | +def parse( |
| 45 | + tokens: Generator[Token, None, None], stop: str = "" |
| 46 | +) -> Generator[Node, None, None]: |
| 47 | + """ |
| 48 | + Generate a stream of nodes from a stream of tokens. This currently bundles all block-like functions |
| 49 | + into a single `Block` node, but this could be changed to be more specific eventually if needed. |
| 50 | + """ |
| 51 | + try: |
| 52 | + while True: |
| 53 | + token = next(tokens) |
| 54 | + if token.type != TokenType.UNQUOTED: |
| 55 | + continue |
| 56 | + name = token.value.lower() |
| 57 | + start = token.start |
| 58 | + token = next(tokens) |
| 59 | + if token.type == TokenType.WHITESPACE: |
| 60 | + token = next(tokens) |
| 61 | + if token.type != TokenType.OPEN_PAREN: |
| 62 | + msg = f"Expected open paren after {name!r}, got {token!r}" |
| 63 | + raise AssertionError(msg) |
| 64 | + count = 1 |
| 65 | + value = "" |
| 66 | + while True: |
| 67 | + token = next(tokens) |
| 68 | + if token.type == TokenType.OPEN_PAREN: |
| 69 | + count += 1 |
| 70 | + elif token.type == TokenType.CLOSE_PAREN: |
| 71 | + count -= 1 |
| 72 | + if count == 0: |
| 73 | + break |
| 74 | + value += token.value |
| 75 | + |
| 76 | + if name in {"if", "foreach", "while", "macro", "function", "block"}: |
| 77 | + contents = list(parse(tokens, f"end{name}")) |
| 78 | + yield Block(name, value, start, contents[-1].stop, contents) |
| 79 | + else: |
| 80 | + yield Node(name, value, start, token.stop) |
| 81 | + if stop and name == stop: |
| 82 | + break |
| 83 | + except StopIteration: |
| 84 | + pass |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + with Path(sys.argv[1]).open(encoding="utf-8-sig") as f: |
| 89 | + for node in parse(tokenize(f.read())): |
| 90 | + cnode = dataclasses.replace( |
| 91 | + node, |
| 92 | + name=f"[bold blue]{node.name}[/bold /blue]", |
| 93 | + value=f"[green]{node.value}[/green]", |
| 94 | + ) |
| 95 | + rich_print(cnode) |
0 commit comments