| Travis CI (Ubuntu) | AppVeyor (Windows) | Coverage | PyPI | Licence |
|---|---|---|---|---|
cmake-ast has been tested against every single CMake module that ships with
recent versions of CMake. These tests also run in the continuous integration
environment on each build. It supports multi-line strings and other corner
cases.
Import cmakeast and ASTify the contents of a cmake file with
cmakeast.ast.parse(contents). You can also pass it a list of tokens obtained
by tokenization with the tokens keyword argument. The return will be a
toplevel node, with node descriptions as follows:
Word
- (One)
Typetype: Variable | String | Number | CompoundLiteral | VariableDereference - (One)
Stringcontents
Body
- (Many) (
FunctionCall,IfStatement,ForeachStatement,WhileStatement)
FunctionCall
- (One)
Wordname - (Many)
Wordarguments
FunctionDefinition
- (One)
FunctionCallheader - (One)
Bodybody - (One)
FunctionCallfooter
MacroDefinition
- (One)
FunctionCallheader - (One)
Bodybody - (One)
FunctionCallfooter
IfStatement
- (One)
FunctionCallheader - (One)
Bodybody
ElseIfStatement
- (One)
FunctionCallheader - (One)
Bodybody
ElseStatement
- (One)
FunctionCallheader - (One)
Bodybody
IfBlock
- (One)
IfStatementif_statement - (Many)
ElseIfStatementelse_ifs - (One Optional)
ElseStatementelse_statement - (One)
FunctionCallfooter
ForeachStatement
- (One)
FunctionCallforeach_function - (One)
Bodybody - (One)
FunctionCallfooter
WhileStatement
- (One)
FunctionCallwhile_function - (One)
Bodybody - (One)
FunctionCallfooter
Each node also has a line and col member to indicate where it can be
found in the source file.
Word type aliases are stored in WordType inside ast.
CMake-AST provides a helper module ast_visitor to make traversing the AST
less verbose. It will traverse every single node by default. Listeners
matching the signature def handler (name, node, depth) can be passed as
the following keyword arguments to recurse (body, **kwargs):
| Keyword | Handles Node Type |
|---|---|
toplevel |
ToplevelBody |
while_stmnt |
WhileStatement |
foreach |
ForeachStatement |
function_def |
FunctionDefinition |
macro_def |
MacroDefinition |
if_block |
IfBlock |
if_stmnt |
IfStatement |
elseif_stmnt |
ElseIfStatement |
else_stmnt |
ElseStatement |
function_call |
FunctionCall |
word |
Word |
If you wish to dump the AST of a cmake file, the cmake-print-ast tool is
also provided. Pass a single filename to dump the AST of to it on the
command line
To get an even lower level representation, use cmakeast.ast.tokenize(contents)
which divides the file only into tokens. Aliases are stored in the TokenType
class in ast. Tokens correspond as follows:
| Token Type | Description |
|---|---|
QuotedLiteral |
Something in quotes |
LeftParen |
( |
RightParen |
) |
Word |
Alphanumeric Sequence |
Number |
Numeric-Only Sequence |
Deref |
Alphanumeric Sequence inside ${} |
RST |
Documentation Comment |
Comment |
Comment |
UnquotedLiteral |
Any character sequence, punctuation included |