Python module to reduce a cmake file to an AST

Related tags

Utilities cmake-ast
Overview

CMake AST

Status

Travis CI (Ubuntu) AppVeyor (Windows) Coverage PyPI Licence
Travis AppVeyor Coveralls PyPIVersionPyPIPythons License

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.

Usage

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) Type type: Variable | String | Number | CompoundLiteral | VariableDereference
  • (One) String contents

Body

  • (Many) (FunctionCall, IfStatement, ForeachStatement, WhileStatement)

FunctionCall

  • (One) Word name
  • (Many) Word arguments

FunctionDefinition

  • (One) FunctionCall header
  • (One) Body body
  • (One) FunctionCall footer

MacroDefinition

  • (One) FunctionCall header
  • (One) Body body
  • (One) FunctionCall footer

IfStatement

  • (One) FunctionCall header
  • (One) Body body

ElseIfStatement

  • (One) FunctionCall header
  • (One) Body body

ElseStatement

  • (One) FunctionCall header
  • (One) Body body

IfBlock

  • (One) IfStatement if_statement
  • (Many) ElseIfStatement else_ifs
  • (One Optional) ElseStatement else_statement
  • (One) FunctionCall footer

ForeachStatement

  • (One) FunctionCall foreach_function
  • (One) Body body
  • (One) FunctionCall footer

WhileStatement

  • (One) FunctionCall while_function
  • (One) Body body
  • (One) FunctionCall footer

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.

Traversing the 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

Dumping the AST of a CMake file

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

Tokenization

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
You might also like...
Python random library for c++

Python random library for c++

Simple .INI file parser in C, good for embedded systems

inih (INI Not Invented Here) inih (INI Not Invented Here) is a simple .INI file parser written in C. It's only a couple of pages of code, and it was d

ini file parser

Iniparser 4 I - Overview This modules offers parsing of ini files from the C level. See a complete documentation in HTML format, from this directory o

A small and portable INI file library with read/write support

minIni minIni is a portable and configurable library for reading and writing ".INI" files. At just below 900 lines of commented source code, minIni tr

Small configuration file parser library for C.

libConfuse Introduction Documentation Examples Build & Install Origin & References Introduction libConfuse is a configuration file parser library writ

Beacon Object File (BOF) for remote process injection via thread hijacking

cThreadHijack ___________.__ .______ ___ .__ __ __ ___\__ ___/| |_________ ____ _____

A Cobalt Strike Beacon Object File (BOF) project which uses direct system calls to enumerate processes for specific loaded modules or process handles.
A Cobalt Strike Beacon Object File (BOF) project which uses direct system calls to enumerate processes for specific loaded modules or process handles.

FindObjects-BOF A Cobalt Strike Beacon Object File (BOF) project which uses direct system calls to enumerate processes for specific modules or process

A Beacon Object File (BOF) for Cobalt Strike which uses direct system calls to enable WDigest credential caching.
A Beacon Object File (BOF) for Cobalt Strike which uses direct system calls to enable WDigest credential caching.

WdToggle A Proof of Concept Cobalt Strike Beacon Object File which uses direct system calls to enable WDigest credential caching and circumvent Creden

Dead simple C logging library contained in a single header (.h) file
Dead simple C logging library contained in a single header (.h) file

