Module TaglessFinal.Expression

This module provides a type that represents a Python expression.

type ('arguments, 'bin_op, 'bool_op, 'compare, 'comprehension, 'constant, 'expr_context, 'identifier, 'keyword, 'location, 'unary_op, 'expr) t = private {
bool_op : location:'location -> op:'bool_op -> values:'expr list -> 'expr;

Represents a boolean expression.

  • Example: a and b
  • Example: a or b or c
named_expr : location:'location -> target:'expr -> value:'expr -> 'expr;

Represents an assignment expression, a.k.a. the "walrus" operator. target is guaranteed by the CPython parser to always be a simple identifier. See PEP 572.

  • Example: (a := b)
bin_op : location:'location -> left:'expr -> op:'bin_op -> right:'expr -> 'expr;

Represents a numerical binary expression.

  • Example: a + b
unary_op : location:'location -> op:'unary_op -> operand:'expr -> 'expr;

Represents an unary expression.

  • Example: not a
lambda : location:'location -> args:'arguments -> body:'expr -> 'expr;

Represents a lambda expression. See PEP 312.

  • Example: lambda x, /, y, *, z: x + y + z
if_exp : location:'location -> test:'expr -> body:'expr -> orelse:'expr -> 'expr;

Represents a conditional expression. See PEP 308.

  • Example: x if x > 0 else None
dict : location:'location -> keys:'expr option list -> values:'expr list -> 'expr;

Represents a Python dictionary display.

  • Example: {}
  • Example: { "a": 1, "b": 2 }
  • Example: { "a": 1, **b }. The element **b here will be represented with a None key and a value of b.
set : location:'location -> elts:'expr list -> 'expr;

Represents a Python set display.

  • Example: { "a", "b" }
list_comp : location:'location -> elt:'expr -> generators:'comprehension list -> 'expr;

Represents a list comprehension. See PEP 202.

  • Example: [y for x in l if len(x) > 1 for y in x if y < 4]
set_comp : location:'location -> elt:'expr -> generators:'comprehension list -> 'expr;

Represents a set comprehension.

  • Example: {x for x in l}
dict_comp : location:'location -> key:'expr -> value:'expr -> generators:'comprehension list -> 'expr;

Represents a dict comprehension. See PEP 274.

  • Example: {x:y for x, y in l}
generator_exp : location:'location -> elt:'expr -> generators:'comprehension list -> 'expr;

Represents a generator expression. See PEP 289.

  • Example: (x for x in l)
await : location:'location -> value:'expr -> 'expr;

Represents an await expression. See PEP 492.

  • Example: await foo
yield : location:'location -> value:'expr option -> 'expr;

Represents a yield expression. See PEP 342.

  • Example: (yield)
  • Example: (yield x)
yield_from : location:'location -> value:'expr -> 'expr;

Represents a yield from expression. See PEP 380.

  • Example: (yield from x)
compare : location:'location -> left:'expr -> ops:'compare list -> comparators:'expr list -> 'expr;

Represents a comparison expression.

  • Example: x is y
  • Example: x > y > z
call : location:'location -> func:'expr -> args:'expr list -> keywords:'keyword list -> 'expr;

Represents a call expression, where args is the list of positionally-specified arguments, and keywords is the list of keyword-specified arguments.

  • Example: foo(x, y=1)
  • Example: foo(x, *y, **z)
formatted_value : location:'location -> value:'expr -> conversion:int -> format_spec:'expr option -> 'expr;

Represents the expression component of an f-string. See PEP 498. conversion is an integer representing the type conversion operator:

  • -1 means no formatting.
  • 115 means !s string formatting.
  • 114 means !r repr formatting
  • 97 means !a ascii formatting

format_spec represents the format specifier. This is always going to be a joined_str expression.

  • Example: f"derp={a+b:{foo}}". The entire expression will be a joined_str but the a+b part will be represented as a formatted_value with the foo part as its format specifier.
joined_str : location:'location -> values:'expr list -> 'expr;

Represents an f-string. See PEP 498.

  • Example: f"a={b}"
constant : location:'location -> value:'constant -> kind:string option -> 'expr;

Represents a constant expression.

  • Example: 42
attribute : location:'location -> value:'expr -> attr:'identifier -> ctx:'expr_context -> 'expr;

Represents an attribute access expression.

  • Example: a.b
subscript : location:'location -> value:'expr -> slice:'expr -> ctx:'expr_context -> 'expr;

Represents a subscript access expression.

  • Example: a[b]
starred : location:'location -> value:'expr -> ctx:'expr_context -> 'expr;

Represents an starred expression, which is used to represent iterable unpacking (inside call, list, or comprehension expressions, for example). See PEP 448.

name : location:'location -> id:'identifier -> ctx:'expr_context -> 'expr;

Represents a simple identifier.

  • Example: a
list : location:'location -> elts:'expr list -> ctx:'expr_context -> 'expr;

Represents a Python list display.

  • Example: []
  • Example: [a, b]
  • Example: [a, *b]
tuple : location:'location -> elts:'expr list -> ctx:'expr_context -> 'expr;

Represents a Python tuple display.

  • Example: ()
  • Example: (a, b)
  • Example: (a, *b)
slice : location:'location -> lower:'expr option -> upper:'expr option -> step:'expr option -> 'expr;

Represents a slice expression. This kind of expression can only appear within the slice field of the subscript expression, either directly or as an element of a tuple expression.

  • Example: a[:]
  • Example: a[1:2, 3:4]
}
val make : bool_op:(location:'a -> op:'b -> values:'c list -> 'c) -> named_expr:(location:'a -> target:'c -> value:'c -> 'c) -> bin_op:(location:'a -> left:'c -> op:'d -> right:'c -> 'c) -> unary_op:(location:'a -> op:'e -> operand:'c -> 'c) -> lambda:(location:'a -> args:'f -> body:'c -> 'c) -> if_exp:(location:'a -> test:'c -> body:'c -> orelse:'c -> 'c) -> dict:(location:'a -> keys:'c option list -> values:'c list -> 'c) -> set:(location:'a -> elts:'c list -> 'c) -> list_comp:(location:'a -> elt:'c -> generators:'g list -> 'c) -> set_comp:(location:'a -> elt:'c -> generators:'g list -> 'c) -> dict_comp:(location:'a -> key:'c -> value:'c -> generators:'g list -> 'c) -> generator_exp:(location:'a -> elt:'c -> generators:'g list -> 'c) -> await:(location:'a -> value:'c -> 'c) -> yield:(location:'a -> value:'c option -> 'c) -> yield_from:(location:'a -> value:'c -> 'c) -> compare:(location:'a -> left:'c -> ops:'h list -> comparators:'c list -> 'c) -> call:(location:'a -> func:'c -> args:'c list -> keywords:'i list -> 'c) -> formatted_value:(location:'a -> value:'c -> conversion:int -> format_spec:'c option -> 'c) -> joined_str:(location:'a -> values:'c list -> 'c) -> constant:(location:'a -> value:'j -> kind:string option -> 'c) -> attribute:(location:'a -> value:'c -> attr:'k -> ctx:'l -> 'c) -> subscript:(location:'a -> value:'c -> slice:'c -> ctx:'l -> 'c) -> starred:(location:'a -> value:'c -> ctx:'l -> 'c) -> name:(location:'a -> id:'k -> ctx:'l -> 'c) -> list:(location:'a -> elts:'c list -> ctx:'l -> 'c) -> tuple:(location:'a -> elts:'c list -> ctx:'l -> 'c) -> slice:(location:'a -> lower:'c option -> upper:'c option -> step:'c option -> 'c) -> unit -> ('f'd'b'h'g'j'l'k'i'a'e'c) t

Constructor of t.