UNPKG

simc-ast-builder

Version:

Parser and AST generator for SimulationCraft files

152 lines 4.26 kB
"use strict"; /** * Builder for creating ExpressionNode instances * Provides a fluent API for constructing nodes with proper type checking */ Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeBuilder = void 0; const Field_1 = require("./Field"); /** * Builder for creating ExpressionNode instances */ class NodeBuilder { constructor() { /** * The node being built */ this.node = { expressionType: "neutral", kind: "expression", }; } /** * Build the node * @returns The built ExpressionNode * @throws Error if the node is invalid */ build() { // Validate the node if (!this.node.nodeType) { throw new Error("Node must have a nodeType"); } return this.node; } /** * Create a new builder with the same properties as this one * @returns A new NodeBuilder with the same properties */ clone() { const builder = new NodeBuilder(); builder.node = Object.assign({}, this.node); return builder; } /** * Reset the builder to its initial state * @returns This builder for chaining */ reset() { this.node = { expressionType: "neutral", kind: "expression", }; return this; } /** * Set the action name * @param actionName Name of the action * @returns This builder for chaining */ withActionName(actionName) { this.node["actionName"] = actionName; return this; } /** * Set the expression type * @param expressionType Type of the expression * @returns This builder for chaining */ withExpressionType(expressionType) { this.node.expressionType = expressionType; return this; } /** * Set a field using a Field instance * @param field Field to set * @returns This builder for chaining */ withField(field) { if (field instanceof Field_1.Field) { this.node["field"] = { displayName: field.displayName, name: field.name, negatedName: field.negatedName, type: field.type, }; // Update expression type based on field type if not explicitly set if (field.type !== "neutral") { this.node.expressionType = field.type; } } else if (field) { // If it's not a Field instance but some other object, use it directly this.node["field"] = field; // Try to update expression type if possible if (field.type && field.type !== "neutral") { this.node.expressionType = field.type; } } return this; } /** * Set a field using a FieldDefinition * @param fieldDef Field definition to set * @returns This builder for chaining */ withFieldDef(fieldDef) { this.node["field"] = fieldDef; // Update expression type based on field type if not explicitly set if (fieldDef.type !== "neutral") { this.node.expressionType = fieldDef.type; } return this; } /** * Set multiple properties on the node * @param properties Properties to set * @returns This builder for chaining */ withProperties(properties) { Object.assign(this.node, properties); return this; } /** * Set any property on the node * @param key Property key * @param value Property value * @returns This builder for chaining */ withProperty(key, value) { this.node[key] = value; return this; } /** * Set the node type * @param nodeType Type of the node * @returns This builder for chaining */ withType(nodeType) { this.node.nodeType = nodeType; return this; } /** * Set the value * @param value Value to set * @returns This builder for chaining */ withValue(value) { this.node["value"] = value; return this; } } exports.NodeBuilder = NodeBuilder; //# sourceMappingURL=NodeBuilder.js.map