xonsh.parsers.ast

Abstract Syntax Tree handler

class xonsh.parsers.ast.CtxAwareTransformer(parser)[source]

Transforms a xonsh AST based to use subprocess calls when the first name in an expression statement is not known in the context. This assumes that the expression statement is instead parseable as a subprocess.

Parameters:
parserxonsh.Parser

A parse instance to try to parse subprocess statements with.

ctxadd(value)[source]

Adds a value the most recent context.

ctxremove(value)[source]

Removes a value the most recent context.

ctxupdate(iterable)[source]

Updated the most recent context.

ctxvisit(node, inp, ctx, mode='exec', filename=None, debug_level=0)[source]

Transforms the node in a context-dependent way.

Parameters:
nodeast.AST

A syntax tree to transform.

inpstr

The input code in string format.

ctxdict

The root context to use.

filenamestr, optional

File we are to transform.

debug_levelint, optional

Debugging level to use in lexing and parsing.

Returns:
nodeast.AST

The transformed node.

generic_visit(node)

Called if no explicit visitor function exists for a node.

is_in_scope(node)[source]

Determines whether or not the current node is in scope.

try_subproc_toks(node, strip_expr=False)[source]

Tries to parse the line of the node as a subprocess.

visit(node)

Visit a node.

visit_AnnAssign(node)[source]

Handle visiting an annotated assignment statement.

visit_Assign(node)[source]

Handle visiting an assignment statement.

visit_BoolOp(node)[source]

Handle visiting an boolean operands, like and/or.

visit_ClassDef(node)[source]

Handle visiting a class definition.

visit_Constant(node)
visit_Delete(node)[source]

Handle visiting a del statement.

visit_Expr(node)[source]

Handle visiting an expression.

visit_Expression(node)[source]

Handle visiting an expression body.

visit_For(node)[source]

Handle visiting a for statement.

visit_FunctionDef(node)[source]

Handle visiting a function definition.

visit_Global(node)[source]

Handle visiting a global statement.

visit_Import(node)[source]

Handle visiting a import statement.

visit_ImportFrom(node)[source]

Handle visiting a “from … import …” statement.

visit_Try(node)[source]

Handle visiting a try statement.

visit_UnaryOp(node)[source]

Handle visiting an unary operands, like not.

visit_With(node)[source]

Handle visiting a with statement.

visit_comprehension(node)[source]

Handles visiting list comprehensions, set comprehensions, dictionary comprehensions, and generator expressions.

xonsh.parsers.ast.const_bytes(s: str, **kwargs)[source]
xonsh.parsers.ast.const_name(value, **kwargs)[source]
xonsh.parsers.ast.const_num(n, **kwargs)[source]
xonsh.parsers.ast.const_str(s: str, **kwargs)[source]
xonsh.parsers.ast.gather_load_store_names(node)[source]

Returns the names present in the node’s tree in a set of load nodes and a set of store nodes.

xonsh.parsers.ast.gather_names(node)[source]

Returns the set of all names present in the node’s tree.

xonsh.parsers.ast.get_col(node, default=-1)[source]

Gets the col_offset of a node, or returns the default

xonsh.parsers.ast.get_id(node, default=None)[source]

Gets the id attribute of a node, or returns a default.

xonsh.parsers.ast.get_id_ctx(node)[source]

Gets the id and attribute of a node, or returns a default.

xonsh.parsers.ast.get_lineno(node, default=0)[source]

Gets the lineno of a node or returns the default.

xonsh.parsers.ast.has_elts(x)[source]

Tests if x is an AST node with elements.

xonsh.parsers.ast.is_const_bytes(node)[source]
xonsh.parsers.ast.is_const_name(node)[source]
xonsh.parsers.ast.is_const_num(node)[source]
xonsh.parsers.ast.is_const_str(node)[source]
xonsh.parsers.ast.isdescendable(node)[source]

Determines whether or not a node is worth visiting. Currently only UnaryOp and BoolOp nodes are visited.

xonsh.parsers.ast.isexpression(node, ctx=None, *args, **kwargs)[source]

Determines whether a node (or code string) is an expression, and does not contain any statements. The execution context (ctx) and other args and kwargs are passed down to the parser, as needed.

xonsh.parsers.ast.leftmostname(node)[source]

Attempts to find the first name in the tree.

xonsh.parsers.ast.load_attribute_chain(name, lineno=None, col=None)[source]

Creates an AST that loads variable name that may (or may not) have attribute chains. For example, “a.b.c”

xonsh.parsers.ast.max_col(node)[source]

Returns the maximum col_offset of the node and all sub-nodes.

xonsh.parsers.ast.max_line(node)[source]

Computes the maximum lineno.

xonsh.parsers.ast.min_col(node)[source]

Computes the minimum col_offset.

xonsh.parsers.ast.min_line(node)[source]

Computes the minimum lineno.

xonsh.parsers.ast.node_len(node)[source]

The length of a node as a string

xonsh.parsers.ast.pdump(s, **kwargs)[source]

performs a pretty dump of an AST node.

xonsh.parsers.ast.pprint_ast(s, *, sep=None, end=None, file=None, flush=False, **kwargs)[source]

Performs a pretty print of the AST nodes.

xonsh.parsers.ast.xonsh_call(name, args, lineno=None, col=None)[source]

Creates the AST node for calling a function of a given name. Functions names may contain attribute access, e.g. __xonsh__.env.