kunwp1 commented on code in PR #9279:
URL: https://github.com/apache/texera/pull/9279#discussion_r4464706716


##########
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:
   This branch only matches double-quoted raw strings, but R also accepts 
single-quoted ones such as `r'(don't)'`. That string is then read as a normal 
quoted string. The inner apostrophe ends it early, the closing quote has no 
partner, and `tokenize` stops. Every declaration after it disappears from the 
panel and the run fails at that declaration. Capturing the quote, for example 
`/^[rR](["'])(-*)([([{])/` and closing with the same quote can fix the issue. 



##########
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:
   This R check is repeated in `addParameter`, and `code-editor.component.ts` 
already keeps its own `R_OPERATOR_TYPES`. A helper next to `isPythonUdf` that 
returns the operator's `UiUdfLanguage` (or undefined) would replace all three. 
Making `language` required in the parser service would also stop a missing 
argument from quietly meaning Python.



-- 
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]

Reply via email to