| 60 | |
| 61 | |
| 62 | class Reader: |
| 63 | def __init__(self, stream: IO[str]) -> None: |
| 64 | self.string = stream.read() |
| 65 | self.position = Position.start() |
| 66 | self.mark = Position.start() |
| 67 | |
| 68 | def has_next(self) -> bool: |
| 69 | return self.position.chars < len(self.string) |
| 70 | |
| 71 | def set_mark(self) -> None: |
| 72 | self.mark.set(self.position) |
| 73 | |
| 74 | def get_marked(self) -> Original: |
| 75 | return Original( |
| 76 | string=self.string[self.mark.chars:self.position.chars], |
| 77 | line=self.mark.line, |
| 78 | ) |
| 79 | |
| 80 | def peek(self, count: int) -> str: |
| 81 | return self.string[self.position.chars:self.position.chars + count] |
| 82 | |
| 83 | def read(self, count: int) -> str: |
| 84 | result = self.string[self.position.chars:self.position.chars + count] |
| 85 | if len(result) < count: |
| 86 | raise Error("read: End of string") |
| 87 | self.position.advance(result) |
| 88 | return result |
| 89 | |
| 90 | def read_regex(self, regex: Pattern[str]) -> Sequence[str]: |
| 91 | match = regex.match(self.string, self.position.chars) |
| 92 | if match is None: |
| 93 | raise Error("read_regex: Pattern not found") |
| 94 | self.position.advance(self.string[match.start():match.end()]) |
| 95 | return match.groups() |
| 96 | |
| 97 | |
| 98 | def decode_escapes(regex: Pattern[str], string: str) -> str: |