A small ECMAScript parser, tokenizer and minifier written in JavaScript.
- Supports for ECMAScript 6
- Supports for Abstract Syntax Tree (JavaScript AST)
<script src="chiffon.js"></script>
or
<script src="chiffon.min.js"></script>
Object Chiffon will defined in the global scope.
npm install chiffon
var Chiffon = require('chiffon');
bower install chiffon
Parse a string source.
The result will be an abstract syntax tree (JavaScript AST) object.
(JavaScript AST is specified by the ESTree spec.)
- {Object} Chiffon.parse ( source [, options ] )
@param {string} source Target source.
@param {Object} [options] Parse options.
@return {Object} Return an abstract syntax tree object.
Example:
var ast = Chiffon.parse('1 + 1');
console.log(ast);
/*
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "BinaryExpression",
"operator": "+",
"left": {
"type": "Literal",
"value": 1,
"raw": "1"
},
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
}
}
]
}
*/
-
range : {boolean} default=false
Include an index-based location range. (array) -
loc : {boolean} default=false
Include line number and column-based location info.
Full options are following.
var options = {
range: Boolean,
loc: Boolean
};
Tokenize a string source.
- {Array} Chiffon.tokenize ( source [, options ] )
@param {string} source Target source.
@param {Object} [options] Tokenize options.
@return {Array} Return an array of the parsed tokens.
var tokens = Chiffon.tokenize('var a = 1');
console.log(tokens);
/*
[ { type: 'Keyword', value: 'var' },
{ type: 'Identifier', value: 'a' },
{ type: 'Punctuator', value: '=' },
{ type: 'Numeric', value: '1' } ]
*/
- Comment
- WhiteSpace
- LineTerminator
- Template
- String
- Punctuator
- RegularExpression
- Numeric
- Identifier
- Null
- Boolean
- Keyword
-
comment : {boolean} default=false
Keep comment tokens. -
whiteSpace : {boolean} default=false
Keep white space tokens. -
lineTerminator : {boolean} default=false
Keep line terminator tokens. -
range : {boolean} default=false
Include an index-based location range. (array) -
loc : {boolean} default=false
Include line number and column-based location info.
Full options are following.
var options = {
comment: Boolean,
whiteSpace: Boolean,
lineTerminator: Boolean,
range: Boolean,
loc: Boolean
};
Concatenate to string from the parsed tokens.
- {string} Chiffon.untokenize ( tokens )
@param {Array} tokens An array of the parsed tokens.
@param {Object} [options] Untokenize options.
@return {string} Return a concatenated string.
- unsafe : {boolean} (default=false)
Untokenizer does not add a space between the identifier and identifier.
Tokens can return to the original string by using the untokenize with these options.
var source = 'var a = 1, b = 2; // comment';
var tokens = Chiffon.tokenize(source, {
comment: true,
whiteSpace: true,
lineTerminator: true
});
var result = Chiffon.untokenize(tokens, { unsafe: true });
console.log(result === source); // true
Minify JavaScript source.
- {string} Chiffon.minify ( source )
@param {string} source Target source.
@param {Object} [options] minify options.
@return {string} Return a minified source.
var min = Chiffon.minify('var a = 1 + 1; // comment');
console.log(min); // 'var a=1+1;'
- maxLineLen : {number} default=32000
Limit the line length in symbols.
MIT