| Revision 43,
1.1 KB
checked in by xi, 7 years ago
(diff) |
|
Scanner is mostly completed. Need to write alias, tag, and scalar scanners.
|
| Line | |
|---|
| 1 | |
|---|
| 2 | class Marker: |
|---|
| 3 | |
|---|
| 4 | def __init__(self, source, data, index, line, column): |
|---|
| 5 | self.source = source |
|---|
| 6 | self.data = data |
|---|
| 7 | self.index = index |
|---|
| 8 | self.line = line |
|---|
| 9 | self.column = column |
|---|
| 10 | |
|---|
| 11 | def get_snippet(self, max_length=79): |
|---|
| 12 | if not isinstance(self.data, basestring): |
|---|
| 13 | return None |
|---|
| 14 | head = '' |
|---|
| 15 | start = self.index |
|---|
| 16 | while start > 0 and self.data[start-1] not in '\r\n': |
|---|
| 17 | start -= 1 |
|---|
| 18 | if self.index-start > max_length/2-1: |
|---|
| 19 | head = ' ... ' |
|---|
| 20 | start += 5 |
|---|
| 21 | break |
|---|
| 22 | tail = '' |
|---|
| 23 | end = self.index |
|---|
| 24 | while end < len(self.data) and self.data[end] not in '\r\n': |
|---|
| 25 | end += 1 |
|---|
| 26 | if end-self.index > max_length/2-1: |
|---|
| 27 | tail = ' ... ' |
|---|
| 28 | end -= 5 |
|---|
| 29 | break |
|---|
| 30 | snippet = self.data[start:end] |
|---|
| 31 | if isinstance(snippet, unicode): |
|---|
| 32 | snippet = snippet.encode('utf-8') |
|---|
| 33 | return head + snippet + tail + '\n' \ |
|---|
| 34 | + ' '*(self.index-start+len(head)) + '^' + '\n' |
|---|
| 35 | |
|---|
Note: See
TracBrowser
for help on using the repository browser.