| 1 | # $Id: SourceReader.py,v 1.15 2007/04/03 01:57:42 tavis_rudd Exp $ |
|---|
| 2 | """SourceReader class for Cheetah's Parser and CodeGenerator |
|---|
| 3 | |
|---|
| 4 | Meta-Data |
|---|
| 5 | ================================================================================ |
|---|
| 6 | Author: Tavis Rudd <tavis@damnsimple.com> |
|---|
| 7 | License: This software is released for unlimited distribution under the |
|---|
| 8 | terms of the MIT license. See the LICENSE file. |
|---|
| 9 | Version: $Revision: 1.15 $ |
|---|
| 10 | Start Date: 2001/09/19 |
|---|
| 11 | Last Revision Date: $Date: 2007/04/03 01:57:42 $ |
|---|
| 12 | """ |
|---|
| 13 | __author__ = "Tavis Rudd <tavis@damnsimple.com>" |
|---|
| 14 | __revision__ = "$Revision: 1.15 $"[11:-2] |
|---|
| 15 | |
|---|
| 16 | import re |
|---|
| 17 | import sys |
|---|
| 18 | |
|---|
| 19 | EOLre = re.compile(r'[ \f\t]*(?:\r\n|\r|\n)') |
|---|
| 20 | EOLZre = re.compile(r'(?:\r\n|\r|\n|\Z)') |
|---|
| 21 | ENCODINGsearch = re.compile("coding[=:]\s*([-\w.]+)").search |
|---|
| 22 | |
|---|
| 23 | class Error(Exception): |
|---|
| 24 | pass |
|---|
| 25 | |
|---|
| 26 | class SourceReader: |
|---|
| 27 | def __init__(self, src, filename=None, breakPoint=None, encoding=None): |
|---|
| 28 | |
|---|
| 29 | ## @@TR 2005-01-17: the following comes from a patch Terrel Shumway |
|---|
| 30 | ## contributed to add unicode support to the reading of Cheetah source |
|---|
| 31 | ## files with dynamically compiled templates. All the existing unit |
|---|
| 32 | ## tests pass but, it needs more testing and some test cases of its |
|---|
| 33 | ## own. My instinct is to move this up into the code that passes in the |
|---|
| 34 | ## src string rather than leaving it here. As implemented here it |
|---|
| 35 | ## forces all src strings to unicode, which IMO is not what we want. |
|---|
| 36 | # if encoding is None: |
|---|
| 37 | # # peek at the encoding in the first two lines |
|---|
| 38 | # m = EOLZre.search(src) |
|---|
| 39 | # pos = m.end() |
|---|
| 40 | # if pos<len(src): |
|---|
| 41 | # m = EOLZre.search(src,pos) |
|---|
| 42 | # pos = m.end() |
|---|
| 43 | # m = ENCODINGsearch(src,0,pos) |
|---|
| 44 | # if m: |
|---|
| 45 | # encoding = m.group(1) |
|---|
| 46 | # else: |
|---|
| 47 | # encoding = sys.getfilesystemencoding() |
|---|
| 48 | # self._encoding = encoding |
|---|
| 49 | # if type(src) is not unicode: |
|---|
| 50 | # src = src.decode(encoding) |
|---|
| 51 | ## end of Terrel's patch |
|---|
| 52 | |
|---|
| 53 | self._src = src |
|---|
| 54 | self._filename = filename |
|---|
| 55 | |
|---|
| 56 | self._srcLen = len(src) |
|---|
| 57 | if breakPoint == None: |
|---|
| 58 | self._breakPoint = self._srcLen |
|---|
| 59 | else: |
|---|
| 60 | self.setBreakPoint(breakPoint) |
|---|
| 61 | self._pos = 0 |
|---|
| 62 | self._bookmarks = {} |
|---|
| 63 | self._posTobookmarkMap = {} |
|---|
| 64 | |
|---|
| 65 | ## collect some meta-information |
|---|
| 66 | self._EOLs = [] |
|---|
| 67 | pos = 0 |
|---|
| 68 | while pos < len(self): |
|---|
| 69 | EOLmatch = EOLZre.search(src, pos) |
|---|
| 70 | self._EOLs.append(EOLmatch.start()) |
|---|
| 71 | pos = EOLmatch.end() |
|---|
| 72 | |
|---|
| 73 | self._BOLs = [] |
|---|
| 74 | for pos in self._EOLs: |
|---|
| 75 | BOLpos = self.findBOL(pos) |
|---|
| 76 | self._BOLs.append(BOLpos) |
|---|
| 77 | |
|---|
| 78 | def src(self): |
|---|
| 79 | return self._src |
|---|
| 80 | |
|---|
| 81 | def filename(self): |
|---|
| 82 | return self._filename |
|---|
| 83 | |
|---|
| 84 | def __len__(self): |
|---|
| 85 | return self._breakPoint |
|---|
| 86 | |
|---|
| 87 | def __getitem__(self, i): |
|---|
| 88 | self.checkPos(i) |
|---|
| 89 | return self._src[i] |
|---|
| 90 | |
|---|
| 91 | def __getslice__(self, i, j): |
|---|
| 92 | i = max(i, 0); j = max(j, 0) |
|---|
| 93 | return self._src[i:j] |
|---|
| 94 | |
|---|
| 95 | def splitlines(self): |
|---|
| 96 | if not hasattr(self, '_srcLines'): |
|---|
| 97 | self._srcLines = self._src.splitlines() |
|---|
| 98 | return self._srcLines |
|---|
| 99 | |
|---|
| 100 | def lineNum(self, pos=None): |
|---|
| 101 | if pos == None: |
|---|
| 102 | pos = self._pos |
|---|
| 103 | |
|---|
| 104 | for i in range(len(self._BOLs)): |
|---|
| 105 | if pos >= self._BOLs[i] and pos <= self._EOLs[i]: |
|---|
| 106 | return i |
|---|
| 107 | |
|---|
| 108 | def getRowCol(self, pos=None): |
|---|
| 109 | if pos == None: |
|---|
| 110 | pos = self._pos |
|---|
| 111 | lineNum = self.lineNum(pos) |
|---|
| 112 | BOL, EOL = self._BOLs[lineNum], self._EOLs[lineNum] |
|---|
| 113 | return lineNum+1, pos-BOL+1 |
|---|
| 114 | |
|---|
| 115 | def getRowColLine(self, pos=None): |
|---|
| 116 | if pos == None: |
|---|
| 117 | pos = self._pos |
|---|
| 118 | row, col = self.getRowCol(pos) |
|---|
| 119 | return row, col, self.splitlines()[row-1] |
|---|
| 120 | |
|---|
| 121 | def getLine(self, pos): |
|---|
| 122 | if pos == None: |
|---|
| 123 | pos = self._pos |
|---|
| 124 | lineNum = self.lineNum(pos) |
|---|
| 125 | return self.splitlines()[lineNum] |
|---|
| 126 | |
|---|
| 127 | def pos(self): |
|---|
| 128 | return self._pos |
|---|
| 129 | |
|---|
| 130 | def setPos(self, pos): |
|---|
| 131 | self.checkPos(pos) |
|---|
| 132 | self._pos = pos |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | def validPos(self, pos): |
|---|
| 136 | return pos <= self._breakPoint and pos >=0 |
|---|
| 137 | |
|---|
| 138 | def checkPos(self, pos): |
|---|
| 139 | if not pos <= self._breakPoint: |
|---|
| 140 | raise Error("pos (" + str(pos) + ") is invalid: beyond the stream's end (" + |
|---|
| 141 | str(self._breakPoint-1) + ")" ) |
|---|
| 142 | elif not pos >=0: |
|---|
| 143 | raise Error("pos (" + str(pos) + ") is invalid: less than 0" ) |
|---|
| 144 | |
|---|
| 145 | def breakPoint(self): |
|---|
| 146 | return self._breakPoint |
|---|
| 147 | |
|---|
| 148 | def setBreakPoint(self, pos): |
|---|
| 149 | if pos > self._srcLen: |
|---|
| 150 | raise Error("New breakpoint (" + str(pos) + |
|---|
| 151 | ") is invalid: beyond the end of stream's source string (" + |
|---|
| 152 | str(self._srcLen) + ")" ) |
|---|
| 153 | elif not pos >= 0: |
|---|
| 154 | raise Error("New breakpoint (" + str(pos) + ") is invalid: less than 0" ) |
|---|
| 155 | |
|---|
| 156 | self._breakPoint = pos |
|---|
| 157 | |
|---|
| 158 | def setBookmark(self, name): |
|---|
| 159 | self._bookmarks[name] = self._pos |
|---|
| 160 | self._posTobookmarkMap[self._pos] = name |
|---|
| 161 | |
|---|
| 162 | def hasBookmark(self, name): |
|---|
| 163 | return self._bookmarks.has_key(name) |
|---|
| 164 | |
|---|
| 165 | def gotoBookmark(self, name): |
|---|
| 166 | if not self.hasBookmark(name): |
|---|
| 167 | raise Error("Invalid bookmark (" + name + ") is invalid: does not exist") |
|---|
| 168 | pos = self._bookmarks[name] |
|---|
| 169 | if not self.validPos(pos): |
|---|
| 170 | raise Error("Invalid bookmark (" + name + ', '+ |
|---|
| 171 | str(pos) + ") is invalid: pos is out of range" ) |
|---|
| 172 | self._pos = pos |
|---|
| 173 | |
|---|
| 174 | def atEnd(self): |
|---|
| 175 | return self._pos >= self._breakPoint |
|---|
| 176 | |
|---|
| 177 | def atStart(self): |
|---|
| 178 | return self._pos == 0 |
|---|
| 179 | |
|---|
| 180 | def peek(self, offset=0): |
|---|
| 181 | self.checkPos(self._pos+offset) |
|---|
| 182 | pos = self._pos + offset |
|---|
| 183 | return self._src[pos] |
|---|
| 184 | |
|---|
| 185 | def getc(self): |
|---|
| 186 | pos = self._pos |
|---|
| 187 | if self.validPos(pos+1): |
|---|
| 188 | self._pos += 1 |
|---|
| 189 | return self._src[pos] |
|---|
| 190 | |
|---|
| 191 | def ungetc(self, c=None): |
|---|
| 192 | if not self.atStart(): |
|---|
| 193 | raise Error('Already at beginning of stream') |
|---|
| 194 | |
|---|
| 195 | self._pos -= 1 |
|---|
| 196 | if not c==None: |
|---|
| 197 | self._src[self._pos] = c |
|---|
| 198 | |
|---|
| 199 | def advance(self, offset=1): |
|---|
| 200 | self.checkPos(self._pos + offset) |
|---|
| 201 | self._pos += offset |
|---|
| 202 | |
|---|
| 203 | def rev(self, offset=1): |
|---|
| 204 | self.checkPos(self._pos - offset) |
|---|
| 205 | self._pos -= offset |
|---|
| 206 | |
|---|
| 207 | def read(self, offset): |
|---|
| 208 | self.checkPos(self._pos + offset) |
|---|
| 209 | start = self._pos |
|---|
| 210 | self._pos += offset |
|---|
| 211 | return self._src[start:self._pos] |
|---|
| 212 | |
|---|
| 213 | def readTo(self, to, start=None): |
|---|
| 214 | self.checkPos(to) |
|---|
| 215 | if start == None: |
|---|
| 216 | start = self._pos |
|---|
| 217 | self._pos = to |
|---|
| 218 | return self._src[start:to] |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | def readToEOL(self, start=None, gobble=True): |
|---|
| 222 | EOLmatch = EOLZre.search(self.src(), self.pos()) |
|---|
| 223 | if gobble: |
|---|
| 224 | pos = EOLmatch.end() |
|---|
| 225 | else: |
|---|
| 226 | pos = EOLmatch.start() |
|---|
| 227 | return self.readTo(to=pos, start=start) |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | def find(self, it, pos=None): |
|---|
| 231 | if pos == None: |
|---|
| 232 | pos = self._pos |
|---|
| 233 | return self._src.find(it, pos ) |
|---|
| 234 | |
|---|
| 235 | def startswith(self, it, pos=None): |
|---|
| 236 | if self.find(it, pos) == self.pos(): |
|---|
| 237 | return True |
|---|
| 238 | else: |
|---|
| 239 | return False |
|---|
| 240 | |
|---|
| 241 | def rfind(self, it, pos): |
|---|
| 242 | if pos == None: |
|---|
| 243 | pos = self._pos |
|---|
| 244 | return self._src.rfind(it, pos) |
|---|
| 245 | |
|---|
| 246 | def findBOL(self, pos=None): |
|---|
| 247 | if pos == None: |
|---|
| 248 | pos = self._pos |
|---|
| 249 | src = self.src() |
|---|
| 250 | return max(src.rfind('\n',0,pos)+1, src.rfind('\r',0,pos)+1, 0) |
|---|
| 251 | |
|---|
| 252 | def findEOL(self, pos=None, gobble=False): |
|---|
| 253 | if pos == None: |
|---|
| 254 | pos = self._pos |
|---|
| 255 | |
|---|
| 256 | match = EOLZre.search(self.src(), pos) |
|---|
| 257 | if gobble: |
|---|
| 258 | return match.end() |
|---|
| 259 | else: |
|---|
| 260 | return match.start() |
|---|
| 261 | |
|---|
| 262 | def isLineClearToPos(self, pos=None): |
|---|
| 263 | if pos == None: |
|---|
| 264 | pos = self.pos() |
|---|
| 265 | self.checkPos(pos) |
|---|
| 266 | src = self.src() |
|---|
| 267 | BOL = self.findBOL() |
|---|
| 268 | return BOL == pos or src[BOL:pos].isspace() |
|---|
| 269 | |
|---|
| 270 | def matches(self, strOrRE): |
|---|
| 271 | if isinstance(strOrRE, (str, unicode)): |
|---|
| 272 | return self.startswith(strOrRE, pos=self.pos()) |
|---|
| 273 | else: # assume an re object |
|---|
| 274 | return strOrRE.match(self.src(), self.pos()) |
|---|
| 275 | |
|---|
| 276 | def matchWhiteSpace(self, WSchars=' \f\t'): |
|---|
| 277 | return (not self.atEnd()) and self.peek() in WSchars |
|---|
| 278 | |
|---|
| 279 | def getWhiteSpace(self, max=None, WSchars=' \f\t'): |
|---|
| 280 | if not self.matchWhiteSpace(WSchars): |
|---|
| 281 | return '' |
|---|
| 282 | start = self.pos() |
|---|
| 283 | breakPoint = self.breakPoint() |
|---|
| 284 | if max is not None: |
|---|
| 285 | breakPoint = min(breakPoint, self.pos()+max) |
|---|
| 286 | while self.pos() < breakPoint: |
|---|
| 287 | self.advance() |
|---|
| 288 | if not self.matchWhiteSpace(WSchars): |
|---|
| 289 | break |
|---|
| 290 | return self.src()[start:self.pos()] |
|---|
| 291 | |
|---|
| 292 | def matchNonWhiteSpace(self, WSchars=' \f\t\n\r'): |
|---|
| 293 | return self.atEnd() or not self.peek() in WSchars |
|---|
| 294 | |
|---|
| 295 | def getNonWhiteSpace(self, WSchars=' \f\t\n\r'): |
|---|
| 296 | if not self.matchNonWhiteSpace(WSchars): |
|---|
| 297 | return '' |
|---|
| 298 | start = self.pos() |
|---|
| 299 | while self.pos() < self.breakPoint(): |
|---|
| 300 | self.advance() |
|---|
| 301 | if not self.matchNonWhiteSpace(WSchars): |
|---|
| 302 | break |
|---|
| 303 | return self.src()[start:self.pos()] |
|---|