github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/dialect/postgresql/postgresql_lexer.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package postgresql 16 17 import ( 18 "context" 19 "strconv" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 "github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect" 23 "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" 24 ) 25 26 func Parse(ctx context.Context, sql string) ([]tree.Statement, error) { 27 lexer := NewLexer(dialect.POSTGRESQL, sql) 28 if yyParse(lexer) != 0 { 29 return nil, lexer.scanner.LastError 30 } 31 return lexer.stmts, nil 32 } 33 34 func ParseOne(ctx context.Context, sql string) (tree.Statement, error) { 35 lexer := NewLexer(dialect.POSTGRESQL, sql) 36 if yyParse(lexer) != 0 { 37 return nil, lexer.scanner.LastError 38 } 39 if len(lexer.stmts) != 1 { 40 return nil, moerr.NewInternalError(ctx, "syntax Error, or too many sql to parse") 41 } 42 return lexer.stmts[0], nil 43 } 44 45 type Lexer struct { 46 scanner *Scanner 47 stmts []tree.Statement 48 } 49 50 func NewLexer(dialectType dialect.DialectType, sql string) *Lexer { 51 return &Lexer{ 52 scanner: NewScanner(dialectType, sql), 53 } 54 } 55 56 func (l *Lexer) Lex(lval *yySymType) int { 57 typ, str := l.scanner.Scan() 58 l.scanner.LastToken = str 59 60 switch typ { 61 case INTEGRAL: 62 return l.toInt(lval, str) 63 case FLOAT: 64 return l.toFloat(lval, str) 65 case HEX: 66 return l.toHex(lval, str) 67 case HEXNUM: 68 return l.toHexNum(lval, str) 69 case BIT_LITERAL: 70 return l.toBit(lval, str) 71 } 72 73 lval.str = str 74 return typ 75 } 76 77 func (l *Lexer) Error(err string) { 78 l.scanner.LastError = PositionedErr{Err: err, Pos: l.scanner.Pos + 1, Near: l.scanner.LastToken} 79 } 80 81 func (l *Lexer) AppendStmt(stmt tree.Statement) { 82 l.stmts = append(l.stmts, stmt) 83 } 84 85 func (l *Lexer) toInt(lval *yySymType, str string) int { 86 ival, err := strconv.ParseInt(str, 10, 64) 87 if err != nil { 88 l.scanner.LastError = err 89 } 90 lval.item = ival 91 return INTEGRAL 92 } 93 94 func (l *Lexer) toFloat(lval *yySymType, str string) int { 95 fval, err := strconv.ParseFloat(str, 64) 96 if err != nil { 97 l.scanner.LastError = err 98 } 99 lval.item = fval 100 return FLOAT 101 } 102 103 func (l *Lexer) toHex(lval *yySymType, str string) int { 104 return HEX 105 } 106 107 func (l *Lexer) toHexNum(lval *yySymType, str string) int { 108 return HEXNUM 109 } 110 111 func (l *Lexer) toBit(lval *yySymType, str string) int { 112 return BIT_LITERAL 113 }