3333import cel .parser .internal .CELParser .CreateMapContext ;
3434import cel .parser .internal .CELParser .CreateMessageContext ;
3535import cel .parser .internal .CELParser .DoubleContext ;
36+ import cel .parser .internal .CELParser .EscapeIdentContext ;
37+ import cel .parser .internal .CELParser .EscapedIdentifierContext ;
3638import cel .parser .internal .CELParser .ExprContext ;
3739import cel .parser .internal .CELParser .ExprListContext ;
3840import cel .parser .internal .CELParser .FieldInitializerListContext ;
39- import cel .parser .internal .CELParser .IdentOrGlobalCallContext ;
41+ import cel .parser .internal .CELParser .GlobalCallContext ;
42+ import cel .parser .internal .CELParser .IdentContext ;
4043import cel .parser .internal .CELParser .IndexContext ;
4144import cel .parser .internal .CELParser .IntContext ;
4245import cel .parser .internal .CELParser .ListInitContext ;
5255import cel .parser .internal .CELParser .PrimaryExprContext ;
5356import cel .parser .internal .CELParser .RelationContext ;
5457import cel .parser .internal .CELParser .SelectContext ;
58+ import cel .parser .internal .CELParser .SimpleIdentifierContext ;
5559import cel .parser .internal .CELParser .StartContext ;
5660import cel .parser .internal .CELParser .StringContext ;
5761import cel .parser .internal .CELParser .UintContext ;
@@ -438,7 +442,7 @@ public CelExpr visitSelect(SelectContext context) {
438442 if (context .id == null ) {
439443 return exprFactory .newExprBuilder (context ).build ();
440444 }
441- String id = context .id . getText ( );
445+ String id = normalizeEscapedIdent ( context .id );
442446
443447 if (context .opt != null && context .opt .getText ().equals ("?" )) {
444448 if (!options .enableOptionalSyntax ()) {
@@ -535,7 +539,7 @@ public CelExpr visitCreateMessage(CreateMessageContext context) {
535539 }
536540
537541 @ Override
538- public CelExpr visitIdentOrGlobalCall ( IdentOrGlobalCallContext context ) {
542+ public CelExpr visitIdent ( IdentContext context ) {
539543 checkNotNull (context );
540544 if (context .id == null ) {
541545 return exprFactory .newExprBuilder (context ).build ();
@@ -547,11 +551,25 @@ public CelExpr visitIdentOrGlobalCall(IdentOrGlobalCallContext context) {
547551 if (context .leadingDot != null ) {
548552 id = "." + id ;
549553 }
550- if (context .op == null ) {
551- return exprFactory
552- .newExprBuilder (context .id )
553- .setIdent (CelExpr .CelIdent .newBuilder ().setName (id ).build ())
554- .build ();
554+
555+ return exprFactory
556+ .newExprBuilder (context .id )
557+ .setIdent (CelExpr .CelIdent .newBuilder ().setName (id ).build ())
558+ .build ();
559+ }
560+
561+ @ Override
562+ public CelExpr visitGlobalCall (GlobalCallContext context ) {
563+ checkNotNull (context );
564+ if (context .id == null ) {
565+ return exprFactory .newExprBuilder (context ).build ();
566+ }
567+ String id = context .id .getText ();
568+ if (options .enableReservedIds () && RESERVED_IDS .contains (id )) {
569+ return exprFactory .reportError (context , "reserved identifier: %s" , id );
570+ }
571+ if (context .leadingDot != null ) {
572+ id = "." + id ;
555573 }
556574
557575 return globalCallOrMacro (context , id );
@@ -671,6 +689,24 @@ private Optional<CelExpr> visitMacro(
671689 return expandedMacro ;
672690 }
673691
692+ private String normalizeEscapedIdent (EscapeIdentContext context ) {
693+ if (context instanceof SimpleIdentifierContext ) {
694+ return ((SimpleIdentifierContext ) context ).getText ();
695+ } else if (context instanceof EscapedIdentifierContext ) {
696+ if (!options .enableQuotedIdentifierSyntax ()) {
697+ exprFactory .reportError (context , "unsupported syntax '`'" );
698+ return "" ;
699+ }
700+ String escaped = ((EscapedIdentifierContext ) context ).getText ();
701+ return escaped .substring (1 , escaped .length () - 1 );
702+ }
703+
704+ // This is normally unreachable, but might happen if the parser is in an error state or if the
705+ // grammar is updated and not handled here.
706+ exprFactory .reportError (context , "unsupported identifier" );
707+ return "" ;
708+ }
709+
674710 private CelExpr .CelStruct .Builder visitStructFields (FieldInitializerListContext context ) {
675711 if (context == null
676712 || context .cols == null
@@ -692,10 +728,10 @@ private CelExpr.CelStruct.Builder visitStructFields(FieldInitializerListContext
692728 }
693729
694730 // The field may be empty due to a prior error.
695- if (fieldContext .IDENTIFIER () == null ) {
731+ if (fieldContext .escapeIdent () == null ) {
696732 return CelExpr .CelStruct .newBuilder ();
697733 }
698- String fieldName = fieldContext .IDENTIFIER (). getText ( );
734+ String fieldName = normalizeEscapedIdent ( fieldContext .escapeIdent () );
699735
700736 CelExpr .CelStruct .Entry .Builder exprBuilder =
701737 CelExpr .CelStruct .Entry .newBuilder ()
@@ -872,7 +908,7 @@ private CelExpr receiverCallOrMacro(MemberCallContext context, String id, CelExp
872908 return macroOrCall (context .args , context .open , id , Optional .of (member ), true );
873909 }
874910
875- private CelExpr globalCallOrMacro (IdentOrGlobalCallContext context , String id ) {
911+ private CelExpr globalCallOrMacro (GlobalCallContext context , String id ) {
876912 return macroOrCall (context .args , context .op , id , Optional .empty (), false );
877913 }
878914
0 commit comments