Skip to content

Commit

Permalink
Changes exports to ExportSpecifier.
Browse files Browse the repository at this point in the history
Adds information about the local name for "export { a as b }" and
whether it's an ambient export or not.

Breaking change for the parse() API since exports is no longer an
array of strings.
  • Loading branch information
tommie committed Jul 26, 2022
1 parent 559a550 commit 70ff0c9
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 181 deletions.
4 changes: 2 additions & 2 deletions chompfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ deps = ['src/lexer.h', 'src/lexer.c']
run = """
${{ WASI_PATH }}/bin/clang src/lexer.c --sysroot=${{ WASI_PATH }}/share/wasi-sysroot -o lib/lexer.wasm -nostartfiles \
"-Wl,-z,stack-size=13312,--no-entry,--compress-relocations,--strip-all,\
--export=parse,--export=sa,--export=e,--export=ri,--export=re,--export=is,--export=ie,--export=ss,--export=ip,--export=se,--export=ai,--export=id,--export=es,--export=ee,--export=f,--export=__heap_base" \
--export=parse,--export=sa,--export=e,--export=ri,--export=re,--export=is,--export=ie,--export=ss,--export=ip,--export=se,--export=ai,--export=id,--export=es,--export=ee,--export=els,--export=ele,--export=ea,--export=f,--export=__heap_base" \
-Wno-logical-op-parentheses -Wno-parentheses \
-Oz
"""
Expand All @@ -87,7 +87,7 @@ run = """
${{ EMSDK_PATH }}/emsdk activate 1.40.1-fastcomp
${{ EMSDK_PATH }}/fastcomp/emscripten/emcc ./src/lexer.c -o lib/lexer.emcc.js -s WASM=0 -Oz --closure 1 \
-s EXPORTED_FUNCTIONS="['_parse','_sa','_e','_ri','_re','_is','_ie','_ss','_ip','_se','_ai','_id','_es','_ee','_f','_setSource']" \
-s EXPORTED_FUNCTIONS="['_parse','_sa','_e','_ri','_re','_is','_ie','_ss','_ip','_se','_ai','_id','_es','_ee','_els','_ele','_ea','_f','_setSource']" \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 -s SINGLE_FILE=1 -s TOTAL_STACK=4997968 -s --separate-asm -Wno-logical-op-parentheses -Wno-parentheses
# rm lib/lexer.emcc.js
Expand Down
26 changes: 19 additions & 7 deletions lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function addImport (ss, s, e, d) {
return impt;
}

function addExport (s, e, ls, le, a) {
exports.push({ s, e, ls, le, a, n: undefined, ln: undefined });
}

function readName (impt) {
let { d, s } = impt;
if (d !== -1)
Expand All @@ -43,7 +47,7 @@ export function parse (_source, _name) {
name = _name || '@';

imports = [];
exports = new Set();
exports = [];

source = _source;
pos = -1;
Expand Down Expand Up @@ -202,7 +206,7 @@ export function parse (_source, _name) {
if (templateDepth !== -1 || openTokenDepth)
syntaxError();

return [imports, [...exports], facade];
return [imports, exports, facade];
}

function tryParseImportStatement () {
Expand Down Expand Up @@ -287,6 +291,7 @@ function tryParseImportStatement () {

function tryParseExportStatement () {
const sStartPos = pos;
const prevExport = exports.length;

pos += 6;

Expand All @@ -300,7 +305,7 @@ function tryParseExportStatement () {
switch (ch) {
// export default ...
case 100/*d*/:
exports.add(source.slice(pos, pos + 7));
addExport(pos, pos + 7, -1, -1, true);
return;

// export async? function*? name () {
Expand All @@ -317,17 +322,18 @@ function tryParseExportStatement () {
}
const startPos = pos;
ch = readToWsOrPunctuator(ch);
exports.add(source.slice(startPos, pos));
addExport(startPos, pos, -1, -1, true);
pos--;
return;

// export class name ...
case 99/*c*/:
if (source.startsWith('lass', pos + 1) && isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos + 5))) {
pos += 5;
ch = commentWhitespace(true);
const startPos = pos;
ch = readToWsOrPunctuator(ch);
exports.add(source.slice(startPos, pos));
addExport(startPos, pos, -1, -1, true);
pos--;
return;
}
Expand All @@ -353,7 +359,7 @@ function tryParseExportStatement () {
}
if (pos === startPos)
return;
exports.add(source.slice(startPos, pos));
addExport(startPos, pos, -1, -1, false);
ch = commentWhitespace(true);
if (ch === 61/*=*/) {
pos--;
Expand Down Expand Up @@ -404,6 +410,11 @@ function tryParseExportStatement () {
if (ch === 102/*f*/ && source.startsWith('rom', pos + 1)) {
pos += 4;
readImportString(sStartPos, commentWhitespace(true));

// There were no local names.
for (let i = prevExport; i < exports.length; ++i) {
exports[i].ls = exports[i].le = -1;
}
}
else {
pos--;
Expand Down Expand Up @@ -556,6 +567,7 @@ function readCodePointToString () {

function readExportAs (startPos, endPos) {
let ch = source.charCodeAt(pos);
let ls = startPos, le = endPos;
if (ch === 97 /*a*/) {
pos += 2;
ch = commentWhitespace(true);
Expand All @@ -565,7 +577,7 @@ function readExportAs (startPos, endPos) {
ch = commentWhitespace(true);
}
if (pos !== startPos)
exports.add(source.slice(startPos, endPos));
addExport(startPos, endPos, ls, le, true);
return ch;
}

Expand Down
11 changes: 9 additions & 2 deletions src/lexer.asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ export function parse (_source, _name = '@') {
imports.push({ n, s, e, ss, se, d, a });
}
while (asm.re()) {
const start = asm.es(), ch = source.charCodeAt(start);
exports.push((ch === 34 || ch === 39) ? readString(start + 1, ch) : source.slice(asm.es(), asm.ee()));
const s = asm.es(), e = asm.ee(), ls = asm.els(), le = asm.ele();
const ch = source.charCodeAt(s);
const lch = ls >= 0 ? source.charCodeAt(ls) : -1;
exports.push({
s, e, ls, le,
a: Boolean(asm.ea()),
n: (ch === 34 || ch === 39) ? readString(s + 1, ch) : source.slice(s, e),
ln: ls < 0 ? undefined : (lch === 34 || lch === 39) ? readString(ls + 1, lch) : source.slice(ls, le),
});
}

return [imports, exports, !!asm.f()];
Expand Down
19 changes: 14 additions & 5 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ void tryParseImportStatement () {

void tryParseExportStatement () {
char16_t* sStartPos = pos;
Export* prev_export_write_head = export_write_head;

pos += 6;

Expand All @@ -359,7 +360,7 @@ void tryParseExportStatement () {
switch (ch) {
// export default ...
case 'd':
addExport(pos, pos + 7);
addExport(pos, pos + 7, NULL, NULL, false);
return;

// export async? function*? name () {
Expand All @@ -376,17 +377,18 @@ void tryParseExportStatement () {
}
const char16_t* startPos = pos;
ch = readToWsOrPunctuator(ch);
addExport(startPos, pos);
addExport(startPos, pos, startPos, pos, true);
pos--;
return;

// export class name ...
case 'c':
if (memcmp(pos + 1, &LASS[0], 4 * 2) == 0 && isBrOrWsOrPunctuatorNotDot(*(pos + 5))) {
pos += 5;
ch = commentWhitespace(true);
const char16_t* startPos = pos;
ch = readToWsOrPunctuator(ch);
addExport(startPos, pos);
addExport(startPos, pos, startPos, pos, true);
pos--;
return;
}
Expand All @@ -412,7 +414,7 @@ void tryParseExportStatement () {
}
if (pos == startPos)
return;
addExport(startPos, pos);
addExport(startPos, pos, startPos, pos, false);
ch = commentWhitespace(true);
if (ch == '=') {
pos--;
Expand Down Expand Up @@ -480,6 +482,11 @@ void tryParseExportStatement () {
if (ch == 'f' && memcmp(pos + 1, &FROM[1], 3 * 2) == 0) {
pos += 4;
readImportString(sStartPos, commentWhitespace(true));

// There were no local names.
for (Export* exprt = prev_export_write_head == NULL ? first_export : prev_export_write_head->next; exprt != NULL; exprt = exprt->next) {
exprt->local_start = exprt->local_end = NULL;
}
}
else {
pos--;
Expand All @@ -488,6 +495,8 @@ void tryParseExportStatement () {

char16_t readExportAs (char16_t* startPos, char16_t* endPos) {
char16_t ch = *pos;
char16_t* localStartPos = startPos == endPos ? NULL : startPos;
char16_t* localEndPos = startPos == endPos ? NULL : endPos;

if (ch == 'a') {
pos += 2;
Expand Down Expand Up @@ -515,7 +524,7 @@ char16_t readExportAs (char16_t* startPos, char16_t* endPos) {
}

if (pos != startPos)
addExport(startPos, endPos);
addExport(startPos, endPos, localStartPos, localEndPos, true);
return ch;
}

Expand Down
20 changes: 19 additions & 1 deletion src/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ typedef struct Import Import;
struct Export {
const char16_t* start;
const char16_t* end;
const char16_t* local_start;
const char16_t* local_end;
bool ambient;
struct Export* next;
};
typedef struct Export Export;
Expand Down Expand Up @@ -109,7 +112,7 @@ void addImport (const char16_t* statement_start, const char16_t* start, const ch
import->next = NULL;
}

void addExport (const char16_t* start, const char16_t* end) {
void addExport (const char16_t* start, const char16_t* end, const char16_t* local_start, const char16_t* local_end, bool ambient) {
Export* export = (Export*)(analysis_head);
analysis_head = analysis_head + sizeof(Export);
if (export_write_head == NULL)
Expand All @@ -119,6 +122,9 @@ void addExport (const char16_t* start, const char16_t* end) {
export_write_head = export;
export->start = start;
export->end = end;
export->local_start = local_start;
export->local_end = local_end;
export->ambient = ambient;
export->next = NULL;
}

Expand Down Expand Up @@ -168,6 +174,18 @@ uint32_t es () {
uint32_t ee () {
return export_read_head->end - source;
}
// getExportLocalStart
int32_t els () {
return export_read_head->local_start ? export_read_head->local_start - source : -1;
}
// getExportLocalEnd
int32_t ele () {
return export_read_head->local_end ? export_read_head->local_end - source : -1;
}
// getExportIsAmbient
bool ea () {
return export_read_head->ambient;
}
// readImport
bool ri () {
if (import_read_head == NULL)
Expand Down
11 changes: 9 additions & 2 deletions src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ export function parse (source, name = '@') {
imports.push({ n, s, e, ss, se, d, a });
}
while (wasm.re()) {
const expt = source.slice(wasm.es(), wasm.ee()), ch = expt[0];
exports.push((ch === '"' || ch === "'") ? decode(expt) : expt);
const s = wasm.es(), e = wasm.ee(), ls = wasm.els(), le = wasm.ele();
const n = source.slice(s, e), ch = n[0];
const ln = ls < 0 ? undefined : source.slice(ls, le), lch = ln ? ln[0] : '';
exports.push({
s, e, ls, le,
a: Boolean(wasm.ea()),
n: (ch === '"' || ch === "'") ? decode(n) : n,
ln: (lch === '"' || lch === "'") ? decode(ln) : ln,
});
}

function decode (str) {
Expand Down
Loading

0 comments on commit 70ff0c9

Please sign in to comment.