-
Notifications
You must be signed in to change notification settings - Fork 0
/
caql.l
142 lines (135 loc) · 6.7 KB
/
caql.l
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
number [+-]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?
name [A-Za-z0-9_-][\.A-Za-z0-9_-]*
escaped_name \[(?:(?!\])[^\\]|\\.)*\]
%options case-insensitive
%x sel fltr asgn loc ordby
%%
<INITIAL,sel,fltr,asgn,loc,ordby>\s+ /* skip whitespace */
"select" {
this.begin('sel');
return 'SELECT';
}
<sel,fltr,loc,ordby>"," return ','
<sel>"*" return '*'
<sel>"as" return 'AS'
<INITIAL,sel>"where" {
this.popState(); /* in: INITIAL */
this.begin('fltr');
return 'WHERE';
}
<sel,fltr>"order by" {
this.popState(); /* in: INITIAL */
this.begin('ordby');
return 'ORDERBY';
}
<sel>{name} return 'NAME'
<sel>{escaped_name} {
yytext = yytext.slice(1, -1);
return 'NAME';
}
<fltr>"and" return 'AND'
<fltr>"or" return 'OR'
<fltr>"not" return 'NOT'
<fltr>"&&" return 'AND'
<fltr>"||" return 'OR'
<sel,fltr,ordby><<EOF>> {
this.popState(); /* in: INITIAL */
return 'EOF';
}
<fltr>{name} {
this.begin('asgn');
return 'NAME';
}
<fltr>{escaped_name} {
this.begin('asgn');
yytext = yytext.slice(1, -1);
return 'NAME';
}
<fltr>"(" return '('
<fltr>")" return ')'
<asgn>"eq"|"gt"|"lt"|"gte"|"lte" return 'COMPARISON'
<asgn>">="|"<=" {
yytext = letterify(yytext);
return 'COMPARISON';
}
<asgn>"="|">"|"<" {
yytext = letterify(yytext);
return 'COMPARISON';
}
<asgn>"!="|"neq" return 'NOTEQUAL'
<asgn>"is" return 'IS'
<asgn>"not" return 'NOT'
<asgn>"contains" return 'CONTAINS'
<asgn>"like" return 'LIKE'
<asgn>"within" {
this.begin('loc');
return 'WITHIN';
}
<asgn>(\@\w+) {
this.popState(); /* in fltr */
return 'PARAM';
}
<asgn>(["'])(?:(?!\1)[^\\]|\\.)*\1 {
this.popState(); /* in: fltr */
return 'STRING';
}
<asgn>{number} {
this.popState(); /* in: fltr */
return 'NUMBER';
}
<asgn>"true" {
this.popState(); /* in fltr */
yytext = true;
return 'TRUE';
}
<asgn>"false" {
this.popState(); /* in fltr */
yytext = false;
return 'FALSE';
}
<asgn>"null" {
this.popState(); /* in fltr */
yytext = null;
return 'NULL';
}
<asgn>"missing" {
this.popState(); /* in fltr */
return 'MISSING';
}
<loc>{number} return 'NUMBER'
<loc>"of" return 'OF'
<loc>"and" {
this.popState(); /* in: asgn */
this.popState(); /* in: fltr */
return 'AND';
}
<loc>"or" {
this.popState(); /* in: asgn */
this.popState(); /* in: fltr */
return 'OR';
}
<loc>"order by" {
this.popState(); /* in: asgn */
this.popState(); /* in: fltr */
this.begin('ordby');
return 'ORDERBY';
}
<ordby>"ASC" return 'ASC'
<ordby>"DESC" return 'DESC'
<ordby>{name} return 'NAME'
<ordby>{escaped_name} {
yytext = yytext.slice(1, -1);
return 'NAME';
}
<INITIAL,loc><<EOF>> return 'EOF'
%%
function letterify(op) {
switch(op) {
case '=' : op = 'eq'; break;
case '>' : op = 'gt'; break;
case '<' : op = 'lt'; break;
case '>=' : op = 'gte'; break;
case '<=' : op = 'lte'; break;
}
return op;
}