Quasiquotes API¶
quasiquotes is designed to make it easy to extend python syntax with arbitrary
parsing logic. To define a new syntax enhancement, create an instance of a
subclass of QuasiQuoter
that overrides the
quote_expr
or quote_stmt
methods.
-
class
quasiquotes.quasiquoter.
QuasiQuoter
¶ Custom parsing logic for python
-
static
locals_to_fast
(frame, *, _locals_to_fast=<_FuncPtr object>, _pyobject=<class 'ctypes.py_object'>, _true=c_int(1))¶ Write the
f_locals
offrame
back into the fast local storage.Parameters: frame : frame
The frame whose
f_locals
and fast will be synced.
-
quote_expr
(expr, frame, col_offset)¶ Quote an expression.
This is called in the oxford brackets case: [$qq|...|]
Parameters: expr : str
The expression to quote.
frame : frame
The stack frame where this expression is being executed.
col_offset : int
The column offset for the quasiquoter.
Returns: v : any
The value of the quoted expression.
-
quote_stmt
(stmt, frame, col_offset)¶ Quote a statment.
This is called in the enhanced with block case: with $qq: ...
Parameters: stmt : str
The statement to quote. This will have the unaltered indentation.
frame : frame
The stack frame where this statement is being executed.
col_offset : int
The column offset for the quasiquoter.
-
static
quote_stmt
has no value. It is used to run normal imperitive code like you
would normally put in the body of a context manager.
quote_expr
has a value. It is used to create expressions that can be plugged
into other expressions.
Both quote_stmt
and quote_expr
are passed 3 arguments:
- String representing the body of either the expression or statement
- Stackframe where this is being executed
- Column offset of the quasiquoter
The string will be the pre-built string literal the we constructed at decode time. The stackframe will be the python stackframe where the quoted statement or expression is being used. Finally the column offset will be the pre-built integer constant that represents the offset of the quasiquote token.
Each quasiquoter is free to do whatever it wants with this information, including mutation of the calling frame’s locals, compiling new code, or just ignoring the body.
A quasiquoter does not need to implement both quote_stmt
and
quote_expr
. In some cases, it only makes sense to support one of these
features. If a quote type is used syntactically; however, the runtime
quasiquoter does not support this featere then a
quasiquotes.quasiquoter.QQNotImplementedError
exception will be
raised.