0.0.8 • Published 8 years ago
peg-cmake v0.0.8
peg-cmake
peg-cmake is a simple CMake parser written in javascript. It aims to provide a core lib for tools such as formatting, refactoring, etc...
The library is under developement and not stable.
Usage
npm install peg-cmake
var CMake = require('peg-cmake');
var fs = require('fs');
function traversAST(ast, matcher) {
ast.forEach((element)=>{
if(matcher[element.type])
matcher[element.type](element);
});
}
fs.readFile('CMakeLists.txt', 'utf8', function (err,data) {
if (!err) {
try {
var ast = CMake.parse(data);
// print all functions defined in the script
function FunctionsVisitor() {
this._func = (elt)=>{
console.log("- " +elt.identifier + " : line:" + elt.location.start.line )
}
this.macro = this._func;
this.function = this._func;
this.if=(elt)=>{
traversAST(elt.body, this);
elt.elseif.foreach((e)=>{traversAST(elt.body, this);});
if(elt.else)traversAST(elt.else.body, this);
}
};
traversAST(result, new FindFunctions());
}catch(e) {
console.log(e.name + " line " + e.location.start.line
+ ", " + e.location.start.column
+ "\n"+ e.message);
}
}
});
AST
The the parse result is an array of node.
Each node owns at least a type
which can be one of the following :
"bracket_argument"
"bracket_comment"
"command_invocation"
"foreach"
"function"
"identifier"
"if"
"line_comment"
"macro"
"newline"
"quoted_argument"
"script"
"unquoted_argument"
"while"
and a location :
{ "start": { "offset": 31, "line": 1, "column": 32 }, "end": { "offset": 43, "line": 1, "column": 44 } }
message("1st Arg\n" #[Bracket Comment] "2nd Arg")
Release notes
0.0.6
- fix :
CMake versions prior to 2.8.12 silently accept an Unquoted Argument or a Quoted Argument immediately following a Quoted Argument and not separated by any whitespace. For compatibility, CMake 2.8.12 and higher accept such code but produce a warning.
- add experimental cmake formater
Known Issues
- Thes buffer must end with "\n", as a workaround the parse function append a new line to the buffer passed as argument.