node-c-lexer

所属分类:C/C++基础
开发工具:JavaScript
文件大小:12KB
下载次数:0
上传日期:2017-07-02 13:14:26
上 传 者sh-1993
说明:  NodeJS中C编程语言的词法分析器库
(Lexical analyzer library for C programming language in NodeJS)

文件列表:
index.js (49, 2017-06-15)
lib (0, 2017-06-15)
lib\cpp-unit.js (3432, 2017-06-15)
lib\lex-unit.js (34234, 2017-06-15)
lib\node-c-lexer.js (143, 2017-06-15)
lib\token-rules.js (4639, 2017-06-15)
package.json (806, 2017-06-15)
test (0, 2017-06-15)
test\cpp_unit (0, 2017-06-15)
test\cpp_unit\cases (0, 2017-06-15)
test\cpp_unit\cases\case_1.c (28, 2017-06-15)
test\cpp_unit\cases\case_1.c.pp (28, 2017-06-15)
test\cpp_unit\cases\case_2.c (61, 2017-06-15)
test\cpp_unit\cases\case_2.c.pp (46, 2017-06-15)
test\cpp_unit\cases\case_3.c (259, 2017-06-15)
test\cpp_unit\cases\case_3.c.pp (241, 2017-06-15)
test\cpp_unit\cases\case_4.c (225, 2017-06-15)
test\cpp_unit\cases\case_4.c.pp (195, 2017-06-15)
test\cpp_unit\cpp_unit.js (4797, 2017-06-15)
test\lex_unit (0, 2017-06-15)
test\lex_unit\cases (0, 2017-06-15)
test\lex_unit\cases\case_1.c.pp (28, 2017-06-15)
test\lex_unit\cases\case_2.c.pp (46, 2017-06-15)
test\lex_unit\cases\case_3.c.pp (241, 2017-06-15)
test\lex_unit\cases\case_4.c.pp (519, 2017-06-15)
test\lex_unit\lex_unit.js (2271, 2017-06-15)
test\mocha.opts (12, 2017-06-15)

# node-c-lexer Lexical analyzer library for C programming language in NodeJS ### Usage There are two units in the library - preprocessing unit and lexical analyzing unit. These two units can be used independently. Purpose of including preprocessing unit within the library is so that user can remove the preprocessor with it and then feed it to scanning unit to get token stream. ##### Preprocessing Unit To use preprocessing unit C code needs to be resided in a file. ```js var lexer = require("node-c-lexer"); lexer.cppUnit.clearPreprocessors("./a.c", function(err, codeText){ if(err){ /* Some error occured */ } else{ /* Do what you want to do with preprocessor free code text */ } }); ``` The clearPreprocessors method by default invokes `cpp` on the first arguement, producing an intermediate preprocessed file. It then finalizes the output by stripping the output of the preprocessor from the intermediate file and passing it to your callback as codeText. If you already have preprocessed files (.ii) at hand, you can skip the preprocessing step by passing the path to your preprocessed file as your last arguement. A reason why would want to do something like this is if you, for instance, have a separate environment on which your .ii(s) are generated(e.g preprocessed files are produced within your windows bash environment). Or if your preprocessing pass diverges from the execution of a simple 'cpp' command in general. ```js var lexer = require("node-c-lexer"); lexer.cppUnit.clearPreprocessors("./a.c", function(err, codeText){ if(err){ /* Some error occured */ } else{ /* Do what you want to do with preprocessor free code text */ } }, "./a.ii"); ``` ##### Scanning Unit ```js var lexer = require("node-c-lexer"); var tokenStream = lexer.lexUnit.tokenize(codeText); /* Now do what you want with token stream */ ``` Token stream is actually an array of tokens. Single token is a single javascript object. Format of a single token is following ```js { "lexeme": "func", "row": 5, "col": 3, "tokenClass": "IDENTIFIER", "keyword": False, "parent": null, "child": null } ``` ```parent``` and ```child``` these two are kept so that parse tree can be built using the tokens as nodes.

近期下载者

相关文件


收藏者