Metavariables for scripts can only be inherited from transformation rules. In the spirit of scripting languages such as Python that use dynamic typing, metavariables for scripts do not include type declarations.
|script_metavariables||::=||@ script:language [rulename] [depends on dep] @ script_metadecl * @@|
||||@ initialize:language [depends on dep] @|
||||@ finalize:language [depends on dep] @|
|script_metadecl||::=||id << rulename_id.id ;|
Currently, the only scripting languages that are supported are Python and OCaml, indicated using python and ocaml, respectively. The set of available scripting languages may be extended at some point.
Script rules declared with initialize are run before the treatment of any file. Script rules declared with finalize are run when the treatment of all of the files has completed. There can be at most one of each per scripting language (thus currently at most one of each). Initialize and finalize script rules do not have access to SmPL metavariables. Nevertheless, a finalize script rule can access any variables initialized by the other script rules, allowing information to be transmitted from the matching process to the finalize rule.
A script metavariable that does not specify an origin, using <<, is newly declared by the script. This metavariable should be assigned to a string and can be inherited by subsequent rules as an identifier. In Python, the assignment of such a metavariable x should refer to the metavariable as coccinelle.x. Examples are in the files demos/pythontococci.cocci and demos/camltococci.cocci.
In an ocaml script, the following extended form of script_metadecl may be used:
|script_metadecl||::=||(id,id) << rulename_id.id ;|
||||id << rulename_id.id ;|
In a declaration of the form (id,id) <<
rulename_id.id ;, the left component of (id,id)
receives a string representation of the value of the inherited metavariable
while the right component receives its abstract syntax tree. The file
parsing_c/ast_c.ml in the Coccinelle implementation gives some
information about the structure of the abstract syntax tree. Either the
left or right component may be replaced by
_, indicating that the
string representation or abstract syntax trees representation is not
The abstract syntax tree of a metavariable declared using metavariable is not available.