-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Codegen: gen to emit trees for source mapping
- Loading branch information
1 parent
badd005
commit bf8adb8
Showing
8 changed files
with
326 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { jsString } from '.' | ||
import { ParseNode } from '../../parser' | ||
|
||
export type EmitNode = EmitToken | EmitTree | ||
|
||
export interface EmitToken { | ||
kind: 'token' | ||
value: string | ||
parseNode?: ParseNode | ||
} | ||
|
||
export const emitToken = (value: string, parseNode?: ParseNode): EmitToken => { | ||
return { | ||
kind: 'token', | ||
parseNode, | ||
value | ||
} | ||
} | ||
|
||
export interface EmitTree { | ||
kind: 'node' | ||
nodes: EmitNode[] | ||
parseNode?: ParseNode | ||
} | ||
|
||
export const emitTree = (nodes: (EmitNode | undefined)[], parseNode?: ParseNode): EmitTree => { | ||
return { | ||
kind: 'node', | ||
nodes: nodes.filter(n => n).map(n => n!), | ||
parseNode | ||
} | ||
} | ||
|
||
export const emitAppendStr = (node: EmitNode, str: string): EmitNode => { | ||
if (node.kind === 'node') { | ||
node.nodes.push(emitToken(str)) | ||
} else { | ||
node.value += str | ||
} | ||
return node | ||
} | ||
|
||
export const jsVariable = (name: string, emit?: EmitNode, pub = false): EmitNode => { | ||
const pubStr = pub ? 'export ' : '' | ||
if (emit) { | ||
return emitTree([emitToken(`${pubStr}let ${name} = `), emit, emitToken(';')]) | ||
} else { | ||
return emitToken(`${pubStr}let ${name};`) | ||
} | ||
} | ||
|
||
export const jsError = (message?: string): EmitToken => { | ||
return emitToken(`(() => { throw Error(${jsString(message ?? '')}); })()`) | ||
} | ||
|
||
export const emitIntersperse = ( | ||
lines: (EmitNode | undefined)[], | ||
separator: string, | ||
parseNode?: ParseNode | ||
): EmitTree => { | ||
return emitTree( | ||
lines | ||
.filter(l => !!l) | ||
.map(l => l!) | ||
.filter(l => l.kind === 'node' || l.value.length > 0) | ||
.map((l, i, ls) => { | ||
if (i !== ls.length - 1) { | ||
return emitAppendStr(l, separator) | ||
} | ||
return l | ||
}), | ||
parseNode | ||
) | ||
} |
Oops, something went wrong.