Skip to content

Commit

Permalink
feat: consistent esm syntax err (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 29, 2023
1 parent 182e276 commit 279682c
Show file tree
Hide file tree
Showing 10 changed files with 2,441 additions and 2,466 deletions.
3 changes: 3 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ const minified = MINIFY && terser.minify(jsSourceProcessed, {
}
});

if (minified.error)
throw minified.error;

fs.writeFileSync('./dist/lexer.mjs', minified ? minified.code : jsSourceProcessed);
6 changes: 2 additions & 4 deletions include-wasm/cjs-module-lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ bool ru () {
return true;
}

bool parse (uint32_t point);

void _addExport (const uint16_t* start, const uint16_t* end) {
Slice* export = (Slice*)(analysis_head);
analysis_head = analysis_head + sizeof(Slice);
Expand Down Expand Up @@ -163,7 +161,7 @@ void (*addExport)(const uint16_t*, const uint16_t*) = &_addExport;
void (*addReexport)(const uint16_t*, const uint16_t*) = &_addReexport;
void (*addUnsafeGetter)(const uint16_t*, const uint16_t*) = &_addUnsafeGetter;
void (*clearReexports)() = &_clearReexports;
bool parseCJS (uint16_t* source, uint32_t sourceLen, void (*addExport)(const uint16_t* start, const uint16_t* end), void (*addReexport)(const uint16_t* start, const uint16_t* end), void (*addUnsafeGetter)(const uint16_t*, const uint16_t*), void (*clearReexports)());
uint32_t parseCJS (uint16_t* source, uint32_t sourceLen, void (*addExport)(const uint16_t* start, const uint16_t* end), void (*addReexport)(const uint16_t* start, const uint16_t* end), void (*addUnsafeGetter)(const uint16_t*, const uint16_t*), void (*clearReexports)());

enum RequireType {
Import,
Expand Down Expand Up @@ -237,4 +235,4 @@ void nextChar (uint16_t ch);
void nextCharSurrogate (uint16_t ch);
uint16_t readChar ();

void syntaxError ();
void syntaxError (uint32_t code);
10 changes: 7 additions & 3 deletions lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,10 @@ function codePointAtLast (bPos) {
return ch;
}

function esmSyntaxErr (msg) {
return Object.assign(new Error(msg), { code: 'ERR_LEXER_ESM_SYNTAX' });
}

function throwIfImportStatement () {
const startPos = pos;
pos += 6;
Expand All @@ -1160,7 +1164,7 @@ function throwIfImportStatement () {
return;
// import.meta
case 46/*.*/:
throw new Error('Unexpected import.meta in CJS module.');
throw esmSyntaxErr('Unexpected import.meta in CJS module.');

default:
// no space after "import" -> not an import keyword
Expand All @@ -1176,7 +1180,7 @@ function throwIfImportStatement () {
return;
}
// import statements are a syntax error in CommonJS
throw new Error('Unexpected import statement in CJS module.');
throw esmSyntaxErr('Unexpected import statement in CJS module.');
}
}

Expand All @@ -1186,7 +1190,7 @@ function throwIfExportStatement () {
const ch = commentWhitespace();
if (pos === curPos && !isPunctuator(ch))
return;
throw new Error('Unexpected export statement in CJS module.');
throw esmSyntaxErr('Unexpected export statement in CJS module.');
}

function commentWhitespace () {
Expand Down
Binary file modified lib/lexer.wasm
Binary file not shown.
4,717 changes: 2,287 additions & 2,430 deletions lib/lexer.wat

Large diffs are not rendered by default.

67 changes: 65 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"types": "lexer.d.ts",
"scripts": {
"test-js": "mocha -b -u tdd test/*.js",
"test-wasm": "WASM=1 mocha -b -u tdd test/*.js",
"test-wasm": "cross-env WASM=1 mocha -b -u tdd test/*.js",
"test": "npm run test-wasm && npm run test-js",
"bench": "node --expose-gc bench/index.mjs",
"build": "node build.js && babel dist/lexer.mjs | terser -o dist/lexer.js",
"build": "node build.js ; babel dist/lexer.mjs -o dist/lexer.js ; terser dist/lexer.js -o dist/lexer.js",
"build-wasm": "make lib/lexer.wasm && node build.js",
"prepublishOnly": "make && npm run build",
"footprint": "npm run build && cat dist/lexer.js | gzip -9f | wc -c"
Expand All @@ -27,6 +27,7 @@
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
"cross-env": "^7.0.3",
"kleur": "^2.0.2",
"mocha": "^9.1.3",
"terser": "^4.1.4"
Expand Down
46 changes: 23 additions & 23 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ uint16_t* lastReexportEnd;
// -> source
// -> analysis starts after source
uint32_t parse_error;
bool has_error = false;
uint32_t error = 0;
uint32_t sourceLen;

uint16_t templateStack_[STACK_DEPTH];
Expand All @@ -45,7 +45,7 @@ void (*addUnsafeGetter)(const uint16_t*, const uint16_t*);
void (*clearReexports)();

// Note: parsing is based on the _assumption_ that the source is already valid
bool parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const uint16_t*, const uint16_t*), void (*_addReexport)(const uint16_t*, const uint16_t*), void (*_addUnsafeGetter)(const uint16_t*, const uint16_t*), void (*_clearReexports)()) {
uint32_t parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const uint16_t*, const uint16_t*), void (*_addReexport)(const uint16_t*, const uint16_t*), void (*_addUnsafeGetter)(const uint16_t*, const uint16_t*), void (*_clearReexports)()) {
source = _source;
sourceLen = _sourceLen;
if (_addExport)
Expand All @@ -61,7 +61,7 @@ bool parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const
lastTokenPos = (uint16_t*)EMPTY_CHAR;
lastSlashWasDivision = false;
parse_error = 0;
has_error = false;
error = 0;
templateStack = &templateStack_[0];
openTokenPosStack = &openTokenPosStack_[0];
starExportStack = &starExportStack_[0];
Expand All @@ -74,7 +74,7 @@ bool parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const
// Handle #!
if (*source == '#' && *(source + 1) == '!') {
if (sourceLen == 2)
return true;
return 0;
pos += 2;
while (pos++ < end) {
ch = *pos;
Expand Down Expand Up @@ -155,7 +155,7 @@ bool parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const
break;
case ')':
if (openTokenDepth == 0)
return syntaxError(), false;
return syntaxError(8), error;
openTokenDepth--;
break;
case '{':
Expand All @@ -165,14 +165,14 @@ bool parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const
break;
case '}':
if (openTokenDepth == 0)
return syntaxError(), false;
return syntaxError(2), false;
if (openTokenDepth-- == templateDepth) {
templateDepth = templateStack[--templateStackDepth];
templateString();
}
else {
if (templateDepth != UINT16_MAX && openTokenDepth < templateDepth)
return syntaxError(), false;
return syntaxError(3), error;
}
break;
case '<':
Expand Down Expand Up @@ -219,18 +219,18 @@ bool parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const
}
case '`':
if (templateDepth == UINT16_MAX - 1)
return syntaxError(), false;
return syntaxError(4), error;
templateString();
break;
}
lastTokenPos = pos;
}

if (templateDepth != UINT16_MAX || openTokenDepth || has_error)
return false;
if (templateDepth != UINT16_MAX || openTokenDepth || error)
return error;

// success
return true;
return 0;
}

