esprima icon indicating copy to clipboard operation
esprima copied to clipboard

Parsing error with unary and exponentiation operator

Open stonegray opened this issue 6 years ago • 0 comments

Esprima does not seem to correctly parse the following code.

Steps to reproduce

require('esprima').parse('(+2)**4');

Actual output


/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:1995
	        throw this.unexpectedTokenError(token, message);
	        ^
Error: Line 1: Unexpected token **
    at ErrorHandler.constructError (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:5012:22)
    at ErrorHandler.createError (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:5028:27)
    at Parser.unexpectedTokenError (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:1985:39)
    at Parser.throwUnexpectedToken (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:1995:21)
    at Parser.consumeSemicolon (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:2297:23)
    at Parser.parseExpressionStatement (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:3649:15)
    at Parser.parseStatement (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:4112:39)
    at Parser.parseStatementListItem (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:3393:31)
    at Parser.parseScript (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:4723:29)
    at Object.parse (/Volumes/User/.npm-packages/lib/node_modules/esprima/dist/esprima.js:122:61) {
  index: 4,
  lineNumber: 1,
  description: 'Unexpected token **'
}


The error does not occur for 2**(+4), so it appears to be specific to the base.

The error does not occur for (1+2)**4, so it appears to be specific to the unary operator.

The same error does occur for the following similar expressions: (-2)**4, (~~2)**4

Relevant references

Unary operator: 12.5.6.1

Exponentiation operator: 12.6.4

stonegray avatar Jul 16 '19 22:07 stonegray