github.com/wfusion/gofusion@v1.1.14/common/utils/sqlparser/parser.go (about) 1 package sqlparser 2 3 import ( 4 "errors" 5 "io" 6 "strings" 7 ) 8 9 var ( 10 // ErrNotImplemented not implemented 11 ErrNotImplemented = errors.New("not implemented") 12 ) 13 14 // Parser represents a SQL parser. 15 type Parser struct { 16 l *Lexer 17 18 pos Pos // current position 19 tok Token // current token 20 lit string // current literal value 21 full bool // buffer full 22 binds int // bind count 23 } 24 25 // NewParser returns a new instance of Parser that reads from r. 26 func NewParser(r io.Reader) *Parser { 27 return &Parser{ 28 l: NewLexer(r), 29 } 30 } 31 32 // ParseExprString parses s into an expression. Returns nil if s is blank. 33 func ParseExprString(s string) (Expr, error) { 34 if s == "" { 35 return nil, nil 36 } 37 return NewParser(strings.NewReader(s)).ParseExpr() 38 } 39 40 // MustParseExprString parses s into an expression. Panic on error. 41 func MustParseExprString(s string) Expr { 42 expr, err := ParseExprString(s) 43 if err != nil { 44 panic(err) 45 } 46 return expr 47 } 48 49 func (p *Parser) ParseStatement() (stmt Statement, err error) { 50 switch tok := p.peek(); tok { 51 case EOF: 52 return nil, io.EOF 53 default: 54 if stmt, err = p.parseStatement(); err != nil { 55 return stmt, err 56 } 57 } 58 59 // Read trailing semicolon or end of file. 60 if tok := p.peek(); tok != EOF && tok != SEMI { 61 return stmt, p.errorExpected(p.pos, p.tok, "semicolon or EOF") 62 } 63 p.lex() 64 65 return stmt, nil 66 } 67 68 // parseStmt parses all statement types. 69 func (p *Parser) parseStatement() (Statement, error) { 70 switch p.peek() { 71 case SELECT: 72 return p.parseSelectStatement(false) 73 case INSERT: 74 return p.parseInsertStatement() 75 case UPDATE: 76 return p.parseUpdateStatement() 77 case DELETE: 78 return p.parseDeleteStatement() 79 default: 80 return nil, p.errorExpected(p.pos, p.tok, "statement") 81 } 82 } 83 84 func (p *Parser) parseIdent(desc string) (*Ident, error) { 85 pos, tok, lit := p.lex() 86 switch tok { 87 case IDENT, QIDENT: 88 return identByNameAndTok(lit, tok), nil 89 default: 90 return nil, p.errorExpected(pos, tok, desc) 91 } 92 } 93 94 func (p *Parser) parseInsertStatement() (_ *InsertStatement, err error) { 95 assert(p.peek() == INSERT) 96 p.lex() 97 98 var stmt InsertStatement 99 100 if p.peek() != INTO { 101 return &stmt, p.errorExpected(p.pos, p.tok, "INTO") 102 } 103 p.lex() 104 105 // Parse table name & optional alias. 106 if stmt.TableName, err = p.parseTableName(); err != nil { 107 return &stmt, err 108 } 109 110 // Parse optional column list. 111 if p.peek() == LP { 112 p.lex() 113 for { 114 col, err := p.parseIdent("column name") 115 if err != nil { 116 return &stmt, err 117 } 118 stmt.ColumnNames = append(stmt.ColumnNames, col) 119 120 if p.peek() == RP { 121 break 122 } else if p.peek() != COMMA { 123 return &stmt, p.errorExpected(p.pos, p.tok, "comma or right paren") 124 } 125 p.lex() 126 } 127 p.lex() 128 } 129 130 if p.peek() == OVERRIDING { 131 p.lex() 132 if p.peek() == SYSTEM { 133 stmt.Overriding = "system" 134 } else if p.peek() == USER { 135 stmt.Overriding = "user" 136 } else { 137 return &stmt, p.errorExpected(p.pos, p.tok, "SYSTEM or USER") 138 } 139 if p.peek() != VALUE { 140 return &stmt, p.errorExpected(p.pos, p.tok, "VALUE") 141 } 142 p.lex() 143 } 144 145 switch p.peek() { 146 case DEFAULT: 147 p.lex() 148 if p.peek() != VALUES { 149 return &stmt, p.errorExpected(p.pos, p.tok, "VALUES") 150 } 151 stmt.DefaultValues = true 152 p.lex() 153 case VALUES: 154 p.lex() 155 for { 156 var exprs Exprs 157 if p.peek() != LP { 158 return &stmt, p.errorExpected(p.pos, p.tok, "left paren") 159 } 160 p.lex() 161 162 for { 163 expr, err := p.ParseExpr() 164 if err != nil { 165 return &stmt, err 166 } 167 exprs.Exprs = append(exprs.Exprs, expr) 168 169 if p.peek() == RP { 170 break 171 } else if p.peek() != COMMA { 172 return &stmt, p.errorExpected(p.pos, p.tok, "comma or right paren") 173 } 174 p.lex() 175 } 176 p.lex() 177 stmt.Expressions = append(stmt.Expressions, &exprs) 178 179 if p.peek() != COMMA { 180 break 181 } 182 p.lex() 183 } 184 case SELECT: 185 if stmt.Query, err = p.parseSelectStatement(false); err != nil { 186 return &stmt, err 187 } 188 default: 189 return &stmt, p.errorExpected(p.pos, p.tok, "DEFAULT VALUES, VALUES or SELECT") 190 } 191 192 // Parse optional upsert clause. 193 if p.peek() == ON { 194 if stmt.UpsertClause, err = p.parseUpsertClause(); err != nil { 195 return &stmt, err 196 } 197 } 198 199 if p.peek() == RETURNING { 200 p.lex() 201 if stmt.OutputExpressions, err = p.parseOutputNames(); err != nil { 202 return &stmt, err 203 } 204 } 205 206 return &stmt, nil 207 } 208 209 func (p *Parser) parseUpsertClause() (_ *UpsertClause, err error) { 210 assert(p.peek() == ON) 211 212 var clause UpsertClause 213 214 // Parse "ON CONFLICT" 215 p.lex() 216 if p.peek() != CONFLICT { 217 return &clause, p.errorExpected(p.pos, p.tok, "CONFLICT") 218 } 219 p.lex() 220 221 // Parse optional indexed column list & WHERE conditional. 222 if p.peek() == LP { 223 p.lex() 224 for { 225 col, err := p.parseIndexedColumn() 226 if err != nil { 227 return &clause, err 228 } 229 clause.Columns = append(clause.Columns, col) 230 231 if p.peek() == RP { 232 break 233 } else if p.peek() != COMMA { 234 return &clause, p.errorExpected(p.pos, p.tok, "comma or right paren") 235 } 236 p.lex() 237 } 238 p.lex() 239 240 if p.peek() == WHERE { 241 p.lex() 242 if clause.WhereExpr, err = p.ParseExpr(); err != nil { 243 return &clause, err 244 } 245 } 246 } 247 248 // Parse "DO NOTHING" or "DO UPDATE SET". 249 if p.peek() != DO { 250 return &clause, p.errorExpected(p.pos, p.tok, "DO") 251 } 252 p.lex() 253 254 // If next token is NOTHING, then read it and exit immediately. 255 if p.peek() == NOTHING { 256 p.lex() 257 clause.DoNothing = true 258 return &clause, nil 259 } else if p.peek() != UPDATE { 260 return &clause, p.errorExpected(p.pos, p.tok, "NOTHING or UPDATE SET") 261 } 262 263 // Otherwise parse "UPDATE SET" 264 p.lex() 265 clause.DoUpdate = true 266 if p.peek() != SET { 267 return &clause, p.errorExpected(p.pos, p.tok, "SET") 268 } 269 p.lex() 270 271 // Parse list of assignments. 272 for { 273 assignment, err := p.parseAssignment() 274 if err != nil { 275 return &clause, err 276 } 277 clause.Assignments = append(clause.Assignments, assignment) 278 279 if p.peek() != COMMA { 280 break 281 } 282 p.lex() 283 } 284 285 // Parse WHERE after DO UPDATE SET. 286 if p.peek() == WHERE { 287 p.lex() 288 if clause.UpdateWhereExpr, err = p.ParseExpr(); err != nil { 289 return &clause, err 290 } 291 } 292 293 return &clause, nil 294 } 295 296 func (p *Parser) parseIndexedColumn() (_ *IndexedColumn, err error) { 297 var col IndexedColumn 298 if col.X, err = p.ParseExpr(); err != nil { 299 return &col, err 300 } 301 if p.peek() == ASC { 302 p.lex() 303 col.Asc = true 304 } else if p.peek() == DESC { 305 p.lex() 306 col.Desc = true 307 } 308 return &col, nil 309 } 310 311 func (p *Parser) parseUpdateStatement() (_ *UpdateStatement, err error) { 312 assert(p.peek() == UPDATE) 313 p.lex() 314 315 var stmt UpdateStatement 316 317 if stmt.TableName, err = p.parseTableName(); err != nil { 318 return &stmt, err 319 } 320 321 // Parse SET + list of assignments. 322 if p.peek() != SET { 323 return &stmt, p.errorExpected(p.pos, p.tok, "SET") 324 } 325 p.lex() 326 327 for { 328 assignment, err := p.parseAssignment() 329 if err != nil { 330 return &stmt, err 331 } 332 stmt.Assignments = append(stmt.Assignments, assignment) 333 334 if p.peek() != COMMA { 335 break 336 } 337 p.lex() 338 } 339 340 if p.peek() == WHERE { 341 p.lex() 342 if p.peek() == CURRENT { 343 p.lex() 344 if p.peek() != OF { 345 return &stmt, p.errorExpected(p.pos, p.tok, "OF") 346 } 347 p.lex() 348 349 if stmt.CursorName, err = p.parseIdent("cursor name"); err != nil { 350 return &stmt, err 351 } 352 } else { 353 if stmt.Condition, err = p.ParseExpr(); err != nil { 354 return &stmt, err 355 } 356 } 357 } 358 359 if p.peek() == RETURNING { 360 p.lex() 361 if stmt.OutputExpressions, err = p.parseOutputNames(); err != nil { 362 return &stmt, err 363 } 364 } 365 366 return &stmt, nil 367 } 368 369 func (p *Parser) parseDeleteStatement() (_ *DeleteStatement, err error) { 370 assert(p.peek() == DELETE) 371 p.lex() 372 assert(p.peek() == FROM) 373 p.lex() 374 375 var stmt DeleteStatement 376 377 if p.peek() == ONLY { 378 stmt.Only = true 379 p.lex() 380 } 381 382 if stmt.TableName, err = p.parseTableName(); err != nil { 383 return &stmt, err 384 } 385 386 if p.peek() == STAR { 387 stmt.TableStar = true 388 p.lex() 389 } 390 391 if p.peek() == AS { 392 p.lex() 393 } 394 if isIdentToken(p.peek()) { 395 if stmt.Alias, err = p.parseIdent("alias"); err != nil { 396 return &stmt, err 397 } 398 } 399 400 if p.peek() == USING { 401 p.lex() 402 return &stmt, ErrNotImplemented 403 } 404 405 if p.peek() == WHERE { 406 p.lex() 407 if p.peek() == CURRENT { 408 p.lex() 409 if p.peek() != OF { 410 return &stmt, p.errorExpected(p.pos, p.tok, "OF") 411 } 412 p.lex() 413 414 if stmt.CursorName, err = p.parseIdent("cursor name"); err != nil { 415 return &stmt, err 416 } 417 } else { 418 if stmt.Condition, err = p.ParseExpr(); err != nil { 419 return &stmt, err 420 } 421 } 422 } 423 424 if p.peek() == RETURNING { 425 p.lex() 426 if stmt.OutputExpressions, err = p.parseOutputNames(); err != nil { 427 return &stmt, err 428 } 429 } 430 431 return &stmt, nil 432 } 433 434 func (p *Parser) parseAssignment() (_ *Assignment, err error) { 435 var assignment Assignment 436 437 // Parse either a single column (IDENT) or a column list (LP IDENT COMMA IDENT RP) 438 if isIdentToken(p.peek()) { 439 col, _ := p.parseIdent("column name") 440 assignment.Columns = []*Ident{col} 441 } else if p.peek() == LP { 442 p.lex() 443 for { 444 col, err := p.parseIdent("column name") 445 if err != nil { 446 return &assignment, err 447 } 448 assignment.Columns = append(assignment.Columns, col) 449 450 if p.peek() == RP { 451 break 452 } else if p.peek() != COMMA { 453 return &assignment, p.errorExpected(p.pos, p.tok, "comma or right paren") 454 } 455 p.lex() 456 } 457 p.lex() 458 } else { 459 return &assignment, p.errorExpected(p.pos, p.tok, "column name or column list") 460 } 461 462 if p.peek() != EQ { 463 return &assignment, p.errorExpected(p.pos, p.tok, "=") 464 } 465 p.lex() 466 467 if assignment.Expr, err = p.ParseExpr(); err != nil { 468 return &assignment, err 469 } 470 471 return &assignment, nil 472 } 473 474 // parseSelectStatement parses a SELECT statement. 475 // If compounded is true, WITH, ORDER BY, & LIMIT/OFFSET are skipped. 476 func (p *Parser) parseSelectStatement(compounded bool) (_ *SelectStatement, err error) { 477 var stmt SelectStatement 478 479 if p.peek() != SELECT { 480 return &stmt, p.errorExpected(p.pos, p.tok, "SELECT") 481 } 482 p.lex() 483 484 // Parse optional hint 485 if tok := p.peek(); tok == MLCOMMENT { 486 _, _, lit := p.lex() 487 stmt.Hint = &Hint{Value: lit} 488 } 489 490 // Parse optional "DISTINCT" or "ALL". 491 if tok := p.peek(); tok == ALL { 492 stmt.All = true 493 p.lex() 494 } else if tok == DISTINCT { 495 stmt.Distinct = true 496 p.lex() 497 } 498 499 // Parse result columns. 500 if stmt.Columns, err = p.parseOutputNames(); err != nil { 501 return &stmt, err 502 } 503 504 // Parse FROM clause. 505 if p.peek() == FROM { 506 p.lex() 507 if stmt.FromItems, err = p.parseSource(); err != nil { 508 return &stmt, err 509 } 510 } 511 512 // Parse WHERE clause. 513 if p.peek() == WHERE { 514 p.lex() 515 if stmt.Condition, err = p.ParseExpr(); err != nil { 516 return &stmt, err 517 } 518 } 519 520 // Parse GROUP BY/HAVING clause. 521 if p.peek() == GROUP { 522 p.lex() 523 if p.peek() != BY { 524 return &stmt, p.errorExpected(p.pos, p.tok, "BY") 525 } 526 p.lex() 527 528 for { 529 expr, err := p.ParseExpr() 530 if err != nil { 531 return &stmt, err 532 } 533 stmt.GroupingElements = append(stmt.GroupingElements, expr) 534 535 if p.peek() != COMMA { 536 break 537 } 538 p.lex() 539 } 540 541 // Parse optional HAVING clause. 542 if p.peek() == HAVING { 543 p.lex() 544 if stmt.HavingCondition, err = p.ParseExpr(); err != nil { 545 return &stmt, err 546 } 547 } 548 } 549 550 // Optionally compound additional SELECT/VALUES. 551 switch tok := p.peek(); tok { 552 case UNION, INTERSECT, EXCEPT: 553 if tok == UNION { 554 p.lex() 555 stmt.Union = true 556 if p.peek() == ALL { 557 p.lex() 558 stmt.UnionAll = true 559 } 560 } else if tok == INTERSECT { 561 p.lex() 562 stmt.Intersect = true 563 } else { 564 p.lex() 565 stmt.Except = true 566 } 567 568 if stmt.Compound, err = p.parseSelectStatement(true); err != nil { 569 return &stmt, err 570 } 571 } 572 573 // Parse ORDER BY clause. 574 if !compounded && p.peek() == ORDER { 575 p.lex() 576 if p.peek() != BY { 577 return &stmt, p.errorExpected(p.pos, p.tok, "BY") 578 } 579 p.lex() 580 581 for { 582 term, err := p.parseOrderingTerm() 583 if err != nil { 584 return &stmt, err 585 } 586 stmt.OrderBy = append(stmt.OrderBy, term) 587 588 if p.peek() != COMMA { 589 break 590 } 591 p.lex() 592 } 593 } 594 595 // Parse LIMIT/OFFSET clause. 596 // The offset is optional. Can be specified with COMMA or OFFSET. 597 // e.g. "LIMIT 1 OFFSET 2" or "LIMIT 1, 2" or "OFFSET 0 ROW FETCH NEXT/FIRST 1 ROWS/ROW ONLY" 598 if !compounded && (p.peek() == LIMIT || p.peek() == OFFSET) { 599 switch p.peek() { 600 case LIMIT: 601 p.lex() 602 if stmt.Limit, err = p.ParseExpr(); err != nil { 603 return &stmt, err 604 } 605 606 if tok := p.peek(); tok == OFFSET || tok == COMMA { 607 p.lex() 608 if stmt.Offset, err = p.ParseExpr(); err != nil { 609 return &stmt, err 610 } 611 } 612 case OFFSET: 613 p.lex() 614 if stmt.Offset, err = p.ParseExpr(); err != nil { 615 return &stmt, err 616 } 617 if tok := p.peek(); tok != ROW && tok != ROWS { 618 return &stmt, p.errorExpected(p.pos, p.tok, "ROW/ROWS") 619 } 620 621 p.lex() 622 if tok := p.peek(); tok == FETCH { 623 p.lex() 624 if tok := p.peek(); tok != NEXT && tok != FIRST { 625 return &stmt, p.errorExpected(p.pos, p.tok, "NEXT/FIRST") 626 } 627 628 p.lex() 629 if stmt.Fetch, err = p.ParseExpr(); err != nil { 630 return &stmt, err 631 } 632 if tok := p.peek(); tok != ROW && tok != ROWS { 633 return &stmt, p.errorExpected(p.pos, p.tok, "ROW/ROWS") 634 } 635 636 p.lex() 637 if tok := p.peek(); tok != ONLY { 638 return &stmt, p.errorExpected(p.pos, p.tok, "ONLY") 639 } 640 p.lex() 641 } 642 } 643 } 644 645 return &stmt, nil 646 } 647 648 func (p *Parser) parseOutputNames() (_ *OutputNames, err error) { 649 var names OutputNames 650 for { 651 name, err := p.parseResultColumn() 652 if err != nil { 653 return &names, err 654 } 655 names = append(names, name) 656 657 if p.peek() != COMMA { 658 break 659 } 660 p.lex() 661 } 662 return &names, nil 663 } 664 665 func (p *Parser) parseResultColumn() (_ *ResultColumn, err error) { 666 var col ResultColumn 667 668 // An initial "*" returns all columns. 669 if p.peek() == STAR { 670 p.lex() 671 col.Star = true 672 return &col, nil 673 } 674 675 // Next can be either "EXPR [[AS] column-alias]" or "IDENT DOT STAR". 676 // We need read the next element as an expression and then determine what next. 677 if col.Expr, err = p.ParseExpr(); err != nil { 678 return &col, err 679 } 680 681 // If we have a qualified ref w/ a star, don't allow an alias. 682 if ref, ok := col.Expr.(*QualifiedRef); ok && ref.Star { 683 return &col, nil 684 } 685 686 // If "AS" is next, the alias must follow. 687 // Otherwise it can optionally be an IDENT alias. 688 if p.peek() == AS { 689 p.lex() 690 if !isIdentToken(p.peek()) { 691 return &col, p.errorExpected(p.pos, p.tok, "column alias") 692 } 693 col.Alias, _ = p.parseIdent("column alias") 694 } else if isIdentToken(p.peek()) { 695 col.Alias, _ = p.parseIdent("column alias") 696 } 697 698 return &col, nil 699 } 700 701 func (p *Parser) parseSource() (source Source, err error) { 702 source, err = p.parseUnarySource() 703 if err != nil { 704 return source, err 705 } 706 707 for { 708 // Exit immediately if not part of a join operator. 709 switch p.peek() { 710 case COMMA, NATURAL, LEFT, INNER, CROSS, JOIN: 711 default: 712 return source, nil 713 } 714 715 // Parse join operator. 716 operator, err := p.parseJoinOperator() 717 if err != nil { 718 return source, err 719 } 720 y, err := p.parseUnarySource() 721 if err != nil { 722 return source, err 723 } 724 constraint, err := p.parseJoinConstraint() 725 if err != nil { 726 return source, err 727 } 728 729 // Rewrite last source to nest next join on right side. 730 if lhs, ok := source.(*JoinClause); ok { 731 source = &JoinClause{ 732 X: lhs.X, 733 Operator: lhs.Operator, 734 Y: &JoinClause{ 735 X: lhs.Y, 736 Operator: operator, 737 Y: y, 738 Constraint: constraint, 739 }, 740 Constraint: lhs.Constraint, 741 } 742 } else { 743 source = &JoinClause{X: source, Operator: operator, Y: y, Constraint: constraint} 744 } 745 } 746 } 747 748 // parseUnarySource parses a quailfied table name or subquery but not a JOIN. 749 func (p *Parser) parseUnarySource() (source Source, err error) { 750 switch p.peek() { 751 case LP: 752 return p.parseParenSource() 753 case IDENT, QIDENT: 754 return p.parseTableName() 755 default: 756 return nil, p.errorExpected(p.pos, p.tok, "table name or left paren") 757 } 758 } 759 760 func (p *Parser) parseJoinOperator() (*JoinOperator, error) { 761 var op JoinOperator 762 763 // Handle single comma join. 764 if p.peek() == COMMA { 765 p.lex() 766 op.Comma = true 767 return &op, nil 768 } 769 770 if p.peek() == NATURAL { 771 p.lex() 772 op.Natural = true 773 } 774 775 // Parse "LEFT", "LEFT OUTER", "INNER", or "CROSS" 776 switch p.peek() { 777 case LEFT: 778 p.lex() 779 op.Left = true 780 if p.peek() == OUTER { 781 p.lex() 782 op.Outer = true 783 } 784 case INNER: 785 p.lex() 786 op.Inner = true 787 case CROSS: 788 p.lex() 789 op.Cross = true 790 } 791 792 // Parse final JOIN. 793 if p.peek() != JOIN { 794 return &op, p.errorExpected(p.pos, p.tok, "JOIN") 795 } 796 p.lex() 797 798 return &op, nil 799 } 800 801 func (p *Parser) parseJoinConstraint() (JoinConstraint, error) { 802 switch p.peek() { 803 case ON: 804 return p.parseOnConstraint() 805 case USING: 806 return p.parseUsingConstraint() 807 default: 808 return nil, nil 809 } 810 } 811 812 func (p *Parser) parseOnConstraint() (_ *OnConstraint, err error) { 813 assert(p.peek() == ON) 814 815 var con OnConstraint 816 p.lex() 817 if con.X, err = p.ParseExpr(); err != nil { 818 return &con, err 819 } 820 return &con, nil 821 } 822 823 func (p *Parser) parseUsingConstraint() (*UsingConstraint, error) { 824 assert(p.peek() == USING) 825 826 var con UsingConstraint 827 p.lex() 828 829 if p.peek() != LP { 830 return &con, p.errorExpected(p.pos, p.tok, "left paren") 831 } 832 p.lex() 833 834 for { 835 col, err := p.parseIdent("column name") 836 if err != nil { 837 return &con, err 838 } 839 con.Columns = append(con.Columns, col) 840 841 if p.peek() == RP { 842 break 843 } else if p.peek() != COMMA { 844 return &con, p.errorExpected(p.pos, p.tok, "comma or right paren") 845 } 846 p.lex() 847 } 848 p.lex() 849 850 return &con, nil 851 } 852 853 func (p *Parser) parseParenSource() (_ *ParenSource, err error) { 854 assert(p.peek() == LP) 855 856 var source ParenSource 857 p.lex() 858 859 if p.peek() == SELECT { 860 if source.X, err = p.parseSelectStatement(false); err != nil { 861 return &source, err 862 } 863 } else { 864 if source.X, err = p.parseSource(); err != nil { 865 return &source, err 866 } 867 } 868 869 if p.peek() != RP { 870 return nil, p.errorExpected(p.pos, p.tok, "right paren") 871 } 872 p.lex() 873 874 // Only parse aliases for nested select statements. 875 if _, ok := source.X.(*SelectStatement); ok && (p.peek() == AS || isIdentToken(p.peek())) { 876 if p.peek() == AS { 877 p.lex() 878 } 879 if source.Alias, err = p.parseIdent("table alias"); err != nil { 880 return &source, err 881 } 882 } 883 884 return &source, nil 885 } 886 887 func (p *Parser) parseTableName() (_ *TableName, err error) { 888 var tbl TableName 889 890 if !isIdentToken(p.peek()) { 891 return &tbl, p.errorExpected(p.pos, p.tok, "table name") 892 } 893 tbl.Name, _ = p.parseIdent("table name") 894 895 // Parse optional table alias ("AS alias" or just "alias"). 896 if tok := p.peek(); tok == AS || isIdentToken(tok) { 897 if p.peek() == AS { 898 p.lex() 899 } 900 if tbl.Alias, err = p.parseIdent("table alias"); err != nil { 901 return &tbl, err 902 } 903 } 904 905 return &tbl, nil 906 } 907 908 func (p *Parser) ParseExpr() (expr Expr, err error) { 909 return p.parseBinaryExpr(LowestPrec + 1) 910 } 911 912 func (p *Parser) parseOperand() (expr Expr, err error) { 913 _, tok, lit := p.lex() 914 switch tok { 915 case IDENT, QIDENT: 916 ident := identByNameAndTok(lit, tok) 917 if p.peek() == DOT { 918 return p.parseQualifiedRef(ident) 919 } else if p.peek() == LP { 920 return p.parseCall(ident) 921 } 922 return ident, nil 923 case STRING: 924 return &StringLit{Value: lit}, nil 925 case BLOB: 926 return &BlobLit{Value: lit}, nil 927 case FLOAT, INTEGER: 928 return &NumberLit{Value: lit}, nil 929 case NULL: 930 return &NullLit{}, nil 931 case TRUE, FALSE: 932 return &BoolLit{Value: tok == TRUE}, nil 933 case BIND: 934 p.binds += 1 935 return &BindExpr{Name: lit, Pos: p.binds - 1}, nil 936 case PLUS, MINUS: 937 expr, err = p.parseOperand() 938 if err != nil { 939 return nil, err 940 } 941 return &UnaryExpr{Op: tok, X: expr}, nil 942 case LP: 943 p.unlex() 944 return p.parseParenExpr() 945 case CASE: 946 p.unlex() 947 return p.parseCaseExpr() 948 case NOT, EXISTS: 949 p.unlex() 950 return p.parseExists() 951 default: 952 return nil, p.errorExpected(p.pos, p.tok, "expression") 953 } 954 } 955 956 func (p *Parser) parseBinaryExpr(prec1 int) (expr Expr, err error) { 957 x, err := p.parseOperand() 958 if err != nil { 959 return nil, err 960 } 961 for { 962 if p.peek().Precedence() < prec1 { 963 return x, nil 964 } 965 966 _, op, err := p.lexBinaryOp() 967 if err != nil { 968 return nil, err 969 } 970 971 switch op { 972 case IN, NOTIN: 973 y, err := p.parseExprs() 974 if err != nil { 975 return x, err 976 } 977 x = &BinaryExpr{X: x, Op: op, Y: y} 978 979 case BETWEEN, NOTBETWEEN: 980 // Parsing the expression should yield a binary expression with AND op. 981 // However, we don't want to conflate the boolean AND and the ranged AND 982 // so we convert the expression to a Range. 983 if rng, err := p.parseBinaryExpr(LowestPrec + 1); err != nil { 984 return x, err 985 } else if rng, ok := rng.(*BinaryExpr); !ok || rng.Op != AND { 986 return x, p.errorExpected(p.pos, p.tok, "range expression") 987 } else { 988 x = &BinaryExpr{ 989 X: x, 990 Op: op, 991 Y: &Range{X: rng.X, Y: rng.Y}, 992 } 993 } 994 995 default: 996 y, err := p.parseBinaryExpr(op.Precedence() + 1) 997 if err != nil { 998 return nil, err 999 } 1000 x = &BinaryExpr{X: x, Op: op, Y: y} 1001 } 1002 } 1003 1004 } 1005 1006 func (p *Parser) parseExprs() (_ *Exprs, err error) { 1007 var list Exprs 1008 if p.peek() != LP { 1009 return &list, p.errorExpected(p.pos, p.tok, "left paren") 1010 } 1011 p.lex() 1012 1013 for p.peek() != RP { 1014 x, err := p.ParseExpr() 1015 if err != nil { 1016 return &list, err 1017 } 1018 list.Exprs = append(list.Exprs, x) 1019 1020 if p.peek() == RP { 1021 break 1022 } else if p.peek() != COMMA { 1023 return &list, p.errorExpected(p.pos, p.tok, "comma or right paren") 1024 } 1025 p.lex() 1026 } 1027 1028 p.lex() 1029 1030 return &list, nil 1031 } 1032 1033 func (p *Parser) parseQualifiedRef(table *Ident) (_ *QualifiedRef, err error) { 1034 assert(p.peek() == DOT) 1035 1036 var expr QualifiedRef 1037 expr.Table = table 1038 p.lex() 1039 1040 if p.peek() == STAR { 1041 p.lex() 1042 expr.Star = true 1043 } else if isIdentToken(p.peek()) { 1044 _, tok, lit := p.lex() 1045 expr.Column = identByNameAndTok(lit, tok) 1046 } else { 1047 return &expr, p.errorExpected(p.pos, p.tok, "column name") 1048 } 1049 1050 return &expr, nil 1051 } 1052 1053 func (p *Parser) parseCall(name *Ident) (_ *Call, err error) { 1054 assert(p.peek() == LP) 1055 1056 var expr Call 1057 expr.Name = name 1058 p.lex() 1059 1060 // Parse argument list: either "*" or "[DISTINCT] expr, expr..." 1061 if p.peek() == STAR { 1062 p.lex() 1063 expr.Star = true 1064 } else { 1065 if p.peek() == DISTINCT { 1066 p.lex() 1067 expr.Distinct = true 1068 } 1069 for p.peek() != RP { 1070 arg, err := p.ParseExpr() 1071 if err != nil { 1072 return &expr, err 1073 } 1074 expr.Args = append(expr.Args, arg) 1075 1076 if tok := p.peek(); tok == COMMA { 1077 p.lex() 1078 } else if tok != RP { 1079 return &expr, p.errorExpected(p.pos, p.tok, "comma or right paren") 1080 } 1081 1082 } 1083 } 1084 1085 if p.peek() != RP { 1086 return &expr, p.errorExpected(p.pos, p.tok, "right paren") 1087 } 1088 p.lex() 1089 1090 // Parse optional filter clause. 1091 if p.peek() == FILTER { 1092 if expr.Filter, err = p.parseFilterClause(); err != nil { 1093 return &expr, err 1094 } 1095 } 1096 1097 return &expr, nil 1098 } 1099 1100 func (p *Parser) parseFilterClause() (_ *FilterClause, err error) { 1101 assert(p.peek() == FILTER) 1102 1103 var clause FilterClause 1104 p.lex() 1105 1106 if p.peek() != LP { 1107 return &clause, p.errorExpected(p.pos, p.tok, "left paren") 1108 } 1109 p.lex() 1110 1111 if p.peek() != WHERE { 1112 return &clause, p.errorExpected(p.pos, p.tok, "WHERE") 1113 } 1114 p.lex() 1115 1116 if clause.X, err = p.ParseExpr(); err != nil { 1117 return &clause, err 1118 } 1119 1120 if p.peek() != RP { 1121 return &clause, p.errorExpected(p.pos, p.tok, "right paren") 1122 } 1123 p.lex() 1124 1125 return &clause, nil 1126 } 1127 1128 func (p *Parser) parseOrderingTerm() (_ *OrderingTerm, err error) { 1129 var term OrderingTerm 1130 if term.X, err = p.ParseExpr(); err != nil { 1131 return &term, err 1132 } 1133 1134 // Parse optional sort direction ("ASC" or "DESC") 1135 switch p.peek() { 1136 case ASC: 1137 p.lex() 1138 term.Asc = true 1139 case DESC: 1140 p.lex() 1141 term.Desc = true 1142 } 1143 1144 // Parse optional "NULLS FIRST" or "NULLS LAST" 1145 if p.peek() == NULLS { 1146 p.lex() 1147 switch p.peek() { 1148 case FIRST: 1149 p.lex() 1150 term.NullsFirst = true 1151 case LAST: 1152 p.lex() 1153 term.NullsLast = true 1154 default: 1155 return &term, p.errorExpected(p.pos, p.tok, "FIRST or LAST") 1156 } 1157 } 1158 1159 return &term, nil 1160 } 1161 1162 func (p *Parser) parseParenExpr() (_ *ParenExpr, err error) { 1163 var expr ParenExpr 1164 p.lex() 1165 if expr.X, err = p.ParseExpr(); err != nil { 1166 return &expr, err 1167 } 1168 p.lex() 1169 return &expr, nil 1170 } 1171 1172 func (p *Parser) parseCaseExpr() (_ *CaseExpr, err error) { 1173 assert(p.peek() == CASE) 1174 1175 var expr CaseExpr 1176 p.lex() 1177 1178 // Parse optional expression if WHEN is not next. 1179 if p.peek() != WHEN { 1180 if expr.Operand, err = p.ParseExpr(); err != nil { 1181 return &expr, err 1182 } 1183 } 1184 1185 // Parse one or more WHEN/THEN pairs. 1186 for { 1187 var blk CaseBlock 1188 if p.peek() != WHEN { 1189 return &expr, p.errorExpected(p.pos, p.tok, "WHEN") 1190 } 1191 p.lex() 1192 1193 if blk.Condition, err = p.ParseExpr(); err != nil { 1194 return &expr, err 1195 } 1196 1197 if p.peek() != THEN { 1198 return &expr, p.errorExpected(p.pos, p.tok, "THEN") 1199 } 1200 p.lex() 1201 1202 if blk.Body, err = p.ParseExpr(); err != nil { 1203 return &expr, err 1204 } 1205 1206 expr.Blocks = append(expr.Blocks, &blk) 1207 1208 if tok := p.peek(); tok == ELSE || tok == END { 1209 break 1210 } else if tok != WHEN { 1211 return &expr, p.errorExpected(p.pos, p.tok, "WHEN, ELSE or END") 1212 } 1213 } 1214 1215 // Parse optional ELSE block. 1216 if p.peek() == ELSE { 1217 p.lex() 1218 if expr.ElseExpr, err = p.ParseExpr(); err != nil { 1219 return &expr, err 1220 } 1221 } 1222 1223 if p.peek() != END { 1224 return &expr, p.errorExpected(p.pos, p.tok, "END") 1225 } 1226 p.lex() 1227 1228 return &expr, nil 1229 } 1230 1231 func (p *Parser) parseExists() (_ *Exists, err error) { 1232 assert(p.peek() == NOT || p.peek() == EXISTS) 1233 1234 var expr Exists 1235 1236 if p.peek() == NOT { 1237 p.lex() 1238 expr.Not = true 1239 } 1240 1241 if p.peek() != EXISTS { 1242 return &expr, p.errorExpected(p.pos, p.tok, "EXISTS") 1243 } 1244 p.lex() 1245 1246 if p.peek() != LP { 1247 return &expr, p.errorExpected(p.pos, p.tok, "left paren") 1248 } 1249 p.lex() 1250 1251 if expr.Select, err = p.parseSelectStatement(false); err != nil { 1252 return &expr, err 1253 } 1254 1255 if p.peek() != RP { 1256 return &expr, p.errorExpected(p.pos, p.tok, "right paren") 1257 } 1258 p.lex() 1259 1260 return &expr, nil 1261 } 1262 1263 func (p *Parser) lex() (Pos, Token, string) { 1264 if p.full { 1265 p.full = false 1266 return p.pos, p.tok, p.lit 1267 } 1268 1269 p.pos, p.tok, p.lit = p.l.Lex() 1270 return p.pos, p.tok, p.lit 1271 } 1272 1273 // lexBinaryOp performs a lex but combines multi-word operations into a single token. 1274 func (p *Parser) lexBinaryOp() (Pos, Token, error) { 1275 pos, tok, _ := p.lex() 1276 switch tok { 1277 case IS: 1278 if p.peek() == NOT { 1279 p.lex() 1280 return pos, ISNOT, nil 1281 } 1282 return pos, IS, nil 1283 case NOT: 1284 switch p.peek() { 1285 case IN: 1286 p.lex() 1287 return pos, NOTIN, nil 1288 case LIKE: 1289 p.lex() 1290 return pos, NOTLIKE, nil 1291 case GLOB: 1292 p.lex() 1293 return pos, NOTGLOB, nil 1294 case REGEXP: 1295 p.lex() 1296 return pos, NOTREGEXP, nil 1297 case MATCH: 1298 p.lex() 1299 return pos, NOTMATCH, nil 1300 case BETWEEN: 1301 p.lex() 1302 return pos, NOTBETWEEN, nil 1303 default: 1304 return pos, tok, p.errorExpected(p.pos, p.tok, "IN, LIKE, GLOB, REGEXP, MATCH, or BETWEEN") 1305 } 1306 default: 1307 return pos, tok, nil 1308 } 1309 } 1310 1311 func (p *Parser) peek() Token { 1312 if !p.full { 1313 p.lex() 1314 p.unlex() 1315 } 1316 return p.tok 1317 } 1318 1319 func (p *Parser) unlex() { 1320 assert(!p.full) 1321 p.full = true 1322 } 1323 1324 func (p *Parser) errorExpected(pos Pos, tok Token, msg string) error { 1325 msg = "expected " + msg 1326 if pos == p.pos { 1327 if p.tok.IsLiteral() { 1328 msg += ", found " + p.lit 1329 } else { 1330 msg += ", found '" + p.tok.String() + "'" 1331 } 1332 } 1333 return &Error{Pos: pos, Msg: msg} 1334 } 1335 1336 func identByNameAndTok(lit string, tok Token) *Ident { 1337 quoted := tok == QIDENT 1338 if quoted { 1339 return &Ident{Name: lit[1 : len(lit)-1], Quoted: quoted, QuoteChar: lit[0:1]} 1340 } 1341 return &Ident{Name: lit, Quoted: false} 1342 } 1343 1344 // Error represents a parse error. 1345 type Error struct { 1346 Pos Pos 1347 Msg string 1348 } 1349 1350 // Error implements the error interface. 1351 func (e Error) Error() string { 1352 if e.Pos.IsValid() { 1353 return e.Pos.String() + ": " + e.Msg 1354 } 1355 return e.Msg 1356 }