carloea2 commented on code in PR #5043: URL: https://github.com/apache/texera/pull/5043#discussion_r3271572406
########## frontend/src/app/workspace/service/code-editor/ui-udf-parameters-parser.service.ts: ########## @@ -0,0 +1,217 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Injectable } from "@angular/core"; +import { parser } from "@lezer/python"; +import { AttributeType, SchemaAttribute } from "../../types/workflow-compiling.interface"; + +export interface UiUdfParameter { + attribute: SchemaAttribute; + value: string; +} + +const CLASSES = new Set(["ProcessTupleOperator", "ProcessBatchOperator", "ProcessTableOperator", "GenerateOperator"]); + +// Java enum constant names (AttributeType.java) +const JAVA_ATTRIBUTE_TYPE_NAMES = [ + "STRING", + "INTEGER", + "LONG", + "DOUBLE", + "BOOLEAN", + "TIMESTAMP", + "BINARY", + "LARGE_BINARY", +] as const; + +type JavaAttributeTypeName = (typeof JAVA_ATTRIBUTE_TYPE_NAMES)[number]; + +// Python enum constant names (core.models.AttributeType) +const PYTHON_ATTRIBUTE_TYPE_NAMES = [ + "STRING", + "INT", + "LONG", + "DOUBLE", + "BOOL", + "TIMESTAMP", + "BINARY", + "LARGE_BINARY", +] as const; + +type PythonAttributeTypeName = (typeof PYTHON_ATTRIBUTE_TYPE_NAMES)[number]; + +type ParserAttributeTypeToken = JavaAttributeTypeName | PythonAttributeTypeName; +type UnsupportedParserAttributeTypeToken = "BINARY" | "LARGE_BINARY"; +type SupportedParserAttributeTypeToken = Exclude<ParserAttributeTypeToken, UnsupportedParserAttributeTypeToken>; +type ParserSyntaxNode = ReturnType<typeof parser.parse>["topNode"]; + +const TYPES: Readonly<Record<SupportedParserAttributeTypeToken, AttributeType>> = { + STRING: "string", + INTEGER: "integer", + INT: "integer", + LONG: "long", + DOUBLE: "double", + BOOLEAN: "boolean", + BOOL: "boolean", + TIMESTAMP: "timestamp", +}; + +const JAVA_ATTRIBUTE_TYPE_NAME_SET = new Set<string>(JAVA_ATTRIBUTE_TYPE_NAMES); +const PYTHON_ATTRIBUTE_TYPE_NAME_SET = new Set<string>(PYTHON_ATTRIBUTE_TYPE_NAMES); +const SUPPORTED_UI_PARAMETER_ATTRIBUTE_TYPES = new Set<AttributeType>([ + "string", + "integer", + "long", + "double", + "boolean", + "timestamp", +]); + +@Injectable({ providedIn: "root" }) +export class UiUdfParametersParserService { + parse(code: string): UiUdfParameter[] { + if (!code) return []; + + const result: UiUdfParameter[] = []; + const seen = new Set<string>(); + const add = (parameter?: UiUdfParameter): void => { + const name = parameter?.attribute.attributeName; + if (parameter && name && !seen.has(name)) { + seen.add(name); + result.push(parameter); + } + }; + + parser.parse(code).iterate({ + enter: ({ name, node }) => { + const className = node.getChild("VariableName"); + if (name !== "ClassDefinition" || !className || !CLASSES.has(code.slice(className.from, className.to))) return; + node + .cursor() + .iterate(ref => (ref.name === "CallExpression" ? (add(readCall(ref.node, code)), false) : undefined)); + return false; + }, + }); + + return result; + } +} + +function readCall(call: ParserSyntaxNode, code: string): UiUdfParameter | undefined { + const args = call.getChild("ArgList"); + if (!args || code.slice(call.from, args.from).replace(/\s+/g, "") !== "self.UiParameter") return undefined; + + let attributeName: string | undefined; + let attributeType: AttributeType | undefined; + let index = 0; + let sawNamed = false; + + for (const arg of splitArgs(code.slice(args.from + 1, args.to - 1))) { + const match = arg.match(/^([A-Za-z_]\w*)\s*=\s*([\s\S]+)$/); + const key = match?.[1]; + const value = match?.[2] ?? arg; + + if (match) sawNamed = true; + else if (sawNamed || index > 1) return undefined; + + if ((match ? key === "name" : index === 0) && !attributeName) attributeName = readString(value)?.trim(); + else if ((match ? key === "type" || key === "attr_type" : index === 1) && !attributeType) + attributeType = readType(value); + else return undefined; + + if (!match) index++; + if (!attributeName && (key === "name" || (!match && index === 1))) return undefined; + if (!attributeType && (key === "type" || key === "attr_type" || (!match && index === 2))) return undefined; + } + + return attributeName && attributeType ? { attribute: { attributeName, attributeType }, value: "" } : undefined; +} + +function splitArgs(input: string): string[] { + const result: string[] = []; Review Comment: Fixed. I removed `splitArgs` entirely. The parser now reads Lezer `ArgList` child nodes in `readArguments(...)` instead of parsing the argument string character by character. Named arguments are read from the `VariableName + AssignOp + value` node pattern, and positional arguments are preserved as parsed syntax nodes. The later validation is also AST-based: `readName(...)` only accepts `String` nodes, and `readType(...)` only accepts `MemberExpression` nodes for `AttributeType.X`. This keeps multiline arguments handled by Lezer and removes the manual argument splitter from the parser. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
