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 possiblyComment
nodes. In particular, hierarchical structures such asif()
orfunction()
blocks are not resolved and its constituents returned as unrelatedCommand
nodes.Unlike
parse_tree()
, this function will happily parse and return ASTs for structurally broken code, such asblock()
statements without correspondingendblock()
, 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 codeskip_comments (
bool
) – ifTrue
, omit anyComment
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 asfunction()
definitions andif()
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 codeskip_comments (
bool
) – ifTrue
, omit anyComment
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