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
99 100 101 102 |
# File 'lib/sqlpp/parser.rb', line 99 def self.parse(string) parser = new(string) parser.parse end |
Instance Method Details
#_eat(type_or_types, pattern = nil) ⇒ Object
488 489 490 |
# File 'lib/sqlpp/parser.rb', line 488 def _eat(type_or_types, pattern=nil) _next if _peek(type_or_types, pattern) end |
#_ensure_stream_empty! ⇒ Object
523 524 525 526 527 |
# File 'lib/sqlpp/parser.rb', line 523 def _ensure_stream_empty! unless _peek(:eof) raise TrailingTokens, _next.inspect end end |
#_expect(type_or_types, pattern = nil) ⇒ Object
509 510 511 512 513 514 515 516 517 |
# File 'lib/sqlpp/parser.rb', line 509 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
499 500 501 502 503 504 505 506 507 |
# File 'lib/sqlpp/parser.rb', line 499 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
519 520 521 |
# File 'lib/sqlpp/parser.rb', line 519 def _next @tokenizer.next end |
#_parse_atom ⇒ Object
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 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/sqlpp/parser.rb', line 397 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_case ⇒ Object
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 464 465 |
# File 'lib/sqlpp/parser.rb', line 437 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
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 |
# File 'lib/sqlpp/parser.rb', line 283 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
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/sqlpp/parser.rb', line 312 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
335 336 337 338 339 340 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 |
# File 'lib/sqlpp/parser.rb', line 335 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_expr3 ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/sqlpp/parser.rb', line 377 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 (op = _eat(:punct, /[-+*\/]/)) _eat :space AST::Expr.new(atom, op.text, _parse_expr3) else atom end end end |
#_parse_from ⇒ Object
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 |
# File 'lib/sqlpp/parser.rb', line 249 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
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/sqlpp/parser.rb', line 470 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_select ⇒ Object
— internal use —
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 |
# File 'lib/sqlpp/parser.rb', line 142 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
492 493 494 495 496 497 |
# File 'lib/sqlpp/parser.rb', line 492 def _peek(type_or_types, pattern=nil) token = _next _match(token, type_or_types, pattern) ensure @tokenizer.push(token) end |
#parse ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/sqlpp/parser.rb', line 108 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 —
122 123 124 125 126 |
# File 'lib/sqlpp/parser.rb', line 122 def parse_expression _parse_expr1 ensure _ensure_stream_empty! end |
#parse_from ⇒ Object
128 129 130 131 132 |
# File 'lib/sqlpp/parser.rb', line 128 def parse_from _parse_from ensure _ensure_stream_empty! end |
#parse_select ⇒ Object
134 135 136 137 138 |
# File 'lib/sqlpp/parser.rb', line 134 def parse_select _parse_select ensure _ensure_stream_empty! end |