UNPKG

polygonjs-engine

Version:

node-based webgl 3D engine https://polygonjs.com

70 lines (69 loc) 1.74 kB
import jsep2 from "jsep"; import {CoreType} from "../../../core/Type"; jsep2.addUnaryOp("@"); let precedence = 10; jsep2.addBinaryOp("**", precedence); const JSEP_IDENTIFIER = "Identifier"; const JSEP_LITERAL = "Literal"; const JSEP_CALL_EXPRESSION = "CallExpression"; const STRING_EXPRESSION_SEPARATOR = "`"; export class ParsedTree { constructor() { } parse_expression(string) { try { this.reset(); this.node = jsep2(string); } catch (e) { const message = `could not parse the expression '${string}' (error: ${e})`; this.error_message = message; } } parse_expression_for_string_param(string) { try { this.reset(); const elements = ParsedTree.string_value_elements(string); const nodes = []; for (let i = 0; i < elements.length; i++) { const element = elements[i]; let node; if (i % 2 == 1) { node = jsep2(element); } else { node = { type: JSEP_LITERAL, value: `'${element}'`, raw: `'${element}'` }; } nodes.push(node); } this.node = { type: JSEP_CALL_EXPRESSION, arguments: nodes, callee: { type: JSEP_IDENTIFIER, name: "strConcat" } }; } catch (e) { const message = `could not parse the expression '${string}' (error: ${e})`; this.error_message = message; } } static string_value_elements(v) { if (v != null) { if (CoreType.isString(v)) { return v.split(STRING_EXPRESSION_SEPARATOR); } else { return []; } } else { return []; } } reset() { this.node = void 0; this.error_message = void 0; } }