Parser

Core functionality for parsing CMake code.

cmake_parser.parser.parse_raw(data, skip_comments=False)

Parse CMake code and return a simplified AST.

The simplified AST is a list of Command and possibly Comment nodes. In particular, hierarchical structures such as if() or function() blocks are not resolved and its constituents returned as unrelated Command nodes.

Unlike parse_tree(), this function will happily parse and return ASTs for structurally broken code, such as block() statements without corresponding endblock(), as long as the individual commands remain valid, i.e., have valid names and no unbalanced parentheses, quotes, or brackets.

Parameters:
  • data (str) – a string containing CMake code

  • skip_comments (bool) – if True, omit any Comment nodes from the output

Return type:

Generator[AstNode, None, None]

Returns:

a generator that yields AST nodes

Raises:

CMakeParseError if the CMake code is not syntactically valid

cmake_parser.parser.parse_tree(data, skip_comments=False)

Parse CMake code and return a fully constructed AST.

Unlike parse_raw(), this function will resolve block structures such as function() definitions and if() conditionals and return specialized AST nodes for them. Therefore, it requires not only individual commands be valid, but the whole structure must be well-formed.

Parameters:
  • data (str) – a string containing CMake code

  • skip_comments (bool) – if True, omit any Comment nodes from the output

Return type:

Generator[AstNode, None, None]

Returns:

a generator that yields AST nodes

Raises:

CMakeParseError if the CMake code is not syntactically valid