Skip to content
Back to top
View this page
Edit this page

# xonsh.parsers.ast

Abstract Syntax Tree handler

class xonsh.parsers.ast.CtxAwareTransformer(parser)

  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:
    parser xonsh.Parser

      A parse instance to try to parse subprocess statements with.

  ctxadd(value)

    Adds a value the most recent context.

  ctxremove(value)

    Removes a value the most recent context.

  ctxupdate(iterable)

    Updated the most recent context.

  ctxvisit(node, inp, ctx, mode='exec', filename=None, debug_level=0, user_names=None)

    Transforms the node in a context-dependent way.

    Parameters:
      node ast.AST

        A syntax tree to transform.

      inp str

        The input code in string format.

      ctx dict

        The root context to use.

      filename str, optional

        File we are to transform.

      debug_level int, optional

        Debugging level to use in lexing and parsing.

    Returns:
      node ast.AST

        The transformed node.

  generic_visit(node)

    Called if no explicit visitor function exists for a node.

  is_in_scope(node)

    Determines whether or not the current node is in scope.

  try_subproc_toks(node, strip_expr=False)

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

  visit(node)

    Visit a node.

  visit_AnnAssign(node)

    Handle visiting an annotated assignment statement.

  visit_Assign(node)

    Handle visiting an assignment statement.

  visit_AsyncFor(node)

    Handle visiting a for statement.

  visit_AsyncFunctionDef(node)

    Handle visiting a function definition.

  visit_AsyncWith(node)

    Handle visiting a with statement.

  visit_BoolOp(node)

    Handle visiting an boolean operands, like and/or.

  visit_ClassDef(node)

    Handle visiting a class definition.

  visit_Delete(node)

    Handle visiting a del statement.

  visit_Expr(node)

    Handle visiting an expression.

  visit_Expression(node)

    Handle visiting an expression body.

  visit_For(node)

    Handle visiting a for statement.

  visit_FunctionDef(node)

    Handle visiting a function definition.

  visit_Global(node)

    Handle visiting a global statement.

  visit_Import(node)

    Handle visiting a import statement.

  visit_ImportFrom(node)

    Handle visiting a “from … import …” statement.

  visit_NamedExpr(node)

    Handle visiting a walrus operator (:=) expression.

  visit_Try(node)

    Handle visiting a try statement.

  visit_UnaryOp(node)

    Handle visiting an unary operands, like not.

  visit_With(node)

    Handle visiting a with statement.

  visit_comprehension(node)

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

xonsh.parsers.ast.const_bytes(s: str, **kwargs)

xonsh.parsers.ast.const_name(value, **kwargs)

xonsh.parsers.ast.const_num(n, **kwargs)

xonsh.parsers.ast.const_str(s: str, lineno: int | None = None, col_offset: int | None = None, is_raw: bool = True)

xonsh.parsers.ast.gather_load_store_names(node)

  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)

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

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

  Gets the col_offset of a node, or returns the default

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

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

xonsh.parsers.ast.get_id_ctx(node)

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

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

  Gets the lineno of a node or returns the default.

xonsh.parsers.ast.has_elts(x)

  Tests if x is an AST node with elements.

xonsh.parsers.ast.is_const_bytes(node)

xonsh.parsers.ast.is_const_name(node)

xonsh.parsers.ast.is_const_num(node)

xonsh.parsers.ast.is_const_str(node)

xonsh.parsers.ast.isdescendable(node)

  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)

  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)

  Attempts to find the first name in the tree.

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

  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)

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

xonsh.parsers.ast.max_line(node)

  Computes the maximum lineno.

xonsh.parsers.ast.min_col(node)

  Computes the minimum col_offset.

xonsh.parsers.ast.min_line(node)

  Computes the minimum lineno.

xonsh.parsers.ast.node_len(node)

  The length of a node as a string (This may need to be added to for more nodes as more cases are found.)

xonsh.parsers.ast.pdump(s, **kwargs)

  performs a pretty dump of an AST node.

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

  Performs a pretty print of the AST nodes.

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

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