github.com/wfusion/gofusion@v1.1.14/common/utils/sqlparser/token.go (about) 1 package sqlparser 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 ) 8 9 var keywords map[string]Token 10 11 func init() { 12 keywords = make(map[string]Token) 13 for i := keyword_beg + 1; i < keyword_end; i++ { 14 keywords[tokens[i]] = i 15 } 16 keywords[tokens[NULL]] = NULL 17 keywords[tokens[TRUE]] = TRUE 18 keywords[tokens[FALSE]] = FALSE 19 } 20 21 // Token is the set of lexical tokens of the Go programming language. 22 type Token int 23 24 // The list of tokens. 25 const ( 26 // Special tokens 27 ILLEGAL Token = iota 28 EOF 29 COMMENT 30 SPACE 31 32 literal_beg 33 IDENT // IDENT 34 QIDENT // "IDENT" or `IDENT` 35 STRING // 'string' 36 BLOB // ??? 37 FLOAT // 123.45 38 INTEGER // 123 39 MLCOMMENT // multiline comment 40 NULL // NULL 41 TRUE // true 42 FALSE // false 43 BIND //? or ?NNN or :VVV or @VVV or $VVV 44 literal_end 45 46 operator_beg 47 SEMI // ; 48 LP // ( 49 RP // ) 50 COMMA // , 51 NE // != 52 EQ // = 53 LE // <= 54 LG // <> 55 LT // < 56 GT // > 57 GE // >= 58 BITAND // & 59 BITOR // | 60 BITNOT // ! 61 LSHIFT // << 62 RSHIFT // >> 63 PLUS // + 64 MINUS // - 65 STAR // * 66 SLASH // / 67 REM // % 68 CONCAT // || 69 DOT // . 70 operator_end 71 72 keyword_beg 73 ABORT 74 ACTION 75 ADD 76 AFTER 77 AGG_COLUMN 78 AGG_FUNCTION 79 ALL 80 ALTER 81 ANALYZE 82 AND 83 AS 84 ASC 85 ASTERISK 86 ATTACH 87 AUTOINCREMENT 88 BEFORE 89 BEGIN 90 BETWEEN 91 BY 92 CASCADE 93 CASE 94 CAST 95 CHECK 96 COLUMN 97 COLUMNKW 98 COMMIT 99 CONFLICT 100 CONSTRAINT 101 CREATE 102 CROSS 103 CTIME_KW 104 CURRENT 105 CURRENT_TIME 106 CURRENT_DATE 107 CURRENT_TIMESTAMP 108 DATABASE 109 DEFAULT 110 DEFERRABLE 111 DEFERRED 112 DELETE 113 DESC 114 DETACH 115 DISTINCT 116 DO 117 DROP 118 EACH 119 ELSE 120 END 121 ESCAPE 122 EXCEPT 123 EXCLUDE 124 EXCLUSIVE 125 EXISTS 126 EXPLAIN 127 FAIL 128 FILTER 129 FIRST 130 FOLLOWING 131 FOR 132 FOREIGN 133 FROM 134 FUNCTION 135 GLOB 136 GROUP 137 GROUPS 138 HAVING 139 IF 140 IF_NULL_ROW 141 IGNORE 142 IMMEDIATE 143 IN 144 INDEX 145 INDEXED 146 INITIALLY 147 INNER 148 INSERT 149 INSTEAD 150 INTERSECT 151 INTO 152 IS 153 ISNOT 154 ISNULL // TODO: REMOVE? 155 JOIN 156 KEY 157 LAST 158 LEFT 159 LIKE 160 LIMIT 161 MATCH 162 NATURAL 163 NO 164 NOT 165 NOTBETWEEN 166 NOTEXISTS 167 NOTGLOB 168 NOTHING 169 NOTIN 170 NOTLIKE 171 NOTMATCH 172 NOTNULL 173 NOTREGEXP 174 NULLS 175 OF 176 OFFSET 177 ON 178 ONLY 179 OR 180 ORDER 181 OTHERS 182 OUTER 183 OVER 184 OVERRIDING 185 PARTITION 186 PLAN 187 PRAGMA 188 PRECEDING 189 PRIMARY 190 QUERY 191 RAISE 192 RANGE 193 RECURSIVE 194 REFERENCES 195 REGEXP 196 REGISTER 197 REINDEX 198 RELEASE 199 RENAME 200 RESTRICT 201 RETURNING 202 ROLLBACK 203 ROW 204 ROWS 205 SELECT 206 SELECT_COLUMN 207 SET 208 SPAN 209 SYSTEM 210 TABLE 211 TEMP 212 THEN 213 TIES 214 TO 215 TRANSACTION 216 TRIGGER 217 TRUTH 218 UNBOUNDED 219 UNION 220 UNIQUE 221 UPDATE 222 USER 223 USING 224 VACUUM 225 VALUE 226 VALUES 227 VARIABLE 228 VECTOR 229 VIEW 230 VIRTUAL 231 WHEN 232 WHERE 233 WINDOW 234 WITH 235 WITHOUT 236 FETCH 237 NEXT 238 keyword_end 239 240 ANY // ??? 241 token_end 242 ) 243 244 var tokens = [...]string{ 245 ILLEGAL: "ILLEGAL", 246 EOF: "EOF", 247 COMMENT: "COMMENT", 248 SPACE: "SPACE", 249 250 IDENT: "IDENT", 251 QIDENT: "QIDENT", 252 STRING: "STRING", 253 BLOB: "BLOB", 254 FLOAT: "FLOAT", 255 INTEGER: "INTEGER", 256 MLCOMMENT: "MLCOMMENT", 257 NULL: "NULL", 258 TRUE: "TRUE", 259 FALSE: "FALSE", 260 BIND: "BIND", 261 262 SEMI: ";", 263 LP: "(", 264 RP: ")", 265 COMMA: ",", 266 NE: "!=", 267 EQ: "=", 268 LE: "<=", 269 LG: "<>", 270 LT: "<", 271 GT: ">", 272 GE: ">=", 273 BITAND: "&", 274 BITOR: "|", 275 BITNOT: "!", 276 LSHIFT: "<<", 277 RSHIFT: ">>", 278 PLUS: "+", 279 MINUS: "-", 280 STAR: "*", 281 SLASH: "/", 282 REM: "%", 283 CONCAT: "||", 284 DOT: ".", 285 286 ABORT: "ABORT", 287 ACTION: "ACTION", 288 ADD: "ADD", 289 AFTER: "AFTER", 290 AGG_COLUMN: "AGG_COLUMN", 291 AGG_FUNCTION: "AGG_FUNCTION", 292 ALL: "ALL", 293 ALTER: "ALTER", 294 ANALYZE: "ANALYZE", 295 AND: "AND", 296 AS: "AS", 297 ASC: "ASC", 298 ASTERISK: "ASTERISK", 299 ATTACH: "ATTACH", 300 AUTOINCREMENT: "AUTOINCREMENT", 301 BEFORE: "BEFORE", 302 BEGIN: "BEGIN", 303 BETWEEN: "BETWEEN", 304 BY: "BY", 305 CASCADE: "CASCADE", 306 CASE: "CASE", 307 CAST: "CAST", 308 CHECK: "CHECK", 309 COLUMN: "COLUMN", 310 COLUMNKW: "COLUMNKW", 311 COMMIT: "COMMIT", 312 CONFLICT: "CONFLICT", 313 CONSTRAINT: "CONSTRAINT", 314 CREATE: "CREATE", 315 CROSS: "CROSS", 316 CTIME_KW: "CTIME_KW", 317 CURRENT: "CURRENT", 318 CURRENT_TIME: "CURRENT_TIME", 319 CURRENT_DATE: "CURRENT_DATE", 320 CURRENT_TIMESTAMP: "CURRENT_TIMESTAMP", 321 DATABASE: "DATABASE", 322 DEFAULT: "DEFAULT", 323 DEFERRABLE: "DEFERRABLE", 324 DEFERRED: "DEFERRED", 325 DELETE: "DELETE", 326 DESC: "DESC", 327 DETACH: "DETACH", 328 DISTINCT: "DISTINCT", 329 DO: "DO", 330 DROP: "DROP", 331 EACH: "EACH", 332 ELSE: "ELSE", 333 END: "END", 334 ESCAPE: "ESCAPE", 335 EXCEPT: "EXCEPT", 336 EXCLUDE: "EXCLUDE", 337 EXCLUSIVE: "EXCLUSIVE", 338 EXISTS: "EXISTS", 339 EXPLAIN: "EXPLAIN", 340 FAIL: "FAIL", 341 FILTER: "FILTER", 342 FIRST: "FIRST", 343 FOLLOWING: "FOLLOWING", 344 FOR: "FOR", 345 FOREIGN: "FOREIGN", 346 FROM: "FROM", 347 FUNCTION: "FUNCTION", 348 GLOB: "GLOB", 349 GROUP: "GROUP", 350 GROUPS: "GROUPS", 351 HAVING: "HAVING", 352 IF: "IF", 353 IF_NULL_ROW: "IF_NULL_ROW", 354 IGNORE: "IGNORE", 355 IMMEDIATE: "IMMEDIATE", 356 IN: "IN", 357 INDEX: "INDEX", 358 INDEXED: "INDEXED", 359 INITIALLY: "INITIALLY", 360 INNER: "INNER", 361 INSERT: "INSERT", 362 INSTEAD: "INSTEAD", 363 INTERSECT: "INTERSECT", 364 INTO: "INTO", 365 IS: "IS", 366 ISNOT: "ISNOT", 367 ISNULL: "ISNULL", 368 JOIN: "JOIN", 369 KEY: "KEY", 370 LAST: "LAST", 371 LEFT: "LEFT", 372 LIKE: "LIKE", 373 LIMIT: "LIMIT", 374 MATCH: "MATCH", 375 NO: "NO", 376 NATURAL: "NATURAL", 377 NOT: "NOT", 378 NOTBETWEEN: "NOTBETWEEN", 379 NOTEXISTS: "NOTEXISTS", 380 NOTGLOB: "NOTGLOB", 381 NOTHING: "NOTHING", 382 NOTIN: "NOTIN", 383 NOTLIKE: "NOTLIKE", 384 NOTMATCH: "NOTMATCH", 385 NOTNULL: "NOTNULL", 386 NOTREGEXP: "NOTREGEXP", 387 NULLS: "NULLS", 388 OF: "OF", 389 OFFSET: "OFFSET", 390 ON: "ON", 391 ONLY: "ONLY", 392 OR: "OR", 393 ORDER: "ORDER", 394 OTHERS: "OTHERS", 395 OUTER: "OUTER", 396 OVER: "OVER", 397 OVERRIDING: "OVERRIDING", 398 PARTITION: "PARTITION", 399 PLAN: "PLAN", 400 PRAGMA: "PRAGMA", 401 PRECEDING: "PRECEDING", 402 PRIMARY: "PRIMARY", 403 QUERY: "QUERY", 404 RAISE: "RAISE", 405 RANGE: "RANGE", 406 RECURSIVE: "RECURSIVE", 407 REFERENCES: "REFERENCES", 408 REGEXP: "REGEXP", 409 REGISTER: "REGISTER", 410 REINDEX: "REINDEX", 411 RELEASE: "RELEASE", 412 RENAME: "RENAME", 413 RESTRICT: "RESTRICT", 414 RETURNING: "RETURNING", 415 ROLLBACK: "ROLLBACK", 416 ROW: "ROW", 417 ROWS: "ROWS", 418 SELECT: "SELECT", 419 SELECT_COLUMN: "SELECT_COLUMN", 420 SET: "SET", 421 SPAN: "SPAN", 422 SYSTEM: "SYSTEM", 423 TABLE: "TABLE", 424 TEMP: "TEMP", 425 THEN: "THEN", 426 TIES: "TIES", 427 TO: "TO", 428 TRANSACTION: "TRANSACTION", 429 TRIGGER: "TRIGGER", 430 TRUTH: "TRUTH", 431 UNBOUNDED: "UNBOUNDED", 432 UNION: "UNION", 433 UNIQUE: "UNIQUE", 434 UPDATE: "UPDATE", 435 USER: "USER", 436 USING: "USING", 437 VACUUM: "VACUUM", 438 VALUE: "VALUE", 439 VALUES: "VALUES", 440 VARIABLE: "VARIABLE", 441 VECTOR: "VECTOR", 442 VIEW: "VIEW", 443 VIRTUAL: "VIRTUAL", 444 WHEN: "WHEN", 445 WHERE: "WHERE", 446 WINDOW: "WINDOW", 447 WITH: "WITH", 448 WITHOUT: "WITHOUT", 449 FETCH: "FETCH", 450 NEXT: "NEXT", 451 } 452 453 func (tok Token) String() string { 454 s := "" 455 if 0 <= tok && tok < Token(len(tokens)) { 456 s = tokens[tok] 457 } 458 if s == "" { 459 s = "token(" + strconv.Itoa(int(tok)) + ")" 460 } 461 return s 462 } 463 464 func Lookup(ident string) Token { 465 if tok, ok := keywords[strings.ToUpper(ident)]; ok { 466 return tok 467 } 468 return IDENT 469 } 470 471 func (tok Token) IsLiteral() bool { 472 return tok > literal_beg && tok < literal_end 473 } 474 475 func (tok Token) IsBinaryOp() bool { 476 switch tok { 477 case PLUS, MINUS, STAR, SLASH, REM, CONCAT, NOT, BETWEEN, 478 LSHIFT, RSHIFT, BITAND, BITOR, LT, LG, LE, GT, GE, EQ, NE, 479 IS, IN, LIKE, GLOB, MATCH, REGEXP, AND, OR: 480 return true 481 default: 482 return false 483 } 484 } 485 486 func isIdentToken(tok Token) bool { 487 return tok == IDENT || tok == QIDENT 488 } 489 490 const ( 491 LowestPrec = 0 // non-operators 492 UnaryPrec = 13 493 HighestPrec = 14 494 ) 495 496 func (op Token) Precedence() int { 497 switch op { 498 case OR: 499 return 1 500 case AND: 501 return 2 502 case NOT: 503 return 3 504 case IS, MATCH, LIKE, GLOB, REGEXP, BETWEEN, IN, ISNULL, NOTNULL, NE, LG, EQ: 505 return 4 506 case GT, LE, LT, GE: 507 return 5 508 case ESCAPE: 509 return 6 510 case BITAND, BITOR, LSHIFT, RSHIFT: 511 return 7 512 case PLUS, MINUS: 513 return 8 514 case STAR, SLASH, REM: 515 return 9 516 case CONCAT: 517 return 10 518 case BITNOT: 519 return 11 520 } 521 return LowestPrec 522 } 523 524 type Pos struct { 525 Offset int // offset, starting at 0 526 Line int // line number, starting at 1 527 Column int // column number, starting at 1 (byte count) 528 } 529 530 // String returns a string representation of the position. 531 func (p Pos) String() string { 532 if !p.IsValid() { 533 return "-" 534 } 535 s := fmt.Sprintf("%d", p.Line) 536 if p.Column != 0 { 537 s += fmt.Sprintf(":%d", p.Column) 538 } 539 return s 540 } 541 542 // IsValid returns true if p is non-zero. 543 func (p Pos) IsValid() bool { 544 return p != Pos{} 545 } 546 547 func assert(condition bool) { 548 if !condition { 549 panic("assert failed") 550 } 551 }