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:

  1. $ node
  2. > var esprima = require('esprima')
  3. > esprima.tokenize('x.if = 1')
  4. [ { type: 'Identifier', value: 'x' },
  5. { type: 'Punctuator', value: '.' },
  6. { type: 'Keyword', value: 'if' },
  7. { type: 'Punctuator', value: '=' },
  8. { 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.

  1. $ node
  2. > var esprima = require('esprima')
  3. > esprima.parseScript('x.if = 1').body[0].expression.left.property
  4. Identifier { type: 'Identifier', name: 'if' }