Class: SQLPP::Parser
- Inherits:
-
Object
- Object
- SQLPP::Parser
- Defined in:
- lib/sqlpp/parser.rb
Defined Under Namespace
Classes: Exception, TrailingTokens, UnexpectedToken
Class Method Summary collapse
Instance Method Summary collapse
- #_eat(type_or_types, pattern = nil) ⇒ Object
- #_ensure_stream_empty! ⇒ Object
- #_expect(type_or_types, pattern = nil) ⇒ Object
- #_match(token, type_or_types, pattern = nil) ⇒ Object
- #_next ⇒ Object
- #_parse_atom ⇒ Object
- #_parse_case ⇒ Object
- #_parse_entity ⇒ Object
- #_parse_expr1 ⇒ Object
- #_parse_expr2 ⇒ Object
- #_parse_expr3 ⇒ Object
- #_parse_from ⇒ Object
-
#_parse_list ⇒ Object
list := ” | expr | expr ‘,’ args.
-
#_parse_select ⇒ Object
— internal use —.
- #_peek(type_or_types, pattern = nil) ⇒ Object
-
#initialize(string) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
-
#parse_expression ⇒ Object
— exposed for testing purposes —.
- #parse_from ⇒ Object
- #parse_select ⇒ Object
Constructor Details
Class Method Details
.parse(string) ⇒ Object
105 106 107 108 |
# File 'lib/sqlpp/parser.rb', line 105 def self.parse(string) parser = new(string) parser.parse end |
Instance Method Details
#_eat(type_or_types, pattern = nil) ⇒ Object
517 518 519 |
# File 'lib/sqlpp/parser.rb', line 517 def _eat(type_or_types, pattern=nil) _next if _peek(type_or_types, pattern) end |
#_ensure_stream_empty! ⇒ Object
552 553 554 555 556 |
# File 'lib/sqlpp/parser.rb', line 552 def _ensure_stream_empty! unless _peek(:eof) raise TrailingTokens, _next.inspect end end |
#_expect(type_or_types, pattern = nil) ⇒ Object
538 539 540 541 542 543 544 545 546 |
# File 'lib/sqlpp/parser.rb', line 538 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
528 529 530 531 532 533 534 535 536 |
# File 'lib/sqlpp/parser.rb', line 528 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 |
#_next ⇒ Object
548 549 550 |
# File 'lib/sqlpp/parser.rb', line 548 def _next @tokenizer.next end |
#_parse_atom ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/sqlpp/parser.rb', line 425 def _parse_atom if (lit = _eat(:lit)) AST::Atom.new(:lit, lit.text) elsif _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_case ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/sqlpp/parser.rb', line 465 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_entity ⇒ Object
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 |
# File 'lib/sqlpp/parser.rb', line 289 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_expr1 ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/sqlpp/parser.rb', line 318 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_expr2 ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/sqlpp/parser.rb', line 341 def _parse_expr2 _eat :space left = _parse_expr3 _eat :space not_kw = _eat(:key, :not) _eat :space if not_kw 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, not_kw != nil) elsif not_kw raise UnexpectedToken, "got #{not_kw.inspect}" else left end end |
#_parse_expr3 ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/sqlpp/parser.rb', line 388 def _parse_expr3 _eat :space if (op = (_eat(:punct, /[-+]/) || _eat(:key, /^(not|distinct)$/))) _eat :space AST::Unary.new(op.text, _parse_expr3) else atom = _parse_atom _eat :space if _eat(:punct, "[") subscript = _parse_expr1 _eat :space _expect(:punct, "]") _eat :space atom = AST::Subscript.new(atom, subscript) end if _eat(:punct, "::") _eat :space type = _parse_atom _eat :space atom = AST::TypeCast.new(atom, type) end if (op = _eat(:punct, /[-+*\/]/)) _eat :space AST::Expr.new(atom, op.text, _parse_expr3) else atom end end end |
#_parse_from ⇒ Object
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 |
# File 'lib/sqlpp/parser.rb', line 255 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_list ⇒ Object
list := ”
| expr
| expr ',' args
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/sqlpp/parser.rb', line 498 def _parse_list _eat :space args = [] return args if _peek(:punct, ')') loop do args << _parse_expr1 _eat :space if _eat(:punct, ",") _eat :space else break end end args end |
#_parse_select ⇒ Object
— internal use —
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 |
# File 'lib/sqlpp/parser.rb', line 148 def _parse_select _expect :key, :select select = AST::Select.new _eat :space if _eat(:key, :distinct) select.distinct = true _eat :space end 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. << 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. << opt end _eat :space break unless _eat(:punct, ",") end select.orders = list end if _eat(:key, :limit) _eat :space atom = _parse_atom _eat :space select.limit = AST::Limit.new(atom) end if _eat(:key, :offset) _eat :space atom = _parse_atom _eat :space select.offset = AST::Offset.new(atom) end select end |
#_peek(type_or_types, pattern = nil) ⇒ Object
521 522 523 524 525 526 |
# File 'lib/sqlpp/parser.rb', line 521 def _peek(type_or_types, pattern=nil) token = _next _match(token, type_or_types, pattern) ensure @tokenizer.push(token) end |
#parse ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sqlpp/parser.rb', line 114 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_expression ⇒ Object
— exposed for testing purposes —
128 129 130 131 132 |
# File 'lib/sqlpp/parser.rb', line 128 def parse_expression _parse_expr1 ensure _ensure_stream_empty! end |
#parse_from ⇒ Object
134 135 136 137 138 |
# File 'lib/sqlpp/parser.rb', line 134 def parse_from _parse_from ensure _ensure_stream_empty! end |
#parse_select ⇒ Object
140 141 142 143 144 |
# File 'lib/sqlpp/parser.rb', line 140 def parse_select _parse_select ensure _ensure_stream_empty! end |