|
62 | 62 | # This for import completion |
63 | 63 | from bpython import importcompletion |
64 | 64 |
|
65 | | -# And these are used for argspec. |
66 | | -from pyparsing import Forward, Suppress, QuotedString, dblQuotedString, \ |
67 | | - Group, OneOrMore, ZeroOrMore, Literal, Optional, Word, \ |
68 | | - alphas, alphanums, printables, ParseException |
69 | | - |
70 | 65 | from bpython import __version__ |
71 | 66 |
|
72 | 67 | def log(x): |
@@ -331,17 +326,6 @@ def __init__(self, scr, interp, statusbar=None, idle=None): |
331 | 326 | 'ignore') as hfile: |
332 | 327 | self.rl_hist = hfile.readlines() |
333 | 328 |
|
334 | | - pexp = Forward() |
335 | | - chars = printables.replace('(', '') |
336 | | - chars = chars.replace(')', '') |
337 | | - pexpnest = ( |
338 | | - Optional(Word(chars)) + |
339 | | - Literal("(") + |
340 | | - Optional(Group(pexp)) + |
341 | | - Optional(Literal(")"))) |
342 | | - pexp << (OneOrMore(Word(chars) | pexpnest)) |
343 | | - self.pparser = pexp |
344 | | - |
345 | 329 | def attr_matches(self, text): |
346 | 330 | """Taken from rlcompleter.py and bent to my will.""" |
347 | 331 |
|
@@ -489,39 +473,24 @@ def getargspec(func): |
489 | 473 | # This happens if no __init__ is found |
490 | 474 | return None |
491 | 475 |
|
492 | | - def parse_parens(s): |
493 | | - """Run a string through the pyparsing pattern for paren |
494 | | - counting.""" |
495 | | - |
496 | | - try: |
497 | | - parsed = self.pparser.parseString(s).asList() |
498 | | - except ParseException: |
499 | | - return False |
500 | | - |
501 | | - return parsed |
502 | | - |
503 | | - def walk(seq): |
504 | | - """Walk a nested list and return the last list found that |
505 | | - doesn't have a close paren in it (i.e. the active function)""" |
506 | | - r = None |
507 | | - if isinstance(seq, list): |
508 | | - if ")" not in seq and "(" in seq: |
509 | | - r = seq[seq.index('(') - 1] |
510 | | - for i in seq: |
511 | | - t = walk(i) |
512 | | - if t: |
513 | | - r = t |
514 | | - return r |
515 | | - |
516 | 476 | if not OPTS.arg_spec: |
517 | 477 | return False |
518 | 478 |
|
519 | | - t = parse_parens(self.s) |
520 | | - if not t: |
521 | | - return False |
522 | | - |
523 | | - func = walk(t) |
524 | | - if not func: |
| 479 | + stack = [''] |
| 480 | + try: |
| 481 | + for (token, value) in PythonLexer().get_tokens(self.s): |
| 482 | + if token is Token.Punctuation: |
| 483 | + if value == '(': |
| 484 | + stack.append('') |
| 485 | + elif value == ')': |
| 486 | + stack.pop() |
| 487 | + elif (token is Token.Name or token in Token.Name.subtypes or |
| 488 | + token is Token.Operatir and value == '.'): |
| 489 | + stack[-1] += value |
| 490 | + else: |
| 491 | + stack[-1] = '' |
| 492 | + func = stack.pop() or stack.pop() |
| 493 | + except IndexError: |
525 | 494 | return False |
526 | 495 |
|
527 | 496 | return getargspec(func) |
|
0 commit comments