Package: asts
Generic Tree-Sitter AST APIQuick IntroExtended TutorialAST CreationConstructorFrom StringAST TemplatesAST CopyAST MethodsCommon OperationsSource LocationsFunctionsFunction CallsitesAST TraversalAST ManipulationMutation PrimitivesTransformersArchitectureFAQLicenseGeneric Tree-Sitter AST APIThe ASTs package provides a Python API into GrammaTech's Software
Evolution Library (SEL) for source code manipulation.  SEL
generalizes over GitHub'stree-sitterparsing libraries providing
a uniform interface over multiple programming languages (primarily
Python, JavaScript/TypeScript, and C/C++), and providing additional
functionality for software inspection and modification.Quick IntroHere's how to create and perform some common operations on an AST:$python3Python3.8.5Type"help","copyright","credits"or"license"formoreinformation.>>>importasts>>>root=asts.AST.from_string("x + 88",language=asts.ASTLanguage.Python)>>>root.children[<asts.types.PythonExpressionStatement00x2>]>>>root.children[0].children[<asts.types.PythonBinaryOperator0x3>]>>>root.children[0].children[0].children[<asts.types.PythonIdentifier0x4>,<asts.types.PythonAdd0x5>,<asts.types.PythonInteger0x6>]>>>root.children[0].children[0].children[0].source_text'x'>>>root.children[0].children[0].children[1].source_text'+'>>>root.children[0].children[0].children[2].source_text'88'>>>root.children[0].children[0].source_text'x + 88'>>>root.children[0].children[0].child_slots()[('LEFT',1),('OPERATOR',1),('RIGHT',1),('CHILDREN',0)]>>>list(map(lambdax:x.source_text,root.children[0].children[0].children))['x','+','88']Extended TutorialThe following examples assume you have imported the asts library usingimport asts.  See the methods provided by asts.py for more
information.AST CreationConstructorThe defaultASTconstructor should not be invoked directly by clients.
Instead, the static factory methods described below should be utilized.From StringASTs may be created from source text using theAST.from_stringfactory method in the Python API.  Using this method requires source
text and (optionally) a language enumeration indicating the source
text language.  The example below shows the creation of a simple AST:>>>root=asts.AST.from_string("x + 88",language=asts.ASTLanguage.Python)Language enumerations exist forC,Cpp,Java,Javascript,Python,Rust,TypescriptTs, andTypescriptTsx.For larger examples where the language may be inferred, the language
parameter may optionally be elided.  For instance:>>>text="""... import sys...... def fib(n: int) -> int:...     if n < 2:...         return n...     else:...         return fib(n - 1) + fib(n - 2)...... def main():...     if len(sys.argv) == 1:...         print(fib(int(sys.argv[1])))...... if __name__ == '__main__':...     main()... """>>>root=asts.AST.from_string(text)If tree-sitter cannot parse the source text into an AST, an error or text
fragment AST node will be created starting at the location of the invalid
parse.  By default, the children of this node will be the best-effort but
potentially incorrect parse tree received from tree-sitter with all terminal
tokens excepting zero-width ones included.  If instead of the tree you wish
to have a flat, text representation child, you may passerror_tree=Falseas a keyword argument.Finally, by default, the AST returned is the top-level, module node of
the parsed AST.  However, in some cases, we may wish get the deepest
subnode still encompassing all of the given source text.  This allows
us to create statement AST nodes, for instance.  To do so, clients
may use thedeepestkeyword, as shown below:>>>root=asts.AST.from_string(..."x + 88",...language=asts.ASTLanguage.Python,...deepest=True...)>>>type(root)<class'asts.types.PythonBinaryOperator'>AST TemplatesTemplates for building ASTsASTs may also be created using the AST template mechanism.  For
instance, the following snippet creates an AST equivalent toasts.AST.from_string("x = 2", language=asts.ASTLanguage.Python, deepest=True):>>>root=asts.AST.ast_template("$ID = 2",asts.ASTLanguage.Python,id="x")>>>root.source_text'x = 2'Metavariable names (e.g.$IDabove) may contain uppercase characters,
digits, or underscores.  Metavariables may be scalars (e.g.$) or
lists (e.g.@), as shown below:>>>root=asts.AST.ast_template("fn(@ARGS)",asts.ASTLanguage.Python,args=[1,2,3])>>>root.source_text'fn(1, 2, 3)'Metavariables may also be positional arguments (e.g.$1,$2), as
shown below:>>>root=asts.AST.ast_template("$1 = 2",asts.ASTLanguage.Python,"x")>>>root.source_text'x = 2'However, you may not combine positional (e.g.$1) and keyword
(e.g.$ID) metavariables in a single template.  The corresponding
metavariable values passed in as arguments toast_templatemay be
ASTs, literals, or lists.Templates for building and destructuring ASTsASTs may also be directly created for the metavariables in an AST
template.  For instance, in the template"$1 = $2", we may create
ASTs for$1and$2usingasts_from_template, as shown below:>>>asts=asts.AST.asts_from_template("$1 = $2",asts.ASTLanguage.Python,"x",1)>>>len(asts)2>>>asts[0].source_text'x'>>>asts[1].source_text'1'For now, only the position syntax (e.g.$1,$2) is supported byasts_from_template.  One AST is returned per metavariable, in
numeric order.More informationMore information on AST templates may be found in the SELtemplate documentation.AST CopyCopies of an AST may created usingAST.copyor the pythoncopymodule, as shown below:>>>root=asts.AST.from_string("x + 1",asts.ASTLanguage.Python,deepest=True)>>>copy=asts.AST.copy(root)>>>copy.source_text'x + 1'In addition, clients may set child slots in the copy by passing in new
ASTs for each slot as keyword arguments, as shown below:>>>root=asts.AST.from_string("x + 1",asts.ASTLanguage.Python,deepest=True)>>>y=asts.AST.from_string("y",asts.ASTLanguage.Python,deepest=True)>>>copy=asts.AST.copy(root,left=y)>>>copy.source_text'y + 1'In addition to ASTs, clients may also pass in literals (e.g. strings,
code snippets, numbers) as keyword values, as shown below.  These are
automatically parsed into an AST to be inserted.>>>root=asts.AST.from_string("x + 1",asts.ASTLanguage.Python,deepest=True)>>>copy=asts.AST.copy(root,left=1)>>>copy.source_text'1 + 1'>>>copy=asts.AST.copy(root,left="y")>>>copy.source_text'y + 1'To view the names of an AST's child slots, you may use thechild_slotsmethod, as shown below:>>>root=asts.AST.from_string("x + 1",asts.ASTLanguage.Python,deepest=True)>>>root.child_slots()[('LEFT',1),('OPERATOR',1),('RIGHT',1),('CHILDREN',0)]Alternatively, you may use thedirbuilt-in to inspect an AST object
and find its child slots as python properties, as shown below:>>>root=asts.AST.from_string("x + 1",asts.ASTLanguage.Python,deepest=True)>>>dir(root)['__class__',...,'left',...,'operator',...,'right',...,'traverse']Beyond these named child slots storing ASTs which are parsed from the language
grammar, internally, each AST has several additional slots which store ASTs
outside of the grammar rules, such as comments or error nodes. These includebefore-asts,after-asts, and (optionally)internal-asts-[0-9]+storing
nodes such as comments and errors before the AST, after the AST, and between
implicit terminal tokens within the AST respectively. The names of these slots
can be accessed using thechild_slotsmethod and passinginternal=True, as
shown below. These slot names may also be usedchild_slot/lookupto
retrieve their current value and and withcopyto modify the existing value.>>>text="""... # This is a comment... x = x + 1... """>>>root=asts.AST.from_string(text,asts.ASTLanguage.Python)>>>stmt=root.children[-1]>>>stmt.source_text'x = x + 1'>>>stmt.child_slots(internal=True)[('BEFORE-ASTS',0),('CHILDREN',0),('AFTER-ASTS',0)]>>>stmt.child_slot("BEFORE-ASTS")[<asts.types.PythonComment0x3>]AST MethodsCommon OperationsIn practice, most clients will interact with ASTs using the AST's type and
retrieving the AST's source text, parent AST, and child ASTs.  All of
these operations are supported by the python API.  To begin, let's examine
using AST types.ASTs are embedded in a rich type hierarchy generated by the tree-sitter
library; this type hierarchy is augmented by generic mixins in SEL for
common AST types across languages.  For instance,IfAST,StatementAST, andIdentifierASTclasses abstract common language
constructs and allow identification of these types of ASTs regardless
of the language of the underlying program.  As with other objects,
clients may useisinstanceto test if an AST is an instance or
subclass of a given type, as shown below:>>>root=asts.AST.from_string(..."print(x)",...language=asts.ASTLanguage.Python,...deepest=True...)>>>isinstance(root,asts.types.PythonCall)True>>>isinstance(root,asts.types.CallAST)True>>>isinstance(root,asts.types.PythonAST)True>>>isinstance(root,asts.types.CStringLiteral)FalseBeyond AST types, accessing the source text of an AST is
another common operation.  This may be accomplished using
thesource_textproperty on ASTs, as shown below:>>>root=asts.AST.from_string(..."print(x)",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root.source_text'print(x)'Additionally, to view a list of the immediate children
of a particular AST, thechildrenproperty may be used,
as shown below:>>>root=asts.AST.from_string(..."print(x)",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root.children[<asts.types.PythonIdentifier0x4>,<asts.types.PythonArgumentList10x5>]>>>root.children[0].source_text'print'>>>identifier=root.children[1].children[0]>>>identifier.source_text'x'An AST is composed of various child slots which are concatenated together
when accessed via thechildrenproperty.  To view the child slots for
a particular AST you may use thechild_slots()method, which returns
a list of slot-name, arity pairs.  An arity of one indicates the slot
is a single AST, while an arity of zero indicates the slot is composed
of zero or more ASTs.  The AST(s) comprising a given slot may be
accessed using the slot name as a python property accessor or by usingchild_slotmethod, as shown below:>>>root=asts.AST.from_string(..."print(x)",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root.child_slots()[('FUNCTION',1),('ARGUMENTS',1),('CHILDREN',0)]>>>root.function.source_text'print'>>>root.child_slot("FUNCTION").source_text'print'Beyond direct child lookup, an AST path composed of the names of the
child slots and, when the child slot is a list, a position in this list
may be used to lookup ASTs at arbritrary depths in the tree using thelookupmethod.  Additionally, an AST's path in a tree may be found
directly using theast_pathmethod, as shown below:>>>root=asts.AST.from_string(..."print(x)",...language=asts.ASTLanguage.Python,...deepest=True...)>>>x=root.lookup(['ARGUMENTS',('CHILDREN',0)])>>>x.source_text'x'>>>root.ast_path(x)['ARGUMENTS',('CHILDREN',0)]Finally, parent trees may be accessed using theparentandparentsmethods, as shown below.  Please note that these methods require the
root of the subtree as a parameter.>>>root=asts.AST.from_string(..."print(x)",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root.children[<asts.types.PythonIdentifier0x4>,<asts.types.PythonArgumentList10x5>]>>>root.children[0].source_text'print'>>>identifier=root.children[1].children[0]>>>identifier.source_text'x'>>>identifier.parent(root).source_text'(x)'>>>[p.source_textforpinidentifier.parents(root)]['(x)','print(x)']Pattern MatchingIn Python 3.10+, AST types and properties may be used inpattern matchingfor conditional logic and to destructure
an AST into its components parts.  For instance, to match
against assignments where the left-hand side of the assignment
is the variable "x", the following match clause may be used:>>>root=asts.AST.from_string(..."x = 1",...language=asts.ASTLanguage.Python,...deepest=True...)>>>matchroot:...caseasts.types.PythonAssignment(left=asts.types.IdentifierAST(source_text="x")):...print("Match found")...MatchfoundAs another example, to destructure the left-hand and right-hand sides
of an assignment into separate variables, the following match
clause may be used:>>>root=asts.AST.from_string(..."x = 1",...language=asts.ASTLanguage.Python,...deepest=True...)>>>matchroot:...caseasts.types.PythonAssignment(left=lhs,right=rhs):...[lhs,rhs]...[<asts.types.PythonIdentifier0x4>,<asts.types.PythonInteger0x5>]Source LocationsFor some applications, it may be useful to know the start/end locations
of each AST or retrieve the AST at a given location.  Clients may do
both using theast_source_rangesandast_at_pointmethods
respectively, as shown below.  Please note that for each method the
lines and columns are 1-indexed.>>>root=asts.AST.from_string(..."print(x)",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root.ast_source_ranges()[(<asts.types.PythonCall0x3>,((1,1),(1,9))),(<asts.types.PythonIdentifier0x4>,((1,1),(1,6))),(<asts.types.PythonArgumentList10x5>,((1,6),(1,9))),(<asts.types.PythonIdentifier0x6>,((1,7),(1,8)))]>>>root.ast_at_point(1,7).source_text'x'FunctionsFunction ASTs have special consideration in the python API, and clients
may retrieve various function attributes, such as name, parameters, and
body, using the respective AST methods,function_name,function_parameters, andfunction_body, as shown below:>>>root=asts.AST.from_string(..."def foo(bar: int) -> int:\nreturn bar / 2",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root.function_name()'foo'>>>[param.source_textforparaminroot.function_parameters()]['bar: int']>>>root.function_body().source_text'    return bar / 2'Function CallsitesIn addition to function ASTs, function callsites also have special
consideration in the python API.  Clients may query for the library
containing the function implementation (provided_by), the function
portion of the callsite (call_function), and the callargs
(call_arguments).  An example incorporating these methods is shown
below:>>>root=asts.AST.from_string(..."import json\njson.dumps({})",...language=asts.ASTLanguage.Python...)>>>callsite=root.children[-1].children[-1]>>>callsite.provided_by(root)'json'>>>callsite.call_function().source_text'json.dumps'>>>[callarg.source_textforcallargincallsite.call_arguments()]['{}']AST TraversalASTs may be explictly traversed in pre-order using thetraversemethod
which creates a generator that may be used anywhere a pythoniterableis required.  An example usage is shown below:>>>root=asts.AST.from_string("x + 88",language=asts.ASTLanguage.Python)>>>forainroot.traverse():...print(a)<asts.types.PythonModule0x1><asts.types.PythonExpressionStatement00x2><asts.types.PythonBinaryOperator0x3><asts.types.PythonIdentifier0x4><asts.types.PythonAdd0x5><asts.types.PythonInteger0x6>Additionally, AST objects are themselves iterators and may be used
anywhere a pythoniterableis required, as shown below:>>>root=asts.AST.from_string("x + 88",language=asts.ASTLanguage.Python)>>>forainroot:...print(a)<asts.types.PythonModule0x1><asts.types.PythonExpressionStatement00x2><asts.types.PythonBinaryOperator0x3><asts.types.PythonIdentifier0x4><asts.types.PythonAdd0x5><asts.types.PythonInteger0x6>As expected, ASTs may be also be used in list comprehensions as shown:>>>root=asts.AST.from_string("x + 88",language=asts.ASTLanguage.Python)>>>ids=[aforainrootifisinstance(a,asts.types.PythonIdentifier)]>>>len(ids)1AST ManipulationASTs may also be manipulated (mutated) using the python API.  Mutation
operations create a new AST distinct from the inputs.  The input ASTs
may continue to be used as before; however, they are unchanged objects
distinct from the AST(s) created.Mutation PrimitivesCurrently, clients may cut, insert, or replace AST subtrees, as shown. The
operations are based on AST serial numbers; we find the AST in the root node
passed as the first parameter with the same serial number as the point
modify and perform the corresponding operation.CUT:>>>root=asts.AST.from_string("x = 2\n",language=asts.ASTLanguage.Python)>>>stmt=root.children[0]>>>root=asts.AST.cut(root,stmt)>>>root.source_text''INSERT:>>>root=asts.AST.from_string(..."y = 3\n",...language=asts.ASTLanguage.Python...)>>>stmt=root.children[0]>>>new_stmt=asts.AST.from_string(..."x = 2\n",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root=asts.AST.insert(root,stmt,new_stmt)>>>root.source_text'x = 2\ny = 3\n'REPLACE:>>>root=asts.AST.from_string(..."x = 2\n",...language=asts.ASTLanguage.Python...)>>>literal=root.children[0].children[0].children[-1]>>>new_literal=asts.AST.from_string(..."3",...language=asts.ASTLanguage.Python,...deepest=True...)>>>root=asts.AST.replace(root,literal,new_literal)>>>root.source_text"x = 3\n"As a useful shortcut, for small mutations, literals may be passed as the
values for insertion and replacement, as shown below:>>>root=asts.AST.from_string(..."x = 2\n",...language=asts.ASTLanguage.Python...)>>>literal=root.children[0].children[0].children[-1]>>>root=asts.AST.replace(root,literal,3)>>>root.source_text"x = 3\n"Finally, AST paths may be used as mutation point parameters to cut,
insert, and replace, as shown below:>>>root=asts.AST.from_string(..."x = 2\n",...language=asts.ASTLanguage.Python...)>>>literal_path=[('CHILDREN',0),('CHILDREN',0),'RIGHT']>>>root=asts.AST.replace(root,literal_path,3)>>>root.source_text"x = 3\n"TransformersIn addition to simple mutation primitives, the API also supports walking
the AST tree and performing transformations on the nodes within.  This
mode of mutation requires the client to define atransformerfunction
which takes an AST node parameter as input and (optionally) returns an
AST or literal which should replace that node in the new tree.  If no
new node is provided by the transformer function, the node is not
replaced in the newly created tree.For instance, consider a scenario where you wish to rewrite an AST,
replacing allxidentifiers withy.  To begin, you would define
anx_to_ytransformer function, as shown below:>>>defx_to_y(ast:asts.AST)->Optional[asts.LiteralOrAST]:..."""Convert 'x' identifier ASTs to 'y'."""...ifisinstance(ast,asts.types.IdentifierAST)and"x"==ast.source_text:...returnasts.AST.from_string("y",ast.language,deepest=True)...As you can see, this function returns ayidentifier when it encounters
anxidentifier.  To use the transformer to replace nodes in an AST tree,
you would use theAST.transformfunction, as shown below:>>>text="""... x = 1... print(x)... """>>>root=asts.AST.from_string(text,asts.ASTLanguage.Python)>>>print(root.source_text.strip())x=1print(x)>>>transformed=asts.AST.transform(root,x_to_y)>>>print(transformed.source_text.strip())y=1print(y)In the example above, thex_to_ytransformer returned an AST.  The
transformer function may also return a literal value, which will be
parsed as an AST.  For instance, the belowx_to_ytransformer is
functionally equivalent to the example above:>>>defx_to_y(ast:asts.AST)->Optional[asts.LiteralOrAST]:..."""Convert 'x' identifier ASTs to 'y'."""...ifisinstance(ast,asts.types.IdentifierAST)and"x"==ast.source_text:...return"y"...Additionally, when using Python 3.10+, you may usepattern matchingto further simplify the implementation of thex_to_ytransformer,
as shown below:>>>defx_to_y(ast:asts.AST)->Optional[asts.LiteralOrAST]:..."""Convert 'x' identifier ASTs to 'y'."""...matchast:...caseasts.types.IdentifierAST(source_text="x"):...return"y"...Transformers may implement more complicated logic than the simplex_to_ytransform above.  For instance, one may wish to convertxidentifiers toy, but only for the left-hand side of assignment
statements, as shown below:>>>defx_to_y_assignment_lhs(ast:asts.AST)->Optional[asts.LiteralOrAST]:..."""Convert 'x' identifier ASTs to 'y' on the lhs of assignments."""...if(...isinstance(ast,asts.types.PythonAssignment)...and"x"==ast.left.source_text...):...returnasts.AST.copy(ast,left="y")...>>>text="""... x = 1... print(y)... """>>>root=asts.AST.from_string(text,asts.ASTLanguage.Python)>>>transformed=asts.AST.transform(root,x_to_y_assignment_lhs)>>>print(transformed.source_text.strip())y=1print(y)As before, thex_to_y_assignment_lhstransformer may be simplified
usingpattern matchingin Python 3.10+, as shown below:>>>defx_to_y_assignment_lhs(ast:asts.AST)->Optional[asts.LiteralOrAST]:..."""Convert 'x' identifier ASTs to 'y' on the lhs of assignments."""...matchast:...caseasts.types.PythonAssignment(left=asts.types.IdentifierAST(source_text="x")):...returnasts.AST.copy(ast,left="y")...For these more complicated transforms, you may need to mutate the parent
of the node you are interested in.  For instance, above we create a
new python assignment node with the left-hand side modified to the
value we want.Tying everything together, consider the case where wish to delete
allprintstatements from an AST.  This may be accomplished by
first defining a predicate for print statements, as shown below:>>>defis_print_statement(ast:asts.AST)->bool:..."""Return TRUE if AST is an statement calling the print function."""...ifisinstance(ast,asts.types.ExpressionStatementAST):...fn_calls=[c.call_function().source_textforcinasts.utility.call_asts(ast)]...return"print"infn_calls...returnFalse...Once this predicate is in place, we may use it to define a transformer which
returns a node with theprintstatements immediately below it elided:>>>defdelete_print_statements(ast:asts.AST)->Optional[asts.LiteralOrAST]:..."""Delete all print statements from the children of AST."""...ifisinstance(ast,(asts.types.RootAST,asts.types.CompoundAST)):...# Build a list of new, non-comment children directly under the AST,...# eliding print statements....new_children=[...cforcinast.child_slot("children")...ifnotis_print_statement(c)...]......# Special case; if no children remain, add a "pass" statement nop to...# avoid syntax errors....new_children=new_childrenifnew_childrenelse["pass\n"]......# Create the new node with print statements removed from the children....returnasts.AST.copy(ast,children=new_children)...Finally, as before, we may use thedelete_print_statementstransformer
to mutate an AST, as shown below:>>> text = """
... x = 1
... y = 2
... if x > 1:
...     x = x ** y
...     print("Test One: %d" % x)
... else:
...     print("Test Two: %d" % x)
... print("y = %d", y)
... """
>>> root = asts.AST.from_string(text, asts.ASTLanguage.Python)
>>> transformed = asts.AST.transform(root, delete_print_statements)
>>> print(transformed.source_text.strip())
x = 1
y = 2
if x > 1:
    x = x ** y
else:
    passArchitectureThe python library is a thin wrapper around a Common Lisp program namedtree-sitter-interfacewhich calls the required pieces of the
Software Evolution Library (SEL).  Most API calls are delegated to
this interface which we communicate with using JSON formatted input/
output over stdio/stdout or a socket.The python AST objects contain a oid attribute representing an
object id (oid) on the Common Lisp side of the interface; in essence,
the python ASTs are pointers to Common Lisp memory locations.  When
calling a python AST method, the oid is serialized to the Common Lisp
side of the interface where the underlying AST object is found
(dereferenced) and the operation performed.  You may get the object id
using theoidproperty on python ASTs; to test for python AST
equality, we check to see if the ASTs point to the same object using
the oids.Additionally, python AST objects contain a serial number attribute
representing a serial number on the Common Lisp side of the interface.
This serial number is used when performing modifications (mutations)
to an AST such as cut/replace/insert. To perform these modifications,
we find the AST in the root with the same serial number as the
point to modify and perform the operation. Serial numbers are stable
in the unmodified, original tree across operations; however, the
serial numbers of values inserted into the tree will differ from the
AST given to insert.To allow for garbage collection, the ASTs are manually reference
counted.  Whenever a python AST (pointer) is created, the reference
count for the associated Common Lisp AST is incremented.  Similarly,
as python ASTs are garbage collected the reference counter is
decremented.  When the reference counter reaches zero, the Common Lisp
AST is released and may be garbage collected at the appropriate time.
To get the reference count for a particular python AST, you may use
theast_ref_countmethod.The underlying Common Lisp ASTs are themselves treated as immutable.
Therefore, when performing mutation operations (e.g. cut, replace,
insert), new ASTs are created in the process.FAQASTs package does not faithfully reproduce original textThe source text property of an AST created using the ASTs package
may not match the original text given to the parser, even if no
mutations are performed.  An automatic source code formatter, such
asblackorclang-format, may be used on the original source
text and the source text attribute of the parsed AST before printing
to ensure consistency if this is required.  This flexibility in
parsing allows for us to perform structured mutation operations -
for instance, automatically inserting a comma when an item is
added to an initializer list in C++.ASTs package inserts/deletes characters upon mutation.As part of a mutation, the ASTs package may insert or delete whitespace
or separator characters (e.g. commas) between ASTs.  One common
idiom is to use an automatic source code formatter, such asblackorclang-format, on the source text after mutation to ensure
consistency before printing to screen or disk.ASTs package throws "Unable to match ... on on AST of type ..."This error occurs after a mutation when an AST is inserted or
replaces another and the type of the new AST does not match that
expected by the tree-sitter grammer.  These errors are often subtle
and may be tricky to diagnose.  As an example, consider the following:>>>func=asts.AST.from_string("print(a)",asts.ASTLanguage.Python,deepest=True)>>>type(func)<class'asts.types.PythonCall'>In the above snippet, we create afuncAST representing a call to the print
function.  Consider now that we wish to replace theprintcall with something
different, sayfoo.  We may believe the following will perform
the operation:>>>foo=asts.AST.from_string("foo",asts.ASTLanguage.Python)>>>new_func=asts.AST.copy(func,function=foo)>>>new_func.source_textTraceback(mostrecentcalllast):File"<stdin>",line1,in<module>File".../Python-3.8.12/lib/python3.8/functools.py",line967,in__get__val=self.func(instance)File".../python/asts/asts.py",line228,insource_textreturn_interface.dispatch(AST.source_text.func.__name__,self)File".../python/asts/asts.py",line589,indispatchreturndeserialize(handle_errors(json.loads(response.decode())))File".../python/asts/asts.py",line539,inhandle_errorsraiseASTException(data["error"])asts.asts.ASTException:Anunhandlederrorconditionhasbeensignalled:Unabletomatch(SEQ(FIELDPYTHON-FUNCTIONPYTHON-PRIMARY-EXPRESSION)(FIELDPYTHON-ARGUMENTSPYTHON-GENERATOR-EXPRESSIONPYTHON-ARGUMENT-LIST))onASToftypePYTHON-CALLHowever, this does not work properly.  If we look at the type offooand the type
of the function child slot ofnew_func, we can see as to why.>>>type(foo)<class'asts.types.PythonModule'>>>>type(new_func.child_slot("function"))<class'asts.types.PythonModule'>In this case, we attempt to insert aPythonModuleAST as the function child
slot offunc.  APythonModuleAST is a "root" AST type, associated with an
entire python file, replete with imports, functions, etc.  This AST type does
not make sense as the "function" portion of a call AST; in this case,
we want an identifier AST.  We can get the identifier by passing thedeepestkeyword during AST creation, retrieving the deepest subnode still
encompassing all of the given source text, as shown below:>>>foo=asts.AST.from_string("foo",asts.ASTLanguage.Python,deepest=True)>>>type(foo)<class'asts.types.PythonIdentifier'>>>>new_func=asts.AST.copy(func,function=foo)>>>new_func.source_text'foo(a)'What is the difference between the "children" property and the "children" child slot?An AST is composed of several child slots, the arity of which may
be 0 (zero or more) or 1 (single AST).  For instance, in the AST
forprint(a), there are two child slots,[['FUNCTION', 1], ['ARGUMENTS', 1]],
allowing for destructuring into component parts of the AST.In addition, internally, each AST has private before/after/internal-AST
slots for storing comments, errors, and internal whitespace that appears
in the source code.  For instance, in the AST for the function body below,
the block AST has the comment# This is a commentstored in the internal
before-AST slot.deffoo():# This is a commentreturn0Thechildrenproperty on AST will return the concatenated list of
component slots of an AST and any comments included in each AST's
internal before or after AST slot.  The order of the ASTs will
match the order they appear in the source text.Thechildrenchild slot on an AST will return all ASTs not
assigned to an explicit slot (e.g. 'FUNCTION', 'ARGUMENTS' above)
and will NOT include any comment ASTs stored internally in an
AST's before/internal/after-AST slots.When using the ASTcopymethod, care should be taken when
using thechildrenkey (e.g.AST.copy(ast, children=...)).
In this case, we are setting thechildrenslot and not the
property.  Therefore, in almost all cases, we will want to
use the children slot, not property, of another AST to populate
the keyword argument (e.g.AST.copy(ast, children=other.child_slot("children"))).
If the property is utilized and a comment is added to the copy
AST's children slot, the error described above in "ASTs package
throws 'Unable to match...'" will occur.Why does every AST has a child slot named "children"?To begin, lets consider the AST forprint(a); for this AST there
are two explicit child slots not namedchildren:[['FUNCTION', 1], ['ARGUMENTS', 1]].  All ASTs not assigned to an
explicit child slot on an AST (e.g. "FUNCTION" or "ARGUMENTS"
previously) are assigned to the "catchall" slot named "children".
Additionally, for ASTs with no named slots (e.g. function
bodies), all of the ASTs are stored in the slot named "children".
In essence, the slot serves as a fall-thru place to store all
ASTs not assigned to an explicit, named slot.ASTs package throws "tree-sitter-interface crashed"This error is raised when the Common Lisp binary (tree-sitter-interface)
backing the ASTs python package crashes.  If available, the error will
report the standard output and standard error streams for thetree-sitter-interfaceprocess prior to the crash.What is the difference between object ID (oid) and serial number?Python AST objects contain a oid attribute representing an
object id (oid) on the Common Lisp side of the interface; in essence,
the python ASTs are pointers to Common Lisp memory locations.  When
calling a python AST method, the oid is serialized to the Common Lisp
side of the interface where the underlying AST object is found
(dereferenced) and the operation performed.Additionally, python AST objects contain a serial number attribute
representing a serial number on the Common Lisp side of the interface.
This serial number is used when performing modifications (mutations)
to an AST such as cut/replace/insert. To perform these modifications,
we find the AST in the root with the same serial number as the
point to modify and perform the operation. Serial numbers are stable
in the unmodified, original tree across operations; however, the
serial numbers of values inserted into the tree will differ from the
AST given to insert.Two objects with the same oid will always the same serial number.
The converse does not hold.LicenseGPLv3+