Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 1 | # A parser for SGML, using the derived class as static DTD. |
| 2 | |
| 3 | # XXX This only supports those SGML features used by HTML. |
| 4 | |
| 5 | # XXX There should be a way to distinguish between PCDATA (parsed |
| 6 | # character data -- the normal case), RCDATA (replaceable character |
| 7 | # data -- only char and entity references and end tags are special) |
| 8 | # and CDATA (character data -- only end tags are special). |
| 9 | |
| 10 | |
| 11 | import regex |
| 12 | import string |
| 13 | |
| 14 | |
| 15 | # Regular expressions used for parsing |
| 16 | |
Guido van Rossum | 1dba24e | 1995-03-04 22:28:49 +0000 | [diff] [blame] | 17 | incomplete = regex.compile( |
| 18 | '<!-?\|</[a-zA-Z][a-zA-Z0-9]*[ \t\n]*\|</?\|' + |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 19 | '&#[a-zA-Z0-9]*\|&[a-zA-Z][a-zA-Z0-9]*\|&') |
| 20 | entityref = regex.compile('&[a-zA-Z][a-zA-Z0-9]*[;.]') |
| 21 | charref = regex.compile('&#[a-zA-Z0-9]+;') |
| 22 | starttagopen = regex.compile('<[a-zA-Z]') |
| 23 | endtag = regex.compile('</[a-zA-Z][a-zA-Z0-9]*[ \t\n]*>') |
| 24 | commentopen = regex.compile('<!--') |
| 25 | |
| 26 | |
| 27 | # SGML parser base class -- find tags and call handler functions. |
| 28 | # Usage: p = SGMLParser(); p.feed(data); ...; p.close(). |
| 29 | # The dtd is defined by deriving a class which defines methods |
| 30 | # with special names to handle tags: start_foo and end_foo to handle |
| 31 | # <foo> and </foo>, respectively, or do_foo to handle <foo> by itself. |
| 32 | # (Tags are converted to lower case for this purpose.) The data |
| 33 | # between tags is passed to the parser by calling self.handle_data() |
| 34 | # with some data as argument (the data may be split up in arbutrary |
| 35 | # chunks). Entity references are passed by calling |
| 36 | # self.handle_entityref() with the entity reference as argument. |
| 37 | |
| 38 | class SGMLParser: |
| 39 | |
| 40 | # Interface -- initialize and reset this instance |
| 41 | def __init__(self): |
| 42 | self.reset() |
| 43 | |
| 44 | # Interface -- reset this instance. Loses all unprocessed data |
| 45 | def reset(self): |
| 46 | self.rawdata = '' |
| 47 | self.stack = [] |
| 48 | self.nomoretags = 0 |
| 49 | self.literal = 0 |
| 50 | |
| 51 | # For derived classes only -- enter literal mode (CDATA) till EOF |
| 52 | def setnomoretags(self): |
| 53 | self.nomoretags = self.literal = 1 |
| 54 | |
| 55 | # For derived classes only -- enter literal mode (CDATA) |
| 56 | def setliteral(self, *args): |
| 57 | self.literal = 1 |
| 58 | |
| 59 | # Interface -- feed some data to the parser. Call this as |
| 60 | # often as you want, with as little or as much text as you |
| 61 | # want (may include '\n'). (This just saves the text, all the |
Guido van Rossum | 1dba24e | 1995-03-04 22:28:49 +0000 | [diff] [blame] | 62 | # processing is done by goahead().) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 63 | def feed(self, data): |
| 64 | self.rawdata = self.rawdata + data |
| 65 | self.goahead(0) |
| 66 | |
| 67 | # Interface -- handle the remaining data |
| 68 | def close(self): |
| 69 | self.goahead(1) |
| 70 | |
| 71 | # Internal -- handle data as far as reasonable. May leave state |
| 72 | # and data to be processed by a subsequent call. If 'end' is |
| 73 | # true, force handling all data as if followed by EOF marker. |
| 74 | def goahead(self, end): |
| 75 | rawdata = self.rawdata |
| 76 | i = 0 |
| 77 | n = len(rawdata) |
| 78 | while i < n: |
| 79 | if self.nomoretags: |
| 80 | self.handle_data(rawdata[i:n]) |
| 81 | i = n |
| 82 | break |
| 83 | j = incomplete.search(rawdata, i) |
| 84 | if j < 0: j = n |
| 85 | if i < j: self.handle_data(rawdata[i:j]) |
| 86 | i = j |
| 87 | if i == n: break |
| 88 | if rawdata[i] == '<': |
| 89 | if starttagopen.match(rawdata, i) >= 0: |
| 90 | if self.literal: |
| 91 | self.handle_data(rawdata[i]) |
| 92 | i = i+1 |
| 93 | continue |
| 94 | k = self.parse_starttag(i) |
| 95 | if k < 0: break |
| 96 | i = i + k |
| 97 | continue |
| 98 | k = endtag.match(rawdata, i) |
| 99 | if k >= 0: |
| 100 | j = i+k |
| 101 | self.parse_endtag(rawdata[i:j]) |
| 102 | i = j |
| 103 | self.literal = 0 |
| 104 | continue |
| 105 | if commentopen.match(rawdata, i) >= 0: |
| 106 | if self.literal: |
| 107 | self.handle_data(rawdata[i]) |
| 108 | i = i+1 |
| 109 | continue |
| 110 | k = self.parse_comment(i) |
| 111 | if k < 0: break |
| 112 | i = i+k |
| 113 | continue |
| 114 | elif rawdata[i] == '&': |
| 115 | k = charref.match(rawdata, i) |
| 116 | if k >= 0: |
| 117 | j = i+k |
| 118 | self.handle_charref(rawdata[i+2:j-1]) |
| 119 | i = j |
| 120 | continue |
| 121 | k = entityref.match(rawdata, i) |
| 122 | if k >= 0: |
| 123 | j = i+k |
| 124 | self.handle_entityref(rawdata[i+1:j-1]) |
| 125 | i = j |
| 126 | continue |
| 127 | else: |
| 128 | raise RuntimeError, 'neither < nor & ??' |
| 129 | # We get here only if incomplete matches but |
| 130 | # nothing else |
| 131 | k = incomplete.match(rawdata, i) |
| 132 | if k < 0: raise RuntimeError, 'no incomplete match ??' |
| 133 | j = i+k |
| 134 | if j == n: break # Really incomplete |
| 135 | self.handle_data(rawdata[i:j]) |
| 136 | i = j |
| 137 | # end while |
| 138 | if end and i < n: |
| 139 | self.handle_data(rawdata[i:n]) |
| 140 | i = n |
| 141 | self.rawdata = rawdata[i:] |
| 142 | # XXX if end: check for empty stack |
| 143 | |
| 144 | # Internal -- parse comment, return length or -1 if not ternimated |
| 145 | def parse_comment(self, i): |
| 146 | rawdata = self.rawdata |
| 147 | if rawdata[i:i+4] <> '<!--': |
| 148 | raise RuntimeError, 'unexpected call to handle_comment' |
| 149 | try: |
| 150 | j = string.index(rawdata, '--', i+4) |
| 151 | except string.index_error: |
| 152 | return -1 |
| 153 | self.handle_comment(rawdata[i+4: j]) |
| 154 | j = j+2 |
| 155 | n = len(rawdata) |
| 156 | while j < n and rawdata[j] in ' \t\n': j = j+1 |
| 157 | if j == n: return -1 # Wait for final '>' |
| 158 | if rawdata[j] == '>': |
| 159 | j = j+1 |
| 160 | else: |
| 161 | print '*** comment not terminated with >' |
| 162 | print repr(rawdata[j-5:j]), '*!*', repr(rawdata[j:j+5]) |
| 163 | return j-i |
| 164 | |
| 165 | # Internal -- handle starttag, return length or -1 if not terminated |
| 166 | def parse_starttag(self, i): |
| 167 | rawdata = self.rawdata |
| 168 | try: |
| 169 | j = string.index(rawdata, '>', i) |
| 170 | except string.index_error: |
| 171 | return -1 |
| 172 | # Now parse the data between i+1 and j into a tag and attrs |
| 173 | attrs = [] |
| 174 | tagfind = regex.compile('[a-zA-Z][a-zA-Z0-9]*') |
Guido van Rossum | 1dba24e | 1995-03-04 22:28:49 +0000 | [diff] [blame] | 175 | attrfind = regex.compile( |
| 176 | '[ \t\n]+\([a-zA-Z][a-zA-Z0-9]*\)' + |
| 177 | '\([ \t\n]*=[ \t\n]*' + |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 178 | '\(\'[^\']*\';\|"[^"]*"\|[-a-zA-Z0-9./:+*%?!()_#]+\)\)?') |
| 179 | k = tagfind.match(rawdata, i+1) |
| 180 | if k < 0: |
| 181 | raise RuntimeError, 'unexpected call to parse_starttag' |
| 182 | k = i+1+k |
| 183 | tag = string.lower(rawdata[i+1:k]) |
| 184 | while k < j: |
| 185 | l = attrfind.match(rawdata, k) |
| 186 | if l < 0: break |
| 187 | regs = attrfind.regs |
| 188 | a1, b1 = regs[1] |
| 189 | a2, b2 = regs[2] |
| 190 | a3, b3 = regs[3] |
| 191 | attrname = rawdata[a1:b1] |
| 192 | if '=' in rawdata[k:k+l]: |
| 193 | attrvalue = rawdata[a3:b3] |
| 194 | if attrvalue[:1] == '\'' == attrvalue[-1:] or \ |
| 195 | attrvalue[:1] == '"' == attrvalue[-1:]: |
| 196 | attrvalue = attrvalue[1:-1] |
| 197 | else: |
| 198 | attrvalue = '' |
Guido van Rossum | 1dba24e | 1995-03-04 22:28:49 +0000 | [diff] [blame] | 199 | attrs.append((string.lower(attrname), attrvalue)) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 200 | k = k + l |
| 201 | j = j+1 |
| 202 | try: |
| 203 | method = getattr(self, 'start_' + tag) |
| 204 | except AttributeError: |
| 205 | try: |
| 206 | method = getattr(self, 'do_' + tag) |
| 207 | except AttributeError: |
| 208 | self.unknown_starttag(tag, attrs) |
| 209 | return j-i |
| 210 | method(attrs) |
| 211 | return j-i |
| 212 | self.stack.append(tag) |
| 213 | method(attrs) |
| 214 | return j-i |
| 215 | |
| 216 | # Internal -- parse endtag |
| 217 | def parse_endtag(self, data): |
| 218 | if data[:2] <> '</' or data[-1:] <> '>': |
| 219 | raise RuntimeError, 'unexpected call to parse_endtag' |
| 220 | tag = string.lower(string.strip(data[2:-1])) |
| 221 | try: |
| 222 | method = getattr(self, 'end_' + tag) |
| 223 | except AttributeError: |
| 224 | self.unknown_endtag(tag) |
| 225 | return |
| 226 | if self.stack and self.stack[-1] == tag: |
| 227 | del self.stack[-1] |
| 228 | else: |
Guido van Rossum | efe5ac4 | 1995-06-22 18:56:36 +0000 | [diff] [blame^] | 229 | self.report_unbalanced(tag) |
| 230 | # Now repair it |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 231 | found = None |
| 232 | for i in range(len(self.stack)): |
| 233 | if self.stack[i] == tag: found = i |
| 234 | if found <> None: |
| 235 | del self.stack[found:] |
| 236 | method() |
| 237 | |
Guido van Rossum | efe5ac4 | 1995-06-22 18:56:36 +0000 | [diff] [blame^] | 238 | # Example -- report an unbalanced </...> tag. |
| 239 | def report_unbalanced(self, tag): |
| 240 | print '*** Unbalanced </' + tag + '>' |
| 241 | print '*** Stack:', self.stack |
| 242 | |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 243 | # Example -- handle character reference, no need to override |
| 244 | def handle_charref(self, name): |
| 245 | try: |
| 246 | n = string.atoi(name) |
| 247 | except string.atoi_error: |
| 248 | self.unknown_charref(name) |
| 249 | return |
| 250 | if not 0 <= n <= 255: |
| 251 | self.unknown_charref(name) |
| 252 | return |
| 253 | self.handle_data(chr(n)) |
| 254 | |
| 255 | # Definition of entities -- derived classes may override |
| 256 | entitydefs = \ |
| 257 | {'lt': '<', 'gt': '>', 'amp': '&', 'quot': '"', 'apos': '\''} |
| 258 | |
| 259 | # Example -- handle entity reference, no need to override |
| 260 | def handle_entityref(self, name): |
Guido van Rossum | 1dba24e | 1995-03-04 22:28:49 +0000 | [diff] [blame] | 261 | table = self.entitydefs |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 262 | name = string.lower(name) |
| 263 | if table.has_key(name): |
| 264 | self.handle_data(table[name]) |
| 265 | else: |
| 266 | self.unknown_entityref(name) |
| 267 | return |
| 268 | |
| 269 | # Example -- handle data, should be overridden |
| 270 | def handle_data(self, data): |
| 271 | pass |
| 272 | |
| 273 | # Example -- handle comment, could be overridden |
| 274 | def handle_comment(self, data): |
| 275 | pass |
| 276 | |
| 277 | # To be overridden -- handlers for unknown objects |
| 278 | def unknown_starttag(self, tag, attrs): pass |
| 279 | def unknown_endtag(self, tag): pass |
| 280 | def unknown_charref(self, ref): pass |
| 281 | def unknown_entityref(self, ref): pass |
| 282 | |
| 283 | |
| 284 | class TestSGML(SGMLParser): |
| 285 | |
| 286 | def handle_data(self, data): |
| 287 | r = repr(data) |
| 288 | if len(r) > 72: |
| 289 | r = r[:35] + '...' + r[-35:] |
| 290 | print 'data:', r |
| 291 | |
| 292 | def handle_comment(self, data): |
| 293 | r = repr(data) |
| 294 | if len(r) > 68: |
| 295 | r = r[:32] + '...' + r[-32:] |
| 296 | print 'comment:', r |
| 297 | |
| 298 | def unknown_starttag(self, tag, attrs): |
| 299 | print 'start tag: <' + tag, |
| 300 | for name, value in attrs: |
| 301 | print name + '=' + '"' + value + '"', |
| 302 | print '>' |
| 303 | |
| 304 | def unknown_endtag(self, tag): |
| 305 | print 'end tag: </' + tag + '>' |
| 306 | |
| 307 | def unknown_entityref(self, ref): |
| 308 | print '*** unknown entity ref: &' + ref + ';' |
| 309 | |
| 310 | def unknown_charref(self, ref): |
| 311 | print '*** unknown char ref: &#' + ref + ';' |
| 312 | |
| 313 | |
| 314 | def test(): |
| 315 | file = 'test.html' |
| 316 | f = open(file, 'r') |
| 317 | x = TestSGML() |
| 318 | while 1: |
| 319 | line = f.readline() |
| 320 | if not line: |
| 321 | x.close() |
| 322 | break |
| 323 | x.feed(line) |
| 324 | |
| 325 | |
Guido van Rossum | 1dba24e | 1995-03-04 22:28:49 +0000 | [diff] [blame] | 326 | if __name__ == '__main__': |
| 327 | test() |