Skip to content

Commit

Permalink
Clean up import generation in file output
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Jan 6, 2023
1 parent 215e986 commit 484d637
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,30 +431,31 @@ 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 : "")
if (!defined[id]) return id
}
}

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
if (name in defined) {
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) {
Expand All @@ -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(",")}]`
})

Expand All @@ -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")
Expand All @@ -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)},
Expand Down

0 comments on commit 484d637

Please sign in to comment.