Copilot commented on code in PR #398: URL: https://github.com/apache/commons-jexl/pull/398#discussion_r2807733955
########## src/main/java/org/apache/commons/jexl3/internal/TemplateInfo.java: ########## @@ -0,0 +1,38 @@ +/* + * 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 + * + * https://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. + */ +package org.apache.commons.jexl3.internal; + +import java.util.Set; + +import org.apache.commons.jexl3.JexlInfo; + +/** + * A JexlInfo that also carries the set of tokens that should be ignored during parsing, + * most notably the prefix for templates. + */ +public class TemplateInfo extends JexlInfo { + private final Set<String> ignoredTokens; + + public TemplateInfo(JexlInfo info, Set<String> ignoredTokens) { + super(info); + this.ignoredTokens = ignoredTokens; + } Review Comment: Consider adding a null check or Objects.requireNonNull for the ignoredTokens parameter to prevent potential NullPointerExceptions and document the contract clearly. While the current usage with Collections.singleton ensures it's never null in practice, defensive programming would make the API more robust against future misuse. ########## src/main/java/org/apache/commons/jexl3/internal/TemplateInfo.java: ########## @@ -0,0 +1,38 @@ +/* + * 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 + * + * https://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. + */ +package org.apache.commons.jexl3.internal; + +import java.util.Set; + +import org.apache.commons.jexl3.JexlInfo; + +/** + * A JexlInfo that also carries the set of tokens that should be ignored during parsing, + * most notably the prefix for templates. + */ +public class TemplateInfo extends JexlInfo { + private final Set<String> ignoredTokens; + + public TemplateInfo(JexlInfo info, Set<String> ignoredTokens) { + super(info); + this.ignoredTokens = ignoredTokens; + } + + public Set<String> getIgnoredTokens() { + return ignoredTokens; + } Review Comment: The TemplateInfo class should override the `at(int l, int c)` method to return a TemplateInfo instance instead of a JexlInfo instance. Without this override, calling `at()` on a TemplateInfo will return a plain JexlInfo, losing the ignoredTokens information. This could cause the token ignoring feature to fail when creating new info instances with different line/column numbers. ```suggestion } @Override public TemplateInfo at(final int l, final int c) { JexlInfo info = super.at(l, c); return new TemplateInfo(info, ignoredTokens); } ``` ########## src/main/java/org/apache/commons/jexl3/parser/Parser.jjt: ########## @@ -288,27 +297,36 @@ TOKEN_MGR_DECLS : { { < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>|<ESCAPE>)* > { - matchedToken.image = StringParser.unescapeIdentifier(matchedToken.image); - final int length = matchedToken.image.length(); - if (comparatorNames && length == 2) { - switch (matchedToken.image) { - case "ne" : matchedToken.kind = NE; break; - case "eq" : matchedToken.kind = EQ; break; - case "lt" : matchedToken.kind = LT; break; - case "le" : matchedToken.kind = LE; break; - case "gt" : matchedToken.kind = GT; break; - case "ge" : matchedToken.kind = GE; break; - } - } else if (jexl331 && length >= 3 && length <= 10) { - switch (matchedToken.image) { - case "try" : matchedToken.kind = TRY; break; - case "catch" : matchedToken.kind = CATCH; break; - case "finally" : matchedToken.kind = FINALLY; break; - case "throw" : matchedToken.kind = THROW; break; - case "instanceof": matchedToken.kind = ISA; break; - case "!instanceof": matchedToken.kind = NISA; break; - } - } + matchedToken.image = StringParser.unescapeIdentifier(matchedToken.image); + final int length = matchedToken.image.length(); + if (ignoredTokens.contains(matchedToken.image)) { + // ignore by getting next token instead + Token n = getNextToken(); + matchedToken.kind = n.kind; + matchedToken.image = n.image; + matchedToken.beginLine = n.beginLine; + matchedToken.endLine = n.endLine; + matchedToken.beginColumn = n.beginColumn; + matchedToken.endColumn = n.endColumn; Review Comment: The token ignoring mechanism appears fragile: calling getNextToken() within a token action and copying fields to matchedToken works for the current use case but could cause issues in edge cases. Specifically: (1) If consecutive ignored tokens appear, only the first would be skipped. (2) The token 'next' field is not updated, which might affect lookahead operations. (3) This approach is tightly coupled to the specific token stream state. Consider documenting these limitations, or refactoring to use a more robust token filtering mechanism that handles these edge cases explicitly. ```suggestion // ignore by getting next non-ignored token instead Token n = getNextToken(); // Skip consecutive ignored identifier tokens while (n.kind == IDENTIFIER) { final String unescaped = StringParser.unescapeIdentifier(n.image); if (!ignoredTokens.contains(unescaped)) { break; } n = getNextToken(); } matchedToken.kind = n.kind; matchedToken.image = n.image; matchedToken.beginLine = n.beginLine; matchedToken.endLine = n.endLine; matchedToken.beginColumn = n.beginColumn; matchedToken.endColumn = n.endColumn; matchedToken.next = n.next; ``` ########## src/main/java/org/apache/commons/jexl3/JexlFeatures.java: ########## @@ -496,11 +499,32 @@ public Set<String> getReservedNames() { @Override public int hashCode() { //CSOFF: MagicNumber int hash = 3; - hash = 53 * hash + (int) (this.flags ^ this.flags >>> 32); + hash = 53 * hash + Long.hashCode(this.flags); hash = 53 * hash + (this.reservedNames != null ? this.reservedNames.hashCode() : 0); return hash; } + /** + * Sets whether to ignore template prefixes in template expressions. + * <p>Note that this precludes using a variable whose name is the template prefix (ie default $$)</p> Review Comment: The documentation for the ignoreTemplatePrefix feature mentions that it "precludes using a variable whose name is the template prefix (ie default $$)", but this should clarify that it only precludes variables with exactly that name. For instance, if the prefix is "$$", then variables like "$", "$a", or "$$$" would still be usable - only the exact prefix "$$" would be ignored. ```suggestion * <p>Note that this only precludes using a variable whose name is exactly the template prefix * (for example, with the default prefix "$$", a variable named "$$" is ignored, but "$", "$a", or "$$$" * are still valid variable names).</p> ``` -- 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]
