Skip to content

Commit

Permalink
Update the CEL Grammar to support the lexis for Google SQL strings (#46)
Browse files Browse the repository at this point in the history
Closes #45: Ensure CEL Grammar is Google SQL compatible.
  • Loading branch information
TristonianJones authored Jun 7, 2018
1 parent d5dcbfb commit 2226ee7
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 563 deletions.
49 changes: 22 additions & 27 deletions parser/gen/CEL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

grammar CEL;

// Note: grammar here should be rather final, but lexis is a moving target.

// Grammar Rules
// =============

Expand Down Expand Up @@ -93,11 +91,6 @@ literal
// Lexer Rules
// ===========

// TODO(b/63180832): align implementation with spec at go/api-expr/langdef.md.
// At this point, it would be premature to do this as the spec is still a moving
// target. The current rules were c&p from some other code in google3, and are
// only approximative.

EQUALS : '==';
NOT_EQUALS : '!=';
LESS : '<';
Expand Down Expand Up @@ -127,31 +120,35 @@ TRUE : 'true';
FALSE : 'false';
NULL : 'null';

fragment EXPONENT : ('e' | 'E') ( '+' | '-' )? DIGIT+ ;
fragment DIGIT : '0'..'9' ;
fragment BACKSLASH : '\\';
fragment LETTER : 'A'..'Z' | 'a'..'z' ;
fragment DIGIT : '0'..'9' ;
fragment EXPONENT : ('e' | 'E') ( '+' | '-' )? DIGIT+ ;
fragment HEXDIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment OCTDIGIT : '0'..'7' ;
fragment RAW : 'r' | 'R';

// TODO: Ensure escape sequences mirror those supported in Google SQL.
fragment ESC_SEQ
: '\\' ~('0'..'9'|'a'..'f'|'A'..'F' | '\r' | '\n' | '\t' | 'u')
| UNI_SEQ
| OCT_SEQ
: ESC_CHAR_SEQ
| ESC_BYTE_SEQ
| ESC_UNI_SEQ
| ESC_OCT_SEQ
;

fragment ESC_CHAR_SEQ
: BACKSLASH ('a'|'b'|'f'|'n'|'r'|'t'|'v'|'"'|'\''|'\\'|'?'|'`')
;

fragment SPECIAL_ESC_CHAR
: ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
fragment ESC_OCT_SEQ
: BACKSLASH ('0'..'3') ('0'..'7') ('0'..'7')
;

fragment OCT_SEQ
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
fragment ESC_BYTE_SEQ
: BACKSLASH ( 'x' | 'X' ) HEXDIGIT HEXDIGIT
;

fragment UNI_SEQ
: '\\' 'u' HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT
fragment ESC_UNI_SEQ
: BACKSLASH 'u' HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT
| BACKSLASH 'U' HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT
;

WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ -> channel(HIDDEN) ;
Expand All @@ -173,8 +170,8 @@ NUM_UINT
;

STRING
: '"' (ESC_SEQ | ~('"'|'\n'|'\r'))* '"'
| '\'' (ESC_SEQ | ~('\''|'\n'|'\r'))* '\''
: '"' (ESC_SEQ | ~('\\'|'"'|'\n'|'\r'))* '"'
| '\'' (ESC_SEQ | ~('\\'|'\''|'\n'|'\r'))* '\''
| '"""' (ESC_SEQ | ~('\\'))*? '"""'
| '\'\'\'' (ESC_SEQ | ~('\\'))*? '\'\'\''
| RAW '"' ~('"'|'\n'|'\r')* '"'
Expand All @@ -183,8 +180,6 @@ STRING
| RAW '\'\'\'' .*? '\'\'\''
;

fragment RAW : 'r' | 'R';

BYTES : ('b' | 'B') STRING;

IDENTIFIER : (LETTER | '_') ( LETTER | DIGIT | '_')*;
IDENTIFIER : (LETTER | '_') ( LETTER | DIGIT | '_')*;
22 changes: 1 addition & 21 deletions parser/gen/cel_base_listener.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
// Copyright 2018 Google LLC
//
// Licensed 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.

// Generated from /Users/ozben/go/src/celgo/bin/../parser/gen/CEL.g4 by ANTLR 4.7.
// Generated from /Users/tswadell/lace/go/src/github.com/google/cel-go/bin/../parser/gen/CEL.g4 by ANTLR 4.7.

package gen // CEL
import "github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down Expand Up @@ -118,12 +104,6 @@ func (s *BaseCELListener) EnterIdentOrGlobalCall(ctx *IdentOrGlobalCallContext)
// ExitIdentOrGlobalCall is called when production IdentOrGlobalCall is exited.
func (s *BaseCELListener) ExitIdentOrGlobalCall(ctx *IdentOrGlobalCallContext) {}

// EnterDeprecatedIn is called when production DeprecatedIn is entered.
func (s *BaseCELListener) EnterDeprecatedIn(ctx *DeprecatedInContext) {}

// ExitDeprecatedIn is called when production DeprecatedIn is exited.
func (s *BaseCELListener) ExitDeprecatedIn(ctx *DeprecatedInContext) {}

// EnterNested is called when production Nested is entered.
func (s *BaseCELListener) EnterNested(ctx *NestedContext) {}

Expand Down
20 changes: 1 addition & 19 deletions parser/gen/cel_base_visitor.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
// Copyright 2018 Google LLC
//
// Licensed 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.

// Generated from /Users/ozben/go/src/celgo/bin/../parser/gen/CEL.g4 by ANTLR 4.7.
// Generated from /Users/tswadell/lace/go/src/github.com/google/cel-go/bin/../parser/gen/CEL.g4 by ANTLR 4.7.

package gen // CEL
import "github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down Expand Up @@ -77,10 +63,6 @@ func (v *BaseCELVisitor) VisitIdentOrGlobalCall(ctx *IdentOrGlobalCallContext) i
return v.VisitChildren(ctx)
}

func (v *BaseCELVisitor) VisitDeprecatedIn(ctx *DeprecatedInContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseCELVisitor) VisitNested(ctx *NestedContext) interface{} {
return v.VisitChildren(ctx)
}
Expand Down
Loading

0 comments on commit 2226ee7

Please sign in to comment.