Abstract Syntax Tree

Abstract Syntax Tree elements which are returned by parse_raw() and parse_tree().

class cmake_parser.ast.AstFileNode(span, line, column)

Bases: AstNode

Base class for all AST nodes with associated file location

Parameters:
  • line (int) – the line number where the corresponding code begins. Note that the code can span multiple lines.

  • column (int) – the column where the corresponding code begins.

class cmake_parser.ast.AstNode(span)

Bases: object

Base class for all Abstract Syntax Tree elements

Parameters:

span (slice) – the code location in the parsed string.

class cmake_parser.ast.Block(span, line, column, args, body)

Bases: BuiltinBlock

Scoped block.

This node represents a block()/endblock() block.

class cmake_parser.ast.Break(span, line, column)

Bases: BuiltinNoArgs

Loop exit.

This node represents the break() command.

class cmake_parser.ast.Builtin(span, line, column, args)

Bases: AstFileNode

Base class for AST nodes which represent CMake built-in instructions with parseable arguments.

Parameters:

args (List[Token]) – the list of arguments.

Note

As variable expansion can split tokens into multiple arguments or remove them from the argument list altogether, there is no 1:1 relation between tokens and arguments.

class cmake_parser.ast.BuiltinBlock(span, line, column, args, body)

Bases: Builtin

Base class for AST nodes which represent a block of CMake commands

Parameters:

body (List[AstNode]) – the list of commands which form the block.

class cmake_parser.ast.BuiltinNoArgs(span, line, column)

Bases: AstFileNode

Base class for AST nodes which represent CMake built-in instructions which can never have arguments

class cmake_parser.ast.Command(span, line, column, identifier, args)

Bases: AstFileNode

Generic representation of a single CMake instruction.

CMake code consists of a serious of command invocations with a (possibly empty) list of arguments in parentheses. Command and Comment are the two possible outputs of parse_raw().

Parameters:
  • identifier (str) – the function or instruction name to be invoked.

  • args (List[Token]) – the tokens which form the argumen list.

Note

As variable expansion can split tokens into multiple arguments or remove them from the argument list altogether, there is no 1:1 relation between tokens and arguments.

class cmake_parser.ast.Comment(span, line, column, comment)

Bases: AstFileNode

CMake Comment.

This is the only AST node which does not represent executable code. Both parse_raw() and parse_tree() can be instructed to omit comments from their output.

Parameters:

comment (str) – The comment string without leading # or enclosing brackets.

class cmake_parser.ast.Continue(span, line, column)

Bases: BuiltinNoArgs

Loop continuation.

This node represents the continue() command.

class cmake_parser.ast.ForEach(span, line, column, args, body)

Bases: BuiltinBlock

ForEach Loop.

This node represents a foreach()/endforeach() block.

class cmake_parser.ast.Function(span, line, column, args, body)

Bases: BuiltinBlock

Macro definition.

This node represents a function()/endfunction() block.

class cmake_parser.ast.If(span, line, column, args, if_true, if_false)

Bases: Builtin

Conditional block.

This node represents a if()/else()/endif() block. elseif() statements are converted into a single If command in if_false.

Parameters:
  • if_true (List[AstNode]) – the list of commands to be executed if the expression evaluates to True.

  • if_false (Optional[List[AstNode]]) – the list of commands to be executed if the expression evaluates to False.

class cmake_parser.ast.Include(span, line, column, args)

Bases: Builtin

Include other CMake file.

This node represents the include() command.

class cmake_parser.ast.Macro(span, line, column, args, body)

Bases: BuiltinBlock

Macro definition.

This node represents a macro()/endmacro() block.

class cmake_parser.ast.Math(span, line, column, args)

Bases: Builtin

Math expression.

This node represents the math() command.

class cmake_parser.ast.Option(span, line, column, args)

Bases: Builtin

Declare CMake option.

This node represents the option() command.

class cmake_parser.ast.Return(span, line, column, args)

Bases: Builtin

Return from function or module.

This node represents the return() command.

class cmake_parser.ast.Set(span, line, column, args)

Bases: Builtin

Set variable.

This node represents the set() command.

class cmake_parser.ast.Unset(span, line, column, args)

Bases: Builtin

Unset variable.

This node represents the unset() command.

class cmake_parser.ast.While(span, line, column, args, body)

Bases: BuiltinBlock

While Loop.

This node represents a while()/endwhile() block.