Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Parser.jay « Mono.Data.SqlExpressions « System.Data « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49078663593b63484b18a87ebd67fc150b59c28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
%{
//
// Parser.jay
//
// Author:
//   Juraj Skripsky (juraj@hotfeet.ch)
//
// (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
//

using System;
using System.Collections;
using System.Data;

namespace Mono.Data.SqlExpressions {

	internal class Parser {
		static Parser ()
		{
			if (Environment.GetEnvironmentVariable ("MONO_DEBUG_SQLEXPRESSIONS") != null)
				yacc_verbose_flag = 2;
		}

		bool cacheAggregationResults = false;
		DataRow[] aggregationRows = null;
		static int yacc_verbose_flag;
		
		//called by DataTable.Select
		//called by DataColumn.set_Expression //FIXME: enable cache in this case?
		public Parser () {
			ErrorOutput = System.IO.TextWriter.Null;
			cacheAggregationResults = true;
		}
		
		//called by DataTable.Compute
		public Parser (DataRow[] aggregationRows)
		{
			ErrorOutput = System.IO.TextWriter.Null;
			this.aggregationRows = aggregationRows;
		}
		
		public IExpression Compile (string sqlExpr)
		{
			try {
				Tokenizer tokenizer = new Tokenizer (sqlExpr);
				if (yacc_verbose_flag > 1)
					return (IExpression) yyparse (tokenizer,
						new yydebug.yyDebugSimple ());
				else
					return (IExpression) yyparse (tokenizer);
			} catch (yyParser.yyException e) {
				throw new SyntaxErrorException (String.Format ("Expression '{0}' is invalid.", sqlExpr));
			}
		}
%}

%token PAROPEN PARCLOSE

%token AND OR
%token NOT

%token TRUE FALSE
%token NULL

%token PARENT CHILD

%token EQ LT GT

%token PLUS MINUS
%token MUL DIV MOD

%token DOT COMMA

%token IS IN LIKE

%token COUNT SUM AVG MAX MIN STDEV VAR
%token IIF SUBSTRING ISNULL LEN TRIM CONVERT

%token StringLiteral NumberLiteral DateLiteral
%token Identifier
%token FunctionName

%start Expr

%left OR
%left AND
%left NOT

%left EQ LT GT

%left PLUS MINUS
%left MUL DIV MOD

%left UMINUS

%%

Expr
	: BoolExpr
	| ArithExpr
	;

BoolExpr
	: PAROPEN BoolExpr PARCLOSE
	{
		$$ = (IExpression)$2;
	}
	| BoolExpr AND BoolExpr
	{
		$$ = new BoolOperation (Operation.AND, (IExpression)$1, (IExpression)$3);
	}
	| BoolExpr OR BoolExpr
	{
		$$ = new BoolOperation (Operation.OR, (IExpression)$1, (IExpression)$3);
	}
	| NOT BoolExpr
	{
		$$ = new Negation ((IExpression)$2);
	}
	| Predicate
	;

Predicate
	: CompPredicate
	| IsPredicate
	| LikePredicate
	| InPredicate
	;

CompPredicate
	: ArithExpr CompOp ArithExpr
	{
		$$ = new Comparison ((Operation)$2, (IExpression)$1, (IExpression)$3);
	}
	;

CompOp
	: EQ	{ $$ = Operation.EQ; }
	| NE	{ $$ = Operation.NE; }
	| LT	{ $$ = Operation.LT; }
	| GT	{ $$ = Operation.GT; }
	| LE	{ $$ = Operation.LE; }
	| GE	{ $$ = Operation.GE; }
	;

LE	: LT EQ;		// <=
NE	: LT GT;		// <>
GE	: GT EQ;		// >=
	
ArithExpr
	: PAROPEN ArithExpr PARCLOSE
	{
		$$ = (IExpression)$2;
	}
	| ArithExpr MUL ArithExpr
	{
		$$ = new ArithmeticOperation (Operation.MUL, (IExpression)$1, (IExpression)$3);
	}
	| ArithExpr DIV ArithExpr
	{
		$$ = new ArithmeticOperation (Operation.DIV, (IExpression)$1, (IExpression)$3);
	}
	| ArithExpr MOD ArithExpr
	{
		$$ = new ArithmeticOperation (Operation.MOD, (IExpression)$1, (IExpression)$3);
	}
	| ArithExpr PLUS ArithExpr
	{
		$$ = new ArithmeticOperation (Operation.ADD, (IExpression)$1, (IExpression)$3);
	}
	| ArithExpr MINUS ArithExpr
	{
		$$ = new ArithmeticOperation (Operation.SUB, (IExpression)$1, (IExpression)$3);
	}
	| MINUS ArithExpr %prec UMINUS
	{
		$$ = new Negative ((IExpression)$2);
	}
	| Function
	| Value
	;

Value
	: LiteralValue
	| SingleColumnValue
	;

LiteralValue
	: StringLiteral	{ $$ = new Literal ($1); }
	| NumberLiteral	{ $$ = new Literal ($1); }
	| DateLiteral		{ $$ = new Literal ($1); }
	| BoolLiteral
	;

BoolLiteral
	: TRUE	{ $$ = new Literal (true); }
	| FALSE	{ $$ = new Literal (false); }
	;

SingleColumnValue
	: LocalColumnValue
	| ParentColumnValue
	;	

MultiColumnValue
	: LocalColumnValue
	| ChildColumnValue
	;

LocalColumnValue
	: ColumnName
	{
		$$ = new ColumnReference ((string)$1);
	}
	;

ParentColumnValue
	: PARENT DOT ColumnName
	{
		$$ = new ColumnReference (ReferencedTable.Parent, null, (string)$3);
	}
	| PARENT PAROPEN RelationName PARCLOSE DOT ColumnName
	{
		$$ = new ColumnReference (ReferencedTable.Parent, (string)$3, (string)$6);
	}
	;

ChildColumnValue
	: CHILD DOT ColumnName
	{
		$$ = new ColumnReference (ReferencedTable.Child, null, (string)$3);
	}
	| CHILD PAROPEN RelationName PARCLOSE DOT ColumnName
	{
		$$ = new ColumnReference (ReferencedTable.Child, (string)$3, (string)$6);
	}
	;
	
ColumnName
	: Identifier;
	
RelationName
	: Identifier;

Function
	: CalcFunction
	| AggFunction
	;

AggFunction
	: AggFunctionName PAROPEN MultiColumnValue PARCLOSE
	{
		$$ = new Aggregation (cacheAggregationResults, aggregationRows, (AggregationFunction)$1, (ColumnReference)$3);
	}
	;

AggFunctionName
	: COUNT		{ $$ = AggregationFunction.Count; }
	| SUM		{ $$ = AggregationFunction.Sum; }
	| AVG		{ $$ = AggregationFunction.Avg; }
	| MAX		{ $$ = AggregationFunction.Max; }
	| MIN		{ $$ = AggregationFunction.Min; }
	| STDEV	{ $$ = AggregationFunction.StDev; }
	| VAR		{ $$ = AggregationFunction.Var; }
	;

CalcFunction
	: IIF PAROPEN BoolExpr COMMA Expr COMMA Expr PARCLOSE
	{
		$$ = new IifFunction ((IExpression)$3, (IExpression)$5, (IExpression)$7);
	}
	| SUBSTRING PAROPEN Expr COMMA NumberLiteral COMMA NumberLiteral PARCLOSE
	{
		long arg1 = (long) $5;
		long arg2 = (long) $7;
		$$ = new SubstringFunction ((IExpression)$3, Convert.ToInt32(arg1), Convert.ToInt32(arg2));
	}
	| ISNULL PAROPEN SingleColumnValue COMMA Expr PARCLOSE
	{
		$$ = new IsNullFunction ((IExpression)$3, (IExpression)$5);
	}
	| LEN PAROPEN Expr PARCLOSE
	{
		$$ = new LenFunction ((IExpression)$3);
	}
	| TRIM PAROPEN Expr PARCLOSE
	{
		$$ = new TrimFunction ((IExpression)$3);
	}
	| CONVERT PAROPEN Expr COMMA TypeSpecifier PARCLOSE
	{
		$$ = new ConvertFunction ((IExpression)$3, (string)$5);
	}
	;

TypeSpecifier
	: StringLiteral
	| Identifier
	;

IsPredicate
	: SingleColumnValue IS NULL
	{
		$$ = new Comparison (Operation.EQ, (IExpression)$1, new Literal (null));
	}
	| SingleColumnValue IS NOT NULL
	{
		$$ = new Comparison (Operation.NE, (IExpression)$1, new Literal (null));
	}
	;

LikePredicate
	: ArithExpr LIKE StringLiteral
	{
		$$ = new Like ((IExpression)$1, (string)$3);
	}
	| ArithExpr NOT LIKE StringLiteral
	{
		$$ = new Negation (new Like ((IExpression)$1, (string)$3));
	}
	;

InPredicate
	: SingleColumnValue IN InPredicateValue
	{
		$$ = new In ((IExpression)$1, (IList)$3);
	}
	| SingleColumnValue NOT IN InPredicateValue
	{
		$$ = new Negation (new In ((IExpression)$1, (IList)$4));
	}
	;

InPredicateValue
	: PAROPEN InValueList PARCLOSE { $$ = $2; }
	;

InValueList
	: LiteralValue
	{
		$$ = new ArrayList();
		((IList)$$).Add ($1);
	}
	| InValueList COMMA LiteralValue
	{
		((IList)($$ = $1)).Add ($3);
	}
	;

%%
}