void tryBacktrackAddStarExportBinding (uint16_t* bPos) {
Expand Down Expand Up @@ -1154,7 +1154,7 @@ void throwIfImportStatement () {
return;
// import.meta
case '.':
syntaxError();
syntaxError(5);
return;

default:
Expand All @@ -1171,7 +1171,7 @@ void throwIfImportStatement () {
return;
}
// import statements are a syntax error in CommonJS
syntaxError();
syntaxError(6);
}
}

Expand All @@ -1181,7 +1181,7 @@ void throwIfExportStatement () {
uint16_t ch = commentWhitespace();
if (pos == curPos && !isPunctuator(ch))
return;
syntaxError();
syntaxError(7);
}

uint16_t commentWhitespace () {
Expand Down Expand Up @@ -1218,7 +1218,7 @@ void templateString () {
if (ch == '\\')
pos++;
}
syntaxError();
syntaxError(8);
}

void blockComment () {
Expand Down Expand Up @@ -1253,7 +1253,7 @@ void stringLiteral (uint16_t quote) {
else if (isBr(ch))
break;
}
syntaxError();
syntaxError(9);
}

uint16_t regexCharacterClass () {
Expand All @@ -1266,7 +1266,7 @@ uint16_t regexCharacterClass () {
else if (ch == '\n' || ch == '\r')
break;
}
syntaxError();
syntaxError(10);
return '\0';
}

Expand All @@ -1282,7 +1282,7 @@ void regularExpression () {
else if (ch == '\n' || ch == '\r')
break;
}
syntaxError();
syntaxError(11);
}

uint16_t readToWsOrPunctuator (uint16_t ch) {
Expand Down Expand Up @@ -1503,14 +1503,14 @@ bool isExpressionTerminator (uint16_t* curPos) {
return false;
}

void bail (uint32_t error) {
has_error = true;
parse_error = error;
void bail (uint32_t err) {
error = 1;
parse_error = err;
pos = end + 1;
}

void syntaxError () {
has_error = true;
void syntaxError (uint32_t code) {
if (error == 0) error = code;
parse_error = pos - source;
pos = end + 1;
}
11 changes: 9 additions & 2 deletions src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ export function parse (source, name = '@') {
const addr = wasm.sa(len);
(isLE ? copyLE : copyBE)(source, new Uint16Array(wasm.memory.buffer, addr, len));

if (!wasm.parseCJS(addr, source.length, 0, 0, 0))
throw Object.assign(new Error(`Parse error ${name}${wasm.e()}:${source.slice(0, wasm.e()).split('\n').length}:${wasm.e() - source.lastIndexOf('\n', wasm.e() - 1)}`), { idx: wasm.e() });
const err_code = wasm.parseCJS(addr, source.length, 0, 0, 0);

if (err_code) {
const err = new Error(`Parse error ${name}${wasm.e()}:${source.slice(0, wasm.e()).split('\n').length}:${wasm.e() - source.lastIndexOf('\n', wasm.e() - 1)}`);
Object.assign(err, { idx: wasm.e() });
if (err_code === 5 || err_code === 6 || err_code === 7)
Object.assign(err, { code: 'ERR_LEXER_ESM_SYNTAX' });
throw err;
}

let exports = new Set(), reexports = new Set(), unsafeGetters = new Set();

Expand Down

0 comments on commit 279682c

Please sign in to comment.