Class: SQLPP::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlpp/parser.rb

Defined Under Namespace

Classes: Exception, TrailingTokens, UnexpectedToken

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Parser

Returns a new instance of Parser.



93
94
95
# File 'lib/sqlpp/parser.rb', line 93

def initialize(string)
  @tokenizer = SQLPP::Tokenizer.new(string)
end

Class Method Details

.parse(string) ⇒ Object



88
89
90
91
# File 'lib/sqlpp/parser.rb', line 88

def self.parse(string)
  parser = new(string)
  parser.parse
end

Instance Method Details

#_eat(type_or_types, pattern = nil) ⇒ Object



457
458
459
# File 'lib/sqlpp/parser.rb', line 457

def _eat(type_or_types, pattern=nil)
  _next if _peek(type_or_types, pattern)
end

#_ensure_stream_empty!Object



492
493
494
495
496
# File 'lib/sqlpp/parser.rb', line 492

def _ensure_stream_empty!
  unless _peek(:eof)
    raise TrailingTokens, _next.inspect
  end
end

#_expect(type_or_types, pattern = nil) ⇒ Object



478
479
480
481
482
483
484
485
486
# File 'lib/sqlpp/parser.rb', line 478

def _expect(type_or_types, pattern=nil)
  token = _next

  if !_match(token, type_or_types, pattern)
    raise UnexpectedToken, "expected #{type_or_types.inspect}(#{pattern.inspect}), got #{token.inspect}"
  end

  token
end

#_match(token, type_or_types, pattern = nil) ⇒ Object



468
469
470
471
472
473
474
475
476
# File 'lib/sqlpp/parser.rb', line 468

def _match(token, type_or_types, pattern=nil)
  types = type_or_types.is_a?(Array) ? type_or_types : [ type_or_types ]

  if types.include?(token.type) && (pattern.nil? || pattern === token.text)
    token
  else
    nil
  end
end

#_nextObject



488
489
490
# File 'lib/sqlpp/parser.rb', line 488

def _next
  @tokenizer.next
end

#_parse_atomObject



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/sqlpp/parser.rb', line 366

def _parse_atom
  if (lit = _eat(:lit))
    AST::Atom.new(:lit, lit.text)

  elsif (key = _eat(:key, :case))
    _parse_case

  elsif _eat(:punct, "(")
    expr = _parse_expr1
    _eat :space
    _expect(:punct, ")")
    AST::Parens.new(expr)

  elsif _eat(:key, :null)
    AST::Atom.new(:lit, "NULL")

  elsif _eat(:punct, "*")
    AST::Atom.new(:lit, "*")

  else
    id = _expect(:id)

    if _eat(:punct, "(")
      args = _parse_list
      _expect(:punct, ")")
      AST::Atom.new(:func, id.text, args)
    elsif _eat(:punct, '.')
      id2 = _eat(:id) || _eat(:punct, '*')

      if !id2
        raise UnexpectedToken, "expected id or *, got #{_peek.inspect}"
      end

      AST::Atom.new(:attr, id.text, id2.text)
    else
      AST::Atom.new(:attr, id.text)
    end
  end
end

#_parse_caseObject



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/sqlpp/parser.rb', line 406

def _parse_case
  _expect :space

  kase = AST::Atom.new(:case)
  unless _peek(:key, :when)
    kase.left = _parse_expr1
    _eat :space
  end

  cases = []
  while _eat(:key, :when)
    condition = _parse_expr1
    _eat :space
    _expect :key, :then
    result = _parse_expr1
    cases << [condition, result]
    _eat :space
  end

  if _eat(:key, :else)
    cases << _parse_expr1
    _eat :space
  end

  _expect :key, :end

  kase.right = cases
  kase
end

#_parse_entityObject



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
# File 'lib/sqlpp/parser.rb', line 252

def _parse_entity
  _eat :space

  entity = if _eat(:punct, '(')
      from = _parse_from
      _eat :space
      _expect :punct, ')'
      AST::Parens.new(from)

    elsif _peek(:key, :select)
      _parse_select

    else
      id = _expect(:id)
      AST::Atom.new(:attr, id.text)
    end

  _eat :space
  if _eat(:key, :as)
    _eat :space
    id = _expect(:id)
    AST::As.new(id.text, entity)
  elsif (id = _eat(:id))
    AST::Alias.new(id.text, entity)
  else
    entity
  end
end

#_parse_expr1Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/sqlpp/parser.rb', line 281

def _parse_expr1
  _eat :space

  left = _parse_expr2
  _eat :space

  if (op = _eat(:key, /^(and|or|is)$/i))
    op = op.text

    if op == :is
      _eat :space
      op2 = _eat(:key, :not)
      op = "#{op} #{op2.text}" if op2
    end

    right = _parse_expr1

    AST::Expr.new(left, op, right)
  else
    left
  end
end

#_parse_expr2Object



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
# File 'lib/sqlpp/parser.rb', line 304

