Limitation on Keywords
Since a tokenization process does not have the context of the syntactic structure, it is unable to infer properly that a particular reserved word is being used not as a keyword. Therefore, it always classifies a reserved word as keyword. A simple example to illustrate this limitation:
- $ node
- > var esprima = require('esprima')
- > esprima.tokenize('x.if = 1')
- [ { type: 'Identifier', value: 'x' },
- { type: 'Punctuator', value: '.' },
- { type: 'Keyword', value: 'if' },
- { type: 'Punctuator', value: '=' },
- { type: 'Numeric', value: '1' } ]
In the above session, the type of the if
token is Keyword
.
This is however different than what will be obtained using Esprima parser since the parser correctly matches the if
token as an object property and therefore constructs an associated Identifier
node, not a Keyword
node.
- $ node
- > var esprima = require('esprima')
- > esprima.parseScript('x.if = 1').body[0].expression.left.property
- Identifier { type: 'Identifier', name: 'if' }