1+ parser grammar CosmosDBParser;
2+
3+ options {
4+ tokenVocab = CosmosDBLexer;
5+ }
6+
7+ root : select EOF ;
8+
9+ select : select_clause from_clause where_clause?;
10+
11+ select_clause : SELECT_SYMBOL select_specification;
12+
13+ select_specification :
14+ MULTIPLY_OPERATOR
15+ | DISTINCT_SYMBOL ? object_property_list;
16+
17+ from_clause : FROM_SYMBOL from_specification;
18+
19+ where_clause : WHERE_SYMBOL scalar_expression_in_where;
20+
21+ from_specification : from_source;
22+
23+ from_source : container_expression;
24+
25+ container_expression : container_name (AS_SYMBOL ? IDENTIFIER )?;
26+
27+ container_name : IDENTIFIER ;
28+
29+ object_property_list :
30+ object_property (COMMA_SYMBOL object_property)*;
31+
32+ object_property : scalar_expression (AS_SYMBOL ? property_alias)?;
33+
34+ property_alias : IDENTIFIER ;
35+
36+ // scalar_expression: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/scalar-expressions
37+ scalar_expression :
38+ input_alias
39+ | scalar_expression DOT_SYMBOL property_name
40+ | scalar_expression LS_BRACKET_SYMBOL (
41+ (DOUBLE_QUOTE_STRING_LITERAL )
42+ | (array_index)
43+ ) RS_BRACKET_SYMBOL
44+ | unary_operator scalar_expression;
45+
46+ // TODO(zp): Merge scalar_expression and scalar_expression_in_where while supporting the project
47+ // fully. https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/scalar-expressions
48+ scalar_expression_in_where :
49+ constant
50+ | input_alias
51+ | parameter_name
52+ | scalar_expression_in_where AND_SYMBOL scalar_expression_in_where
53+ | scalar_expression_in_where OR_SYMBOL scalar_expression_in_where
54+ | scalar_expression_in_where DOT_SYMBOL property_name
55+ | scalar_expression_in_where LS_BRACKET_SYMBOL (
56+ (DOUBLE_QUOTE_STRING_LITERAL )
57+ | (array_index)
58+ ) RS_BRACKET_SYMBOL
59+ | unary_operator scalar_expression_in_where
60+ | scalar_expression_in_where binary_operator scalar_expression_in_where
61+ | scalar_expression_in_where QUESTION_MARK_SYMBOL scalar_expression_in_where COLON_SYMBOL
62+ scalar_expression_in_where
63+ | scalar_function_expression
64+ | create_object_expression
65+ | create_array_expression
66+ | LR_BRACKET_SYMBOL scalar_expression_in_where RR_BRACKET_SYMBOL ;
67+
68+ create_array_expression : array_constant;
69+
70+ create_object_expression : object_constant;
71+
72+ scalar_function_expression :
73+ udf_scalar_function_expression
74+ | builtin_function_expression;
75+
76+ udf_scalar_function_expression :
77+ UDF_SYMBOL DOT_SYMBOL IDENTIFIER LR_BRACKET_SYMBOL (
78+ scalar_expression_in_where (
79+ COMMA_SYMBOL scalar_expression_in_where
80+ )*
81+ ) RR_BRACKET_SYMBOL ;
82+
83+ builtin_function_expression :
84+ IDENTIFIER LR_BRACKET_SYMBOL (
85+ scalar_expression_in_where (
86+ COMMA_SYMBOL scalar_expression_in_where
87+ )*
88+ ) RR_BRACKET_SYMBOL ;
89+
90+ binary_operator :
91+ MULTIPLY_OPERATOR
92+ | DIVIDE_SYMBOL
93+ | MODULO_SYMBOL
94+ | PLUS_SYMBOL
95+ | MINUS_SYMBOL
96+ | BIT_AND_SYMBOL
97+ | BIT_XOR_SYMBOL
98+ | BIT_OR_SYMBOL
99+ | DOUBLE_BAR_SYMBOL
100+ | EQUAL_SYMBOL ;
101+
102+ unary_operator : BIT_NOT_SYMBOL | PLUS_SYMBOL | MINUS_SYMBOL ;
103+
104+ parameter_name : AT_SYMBOL IDENTIFIER ;
105+
106+ // https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/constants
107+ constant :
108+ undefined_constant
109+ | null_constant
110+ | boolean_constant
111+ | number_constant
112+ | string_constant
113+ | array_constant
114+ | object_constant;
115+
116+ object_constant :
117+ LC_BRACKET_SYMBOL (
118+ object_constant_field_pair (
119+ COMMA_SYMBOL object_constant_field_pair
120+ )*
121+ ) RC_BRACKET_SYMBOL ;
122+
123+ object_constant_field_pair : (
124+ property_name
125+ | (DOUBLE_QUOTE_SYMBOL property_name DOUBLE_QUOTE_SYMBOL )
126+ ) COMMA_SYMBOL constant;
127+
128+ array_constant :
129+ LS_BRACKET_SYMBOL (constant (COMMA_SYMBOL constant)*)? RS_BRACKET_SYMBOL ;
130+
131+ string_constant : string_literal;
132+
133+ undefined_constant : UNDEFINED_SYMBOL ;
134+
135+ null_constant : NULL_SYMBOL ;
136+
137+ boolean_constant : TRUE_SYMBOL | FALSE_SYMBOL ;
138+
139+ number_constant : decimal_literal | hexadecimal_literal;
140+
141+ string_literal :
142+ SINGLE_QUOTE_STRING_LITERAL
143+ | DOUBLE_QUOTE_STRING_LITERAL ;
144+
145+ decimal_literal : DECIMAL | REAL | FLOAT ;
146+
147+ hexadecimal_literal : HEXADECIMAL ;
148+
149+ property_name : IDENTIFIER ;
150+
151+ array_index : DECIMAL ;
152+
153+ input_alias : IDENTIFIER ;
0 commit comments