MCPcopy Index your code
hub / github.com/bpython/bpython / _funcname_and_argnum

Method _funcname_and_argnum

bpython/repl.py:601–660  ·  view source on GitHub ↗

Parse out the current function name and arg from a line of code.

(
        cls, line: str
    )

Source from the content-addressed store, hash-verified

599
600 @classmethod
601 def _funcname_and_argnum(
602 cls, line: str
603 ) -> tuple[str | None, str | int | None]:
604 """Parse out the current function name and arg from a line of code."""
605 # each element in stack is a _FuncExpr instance
606 # if keyword is not None, we've encountered a keyword and so we're done counting
607 stack = [_FuncExpr("", "", 0, "")]
608 try:
609 for token, value in Python3Lexer().get_tokens(line):
610 if token is Token.Punctuation:
611 if value in "([{":
612 stack.append(_FuncExpr("", "", 0, value))
613 elif value in ")]}":
614 element = stack.pop()
615 expr = element.opening + element.full_expr + value
616 stack[-1].function_expr += expr
617 stack[-1].full_expr += expr
618 elif value == ",":
619 if stack[-1].keyword is None:
620 stack[-1].arg_number += 1
621 else:
622 stack[-1].keyword = ""
623 stack[-1].function_expr = ""
624 stack[-1].full_expr += value
625 elif value == ":" and stack[-1].opening == "lambda":
626 expr = stack.pop().full_expr + ":"
627 stack[-1].function_expr += expr
628 stack[-1].full_expr += expr
629 else:
630 stack[-1].function_expr = ""
631 stack[-1].full_expr += value
632 elif (
633 token is Token.Number
634 or token in Token.Number.subtypes
635 or token is Token.Name
636 or token in Token.Name.subtypes
637 or token is Token.Operator
638 and value == "."
639 ):
640 stack[-1].function_expr += value
641 stack[-1].full_expr += value
642 elif token is Token.Operator and value == "=":
643 stack[-1].keyword = stack[-1].function_expr
644 stack[-1].function_expr = ""
645 stack[-1].full_expr += value
646 elif token is Token.Number or token in Token.Number.subtypes:
647 stack[-1].function_expr = value
648 stack[-1].full_expr += value
649 elif token is Token.Keyword and value == "lambda":
650 stack.append(_FuncExpr(value, "", 0, value))
651 else:
652 stack[-1].function_expr = ""
653 stack[-1].full_expr += value
654 while stack[-1].opening in "[{":
655 stack.pop()
656 elem1 = stack.pop()
657 elem2 = stack.pop()
658 return elem2.function_expr, elem1.keyword or elem1.arg_number

Callers 2

get_argsMethod · 0.95

Calls 2

_FuncExprClass · 0.85
appendMethod · 0.80

Tested by 1