jpayne@69: """Define partial Python code Parser used by editor and hyperparser. jpayne@69: jpayne@69: Instances of ParseMap are used with str.translate. jpayne@69: jpayne@69: The following bound search and match functions are defined: jpayne@69: _synchre - start of popular statement; jpayne@69: _junkre - whitespace or comment line; jpayne@69: _match_stringre: string, possibly without closer; jpayne@69: _itemre - line that may have bracket structure start; jpayne@69: _closere - line that must be followed by dedent. jpayne@69: _chew_ordinaryre - non-special characters. jpayne@69: """ jpayne@69: import re jpayne@69: jpayne@69: # Reason last statement is continued (or C_NONE if it's not). jpayne@69: (C_NONE, C_BACKSLASH, C_STRING_FIRST_LINE, jpayne@69: C_STRING_NEXT_LINES, C_BRACKET) = range(5) jpayne@69: jpayne@69: # Find what looks like the start of a popular statement. jpayne@69: jpayne@69: _synchre = re.compile(r""" jpayne@69: ^ jpayne@69: [ \t]* jpayne@69: (?: while jpayne@69: | else jpayne@69: | def jpayne@69: | return jpayne@69: | assert jpayne@69: | break jpayne@69: | class jpayne@69: | continue jpayne@69: | elif jpayne@69: | try jpayne@69: | except jpayne@69: | raise jpayne@69: | import jpayne@69: | yield jpayne@69: ) jpayne@69: \b jpayne@69: """, re.VERBOSE | re.MULTILINE).search jpayne@69: jpayne@69: # Match blank line or non-indenting comment line. jpayne@69: jpayne@69: _junkre = re.compile(r""" jpayne@69: [ \t]* jpayne@69: (?: \# \S .* )? jpayne@69: \n jpayne@69: """, re.VERBOSE).match jpayne@69: jpayne@69: # Match any flavor of string; the terminating quote is optional jpayne@69: # so that we're robust in the face of incomplete program text. jpayne@69: jpayne@69: _match_stringre = re.compile(r""" jpayne@69: \""" [^"\\]* (?: jpayne@69: (?: \\. | "(?!"") ) jpayne@69: [^"\\]* jpayne@69: )* jpayne@69: (?: \""" )? jpayne@69: jpayne@69: | " [^"\\\n]* (?: \\. [^"\\\n]* )* "? jpayne@69: jpayne@69: | ''' [^'\\]* (?: jpayne@69: (?: \\. | '(?!'') ) jpayne@69: [^'\\]* jpayne@69: )* jpayne@69: (?: ''' )? jpayne@69: jpayne@69: | ' [^'\\\n]* (?: \\. [^'\\\n]* )* '? jpayne@69: """, re.VERBOSE | re.DOTALL).match jpayne@69: jpayne@69: # Match a line that starts with something interesting; jpayne@69: # used to find the first item of a bracket structure. jpayne@69: jpayne@69: _itemre = re.compile(r""" jpayne@69: [ \t]* jpayne@69: [^\s#\\] # if we match, m.end()-1 is the interesting char jpayne@69: """, re.VERBOSE).match jpayne@69: jpayne@69: # Match start of statements that should be followed by a dedent. jpayne@69: jpayne@69: _closere = re.compile(r""" jpayne@69: \s* jpayne@69: (?: return jpayne@69: | break jpayne@69: | continue jpayne@69: | raise jpayne@69: | pass jpayne@69: ) jpayne@69: \b jpayne@69: """, re.VERBOSE).match jpayne@69: jpayne@69: # Chew up non-special chars as quickly as possible. If match is jpayne@69: # successful, m.end() less 1 is the index of the last boring char jpayne@69: # matched. If match is unsuccessful, the string starts with an jpayne@69: # interesting char. jpayne@69: jpayne@69: _chew_ordinaryre = re.compile(r""" jpayne@69: [^[\](){}#'"\\]+ jpayne@69: """, re.VERBOSE).match jpayne@69: jpayne@69: jpayne@69: class ParseMap(dict): jpayne@69: r"""Dict subclass that maps anything not in dict to 'x'. jpayne@69: jpayne@69: This is designed to be used with str.translate in study1. jpayne@69: Anything not specifically mapped otherwise becomes 'x'. jpayne@69: Example: replace everything except whitespace with 'x'. jpayne@69: jpayne@69: >>> keepwhite = ParseMap((ord(c), ord(c)) for c in ' \t\n\r') jpayne@69: >>> "a + b\tc\nd".translate(keepwhite) jpayne@69: 'x x x\tx\nx' jpayne@69: """ jpayne@69: # Calling this triples access time; see bpo-32940 jpayne@69: def __missing__(self, key): jpayne@69: return 120 # ord('x') jpayne@69: jpayne@69: jpayne@69: # Map all ascii to 120 to avoid __missing__ call, then replace some. jpayne@69: trans = ParseMap.fromkeys(range(128), 120) jpayne@69: trans.update((ord(c), ord('(')) for c in "({[") # open brackets => '('; jpayne@69: trans.update((ord(c), ord(')')) for c in ")}]") # close brackets => ')'. jpayne@69: trans.update((ord(c), ord(c)) for c in "\"'\\\n#") # Keep these. jpayne@69: jpayne@69: jpayne@69: class Parser: jpayne@69: jpayne@69: def __init__(self, indentwidth, tabwidth): jpayne@69: self.indentwidth = indentwidth jpayne@69: self.tabwidth = tabwidth jpayne@69: jpayne@69: def set_code(self, s): jpayne@69: assert len(s) == 0 or s[-1] == '\n' jpayne@69: self.code = s jpayne@69: self.study_level = 0 jpayne@69: jpayne@69: def find_good_parse_start(self, is_char_in_string=None, jpayne@69: _synchre=_synchre): jpayne@69: """ jpayne@69: Return index of a good place to begin parsing, as close to the jpayne@69: end of the string as possible. This will be the start of some jpayne@69: popular stmt like "if" or "def". Return None if none found: jpayne@69: the caller should pass more prior context then, if possible, or jpayne@69: if not (the entire program text up until the point of interest jpayne@69: has already been tried) pass 0 to set_lo(). jpayne@69: jpayne@69: This will be reliable iff given a reliable is_char_in_string() jpayne@69: function, meaning that when it says "no", it's absolutely jpayne@69: guaranteed that the char is not in a string. jpayne@69: """ jpayne@69: code, pos = self.code, None jpayne@69: jpayne@69: if not is_char_in_string: jpayne@69: # no clue -- make the caller pass everything jpayne@69: return None jpayne@69: jpayne@69: # Peek back from the end for a good place to start, jpayne@69: # but don't try too often; pos will be left None, or jpayne@69: # bumped to a legitimate synch point. jpayne@69: limit = len(code) jpayne@69: for tries in range(5): jpayne@69: i = code.rfind(":\n", 0, limit) jpayne@69: if i < 0: jpayne@69: break jpayne@69: i = code.rfind('\n', 0, i) + 1 # start of colon line (-1+1=0) jpayne@69: m = _synchre(code, i, limit) jpayne@69: if m and not is_char_in_string(m.start()): jpayne@69: pos = m.start() jpayne@69: break jpayne@69: limit = i jpayne@69: if pos is None: jpayne@69: # Nothing looks like a block-opener, or stuff does jpayne@69: # but is_char_in_string keeps returning true; most likely jpayne@69: # we're in or near a giant string, the colorizer hasn't jpayne@69: # caught up enough to be helpful, or there simply *aren't* jpayne@69: # any interesting stmts. In any of these cases we're jpayne@69: # going to have to parse the whole thing to be sure, so jpayne@69: # give it one last try from the start, but stop wasting jpayne@69: # time here regardless of the outcome. jpayne@69: m = _synchre(code) jpayne@69: if m and not is_char_in_string(m.start()): jpayne@69: pos = m.start() jpayne@69: return pos jpayne@69: jpayne@69: # Peeking back worked; look forward until _synchre no longer jpayne@69: # matches. jpayne@69: i = pos + 1 jpayne@69: while 1: jpayne@69: m = _synchre(code, i) jpayne@69: if m: jpayne@69: s, i = m.span() jpayne@69: if not is_char_in_string(s): jpayne@69: pos = s jpayne@69: else: jpayne@69: break jpayne@69: return pos jpayne@69: jpayne@69: def set_lo(self, lo): jpayne@69: """ Throw away the start of the string. jpayne@69: jpayne@69: Intended to be called with the result of find_good_parse_start(). jpayne@69: """ jpayne@69: assert lo == 0 or self.code[lo-1] == '\n' jpayne@69: if lo > 0: jpayne@69: self.code = self.code[lo:] jpayne@69: jpayne@69: def _study1(self): jpayne@69: """Find the line numbers of non-continuation lines. jpayne@69: jpayne@69: As quickly as humanly possible , find the line numbers (0- jpayne@69: based) of the non-continuation lines. jpayne@69: Creates self.{goodlines, continuation}. jpayne@69: """ jpayne@69: if self.study_level >= 1: jpayne@69: return jpayne@69: self.study_level = 1 jpayne@69: jpayne@69: # Map all uninteresting characters to "x", all open brackets jpayne@69: # to "(", all close brackets to ")", then collapse runs of jpayne@69: # uninteresting characters. This can cut the number of chars jpayne@69: # by a factor of 10-40, and so greatly speed the following loop. jpayne@69: code = self.code jpayne@69: code = code.translate(trans) jpayne@69: code = code.replace('xxxxxxxx', 'x') jpayne@69: code = code.replace('xxxx', 'x') jpayne@69: code = code.replace('xx', 'x') jpayne@69: code = code.replace('xx', 'x') jpayne@69: code = code.replace('\nx', '\n') jpayne@69: # Replacing x\n with \n would be incorrect because jpayne@69: # x may be preceded by a backslash. jpayne@69: jpayne@69: # March over the squashed version of the program, accumulating jpayne@69: # the line numbers of non-continued stmts, and determining jpayne@69: # whether & why the last stmt is a continuation. jpayne@69: continuation = C_NONE jpayne@69: level = lno = 0 # level is nesting level; lno is line number jpayne@69: self.goodlines = goodlines = [0] jpayne@69: push_good = goodlines.append jpayne@69: i, n = 0, len(code) jpayne@69: while i < n: jpayne@69: ch = code[i] jpayne@69: i = i+1 jpayne@69: jpayne@69: # cases are checked in decreasing order of frequency jpayne@69: if ch == 'x': jpayne@69: continue jpayne@69: jpayne@69: if ch == '\n': jpayne@69: lno = lno + 1 jpayne@69: if level == 0: jpayne@69: push_good(lno) jpayne@69: # else we're in an unclosed bracket structure jpayne@69: continue jpayne@69: jpayne@69: if ch == '(': jpayne@69: level = level + 1 jpayne@69: continue jpayne@69: jpayne@69: if ch == ')': jpayne@69: if level: jpayne@69: level = level - 1 jpayne@69: # else the program is invalid, but we can't complain jpayne@69: continue jpayne@69: jpayne@69: if ch == '"' or ch == "'": jpayne@69: # consume the string jpayne@69: quote = ch jpayne@69: if code[i-1:i+2] == quote * 3: jpayne@69: quote = quote * 3 jpayne@69: firstlno = lno jpayne@69: w = len(quote) - 1 jpayne@69: i = i+w jpayne@69: while i < n: jpayne@69: ch = code[i] jpayne@69: i = i+1 jpayne@69: jpayne@69: if ch == 'x': jpayne@69: continue jpayne@69: jpayne@69: if code[i-1:i+w] == quote: jpayne@69: i = i+w jpayne@69: break jpayne@69: jpayne@69: if ch == '\n': jpayne@69: lno = lno + 1 jpayne@69: if w == 0: jpayne@69: # unterminated single-quoted string jpayne@69: if level == 0: jpayne@69: push_good(lno) jpayne@69: break jpayne@69: continue jpayne@69: jpayne@69: if ch == '\\': jpayne@69: assert i < n jpayne@69: if code[i] == '\n': jpayne@69: lno = lno + 1 jpayne@69: i = i+1 jpayne@69: continue jpayne@69: jpayne@69: # else comment char or paren inside string jpayne@69: jpayne@69: else: jpayne@69: # didn't break out of the loop, so we're still jpayne@69: # inside a string jpayne@69: if (lno - 1) == firstlno: jpayne@69: # before the previous \n in code, we were in the first jpayne@69: # line of the string jpayne@69: continuation = C_STRING_FIRST_LINE jpayne@69: else: jpayne@69: continuation = C_STRING_NEXT_LINES jpayne@69: continue # with outer loop jpayne@69: jpayne@69: if ch == '#': jpayne@69: # consume the comment jpayne@69: i = code.find('\n', i) jpayne@69: assert i >= 0 jpayne@69: continue jpayne@69: jpayne@69: assert ch == '\\' jpayne@69: assert i < n jpayne@69: if code[i] == '\n': jpayne@69: lno = lno + 1 jpayne@69: if i+1 == n: jpayne@69: continuation = C_BACKSLASH jpayne@69: i = i+1 jpayne@69: jpayne@69: # The last stmt may be continued for all 3 reasons. jpayne@69: # String continuation takes precedence over bracket jpayne@69: # continuation, which beats backslash continuation. jpayne@69: if (continuation != C_STRING_FIRST_LINE jpayne@69: and continuation != C_STRING_NEXT_LINES and level > 0): jpayne@69: continuation = C_BRACKET jpayne@69: self.continuation = continuation jpayne@69: jpayne@69: # Push the final line number as a sentinel value, regardless of jpayne@69: # whether it's continued. jpayne@69: assert (continuation == C_NONE) == (goodlines[-1] == lno) jpayne@69: if goodlines[-1] != lno: jpayne@69: push_good(lno) jpayne@69: jpayne@69: def get_continuation_type(self): jpayne@69: self._study1() jpayne@69: return self.continuation jpayne@69: jpayne@69: def _study2(self): jpayne@69: """ jpayne@69: study1 was sufficient to determine the continuation status, jpayne@69: but doing more requires looking at every character. study2 jpayne@69: does this for the last interesting statement in the block. jpayne@69: Creates: jpayne@69: self.stmt_start, stmt_end jpayne@69: slice indices of last interesting stmt jpayne@69: self.stmt_bracketing jpayne@69: the bracketing structure of the last interesting stmt; for jpayne@69: example, for the statement "say(boo) or die", jpayne@69: stmt_bracketing will be ((0, 0), (0, 1), (2, 0), (2, 1), jpayne@69: (4, 0)). Strings and comments are treated as brackets, for jpayne@69: the matter. jpayne@69: self.lastch jpayne@69: last interesting character before optional trailing comment jpayne@69: self.lastopenbracketpos jpayne@69: if continuation is C_BRACKET, index of last open bracket jpayne@69: """ jpayne@69: if self.study_level >= 2: jpayne@69: return jpayne@69: self._study1() jpayne@69: self.study_level = 2 jpayne@69: jpayne@69: # Set p and q to slice indices of last interesting stmt. jpayne@69: code, goodlines = self.code, self.goodlines jpayne@69: i = len(goodlines) - 1 # Index of newest line. jpayne@69: p = len(code) # End of goodlines[i] jpayne@69: while i: jpayne@69: assert p jpayne@69: # Make p be the index of the stmt at line number goodlines[i]. jpayne@69: # Move p back to the stmt at line number goodlines[i-1]. jpayne@69: q = p jpayne@69: for nothing in range(goodlines[i-1], goodlines[i]): jpayne@69: # tricky: sets p to 0 if no preceding newline jpayne@69: p = code.rfind('\n', 0, p-1) + 1 jpayne@69: # The stmt code[p:q] isn't a continuation, but may be blank jpayne@69: # or a non-indenting comment line. jpayne@69: if _junkre(code, p): jpayne@69: i = i-1 jpayne@69: else: jpayne@69: break jpayne@69: if i == 0: jpayne@69: # nothing but junk! jpayne@69: assert p == 0 jpayne@69: q = p jpayne@69: self.stmt_start, self.stmt_end = p, q jpayne@69: jpayne@69: # Analyze this stmt, to find the last open bracket (if any) jpayne@69: # and last interesting character (if any). jpayne@69: lastch = "" jpayne@69: stack = [] # stack of open bracket indices jpayne@69: push_stack = stack.append jpayne@69: bracketing = [(p, 0)] jpayne@69: while p < q: jpayne@69: # suck up all except ()[]{}'"#\\ jpayne@69: m = _chew_ordinaryre(code, p, q) jpayne@69: if m: jpayne@69: # we skipped at least one boring char jpayne@69: newp = m.end() jpayne@69: # back up over totally boring whitespace jpayne@69: i = newp - 1 # index of last boring char jpayne@69: while i >= p and code[i] in " \t\n": jpayne@69: i = i-1 jpayne@69: if i >= p: jpayne@69: lastch = code[i] jpayne@69: p = newp jpayne@69: if p >= q: jpayne@69: break jpayne@69: jpayne@69: ch = code[p] jpayne@69: jpayne@69: if ch in "([{": jpayne@69: push_stack(p) jpayne@69: bracketing.append((p, len(stack))) jpayne@69: lastch = ch jpayne@69: p = p+1 jpayne@69: continue jpayne@69: jpayne@69: if ch in ")]}": jpayne@69: if stack: jpayne@69: del stack[-1] jpayne@69: lastch = ch jpayne@69: p = p+1 jpayne@69: bracketing.append((p, len(stack))) jpayne@69: continue jpayne@69: jpayne@69: if ch == '"' or ch == "'": jpayne@69: # consume string jpayne@69: # Note that study1 did this with a Python loop, but jpayne@69: # we use a regexp here; the reason is speed in both jpayne@69: # cases; the string may be huge, but study1 pre-squashed jpayne@69: # strings to a couple of characters per line. study1 jpayne@69: # also needed to keep track of newlines, and we don't jpayne@69: # have to. jpayne@69: bracketing.append((p, len(stack)+1)) jpayne@69: lastch = ch jpayne@69: p = _match_stringre(code, p, q).end() jpayne@69: bracketing.append((p, len(stack))) jpayne@69: continue jpayne@69: jpayne@69: if ch == '#': jpayne@69: # consume comment and trailing newline jpayne@69: bracketing.append((p, len(stack)+1)) jpayne@69: p = code.find('\n', p, q) + 1 jpayne@69: assert p > 0 jpayne@69: bracketing.append((p, len(stack))) jpayne@69: continue jpayne@69: jpayne@69: assert ch == '\\' jpayne@69: p = p+1 # beyond backslash jpayne@69: assert p < q jpayne@69: if code[p] != '\n': jpayne@69: # the program is invalid, but can't complain jpayne@69: lastch = ch + code[p] jpayne@69: p = p+1 # beyond escaped char jpayne@69: jpayne@69: # end while p < q: jpayne@69: jpayne@69: self.lastch = lastch jpayne@69: self.lastopenbracketpos = stack[-1] if stack else None jpayne@69: self.stmt_bracketing = tuple(bracketing) jpayne@69: jpayne@69: def compute_bracket_indent(self): jpayne@69: """Return number of spaces the next line should be indented. jpayne@69: jpayne@69: Line continuation must be C_BRACKET. jpayne@69: """ jpayne@69: self._study2() jpayne@69: assert self.continuation == C_BRACKET jpayne@69: j = self.lastopenbracketpos jpayne@69: code = self.code jpayne@69: n = len(code) jpayne@69: origi = i = code.rfind('\n', 0, j) + 1 jpayne@69: j = j+1 # one beyond open bracket jpayne@69: # find first list item; set i to start of its line jpayne@69: while j < n: jpayne@69: m = _itemre(code, j) jpayne@69: if m: jpayne@69: j = m.end() - 1 # index of first interesting char jpayne@69: extra = 0 jpayne@69: break jpayne@69: else: jpayne@69: # this line is junk; advance to next line jpayne@69: i = j = code.find('\n', j) + 1 jpayne@69: else: jpayne@69: # nothing interesting follows the bracket; jpayne@69: # reproduce the bracket line's indentation + a level jpayne@69: j = i = origi jpayne@69: while code[j] in " \t": jpayne@69: j = j+1 jpayne@69: extra = self.indentwidth jpayne@69: return len(code[i:j].expandtabs(self.tabwidth)) + extra jpayne@69: jpayne@69: def get_num_lines_in_stmt(self): jpayne@69: """Return number of physical lines in last stmt. jpayne@69: jpayne@69: The statement doesn't have to be an interesting statement. This is jpayne@69: intended to be called when continuation is C_BACKSLASH. jpayne@69: """ jpayne@69: self._study1() jpayne@69: goodlines = self.goodlines jpayne@69: return goodlines[-1] - goodlines[-2] jpayne@69: jpayne@69: def compute_backslash_indent(self): jpayne@69: """Return number of spaces the next line should be indented. jpayne@69: jpayne@69: Line continuation must be C_BACKSLASH. Also assume that the new jpayne@69: line is the first one following the initial line of the stmt. jpayne@69: """ jpayne@69: self._study2() jpayne@69: assert self.continuation == C_BACKSLASH jpayne@69: code = self.code jpayne@69: i = self.stmt_start jpayne@69: while code[i] in " \t": jpayne@69: i = i+1 jpayne@69: startpos = i jpayne@69: jpayne@69: # See whether the initial line starts an assignment stmt; i.e., jpayne@69: # look for an = operator jpayne@69: endpos = code.find('\n', startpos) + 1 jpayne@69: found = level = 0 jpayne@69: while i < endpos: jpayne@69: ch = code[i] jpayne@69: if ch in "([{": jpayne@69: level = level + 1 jpayne@69: i = i+1 jpayne@69: elif ch in ")]}": jpayne@69: if level: jpayne@69: level = level - 1 jpayne@69: i = i+1 jpayne@69: elif ch == '"' or ch == "'": jpayne@69: i = _match_stringre(code, i, endpos).end() jpayne@69: elif ch == '#': jpayne@69: # This line is unreachable because the # makes a comment of jpayne@69: # everything after it. jpayne@69: break jpayne@69: elif level == 0 and ch == '=' and \ jpayne@69: (i == 0 or code[i-1] not in "=<>!") and \ jpayne@69: code[i+1] != '=': jpayne@69: found = 1 jpayne@69: break jpayne@69: else: jpayne@69: i = i+1 jpayne@69: jpayne@69: if found: jpayne@69: # found a legit =, but it may be the last interesting jpayne@69: # thing on the line jpayne@69: i = i+1 # move beyond the = jpayne@69: found = re.match(r"\s*\\", code[i:endpos]) is None jpayne@69: jpayne@69: if not found: jpayne@69: # oh well ... settle for moving beyond the first chunk jpayne@69: # of non-whitespace chars jpayne@69: i = startpos jpayne@69: while code[i] not in " \t\n": jpayne@69: i = i+1 jpayne@69: jpayne@69: return len(code[self.stmt_start:i].expandtabs(\ jpayne@69: self.tabwidth)) + 1 jpayne@69: jpayne@69: def get_base_indent_string(self): jpayne@69: """Return the leading whitespace on the initial line of the last jpayne@69: interesting stmt. jpayne@69: """ jpayne@69: self._study2() jpayne@69: i, n = self.stmt_start, self.stmt_end jpayne@69: j = i jpayne@69: code = self.code jpayne@69: while j < n and code[j] in " \t": jpayne@69: j = j + 1 jpayne@69: return code[i:j] jpayne@69: jpayne@69: def is_block_opener(self): jpayne@69: "Return True if the last interesting statement opens a block." jpayne@69: self._study2() jpayne@69: return self.lastch == ':' jpayne@69: jpayne@69: def is_block_closer(self): jpayne@69: "Return True if the last interesting statement closes a block." jpayne@69: self._study2() jpayne@69: return _closere(self.code, self.stmt_start) is not None jpayne@69: jpayne@69: def get_last_stmt_bracketing(self): jpayne@69: """Return bracketing structure of the last interesting statement. jpayne@69: jpayne@69: The returned tuple is in the format defined in _study2(). jpayne@69: """ jpayne@69: self._study2() jpayne@69: return self.stmt_bracketing jpayne@69: jpayne@69: jpayne@69: if __name__ == '__main__': jpayne@69: from unittest import main jpayne@69: main('idlelib.idle_test.test_pyparse', verbosity=2)