def _parse_expr2
  _eat :space

  left = _parse_expr3
  _eat :space

  if (op = _eat(:key, :between))
    op = op.text

    _eat :space
    lo = _parse_expr3

    _eat :space
    _expect :key, :and

    _eat :space
    hi = _parse_expr3

    right = AST::Atom.new(:range, lo, hi)

  elsif (op = _eat(:key, :in))
    op = op.text

    _eat :space
    _expect :punct, "("

    right = AST::Atom.new(:list, _parse_list)
    _eat :space
    _expect :punct, ")"

  elsif (op = _eat(:punct, /<=|<>|>=|=|<|>/) || _eat(:key, /^i?like$/))
    op = op.text
    right = _parse_expr3
  end

  if right
    AST::Expr.new(left, op, right)
  else
    left
  end
end

#_parse_expr3Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/sqlpp/parser.rb', line 346

def _parse_expr3
  _eat :space

  if (op = (_eat(:punct, /[-+]/) || _eat(:key, :not)))
    _eat :space
    AST::Unary.new(op.text, _parse_expr3)

  else
    atom = _parse_atom
    _eat :space

    if (op = _eat(:punct, /[-+*\/]/))
      _eat :space
      AST::Expr.new(atom, op.text, _parse_expr3)
    else
      atom
    end
  end
end

#_parse_fromObject



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
# File 'lib/sqlpp/parser.rb', line 218

def _parse_from
  entity = _parse_entity

  loop do
    _eat :space

    if (which = _eat(:key, /^(inner|cross|left|right|full|outer)$/))
      type = which.text.to_s

      if type == "full" || type == "left" || type == "right"
        _eat :space
        _expect :key, :outer
        type << " outer"
      end

      _eat :space
      _expect :key, :join

      entity = AST::Join.new(type.downcase, entity, _parse_from)

      _eat :space
      if _eat(:key, :on)
        _eat :space
        entity.on = _parse_expr1
      end

    else
      break
    end
  end

  entity
end

#_parse_listObject

list := ”

| expr
| expr ',' args


439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/sqlpp/parser.rb', line 439

def _parse_list
  _eat :space
  args = []

  loop do
    args << _parse_expr1

    _eat :space
    if _eat(:punct, ",")
      _eat :space
    else
      break
    end
  end

  args
end

#_parse_selectObject

— internal use —



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
# File 'lib/sqlpp/parser.rb', line 131

def _parse_select
  _expect :key, :select
  select = AST::Select.new

  _eat :space

  if !_peek(:key, /^(from|where)$/) && !_peek(:eof)
    list = []

    loop do
      expr = _parse_expr1
      _eat :space
      if _peek(:key, :as)
        _next
        _eat :space
        name = _expect(:id)
        expr = AST::As.new(name.text, expr)
      end
      list.push expr
      break unless _eat(:punct, ",")
    end
    _eat :space

    select.projections = list
  end

  if _eat(:key, :from)
    list = []

    loop do
      _eat :space
      list << _parse_from
      _eat :space
      break unless _eat(:punct, ',')
    end
    _eat :space

    select.froms = list
  end

  if _eat(:key, :where)
    select.wheres = _parse_expr1
    _eat :space
  end

  if _eat(:key, :group)
    _eat :space
    _expect :key, :by
    _eat :space
    select.groups = _parse_list
  end

  if _eat(:key, :order)
    _eat :space
    _expect :key, :by
    _eat :space

    list = []
    loop do
      key = AST::SortKey.new(_parse_expr1, [])
      list << key

      _eat :space

      if (dir = _eat(:key, /^(asc|desc)$/))
        _eat :space
        key.options << dir.text
      end

      if (opt = _eat(:key, :nulls))
        opt = opt.text.to_s
        _eat :space
        sort = _eat(:key, /^(first|last)$/)
        opt << " " << sort.text.to_s if sort
        key.options << opt
      end

      _eat :space
      break unless _eat(:punct, ",")
    end

    select.orders = list
  end

  select
end

#_peek(type_or_types, pattern = nil) ⇒ Object



461
462
463
464
465
466
# File 'lib/sqlpp/parser.rb', line 461

def _peek(type_or_types, pattern=nil)
  token = _next
  _match(token, type_or_types, pattern)
ensure
  @tokenizer.push(token)
end

#parseObject

Raises:



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sqlpp/parser.rb', line 97

def parse
  _eat :space

  token = _peek(:key)
  raise UnexpectedToken, token.inspect unless token

  case token.text
    when :select then parse_select
    else raise UnexpectedToken, token.inspect
  end
end

#parse_expressionObject

— exposed for testing purposes —



111
112
113
114
115
# File 'lib/sqlpp/parser.rb', line 111

def parse_expression
  _parse_expr1
ensure
  _ensure_stream_empty!
end

#parse_fromObject



117
118
119
120
121
# File 'lib/sqlpp/parser.rb', line 117

def parse_from
  _parse_from
ensure
  _ensure_stream_empty!
end

#parse_selectObject



123
124
125
126
127
# File 'lib/sqlpp/parser.rb', line 123

def parse_select
  _parse_select
ensure
  _ensure_stream_empty!
end