Skip to content

Commit 9f59a54

Browse files
l46kokcopybara-github
authored andcommitted
Retain the original identifier during parse when quoted identifier is disabled
PiperOrigin-RevId: 713410853
1 parent 0386196 commit 9f59a54

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

parser/src/main/java/dev/cel/parser/Parser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,21 +690,21 @@ private Optional<CelExpr> visitMacro(
690690
}
691691

692692
private String normalizeEscapedIdent(EscapeIdentContext context) {
693+
String identifier = context.getText();
693694
if (context instanceof SimpleIdentifierContext) {
694-
return ((SimpleIdentifierContext) context).getText();
695+
return identifier;
695696
} else if (context instanceof EscapedIdentifierContext) {
696697
if (!options.enableQuotedIdentifierSyntax()) {
697698
exprFactory.reportError(context, "unsupported syntax '`'");
698-
return "";
699+
return identifier;
699700
}
700-
String escaped = ((EscapedIdentifierContext) context).getText();
701-
return escaped.substring(1, escaped.length() - 1);
701+
return identifier.substring(1, identifier.length() - 1);
702702
}
703703

704704
// This is normally unreachable, but might happen if the parser is in an error state or if the
705705
// grammar is updated and not handled here.
706706
exprFactory.reportError(context, "unsupported identifier");
707-
return "";
707+
return identifier;
708708
}
709709

710710
private CelExpr.CelStruct.Builder visitStructFields(FieldInitializerListContext context) {

parser/src/test/java/dev/cel/parser/CelParserParameterizedTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,12 @@ public void parser_errors() {
296296

297297
CelParser parserWithoutQuotedFields =
298298
CelParserImpl.newBuilder()
299+
.setStandardMacros(CelStandardMacro.HAS)
299300
.setOptions(CelOptions.current().enableQuotedIdentifierSyntax(false).build())
300301
.build();
301302
runTest(parserWithoutQuotedFields, "foo.`bar`");
302303
runTest(parserWithoutQuotedFields, "Struct{`bar`: false}");
304+
runTest(parserWithoutQuotedFields, "has(.`.`");
303305
}
304306

305307
@Test

parser/src/test/resources/parser_errors.baseline

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,16 @@ I: Struct{`bar`: false}
332332
=====>
333333
E: ERROR: <input>:1:8: unsupported syntax '`'
334334
| Struct{`bar`: false}
335-
| .......^
335+
| .......^
336+
337+
I: has(.`.`
338+
=====>
339+
E: ERROR: <input>:1:6: no viable alternative at input '.`.`'
340+
| has(.`.`
341+
| .....^
342+
ERROR: <input>:1:6: unsupported syntax '`'
343+
| has(.`.`
344+
| .....^
345+
ERROR: <input>:1:9: missing ')' at '<EOF>'
346+
| has(.`.`
347+
| ........^

0 commit comments

Comments
 (0)