| 4 | |
| 5 | |
| 6 | class TextInfo: |
| 7 | |
| 8 | def __init__(self, text, start=None, end=None): |
| 9 | # immutable: |
| 10 | if not start: |
| 11 | start = 1 |
| 12 | self.start = start |
| 13 | |
| 14 | # mutable: |
| 15 | lines = text.splitlines() or [''] |
| 16 | self.text = text.strip() |
| 17 | if not end: |
| 18 | end = start + len(lines) - 1 |
| 19 | self.end = end |
| 20 | self.line = lines[-1] |
| 21 | |
| 22 | def __repr__(self): |
| 23 | args = (f'{a}={getattr(self, a)!r}' |
| 24 | for a in ['text', 'start', 'end']) |
| 25 | return f'{type(self).__name__}({", ".join(args)})' |
| 26 | |
| 27 | def add_line(self, line, lno=None): |
| 28 | if lno is None: |
| 29 | lno = self.end + 1 |
| 30 | else: |
| 31 | if isinstance(lno, FileInfo): |
| 32 | fileinfo = lno |
| 33 | if fileinfo.filename != self.filename: |
| 34 | raise NotImplementedError((fileinfo, self.filename)) |
| 35 | lno = fileinfo.lno |
| 36 | # XXX |
| 37 | #if lno < self.end: |
| 38 | # raise NotImplementedError((lno, self.end)) |
| 39 | line = line.lstrip() |
| 40 | self.text += ' ' + line |
| 41 | self.line = line |
| 42 | self.end = lno |
| 43 | |
| 44 | |
| 45 | class SourceInfo: |