$ npm install cherow
In active development!!
A very fast and lightweight, standards-compliant, self-hosted javascript parser with high focus on both performance and stability.
Stage 3
features support. These need to be enabled with the next
option.
Cherow contains 3 different builds:
Name | Description |
---|---|
Stable |
Stable release |
Next |
Has the next option enabled by default, and support all latest ECMAScript proposals. |
Bleeding |
The active development branch. You can and will expect bugs with this branch because it's not stable |
Cherow generates AST according to ESTree AST format, and can be used to perform syntactic analysis (parsing) of a JavaScript program, and with ES2015 and later a JavaScript program can be either a script or a module and this is achieved by choosing parseScript
function to parse a script and parseModule
function to parse a module.
Here is a quick example:
cherow.parseScript('const fooBar = 123;', { ranges: true });
This will return when serialized in json:
{
"type": "Program",
"sourceType": "script",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"init": {
"type": "Literal",
"value": 123,
"start": 15,
"end": 18
},
"id": {
"type": "Identifier",
"name": "fooBar",
"start": 6,
"end": 12
},
"start": 6,
"end": 18
}
],
"kind": "const",
"start": 0,
"end": 19
}
],
"start": 0,
"end": 19
}
There is a second argument to both methods that allows you to specify various options:
Option | Description |
---|---|
delegate |
Accepts a callback function to be invoked for each syntax node (as the node is constructed) |
loc |
Attach line/column location information to each node |
ranges |
Append start and end offsets to each node |
globalReturn |
Allow return in the global scope |
skipShebang |
Allow to skip shebang - '#' |
impliedStrict |
Enable implied strict mode |
next |
Enable stage 3 support (ESNext) |
source |
Set to true to record the source file in every node's loc object when the loc option is set. |
raw |
Attach raw property to each literal node |
In Cherow this replace the traditional plugin system, and let you import the parser instance and develop your own functions with it, and / or re-use existing one.
Example:
import { parser, scanSignedInteger, parseScript, parserModule } from 'cherow';
function doWhatYouWant(parser, context) {
// do some magic with numbers
value = scanSignedInteger(parser, context); / '+', '-'
// ...
}
Note! This Jazzle parser couldn't parse the TypeScript library, so the results is not 100%.
© 2010 - cnpmjs.org x YWFE | Home | YWFE