70
src/lang/parser.ts
Normal file
70
src/lang/parser.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import {
|
||||
ParserWorkerResponse,
|
||||
WasmWorker,
|
||||
WasmWorkerEventType,
|
||||
} from 'lang/workers/types'
|
||||
import ParserWorker from 'lang/workers/parser?worker'
|
||||
import { wasmUrl } from 'lang/wasm'
|
||||
import { KCLError } from 'lang/errors'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
export type { Program } from 'wasm-lib/kcl/bindings/Program'
|
||||
|
||||
export default class Parser {
|
||||
worker: ParserWorker | null = null
|
||||
mappings: Map<string, Program | KCLError> = new Map()
|
||||
|
||||
async parse(code: string): Program {
|
||||
this.ensureWorker()
|
||||
const uuid = uuidv4()
|
||||
this.worker.postMessage({
|
||||
worker: WasmWorker.Parser,
|
||||
eventType: WasmWorkerEventType.Call,
|
||||
eventData: {
|
||||
uuid,
|
||||
code,
|
||||
},
|
||||
})
|
||||
let result = await this.waitForResult(uuid)
|
||||
if (result instanceof KCLError) {
|
||||
throw result
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
waitForResult(uuid: string): Promise<Program | KCLError> {
|
||||
return new Promise((resolve) => {
|
||||
const result = this.mappings.get(uuid)
|
||||
if (result) {
|
||||
this.mappings.delete(uuid)
|
||||
resolve(result)
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
resolve(this.waitForResult(uuid))
|
||||
}, 100)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
ensureWorker() {
|
||||
if (!this.worker) {
|
||||
this.start()
|
||||
}
|
||||
}
|
||||
|
||||
// Start the worker.
|
||||
start() {
|
||||
this.worker = new ParserWorker({ name: 'parse' })
|
||||
this.worker.postMessage({
|
||||
worker: WasmWorker.Parser,
|
||||
eventType: WasmWorkerEventType.Init,
|
||||
eventData: {
|
||||
wasmUrl: wasmUrl(),
|
||||
},
|
||||
})
|
||||
|
||||
this.worker.onmessage = function (e) {
|
||||
const { uuid, response } = e.data as ParserWorkerResponse
|
||||
this.mappings.set(uuid, response)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,8 +25,7 @@ import { AppInfo } from 'wasm-lib/kcl/bindings/AppInfo'
|
||||
import { CoreDumpManager } from 'lib/coredump'
|
||||
import openWindow from 'lib/openWindow'
|
||||
import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes'
|
||||
import { WasmWorker, WasmWorkerEventType, WasmWorkerOptions, rangeTypeFix } from 'lang/workers/types'
|
||||
import ParserWorker from 'lang/workers/parser?worker'
|
||||
import { parser } from 'lib/singletons'
|
||||
|
||||
export type { Program } from '../wasm-lib/kcl/bindings/Program'
|
||||
export type { Value } from '../wasm-lib/kcl/bindings/Value'
|
||||
@ -105,20 +104,21 @@ const initialise = async () => {
|
||||
|
||||
export const initPromise = initialise()
|
||||
|
||||
export type PathToNode = [string | number, string][]
|
||||
|
||||
interface Memory {
|
||||
[key: string]: MemoryItem
|
||||
}
|
||||
|
||||
export interface ProgramMemory {
|
||||
root: Memory
|
||||
return: ProgramReturn | null
|
||||
}
|
||||
|
||||
export const parse = (code: string): Program => {
|
||||
try {
|
||||
const parserWorker = new ParserWorker({ name: 'parse' })
|
||||
const initEvent: WasmWorkerOptions = {
|
||||
wasmUrl: wasmUrl(),
|
||||
}
|
||||
parserWorker.postMessage({
|
||||
worker: WasmWorker.Parser,
|
||||
eventType: WasmWorkerEventType.Init,
|
||||
eventData: initEvent,
|
||||
})
|
||||
parserWorker.onmessage = function (e) {
|
||||
fromServer.add(e.data)
|
||||
}
|
||||
const program: Program = parse_wasm(code)
|
||||
return program
|
||||
} catch (e: any) {
|
||||
const parsed: RustKclError = JSON.parse(e.toString())
|
||||
const kclError = new KCLError(
|
||||
@ -132,16 +132,9 @@ parserWorker.onmessage = function (e) {
|
||||
}
|
||||
}
|
||||
|
||||
export type PathToNode = [string | number, string][]
|
||||
|
||||
interface Memory {
|
||||
[key: string]: MemoryItem
|
||||
}
|
||||
|
||||
export interface ProgramMemory {
|
||||
root: Memory
|
||||
return: ProgramReturn | null
|
||||
}
|
||||
/*export const parse = async (code: string): Promise<Program> => {
|
||||
return parser.parse(code)
|
||||
}*/
|
||||
|
||||
export const executor = async (
|
||||
node: Program,
|
||||
|
||||
@ -14,10 +14,14 @@ export interface WasmWorkerOptions {
|
||||
}
|
||||
|
||||
export interface ParserWorkerCall {
|
||||
uuid: string
|
||||
code: string
|
||||
}
|
||||
|
||||
export type ParserWorkerResponse = Program | KCLError
|
||||
export interface ParserWorkerResponse {
|
||||
uuid: string
|
||||
response: Program | KCLError
|
||||
}
|
||||
|
||||
export interface WasmWorkerEvent {
|
||||
eventType: WasmWorkerEventType
|
||||
|
||||
Reference in New Issue
Block a user