carloea2 commented on code in PR #9279: URL: https://github.com/apache/texera/pull/9279#discussion_r4471067219
########## frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.ts: ########## @@ -0,0 +1,220 @@ +/** + * 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 { + UiUdfParameter, + ATTRIBUTE_TYPES_BY_TOKEN, + INPUT_TYPES_BY_RESOURCE_TOKEN, + createUiParameter, +} from "./ui-udf-parameter"; + +type Token = Readonly<{ text: string; kind: "name" | "string" | "symbol" }>; +const R_KEYWORDS = new Set( + "if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_".split( + " " + ) +); + +const R_ESCAPES: Readonly<Record<string, string>> = { + n: "\n", + r: "\r", + t: "\t", + b: "\b", + f: "\f", + a: "\x07", + v: "\v", + "\\": "\\", + "'": "'", + '"': '"', + "`": "`", +}; + +function decodeRString(text: string): string { + let valid = true; + const decoded = text.replace( + /\\(u(?:\{[\da-fA-F]{1,4}\}|[\da-fA-F]{4})|U(?:\{[\da-fA-F]{1,8}\}|[\da-fA-F]{8})|x[\da-fA-F]{1,2}|[0-7]{1,3}|[\s\S])/g, + (_, escaped: string) => { + if (escaped in R_ESCAPES) return R_ESCAPES[escaped]; + const octal = /^[0-7]/.test(escaped); + const point = parseInt(escaped.replace(/^[uUx]\{?|\}$/g, ""), octal ? 8 : 16); + if ((octal || /^[uUx]/.test(escaped)) && (octal || point > 0) && point <= 0x10ffff) + return String.fromCodePoint(point); + valid = false; + return ""; + } + ); + return valid ? decoded : ""; +} + +/** Tokenize only the R syntax needed to recognize declarations, never evaluating user code. */ +function tokenize(code: string): Token[] { + const tokens: Token[] = []; + for (let index = 0; index < code.length; ) { + const rest = code.slice(index); + if (/\s/.test(code[index])) { + index++; + continue; + } + if (code[index] === "#") { + const end = code.indexOf("\n", index); + index = end < 0 ? code.length : end + 1; + continue; + } + // R raw strings may use paired (), [], or {} and any number of dashes. + const raw = /^[rR]"(-*)([([{])/.exec(rest); + if (raw) { + const close = ({ "(": ")", "[": "]", "{": "}" } as Record<string, string>)[raw[2]] + raw[1] + '"'; Review Comment: Fixed in 615bb576be. Raw strings now close with the same quote, bracket, and dash delimiter as their opening. Regression cases cover single quotes with apostrophes, uppercase R, bracket variants, mismatched closers, and declarations following quoted examples. All 301 targeted frontend checks pass. The full frontend suite also passes: 7,098 tests, one existing skip. ########## frontend/src/app/workspace/service/code-editor/ui-udf-parameters-sync.service.ts: ########## @@ -79,13 +79,14 @@ export class UiUdfParametersSyncService { } /** - * Parses Python UDF code for a known Python UDF operator and emits merged parameter rows when the shape changes. + * Parses Python or R UDF code and emits merged parameter rows when the shape changes. * If codeFromEditor is omitted, reads from Yjs; does nothing if the operator or code is unavailable. */ syncStructureFromCode(operatorId: string, codeFromEditor?: string): void { const operator = this.workflowActionService.getTexeraGraph().getOperator(operatorId); - if (!operator || !isPythonUdf(operator)) return; + const isR = operator?.operatorType === "RUDF" || operator?.operatorType === "RUDFSource"; Review Comment: Fixed in 615bb576be. Added getUiUdfLanguage next to isPythonUdf and reused it in the editor and both synchronization paths. Removed the duplicate operator sets/checks. Both parser methods now require an explicit language, with Python callers updated. Existing Python/R/Java/unknown editor behavior and all 301 targeted checks pass. The full frontend suite also passes: 7,098 tests, one existing skip. -- 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]
