diff --git a/src/build.ts b/src/build.ts index 9730091..01b794e 100644 --- a/src/build.ts +++ b/src/build.ts @@ -431,12 +431,11 @@ class Builder { let mod = this.options.moduleStyle || "es" let gen = "// This file was generated by lezer-generator. You probably shouldn't edit it.\n", head = gen - head += mod == "cjs" ? `const {LRParser} = require("@lezer/lr")\n` - : `import {LRParser} from "@lezer/lr"\n` let imports: {[source: string]: string[]} = {}, imported: {[spec: string]: string} = Object.create(null) - let defined = Object.create(null) + let defined: {[name: string]: boolean} = Object.create(null) + for (let word of KEYWORDS) defined[word] = true let exportName = this.options.exportName || "parser" - defined.Parser = defined[exportName] = true + defined[exportName] = true let getName = (prefix: string) => { for (let i = 0;; i++) { let id = prefix + (i ? "_" + i : "") @@ -444,7 +443,7 @@ class Builder { } } - let importName = (name: string, source: string, prefix: string) => { + let importName = (name: string, source: string, prefix: string = name) => { let spec = name + " from " + source if (imported[spec]) return imported[spec] let src = JSON.stringify(source), varName = name @@ -452,9 +451,11 @@ class Builder { varName = getName(prefix) name += `${mod == "cjs" ? ":" : " as"} ${varName}` } + defined[varName] = true ;(imports[src] || (imports[src] = [])).push(name) return imported[spec] = varName } + let lrParser = importName("LRParser", "@lezer/lr") let tokenizers = rawTokenizers.map(tok => { if (tok instanceof ExternalTokenDeclaration) { @@ -465,11 +466,11 @@ class Builder { } }) - let context = this.ast.context ? importName(this.ast.context.id.name, this.ast.context.source, "cx") : null + let context = this.ast.context ? importName(this.ast.context.id.name, this.ast.context.source) : null let nodeProps = rawNodeProps.map(({prop, terms}) => { let {source} = this.knownProps[prop] - let propID = source.from ? importName(source.name, source.from, "prop") : JSON.stringify(source.name) + let propID = source.from ? importName(source.name, source.from) : JSON.stringify(source.name) return `[${propID}, ${terms.map(serializePropValue).join(",")}]` }) @@ -481,18 +482,19 @@ class Builder { let specHead = "" let specialized = rawSpecialized.map(v => { if (v instanceof ExternalSpecializer) { - let name = importName(v.ast.id.name, v.ast.source, v.ast.id.name) + let name = importName(v.ast.id.name, v.ast.source) return `{term: ${v.term!.id}, get: (value, stack) => (${name}(value, stack) << 1)${ v.ast.type == "extend" ? ` | ${Specialize.Extend}` : ''}, external: ${name}${ v.ast.type == "extend" ? ', extend: true' : ''}}` } else { let tableName = getName("spec_" + v.token.name.replace(/\W/g, "")) + defined[tableName] = true specHead += `const ${tableName} = ${specializationTableString(v.table)}\n` return `{term: ${v.token.id}, get: value => ${tableName}[value] || -1}` } }) - let propSources = this.ast.externalPropSources.map(s => importName(s.id.name, s.source, "props")) + let propSources = this.ast.externalPropSources.map(s => importName(s.id.name, s.source)) for (let source in imports) { if (mod == "cjs") @@ -509,7 +511,7 @@ class Builder { let dialects = Object.keys(rawDialects).map(d => `${d}: ${rawDialects[d]}`) - let parserStr = `LRParser.deserialize({ + let parserStr = `${lrParser}.deserialize({ version: ${File.Version}, states: ${encodeArray(states, 0xffffffff)}, stateData: ${encodeArray(stateData)},