Represents a position (line and column) in a text file.
| 85 | |
| 86 | |
| 87 | class Cursor: |
| 88 | """Represents a position (line and column) in a text file.""" |
| 89 | |
| 90 | def __init__(self, line=-1, column=-1): |
| 91 | self.line = line |
| 92 | self.column = column |
| 93 | |
| 94 | def __eq__(self, rhs): |
| 95 | return self.line == rhs.line and self.column == rhs.column |
| 96 | |
| 97 | def __ne__(self, rhs): |
| 98 | return not self == rhs |
| 99 | |
| 100 | def __lt__(self, rhs): |
| 101 | return self.line < rhs.line or ( |
| 102 | self.line == rhs.line and self.column < rhs.column) |
| 103 | |
| 104 | def __le__(self, rhs): |
| 105 | return self < rhs or self == rhs |
| 106 | |
| 107 | def __gt__(self, rhs): |
| 108 | return rhs < self |
| 109 | |
| 110 | def __ge__(self, rhs): |
| 111 | return rhs <= self |
| 112 | |
| 113 | def __str__(self): |
| 114 | if self == Eof(): |
| 115 | return 'EOF' |
| 116 | else: |
| 117 | return '%s(%s)' % (self.line + 1, self.column) |
| 118 | |
| 119 | def __add__(self, offset): |
| 120 | return Cursor(self.line, self.column + offset) |
| 121 | |
| 122 | def __sub__(self, offset): |
| 123 | return Cursor(self.line, self.column - offset) |
| 124 | |
| 125 | def Clone(self): |
| 126 | """Returns a copy of self.""" |
| 127 | |
| 128 | return Cursor(self.line, self.column) |
| 129 | |
| 130 | |
| 131 | # Special cursor to indicate the end-of-file. |