Seethe Logging so simple, you only need to include a single header file. seethe supports 6 different log levels (DEBUG, INFO, NOTICE, WARNING, ERROR,

Comments
  • 4YI: CMake interpreter in python

    4YI: CMake interpreter in python

    https://github.com/KOLANICH/CMake.py - just a very incomplete and very dumb and probably very incorrect CMake interpreter in python. I have made it in order to just get CPack variables for my build system.

    opened by KOLANICH 2
  • Merge all subsequent passes into one.

    Merge all subsequent passes into one.

    The basic structure of this change is that each pass is now expressed as a "recorder", one of which can be active at any particular time. The recorder is responsible for marking a begin point of tokens to compress and then once it is done consuming tokens, returning a new list of compressed tokens at that point.

    opened by smspillaz 1
  • Support uppercase commands

    Support uppercase commands

    Hi,

    Thanks for developing this cool module!

    When using cmakeast, I find that it cannot recognize uppercase commands like IF(...) and ENDIF(). They'll be parsed as FunctionCalls instead of IfBlocks. Do you have plans on supporting them? Thanks!

    opened by tandf 0
  • Extract the list of find_package attributes, kvs

    Extract the list of find_package attributes, kvs

    Hi,

    Hope you are all well !

    Is it possible to extract the tree of package dependencies from a CMakeLists.txt and export it into a yaml with cmake-ast ?

    More clearly, is it possible to get all the commands attributes key/value from a standard cmake macro and from custom cmake macros/functions ?

    Custom functions/macros: https://github.com/headupinclouds/gatherer/blob/develop/CMakeLists.txt#L66

    Packages,Components,Version https://github.com/halismai/bpvo/blob/master/CMakeLists.txt#L124 https://github.com/halismai/bpvo/blob/master/CMakeLists.txt#L136

    I would find it useful for building Docker Image for any c++ project, alpine based, more quickly by knowing the list of missing dependencies to add with the apk manager.

    I haven't find any good cmake lexer/parser in go, as I am writing my docker bundler in golang, so I started to check for python based script, neither :-(

    Thanks for any tips or insights about this question ! :-)

    Have a good day !

    Cheers, Richard

    opened by roscopecoltran 1
Owner
ポリ平方 POLYSQUARE
ポリ平方 POLYSQUARE
An asynchronous directory file change watcher module for Windows, macOS and Linux wrapped for V

A V module for asynchronously watching for file changes in a directory. The module is essentially a wrapper for septag/dmon. It works for Windows, macOS and Linux.

null 18 Dec 14, 2022
Simple and lightweight pathname parser for C. This module helps to parse dirname, basename, filename and file extension .

Path Module For C File name and extension parsing functionality are removed because it's difficult to distinguish between a hidden dir (ex: .git) and

Prajwal Chapagain 3 Feb 25, 2022
convert elf file to single c/c++ header file

elf-to-c-header Split ELF to single C/C++ header file

Musa Ünal 2 Nov 4, 2021
Modify Android linker to provide loading module and hook function

fake-linker Chinese document click here Project description Modify Android linker to provide loading module and plt hook features.Please check the det

sanfengAndroid 216 Jan 4, 2023
[WIP] A Riru module tries to enable Magisk hide for isolated processes.

Riru-IsolatedMagiskHider Background Many applications now detect Magisk for security, Magisk provided "Magisk Hide" to prevent detection, but isolated

残页 562 Jan 3, 2023
Documenting the development of a simple first module.

Your First Module This guide will look at writing a complete module, with many common features in a reduced form. This includes the module initialisat

Open Multiplayer 16 Jun 3, 2021
Linux Kernel module-less implant (backdoor)

0 KOPYCAT - Linux Kernel module-less implant (backdoor) Usage $ make $ sudo insmod kopycat.ko insmod: ERROR: could not insert module kopycat.ko: Inapp

Ilya V. Matveychikov 52 Dec 28, 2022
zsh module for automatically compiling sourced files

Zinit Module Motivation The module is a binary Zsh module (think about zmodload Zsh command, it's that topic) which transparently and automatically co

zdharma-continuum 13 Dec 25, 2022
Python bindings for Wasm3, the fastest WebAssembly interpreter

pywasm3 Python bindings for Wasm3, the fastest WebAssembly interpreter Main repository: Wasm3 project Install # Latest release: pip3 install pywasm3

Wasm3 Labs 49 Dec 27, 2022
A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀

A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly ??

Lingdong Huang 587 Jan 7, 2023