rossoc 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ #
2
+ # Original source code
3
+ # sql-parser
4
+ # https://github.com/cryodex/sql-parser
5
+ #
6
+
7
+ class SQLParser::Parser
8
+
9
+ option
10
+ ignorecase
11
+
12
+ macro
13
+ DIGIT [0-9]
14
+ UINT {DIGIT}+
15
+ BLANK \s+
16
+
17
+ YEARS {UINT}
18
+ MONTHS {UINT}
19
+ DAYS {UINT}
20
+ DATE {YEARS}-{MONTHS}-{DAYS}
21
+
22
+ IDENT \w+
23
+
24
+ rule
25
+ # [:state] pattern [actions]
26
+
27
+ # literals
28
+ \"{DATE}\" { [:date_string, Date.parse(text)] }
29
+ \'{DATE}\' { [:date_string, Date.parse(text)] }
30
+
31
+ \' { @state = :STRS; [:quote, text] }
32
+ :STRS \' { @state = nil; [:quote, text] }
33
+ :STRS .*(?=\') { [:character_string_literal, text.gsub("''", "'")] }
34
+
35
+ \" { @state = :STRD; [:quote, text] }
36
+ :STRD \" { @state = nil; [:quote, text] }
37
+ :STRD .*(?=\") { [:character_string_literal, text.gsub('""', '"')] }
38
+
39
+ {UINT} { [:unsigned_integer, text.to_i] }
40
+
41
+ # skip
42
+ {BLANK} # no action
43
+
44
+ # keywords
45
+ SELECT { [:SELECT, text] }
46
+ DATE { [:DATE, text] }
47
+ ASC { [:ASC, text] }
48
+ AS { [:AS, text] }
49
+ FROM { [:FROM, text] }
50
+ WHERE { [:WHERE, text] }
51
+ BETWEEN { [:BETWEEN, text] }
52
+ AND { [:AND, text] }
53
+ NOT { [:NOT, text] }
54
+ INNER { [:INNER, text] }
55
+ INSERT { [:INSERT, text] }
56
+ INTO { [:INTO, text] }
57
+ IN { [:IN, text] }
58
+ ORDER { [:ORDER, text] }
59
+ OR { [:OR, text] }
60
+ LIKE { [:LIKE, text] }
61
+ IS { [:IS, text] }
62
+ NULL { [:NULL, text] }
63
+ COUNT { [:COUNT, text] }
64
+ AVG { [:AVG, text] }
65
+ MAX { [:MAX, text] }
66
+ MIN { [:MIN, text] }
67
+ SUM { [:SUM, text] }
68
+ GROUP { [:GROUP, text] }
69
+ BY { [:BY, text] }
70
+ HAVING { [:HAVING, text] }
71
+ CROSS { [:CROSS, text] }
72
+ JOIN { [:JOIN, text] }
73
+ ON { [:ON, text] }
74
+ LEFT { [:LEFT, text] }
75
+ OUTER { [:OUTER, text] }
76
+ RIGHT { [:RIGHT, text] }
77
+ FULL { [:FULL, text] }
78
+ USING { [:USING, text] }
79
+ EXISTS { [:EXISTS, text] }
80
+ DESC { [:DESC, text] }
81
+ CURRENT_USER { [:CURRENT_USER, text] }
82
+ VALUES { [:VALUES, text] }
83
+ RSLEEP { [:RSLEEP, text] }
84
+ RSPEED { [:RSPEED, text] }
85
+
86
+ # tokens
87
+ E { [:E, text] }
88
+
89
+ <> { [:not_equals_operator, text] }
90
+ != { [:not_equals_operator, text] }
91
+ = { [:equals_operator, text] }
92
+ <= { [:less_than_or_equals_operator, text] }
93
+ < { [:less_than_operator, text] }
94
+ >= { [:greater_than_or_equals_operator, text] }
95
+ > { [:greater_than_operator, text] }
96
+
97
+ \( { [:left_paren, text] }
98
+ \) { [:right_paren, text] }
99
+ \* { [:asterisk, text] }
100
+ \/ { [:solidus, text] }
101
+ \+ { [:plus_sign, text] }
102
+ \- { [:minus_sign, text] }
103
+ \. { [:period, text] }
104
+ , { [:comma, text] }
105
+
106
+ # identifier
107
+ `{IDENT}` { [:identifier, text[1..-2]] }
108
+ {IDENT} { [:identifier, text] }
109
+
110
+ ---- header ----
111
+ require 'date'
@@ -0,0 +1,301 @@
1
+ #--
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by rex 1.0.5
4
+ # from lexical definition file "lib/rossoc/parser.rex".
5
+ #++
6
+
7
+ require 'racc/parser'
8
+ #
9
+ # Original source code
10
+ # sql-parser
11
+ # https://github.com/cryodex/sql-parser
12
+ #
13
+
14
+ class SQLParser::Parser < Racc::Parser
15
+ require 'strscan'
16
+
17
+ class ScanError < StandardError ; end
18
+
19
+ attr_reader :lineno
20
+ attr_reader :filename
21
+ attr_accessor :state
22
+
23
+ def scan_setup(str)
24
+ @ss = StringScanner.new(str)
25
+ @lineno = 1
26
+ @state = nil
27
+ end
28
+
29
+ def action
30
+ yield
31
+ end
32
+
33
+ def scan_str(str)
34
+ scan_setup(str)
35
+ do_parse
36
+ end
37
+ alias :scan :scan_str
38
+
39
+ def load_file( filename )
40
+ @filename = filename
41
+ open(filename, "r") do |f|
42
+ scan_setup(f.read)
43
+ end
44
+ end
45
+
46
+ def scan_file( filename )
47
+ load_file(filename)
48
+ do_parse
49
+ end
50
+
51
+
52
+ def next_token
53
+ return if @ss.eos?
54
+
55
+ # skips empty actions
56
+ until token = _next_token or @ss.eos?; end
57
+ token
58
+ end
59
+
60
+ def _next_token
61
+ text = @ss.peek(1)
62
+ @lineno += 1 if text == "\n"
63
+ token = case @state
64
+ when nil
65
+ case
66
+ when (text = @ss.scan(/\"[0-9]+-[0-9]+-[0-9]+\"/i))
67
+ action { [:date_string, Date.parse(text)] }
68
+
69
+ when (text = @ss.scan(/\'[0-9]+-[0-9]+-[0-9]+\'/i))
70
+ action { [:date_string, Date.parse(text)] }
71
+
72
+ when (text = @ss.scan(/\'/i))
73
+ action { @state = :STRS; [:quote, text] }
74
+
75
+ when (text = @ss.scan(/\"/i))
76
+ action { @state = :STRD; [:quote, text] }
77
+
78
+ when (text = @ss.scan(/[0-9]+/i))
79
+ action { [:unsigned_integer, text.to_i] }
80
+
81
+ when (text = @ss.scan(/\s+/i))
82
+ ;
83
+
84
+ when (text = @ss.scan(/SELECT/i))
85
+ action { [:SELECT, text] }
86
+
87
+ when (text = @ss.scan(/DATE/i))
88
+ action { [:DATE, text] }
89
+
90
+ when (text = @ss.scan(/ASC/i))
91
+ action { [:ASC, text] }
92
+
93
+ when (text = @ss.scan(/AS/i))
94
+ action { [:AS, text] }
95
+
96
+ when (text = @ss.scan(/FROM/i))
97
+ action { [:FROM, text] }
98
+
99
+ when (text = @ss.scan(/WHERE/i))
100
+ action { [:WHERE, text] }
101
+
102
+ when (text = @ss.scan(/BETWEEN/i))
103
+ action { [:BETWEEN, text] }
104
+
105
+ when (text = @ss.scan(/AND/i))
106
+ action { [:AND, text] }
107
+
108
+ when (text = @ss.scan(/NOT/i))
109
+ action { [:NOT, text] }
110
+
111
+ when (text = @ss.scan(/INNER/i))
112
+ action { [:INNER, text] }
113
+
114
+ when (text = @ss.scan(/INSERT/i))
115
+ action { [:INSERT, text] }
116
+
117
+ when (text = @ss.scan(/INTO/i))
118
+ action { [:INTO, text] }
119
+
120
+ when (text = @ss.scan(/IN/i))
121
+ action { [:IN, text] }
122
+
123
+ when (text = @ss.scan(/ORDER/i))
124
+ action { [:ORDER, text] }
125
+
126
+ when (text = @ss.scan(/OR/i))
127
+ action { [:OR, text] }
128
+
129
+ when (text = @ss.scan(/LIKE/i))
130
+ action { [:LIKE, text] }
131
+
132
+ when (text = @ss.scan(/IS/i))
133
+ action { [:IS, text] }
134
+
135
+ when (text = @ss.scan(/NULL/i))
136
+ action { [:NULL, text] }
137
+
138
+ when (text = @ss.scan(/COUNT/i))
139
+ action { [:COUNT, text] }
140
+
141
+ when (text = @ss.scan(/AVG/i))
142
+ action { [:AVG, text] }
143
+
144
+ when (text = @ss.scan(/MAX/i))
145
+ action { [:MAX, text] }
146
+
147
+ when (text = @ss.scan(/MIN/i))
148
+ action { [:MIN, text] }
149
+
150
+ when (text = @ss.scan(/SUM/i))
151
+ action { [:SUM, text] }
152
+
153
+ when (text = @ss.scan(/GROUP/i))
154
+ action { [:GROUP, text] }
155
+
156
+ when (text = @ss.scan(/BY/i))
157
+ action { [:BY, text] }
158
+
159
+ when (text = @ss.scan(/HAVING/i))
160
+ action { [:HAVING, text] }
161
+
162
+ when (text = @ss.scan(/CROSS/i))
163
+ action { [:CROSS, text] }
164
+
165
+ when (text = @ss.scan(/JOIN/i))
166
+ action { [:JOIN, text] }
167
+
168
+ when (text = @ss.scan(/ON/i))
169
+ action { [:ON, text] }
170
+
171
+ when (text = @ss.scan(/LEFT/i))
172
+ action { [:LEFT, text] }
173
+
174
+ when (text = @ss.scan(/OUTER/i))
175
+ action { [:OUTER, text] }
176
+
177
+ when (text = @ss.scan(/RIGHT/i))
178
+ action { [:RIGHT, text] }
179
+
180
+ when (text = @ss.scan(/FULL/i))
181
+ action { [:FULL, text] }
182
+
183
+ when (text = @ss.scan(/USING/i))
184
+ action { [:USING, text] }
185
+
186
+ when (text = @ss.scan(/EXISTS/i))
187
+ action { [:EXISTS, text] }
188
+
189
+ when (text = @ss.scan(/DESC/i))
190
+ action { [:DESC, text] }
191
+
192
+ when (text = @ss.scan(/CURRENT_USER/i))
193
+ action { [:CURRENT_USER, text] }
194
+
195
+ when (text = @ss.scan(/VALUES/i))
196
+ action { [:VALUES, text] }
197
+
198
+ when (text = @ss.scan(/RSLEEP/i))
199
+ action { [:RSLEEP, text] }
200
+
201
+ when (text = @ss.scan(/RSPEED/i))
202
+ action { [:RSPEED, text] }
203
+
204
+ when (text = @ss.scan(/E/i))
205
+ action { [:E, text] }
206
+
207
+ when (text = @ss.scan(/<>/i))
208
+ action { [:not_equals_operator, text] }
209
+
210
+ when (text = @ss.scan(/!=/i))
211
+ action { [:not_equals_operator, text] }
212
+
213
+ when (text = @ss.scan(/=/i))
214
+ action { [:equals_operator, text] }
215
+
216
+ when (text = @ss.scan(/<=/i))
217
+ action { [:less_than_or_equals_operator, text] }
218
+
219
+ when (text = @ss.scan(/</i))
220
+ action { [:less_than_operator, text] }
221
+
222
+ when (text = @ss.scan(/>=/i))
223
+ action { [:greater_than_or_equals_operator, text] }
224
+
225
+ when (text = @ss.scan(/>/i))
226
+ action { [:greater_than_operator, text] }
227
+
228
+ when (text = @ss.scan(/\(/i))
229
+ action { [:left_paren, text] }
230
+
231
+ when (text = @ss.scan(/\)/i))
232
+ action { [:right_paren, text] }
233
+
234
+ when (text = @ss.scan(/\*/i))
235
+ action { [:asterisk, text] }
236
+
237
+ when (text = @ss.scan(/\//i))
238
+ action { [:solidus, text] }
239
+
240
+ when (text = @ss.scan(/\+/i))
241
+ action { [:plus_sign, text] }
242
+
243
+ when (text = @ss.scan(/\-/i))
244
+ action { [:minus_sign, text] }
245
+
246
+ when (text = @ss.scan(/\./i))
247
+ action { [:period, text] }
248
+
249
+ when (text = @ss.scan(/,/i))
250
+ action { [:comma, text] }
251
+
252
+ when (text = @ss.scan(/`\w+`/i))
253
+ action { [:identifier, text[1..-2]] }
254
+
255
+ when (text = @ss.scan(/\w+/i))
256
+ action { [:identifier, text] }
257
+
258
+ when (text = @ss.scan(/----/i))
259
+ ;
260
+
261
+ when (text = @ss.scan(/require/i))
262
+ ;
263
+
264
+ else
265
+ text = @ss.string[@ss.pos .. -1]
266
+ raise ScanError, "can not match: '" + text + "'"
267
+ end # if
268
+
269
+ when :STRS
270
+ case
271
+ when (text = @ss.scan(/\'/i))
272
+ action { @state = nil; [:quote, text] }
273
+
274
+ when (text = @ss.scan(/.*(?=\')/i))
275
+ action { [:character_string_literal, text.gsub("''", "'")] }
276
+
277
+ else
278
+ text = @ss.string[@ss.pos .. -1]
279
+ raise ScanError, "can not match: '" + text + "'"
280
+ end # if
281
+
282
+ when :STRD
283
+ case
284
+ when (text = @ss.scan(/\"/i))
285
+ action { @state = nil; [:quote, text] }
286
+
287
+ when (text = @ss.scan(/.*(?=\")/i))
288
+ action { [:character_string_literal, text.gsub('""', '"')] }
289
+
290
+ else
291
+ text = @ss.string[@ss.pos .. -1]
292
+ raise ScanError, "can not match: '" + text + "'"
293
+ end # if
294
+
295
+ else
296
+ raise ScanError, "undefined state: '" + state.to_s + "'"
297
+ end # case state
298
+ token
299
+ end # def _next_token
300
+
301
+ end # class