inputtransformer2
Notebook syntax from IPython/core/inputtransformer2.py and IPython/utils/tokenutil.py.
generate_tokens_catch_errors
def generate_tokens_catch_errors(
readline, extra_errors_to_catch:list[str] | None=None
):
leading_empty_lines
def leading_empty_lines(
lines
):
Remove leading empty lines
If the leading lines are empty or contain only whitespace, they will be removed.
leading_indent
def leading_indent(
lines
):
Remove leading indentation.
Removes the minimum common leading indentation from all lines.
cell_magic
def cell_magic(
lines
):
find_end_of_continued_line
def find_end_of_continued_line(
lines, start_line:int
):
Find the last line of a line explicitly extended using backslashes.
Uses 0-indexed line numbers.
assemble_continued_line
def assemble_continued_line(
lines, start:tuple[int, int], end_line:int
):
Assemble a single line from multiple continued line pieces
Continued lines are lines ending in \, and the line following the last \ in the block.
For example, this code continues over multiple lines::
if (assign_ix is not None) \
and (len(line) > assign_ix + 2) \
and (line[assign_ix+1].string == '%') \
and (line[assign_ix+2].type == tokenize.NAME):
This statement contains four continued line pieces. Assembling these pieces into a single line would give::
if (assign_ix is not None) and (len(line) > assign_ix + 2) and (line[...
This uses 0-indexed line numbers. start is (lineno, colno).
Used to allow %magic and !system commands to be continued over multiple lines.
TokenTransformBase
def TokenTransformBase(
start
):
Base class for transformations which examine tokens.
Special syntax should not be transformed when it occurs inside strings or comments. This is hard to reliably avoid with regexes. The solution is to tokenise the code as Python, and recognise the special syntax in the tokens.
IPython's special syntax is not valid Python syntax, so tokenising may go wrong after the special syntax starts. These classes therefore find and transform one instance of special syntax at a time into regular Python syntax. After each transformation, tokens are regenerated to find the next piece of special syntax.
Subclasses need to implement one class method (find) and one regular method (transform).
The priority attribute can select which transformation to apply if multiple transformers match in the same place. Lower numbers have higher priority. This allows "%magic?" to be turned into a help call rather than a magic call.
TokenTransformBase.sortby
def sortby():
TokenTransformBase.find
def find(
tokens_by_line
):
Find one instance of special syntax in the provided tokens.
Tokens are grouped into logical lines for convenience, so it is easy to e.g. look at the first token of each line. tokens_by_line is a list of lists of tokenize.TokenInfo objects.
This should return an instance of its class, pointing to the start position it has found, or None if it found no match.
TokenTransformBase.transform
def transform(
lines:list[str]
):
Transform one instance of special syntax found by find()
Takes a list of strings representing physical lines, returns a similar list of transformed lines.
MagicAssign
def MagicAssign(
start
):
Transformer for assignments from magics (a = %foo)
MagicAssign.find
def find(
tokens_by_line
):
Find the first magic assignment (a = %foo) in the cell.
MagicAssign.transform
def transform(
lines
):
Transform one instance of special syntax found by find()
Takes a list of strings representing physical lines, returns a similar list of transformed lines.
SystemAssign
def SystemAssign(
start
):
Transformer for assignments from system commands (a = !foo)
SystemAssign.find_pre_312
def find_pre_312(
tokens_by_line
):
SystemAssign.find_post_312
def find_post_312(
tokens_by_line
):
SystemAssign.find
def find(
tokens_by_line
):
Find the first system assignment (a = !foo) in the cell.
SystemAssign.transform
def transform(
lines
):
Transform one instance of special syntax found by find()
Takes a list of strings representing physical lines, returns a similar list of transformed lines.
EscapedCommand
def EscapedCommand(
start
):
Transformer for escaped commands like %foo and !foo.
EscapedCommand.find
def find(
tokens_by_line
):
Find the first escaped command (%foo, !foo, etc.) in the cell.
EscapedCommand.transform
def transform(
lines
):
Transform one instance of special syntax found by find()
Takes a list of strings representing physical lines, returns a similar list of transformed lines.
HelpEnd
def HelpEnd(
start, q_locn
):
Transformer for help syntax: obj? and obj??
HelpEnd.find
def find(
tokens_by_line
):
Find the first help command (foo?) in the cell.
HelpEnd.transform
def transform(
lines
):
Transform a help command found by the find() classmethod.
make_tokens_by_line
def make_tokens_by_line(
lines:list[str]
):
Group tokens by logical line, keeping multiline expressions and strings together. Pass source.splitlines(keepends=True).
TransformerManager
def TransformerManager():
Apply line and token transformations to a cell.
TransformerManager.do_one_token_transform
def do_one_token_transform(
lines
):
Find and run the transform earliest in the code.
Returns (changed, lines).
This method is called repeatedly until changed is False, indicating that all available transformations are complete.
The tokens following IPython special syntax might not be valid, so the transformed code is retokenised every time to identify the next piece of special syntax. Hopefully long code cells are mostly valid Python, not using lots of IPython special syntax, so this shouldn't be a performance issue.
TransformerManager.do_token_transforms
def do_token_transforms(
lines
):
TransformerManager.transform_cell
def transform_cell(
cell:str
)->str:
Transforms a cell of input code