PyParse.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. import string
  2. import re
  3. import sys
  4. # Reason last stmt is continued (or C_NONE if it's not).
  5. C_NONE, C_BACKSLASH, C_STRING, C_BRACKET = list(range(4))
  6. if 0: # for throwaway debugging output
  7. def dump(*stuff):
  8. sys.__stdout__.write(" ".join(map(str, stuff)) + "\n")
  9. # Find what looks like the start of a popular stmt.
  10. _synchre = re.compile(r"""
  11. ^
  12. [ \t]*
  13. (?: if
  14. | for
  15. | while
  16. | else
  17. | def
  18. | return
  19. | assert
  20. | break
  21. | class
  22. | continue
  23. | elif
  24. | try
  25. | except
  26. | raise
  27. | import
  28. )
  29. \b
  30. """, re.VERBOSE | re.MULTILINE).search
  31. # Match blank line or non-indenting comment line.
  32. _junkre = re.compile(r"""
  33. [ \t]*
  34. (?: \# \S .* )?
  35. \n
  36. """, re.VERBOSE).match
  37. # Match any flavor of string; the terminating quote is optional
  38. # so that we're robust in the face of incomplete program text.
  39. _match_stringre = re.compile(r"""
  40. \""" [^"\\]* (?:
  41. (?: \\. | "(?!"") )
  42. [^"\\]*
  43. )*
  44. (?: \""" )?
  45. | " [^"\\\n]* (?: \\. [^"\\\n]* )* "?
  46. | ''' [^'\\]* (?:
  47. (?: \\. | '(?!'') )
  48. [^'\\]*
  49. )*
  50. (?: ''' )?
  51. | ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?
  52. """, re.VERBOSE | re.DOTALL).match
  53. # Match a line that starts with something interesting;
  54. # used to find the first item of a bracket structure.
  55. _itemre = re.compile(r"""
  56. [ \t]*
  57. [^\s#\\] # if we match, m.end()-1 is the interesting char
  58. """, re.VERBOSE).match
  59. # Match start of stmts that should be followed by a dedent.
  60. _closere = re.compile(r"""
  61. \s*
  62. (?: return
  63. | break
  64. | continue
  65. | raise
  66. | pass
  67. )
  68. \b
  69. """, re.VERBOSE).match
  70. # Chew up non-special chars as quickly as possible. If match is
  71. # successful, m.end() less 1 is the index of the last boring char
  72. # matched. If match is unsuccessful, the string starts with an
  73. # interesting char.
  74. _chew_ordinaryre = re.compile(r"""
  75. [^[\](){}#'"\\]+
  76. """, re.VERBOSE).match
  77. # Build translation table to map uninteresting chars to "x", open
  78. # brackets to "(", and close brackets to ")".
  79. _tran = ['x'] * 256
  80. for ch in "({[":
  81. _tran[ord(ch)] = '('
  82. for ch in ")}]":
  83. _tran[ord(ch)] = ')'
  84. for ch in "\"'\\\n#":
  85. _tran[ord(ch)] = ch
  86. # We are called with unicode strings, and str.translate is one of the few
  87. # py2k functions which can't 'do the right thing' - so take care to ensure
  88. # _tran is full of unicode...
  89. _tran = ''.join(_tran)
  90. del ch
  91. class Parser:
  92. def __init__(self, indentwidth, tabwidth):
  93. self.indentwidth = indentwidth
  94. self.tabwidth = tabwidth
  95. def set_str(self, str):
  96. assert len(str) == 0 or str[-1] == '\n', "Oops - have str %r" % (str,)
  97. self.str = str
  98. self.study_level = 0
  99. # Return index of a good place to begin parsing, as close to the
  100. # end of the string as possible. This will be the start of some
  101. # popular stmt like "if" or "def". Return None if none found:
  102. # the caller should pass more prior context then, if possible, or
  103. # if not (the entire program text up until the point of interest
  104. # has already been tried) pass 0 to set_lo.
  105. #
  106. # This will be reliable iff given a reliable is_char_in_string
  107. # function, meaning that when it says "no", it's absolutely
  108. # guaranteed that the char is not in a string.
  109. #
  110. # Ack, hack: in the shell window this kills us, because there's
  111. # no way to tell the differences between output, >>> etc and
  112. # user input. Indeed, IDLE's first output line makes the rest
  113. # look like it's in an unclosed paren!:
  114. # Python 1.5.2 (#0, Apr 13 1999, ...
  115. def find_good_parse_start(self, use_ps1, is_char_in_string=None):
  116. str, pos = self.str, None
  117. if use_ps1:
  118. # shell window
  119. ps1 = '\n' + sys.ps1
  120. i = str.rfind(ps1)
  121. if i >= 0:
  122. pos = i + len(ps1)
  123. # make it look like there's a newline instead
  124. # of ps1 at the start -- hacking here once avoids
  125. # repeated hackery later
  126. self.str = str[:pos-1] + '\n' + str[pos:]
  127. return pos
  128. # File window -- real work.
  129. if not is_char_in_string:
  130. # no clue -- make the caller pass everything
  131. return None
  132. # Peek back from the end for a good place to start,
  133. # but don't try too often; pos will be left None, or
  134. # bumped to a legitimate synch point.
  135. limit = len(str)
  136. for tries in range(5):
  137. i = str.rfind(":\n", 0, limit)
  138. if i < 0:
  139. break
  140. i = str.rfind('\n', 0, i) + 1 # start of colon line
  141. m = _synchre(str, i, limit)
  142. if m and not is_char_in_string(m.start()):
  143. pos = m.start()
  144. break
  145. limit = i
  146. if pos is None:
  147. # Nothing looks like a block-opener, or stuff does
  148. # but is_char_in_string keeps returning true; most likely
  149. # we're in or near a giant string, the colorizer hasn't
  150. # caught up enough to be helpful, or there simply *aren't*
  151. # any interesting stmts. In any of these cases we're
  152. # going to have to parse the whole thing to be sure, so
  153. # give it one last try from the start, but stop wasting
  154. # time here regardless of the outcome.
  155. m = _synchre(str)
  156. if m and not is_char_in_string(m.start()):
  157. pos = m.start()
  158. return pos
  159. # Peeking back worked; look forward until _synchre no longer
  160. # matches.
  161. i = pos + 1
  162. while 1:
  163. m = _synchre(str, i)
  164. if m:
  165. s, i = m.span()
  166. if not is_char_in_string(s):
  167. pos = s
  168. else:
  169. break
  170. return pos
  171. # Throw away the start of the string. Intended to be called with
  172. # find_good_parse_start's result.
  173. def set_lo(self, lo):
  174. assert lo == 0 or self.str[lo-1] == '\n'
  175. if lo > 0:
  176. self.str = self.str[lo:]
  177. # As quickly as humanly possible <wink>, find the line numbers (0-
  178. # based) of the non-continuation lines.
  179. # Creates self.{goodlines, continuation}.
  180. def _study1(self):
  181. if self.study_level >= 1:
  182. return
  183. self.study_level = 1
  184. # Map all uninteresting characters to "x", all open brackets
  185. # to "(", all close brackets to ")", then collapse runs of
  186. # uninteresting characters. This can cut the number of chars
  187. # by a factor of 10-40, and so greatly speed the following loop.
  188. str = self.str
  189. str = str.translate(_tran)
  190. str = str.replace('xxxxxxxx', 'x')
  191. str = str.replace('xxxx', 'x')
  192. str = str.replace('xx', 'x')
  193. str = str.replace('xx', 'x')
  194. str = str.replace('\nx', '\n')
  195. # note that replacing x\n with \n would be incorrect, because
  196. # x may be preceded by a backslash
  197. # March over the squashed version of the program, accumulating
  198. # the line numbers of non-continued stmts, and determining
  199. # whether & why the last stmt is a continuation.
  200. continuation = C_NONE
  201. level = lno = 0 # level is nesting level; lno is line number
  202. self.goodlines = goodlines = [0]
  203. push_good = goodlines.append
  204. i, n = 0, len(str)
  205. while i < n:
  206. ch = str[i]
  207. i = i+1
  208. # cases are checked in decreasing order of frequency
  209. if ch == 'x':
  210. continue
  211. if ch == '\n':
  212. lno = lno + 1
  213. if level == 0:
  214. push_good(lno)
  215. # else we're in an unclosed bracket structure
  216. continue
  217. if ch == '(':
  218. level = level + 1
  219. continue
  220. if ch == ')':
  221. if level:
  222. level = level - 1
  223. # else the program is invalid, but we can't complain
  224. continue
  225. if ch == '"' or ch == "'":
  226. # consume the string
  227. quote = ch
  228. if str[i-1:i+2] == quote * 3:
  229. quote = quote * 3
  230. w = len(quote) - 1
  231. i = i+w
  232. while i < n:
  233. ch = str[i]
  234. i = i+1
  235. if ch == 'x':
  236. continue
  237. if str[i-1:i+w] == quote:
  238. i = i+w
  239. break
  240. if ch == '\n':
  241. lno = lno + 1
  242. if w == 0:
  243. # unterminated single-quoted string
  244. if level == 0:
  245. push_good(lno)
  246. break
  247. continue
  248. if ch == '\\':
  249. assert i < n
  250. if str[i] == '\n':
  251. lno = lno + 1
  252. i = i+1
  253. continue
  254. # else comment char or paren inside string
  255. else:
  256. # didn't break out of the loop, so we're still
  257. # inside a string
  258. continuation = C_STRING
  259. continue # with outer loop
  260. if ch == '#':
  261. # consume the comment
  262. i = str.find('\n', i)
  263. assert i >= 0
  264. continue
  265. assert ch == '\\'
  266. assert i < n
  267. if str[i] == '\n':
  268. lno = lno + 1
  269. if i+1 == n:
  270. continuation = C_BACKSLASH
  271. i = i+1
  272. # The last stmt may be continued for all 3 reasons.
  273. # String continuation takes precedence over bracket
  274. # continuation, which beats backslash continuation.
  275. if continuation != C_STRING and level > 0:
  276. continuation = C_BRACKET
  277. self.continuation = continuation
  278. # Push the final line number as a sentinel value, regardless of
  279. # whether it's continued.
  280. assert (continuation == C_NONE) == (goodlines[-1] == lno)
  281. if goodlines[-1] != lno:
  282. push_good(lno)
  283. def get_continuation_type(self):
  284. self._study1()
  285. return self.continuation
  286. # study1 was sufficient to determine the continuation status,
  287. # but doing more requires looking at every character. study2
  288. # does this for the last interesting statement in the block.
  289. # Creates:
  290. # self.stmt_start, stmt_end
  291. # slice indices of last interesting stmt
  292. # self.lastch
  293. # last non-whitespace character before optional trailing
  294. # comment
  295. # self.lastopenbracketpos
  296. # if continuation is C_BRACKET, index of last open bracket
  297. def _study2(self):
  298. _ws=string.whitespace
  299. if self.study_level >= 2:
  300. return
  301. self._study1()
  302. self.study_level = 2
  303. # Set p and q to slice indices of last interesting stmt.
  304. str, goodlines = self.str, self.goodlines
  305. i = len(goodlines) - 1
  306. p = len(str) # index of newest line
  307. while i:
  308. assert p
  309. # p is the index of the stmt at line number goodlines[i].
  310. # Move p back to the stmt at line number goodlines[i-1].
  311. q = p
  312. for nothing in range(goodlines[i-1], goodlines[i]):
  313. # tricky: sets p to 0 if no preceding newline
  314. p = str.rfind('\n', 0, p-1) + 1
  315. # The stmt str[p:q] isn't a continuation, but may be blank
  316. # or a non-indenting comment line.
  317. if _junkre(str, p):
  318. i = i-1
  319. else:
  320. break
  321. if i == 0:
  322. # nothing but junk!
  323. assert p == 0
  324. q = p
  325. self.stmt_start, self.stmt_end = p, q
  326. # Analyze this stmt, to find the last open bracket (if any)
  327. # and last interesting character (if any).
  328. lastch = ""
  329. stack = [] # stack of open bracket indices
  330. push_stack = stack.append
  331. while p < q:
  332. # suck up all except ()[]{}'"#\\
  333. m = _chew_ordinaryre(str, p, q)
  334. if m:
  335. # we skipped at least one boring char
  336. newp = m.end()
  337. # back up over totally boring whitespace
  338. i = newp - 1 # index of last boring char
  339. while i >= p and str[i] in " \t\n":
  340. i = i-1
  341. if i >= p:
  342. lastch = str[i]
  343. p = newp
  344. if p >= q:
  345. break
  346. ch = str[p]
  347. if ch in "([{":
  348. push_stack(p)
  349. lastch = ch
  350. p = p+1
  351. continue
  352. if ch in ")]}":
  353. if stack:
  354. del stack[-1]
  355. lastch = ch
  356. p = p+1
  357. continue
  358. if ch == '"' or ch == "'":
  359. # consume string
  360. # Note that study1 did this with a Python loop, but
  361. # we use a regexp here; the reason is speed in both
  362. # cases; the string may be huge, but study1 pre-squashed
  363. # strings to a couple of characters per line. study1
  364. # also needed to keep track of newlines, and we don't
  365. # have to.
  366. lastch = ch
  367. p = _match_stringre(str, p, q).end()
  368. continue
  369. if ch == '#':
  370. # consume comment and trailing newline
  371. p = str.find('\n', p, q) + 1
  372. assert p > 0
  373. continue
  374. assert ch == '\\'
  375. p = p+1 # beyond backslash
  376. assert p < q
  377. if str[p] != '\n':
  378. # the program is invalid, but can't complain
  379. lastch = ch + str[p]
  380. p = p+1 # beyond escaped char
  381. # end while p < q:
  382. self.lastch = lastch
  383. if stack:
  384. self.lastopenbracketpos = stack[-1]
  385. # Assuming continuation is C_BRACKET, return the number
  386. # of spaces the next line should be indented.
  387. def compute_bracket_indent(self):
  388. self._study2()
  389. assert self.continuation == C_BRACKET
  390. j = self.lastopenbracketpos
  391. str = self.str
  392. n = len(str)
  393. origi = i = str.rfind('\n', 0, j) + 1
  394. j = j+1 # one beyond open bracket
  395. # find first list item; set i to start of its line
  396. while j < n:
  397. m = _itemre(str, j)
  398. if m:
  399. j = m.end() - 1 # index of first interesting char
  400. extra = 0
  401. break
  402. else:
  403. # this line is junk; advance to next line
  404. i = j = str.find('\n', j) + 1
  405. else:
  406. # nothing interesting follows the bracket;
  407. # reproduce the bracket line's indentation + a level
  408. j = i = origi
  409. while str[j] in " \t":
  410. j = j+1
  411. extra = self.indentwidth
  412. return len(str[i:j].expandtabs(self.tabwidth)) + extra
  413. # Return number of physical lines in last stmt (whether or not
  414. # it's an interesting stmt! this is intended to be called when
  415. # continuation is C_BACKSLASH).
  416. def get_num_lines_in_stmt(self):
  417. self._study1()
  418. goodlines = self.goodlines
  419. return goodlines[-1] - goodlines[-2]
  420. # Assuming continuation is C_BACKSLASH, return the number of spaces
  421. # the next line should be indented. Also assuming the new line is
  422. # the first one following the initial line of the stmt.
  423. def compute_backslash_indent(self):
  424. self._study2()
  425. assert self.continuation == C_BACKSLASH
  426. str = self.str
  427. i = self.stmt_start
  428. while str[i] in " \t":
  429. i = i+1
  430. startpos = i
  431. # See whether the initial line starts an assignment stmt; i.e.,
  432. # look for an = operator
  433. endpos = str.find('\n', startpos) + 1
  434. found = level = 0
  435. while i < endpos:
  436. ch = str[i]
  437. if ch in "([{":
  438. level = level + 1
  439. i = i+1
  440. elif ch in ")]}":
  441. if level:
  442. level = level - 1
  443. i = i+1
  444. elif ch == '"' or ch == "'":
  445. i = _match_stringre(str, i, endpos).end()
  446. elif ch == '#':
  447. break
  448. elif level == 0 and ch == '=' and \
  449. (i == 0 or str[i-1] not in "=<>!") and \
  450. str[i+1] != '=':
  451. found = 1
  452. break
  453. else:
  454. i = i+1
  455. if found:
  456. # found a legit =, but it may be the last interesting
  457. # thing on the line
  458. i = i+1 # move beyond the =
  459. found = re.match(r"\s*\\", str[i:endpos]) is None
  460. if not found:
  461. # oh well ... settle for moving beyond the first chunk
  462. # of non-whitespace chars
  463. i = startpos
  464. while str[i] not in " \t\n":
  465. i = i+1
  466. return len(str[self.stmt_start : i].expandtabs(self.tabwidth)) + 1
  467. # Return the leading whitespace on the initial line of the last
  468. # interesting stmt.
  469. def get_base_indent_string(self):
  470. self._study2()
  471. i, n = self.stmt_start, self.stmt_end
  472. j = i
  473. str = self.str
  474. while j < n and str[j] in " \t":
  475. j = j + 1
  476. return str[i:j]
  477. # Did the last interesting stmt open a block?
  478. def is_block_opener(self):
  479. self._study2()
  480. return self.lastch == ':'
  481. # Did the last interesting stmt close a block?
  482. def is_block_closer(self):
  483. self._study2()
  484. return _closere(self.str, self.stmt_start) is not None
  485. # index of last open bracket ({[, or None if none
  486. lastopenbracketpos = None
  487. def get_last_open_bracket_pos(self):
  488. self._study2()
  489. return self.lastopenbracketpos