123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- import re
- import sys
- (C_NONE, C_BACKSLASH, C_STRING_FIRST_LINE,
- C_STRING_NEXT_LINES, C_BRACKET) = range(5)
- if 0:
- def dump(*stuff):
- sys.__stdout__.write(" ".join(map(str, stuff)) + "\n")
- _synchre = re.compile(r"""
- ^
- [ \t]*
- (?: while
- | else
- | def
- | return
- | assert
- | break
- | class
- | continue
- | elif
- | try
- | except
- | raise
- | import
- | yield
- )
- \b
- """, re.VERBOSE | re.MULTILINE).search
- _junkre = re.compile(r"""
- [ \t]*
- (?: \# \S .* )?
- \n
- """, re.VERBOSE).match
- _match_stringre = re.compile(r"""
- \""" [^"\\]* (?:
- (?: \\. | "(?!"") )
- [^"\\]*
- )*
- (?: \""" )?
- | " [^"\\\n]* (?: \\. [^"\\\n]* )* "?
- | ''' [^'\\]* (?:
- (?: \\. | '(?!'') )
- [^'\\]*
- )*
- (?: ''' )?
- | ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?
- """, re.VERBOSE | re.DOTALL).match
- _itemre = re.compile(r"""
- [ \t]*
- [^\s#\\] # if we match, m.end()-1 is the interesting char
- """, re.VERBOSE).match
- _closere = re.compile(r"""
- \s*
- (?: return
- | break
- | continue
- | raise
- | pass
- )
- \b
- """, re.VERBOSE).match
- _chew_ordinaryre = re.compile(r"""
- [^[\](){}#'"\\]+
- """, re.VERBOSE).match
- _tran = ['x'] * 256
- for ch in "({[":
- _tran[ord(ch)] = '('
- for ch in ")}]":
- _tran[ord(ch)] = ')'
- for ch in "\"'\\\n#":
- _tran[ord(ch)] = ch
- _tran = ''.join(_tran)
- del ch
- try:
- UnicodeType = type(unicode(""))
- except NameError:
- UnicodeType = None
- class Parser:
- def __init__(self, indentwidth, tabwidth):
- self.indentwidth = indentwidth
- self.tabwidth = tabwidth
- def set_str(self, str):
- assert len(str) == 0 or str[-1] == '\n'
- if type(str) is UnicodeType:
-
-
-
-
-
-
- uniphooey = str
- str = []
- push = str.append
- for raw in map(ord, uniphooey):
- push(raw < 127 and chr(raw) or "x")
- str = "".join(str)
- self.str = str
- self.study_level = 0
-
-
-
-
-
-
-
-
-
-
- def find_good_parse_start(self, is_char_in_string=None,
- _synchre=_synchre):
- str, pos = self.str, None
- if not is_char_in_string:
-
- return None
-
-
-
- limit = len(str)
- for tries in range(5):
- i = str.rfind(":\n", 0, limit)
- if i < 0:
- break
- i = str.rfind('\n', 0, i) + 1
- m = _synchre(str, i, limit)
- if m and not is_char_in_string(m.start()):
- pos = m.start()
- break
- limit = i
- if pos is None:
-
-
-
-
-
-
-
-
- m = _synchre(str)
- if m and not is_char_in_string(m.start()):
- pos = m.start()
- return pos
-
-
- i = pos + 1
- while 1:
- m = _synchre(str, i)
- if m:
- s, i = m.span()
- if not is_char_in_string(s):
- pos = s
- else:
- break
- return pos
-
-
- def set_lo(self, lo):
- assert lo == 0 or self.str[lo-1] == '\n'
- if lo > 0:
- self.str = self.str[lo:]
-
-
-
- def _study1(self):
- if self.study_level >= 1:
- return
- self.study_level = 1
-
-
-
-
- str = self.str
- str = str.translate(_tran)
- str = str.replace('xxxxxxxx', 'x')
- str = str.replace('xxxx', 'x')
- str = str.replace('xx', 'x')
- str = str.replace('xx', 'x')
- str = str.replace('\nx', '\n')
-
-
-
-
-
- continuation = C_NONE
- level = lno = 0
- self.goodlines = goodlines = [0]
- push_good = goodlines.append
- i, n = 0, len(str)
- while i < n:
- ch = str[i]
- i = i+1
-
- if ch == 'x':
- continue
- if ch == '\n':
- lno = lno + 1
- if level == 0:
- push_good(lno)
-
- continue
- if ch == '(':
- level = level + 1
- continue
- if ch == ')':
- if level:
- level = level - 1
-
- continue
- if ch == '"' or ch == "'":
-
- quote = ch
- if str[i-1:i+2] == quote * 3:
- quote = quote * 3
- firstlno = lno
- w = len(quote) - 1
- i = i+w
- while i < n:
- ch = str[i]
- i = i+1
- if ch == 'x':
- continue
- if str[i-1:i+w] == quote:
- i = i+w
- break
- if ch == '\n':
- lno = lno + 1
- if w == 0:
-
- if level == 0:
- push_good(lno)
- break
- continue
- if ch == '\\':
- assert i < n
- if str[i] == '\n':
- lno = lno + 1
- i = i+1
- continue
-
- else:
-
-
- if (lno - 1) == firstlno:
-
-
- continuation = C_STRING_FIRST_LINE
- else:
- continuation = C_STRING_NEXT_LINES
- continue
- if ch == '#':
-
- i = str.find('\n', i)
- assert i >= 0
- continue
- assert ch == '\\'
- assert i < n
- if str[i] == '\n':
- lno = lno + 1
- if i+1 == n:
- continuation = C_BACKSLASH
- i = i+1
-
-
-
- if (continuation != C_STRING_FIRST_LINE
- and continuation != C_STRING_NEXT_LINES and level > 0):
- continuation = C_BRACKET
- self.continuation = continuation
-
-
- assert (continuation == C_NONE) == (goodlines[-1] == lno)
- if goodlines[-1] != lno:
- push_good(lno)
- def get_continuation_type(self):
- self._study1()
- return self.continuation
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def _study2(self):
- if self.study_level >= 2:
- return
- self._study1()
- self.study_level = 2
-
- str, goodlines = self.str, self.goodlines
- i = len(goodlines) - 1
- p = len(str)
- while i:
- assert p
-
-
- q = p
- for nothing in range(goodlines[i-1], goodlines[i]):
-
- p = str.rfind('\n', 0, p-1) + 1
-
-
- if _junkre(str, p):
- i = i-1
- else:
- break
- if i == 0:
-
- assert p == 0
- q = p
- self.stmt_start, self.stmt_end = p, q
-
-
- lastch = ""
- stack = []
- push_stack = stack.append
- bracketing = [(p, 0)]
- while p < q:
-
- m = _chew_ordinaryre(str, p, q)
- if m:
-
- newp = m.end()
-
- i = newp - 1
- while i >= p and str[i] in " \t\n":
- i = i-1
- if i >= p:
- lastch = str[i]
- p = newp
- if p >= q:
- break
- ch = str[p]
- if ch in "([{":
- push_stack(p)
- bracketing.append((p, len(stack)))
- lastch = ch
- p = p+1
- continue
- if ch in ")]}":
- if stack:
- del stack[-1]
- lastch = ch
- p = p+1
- bracketing.append((p, len(stack)))
- continue
- if ch == '"' or ch == "'":
-
-
-
-
-
-
-
- bracketing.append((p, len(stack)+1))
- lastch = ch
- p = _match_stringre(str, p, q).end()
- bracketing.append((p, len(stack)))
- continue
- if ch == '#':
-
- bracketing.append((p, len(stack)+1))
- p = str.find('\n', p, q) + 1
- assert p > 0
- bracketing.append((p, len(stack)))
- continue
- assert ch == '\\'
- p = p+1
- assert p < q
- if str[p] != '\n':
-
- lastch = ch + str[p]
- p = p+1
-
- self.lastch = lastch
- if stack:
- self.lastopenbracketpos = stack[-1]
- self.stmt_bracketing = tuple(bracketing)
-
-
- def compute_bracket_indent(self):
- self._study2()
- assert self.continuation == C_BRACKET
- j = self.lastopenbracketpos
- str = self.str
- n = len(str)
- origi = i = str.rfind('\n', 0, j) + 1
- j = j+1
-
- while j < n:
- m = _itemre(str, j)
- if m:
- j = m.end() - 1
- extra = 0
- break
- else:
-
- i = j = str.find('\n', j) + 1
- else:
-
-
- j = i = origi
- while str[j] in " \t":
- j = j+1
- extra = self.indentwidth
- return len(str[i:j].expandtabs(self.tabwidth)) + extra
-
-
-
- def get_num_lines_in_stmt(self):
- self._study1()
- goodlines = self.goodlines
- return goodlines[-1] - goodlines[-2]
-
-
-
- def compute_backslash_indent(self):
- self._study2()
- assert self.continuation == C_BACKSLASH
- str = self.str
- i = self.stmt_start
- while str[i] in " \t":
- i = i+1
- startpos = i
-
-
- endpos = str.find('\n', startpos) + 1
- found = level = 0
- while i < endpos:
- ch = str[i]
- if ch in "([{":
- level = level + 1
- i = i+1
- elif ch in ")]}":
- if level:
- level = level - 1
- i = i+1
- elif ch == '"' or ch == "'":
- i = _match_stringre(str, i, endpos).end()
- elif ch == '#':
- break
- elif level == 0 and ch == '=' and \
- (i == 0 or str[i-1] not in "=<>!") and \
- str[i+1] != '=':
- found = 1
- break
- else:
- i = i+1
- if found:
-
-
- i = i+1
- found = re.match(r"\s*\\", str[i:endpos]) is None
- if not found:
-
-
- i = startpos
- while str[i] not in " \t\n":
- i = i+1
- return len(str[self.stmt_start:i].expandtabs(\
- self.tabwidth)) + 1
-
-
- def get_base_indent_string(self):
- self._study2()
- i, n = self.stmt_start, self.stmt_end
- j = i
- str = self.str
- while j < n and str[j] in " \t":
- j = j + 1
- return str[i:j]
-
- def is_block_opener(self):
- self._study2()
- return self.lastch == ':'
-
- def is_block_closer(self):
- self._study2()
- return _closere(self.str, self.stmt_start) is not None
-
- lastopenbracketpos = None
- def get_last_open_bracket_pos(self):
- self._study2()
- return self.lastopenbracketpos
-
-
-
- stmt_bracketing = None
- def get_last_stmt_bracketing(self):
- self._study2()
- return self.stmt_bracketing
|