github.com/runner-mei/ql@v1.1.0/scanner.l (about) 1 /* 2 Copyright (c) 2014 The ql Authors. All rights reserved. 3 Use of this source code is governed by a BSD-style 4 license that can be found in the LICENSE file. 5 */ 6 7 %{ 8 9 package ql 10 11 import ( 12 "fmt" 13 "math" 14 "strconv" 15 "unicode" 16 ) 17 18 type lexer struct { 19 agg []bool 20 c int 21 col int 22 errs []error 23 expr expression 24 i int 25 inj int 26 lcol int 27 line int 28 list []stmt 29 ncol int 30 nline int 31 params int 32 sc int 33 src string 34 val []byte 35 root bool 36 } 37 38 func newLexer(src string) (l *lexer) { 39 l = &lexer{ 40 src: src, 41 nline: 1, 42 ncol: 0, 43 } 44 l.next() 45 return 46 } 47 48 func (l *lexer) next() int { 49 if l.c != 0 { 50 l.val = append(l.val, byte(l.c)) 51 } 52 l.c = 0 53 if l.i < len(l.src) { 54 l.c = int(l.src[l.i]) 55 l.i++ 56 } 57 switch l.c { 58 case '\n': 59 l.lcol = l.ncol 60 l.nline++ 61 l.ncol = 0 62 default: 63 l.ncol++ 64 } 65 return l.c 66 } 67 68 func (l *lexer) err0(ln, c int, s string, arg ...interface{}) { 69 err := fmt.Errorf(fmt.Sprintf("%d:%d ", ln, c)+s, arg...) 70 l.errs = append(l.errs, err) 71 } 72 73 func (l *lexer) err(s string, arg ...interface{}) { 74 l.err0(l.line, l.col, s, arg...) 75 } 76 77 func (l *lexer) Error(s string) { 78 l.err(s) 79 } 80 81 func (l *lexer) Lex(lval *yySymType) (r int) { 82 //defer func() { dbg("Lex -> %d(%#x)", r, r) }() 83 defer func() { 84 lval.line, lval.col = l.line, l.col 85 }() 86 const ( 87 INITIAL = iota 88 S1 89 S2 90 ) 91 92 if n := l.inj; n != 0 { 93 l.inj = 0 94 return n 95 } 96 97 c0, c := 0, l.c 98 %} 99 100 int_lit {decimal_lit}|{octal_lit}|{hex_lit} 101 decimal_lit [1-9][0-9]* 102 octal_lit 0[0-7]* 103 hex_lit 0[xX][0-9a-fA-F]+ 104 105 float_lit {D}"."{D}?{E}?|{D}{E}|"."{D}{E}? 106 D [0-9]+ 107 E [eE][-+]?[0-9]+ 108 109 imaginary_ilit {D}i 110 imaginary_lit {float_lit}i 111 112 a [aA] 113 b [bB] 114 c [cC] 115 d [dD] 116 e [eE] 117 f [fF] 118 g [gG] 119 h [hH] 120 i [iI] 121 j [jJ] 122 k [kK] 123 l [lL] 124 m [mM] 125 n [nN] 126 o [oO] 127 p [pP] 128 q [qQ] 129 r [rR] 130 s [sS] 131 t [tT] 132 u [uU] 133 v [vV] 134 w [wW] 135 x [xX] 136 y [yY] 137 z [zZ] 138 139 add {a}{d}{d} 140 alter {a}{l}{t}{e}{r} 141 and {a}{n}{d} 142 as {a}{s} 143 asc {a}{s}{c} 144 begin {b}{e}{g}{i}{n} 145 between {b}{e}{t}{w}{e}{e}{n} 146 by {b}{y} 147 column {c}{o}{l}{u}{m}{n} 148 commit {c}{o}{m}{m}{i}{t} 149 create {c}{r}{e}{a}{t}{e} 150 default {d}{e}{f}{a}{u}{l}{t} 151 delete {d}{e}{l}{e}{t}{e} 152 desc {d}{e}{s}{c} 153 distinct {d}{i}{s}{t}{i}{n}{c}{t} 154 drop {d}{r}{o}{p} 155 exists {e}{x}{i}{s}{t}{s} 156 explain {e}{x}{p}{l}{a}{i}{n} 157 from {f}{r}{o}{m} 158 full {f}{u}{l}{l} 159 group {g}{r}{o}{u}{p} 160 if {i}{f} 161 in {i}{n} 162 index {i}{n}{d}{e}{x} 163 insert {i}{n}{s}{e}{r}{t} 164 into {i}{n}{t}{o} 165 is {i}{s} 166 join {j}{o}{i}{n} 167 left {l}{e}{f}{t} 168 like {l}{i}{k}{e} 169 limit {l}{i}{m}{i}{t} 170 not {n}{o}{t} 171 offset {o}{f}{f}{s}{e}{t} 172 on {o}{n} 173 or {o}{r} 174 order {o}{r}{d}{e}{r} 175 outer {o}{u}{t}{e}{r} 176 right {r}{i}{g}{h}{t} 177 rollback {r}{o}{l}{l}{b}{a}{c}{k} 178 select {s}{e}{l}{e}{c}{t} 179 set {s}{e}{t} 180 table {t}{a}{b}{l}{e} 181 transaction {t}{r}{a}{n}{s}{a}{c}{t}{i}{o}{n} 182 truncate {t}{r}{u}{n}{c}{a}{t}{e} 183 unique {u}{n}{i}{q}{u}{e} 184 update {u}{p}{d}{a}{t}{e} 185 values {v}{a}{l}{u}{e}{s} 186 where {w}{h}{e}{r}{e} 187 188 null {n}{u}{l}{l} 189 false {f}{a}{l}{s}{e} 190 true {t}{r}{u}{e} 191 192 bigint {b}{i}{g}{i}{n}{t} 193 bigrat {b}{i}{g}{r}{a}{t} 194 blob {b}{l}{o}{b} 195 bool {b}{o}{o}{l} 196 byte {b}{y}{t}{e} 197 complex {c}{o}{m}{p}{l}{e}{x} 198 duration {d}{u}{r}{a}{t}{i}{o}{n} 199 float {f}{l}{o}{a}{t} 200 int {i}{n}{t} 201 rune {r}{u}{n}{e} 202 string {s}{t}{r}{i}{n}{g} 203 time {t}{i}{m}{e} 204 uint {u}{i}{n}{t} 205 206 idchar0 [a-zA-Z_] 207 idchars {idchar0}|[0-9] 208 ident {idchar0}{idchars}* 209 210 %yyc c 211 %yyn c = l.next() 212 %yyt l.sc 213 214 %x S1 S2 215 216 %% 217 l.val = l.val[:0] 218 c0, l.line, l.col = l.c, l.nline, l.ncol 219 220 <*>\0 return 0 221 222 [ \t\n\r]+ 223 --.* 224 \/\/.* 225 \/\*([^*]|\*+[^*/])*\*+\/ 226 227 {imaginary_ilit} return l.int(lval, true) 228 {imaginary_lit} return l.float(lval, true) 229 {int_lit} return l.int(lval, false) 230 {float_lit} return l.float(lval, false) 231 232 \" l.sc = S1 233 ` l.sc = S2 234 235 '(\\.|[^'])*' if ret := l.str(lval, ""); ret != stringLit { 236 return ret 237 } 238 lval.item = idealRune(lval.item.(string)[0]) 239 return intLit 240 241 <S1>(\\.|[^\"])*\" return l.str(lval, "\"") 242 <S2>([^`]|\n)*` return l.str(lval, "`") 243 244 "&&" return andand 245 "&^" return andnot 246 "<<" return lsh 247 "<=" return le 248 "==" return eq 249 ">=" return ge 250 "!=" return neq 251 "||" return oror 252 ">>" return rsh 253 254 {add} return add 255 {alter} return alter 256 {and} return and 257 {asc} return asc 258 {as} return as 259 {begin} return begin 260 {between} return between 261 {by} return by 262 {column} return column 263 {commit} return commit 264 {create} return create 265 {default} return defaultKwd 266 {delete} return deleteKwd 267 {desc} return desc 268 {distinct} return distinct 269 {drop} return drop 270 {exists} return exists 271 {explain} return explain 272 {from} return from 273 {full} return full 274 {group} return group 275 {if} return ifKwd 276 {index} return index 277 {insert} return insert 278 {into} return into 279 {in} return in 280 {is} return is 281 {join} return join 282 {left} return left 283 {like} return like 284 {limit} return limit 285 {not} return not 286 {offset} return offset 287 {on} return on 288 {order} return order 289 {or} return or 290 {outer} return outer 291 {right} return right 292 293 {rollback} return rollback 294 295 {select} l.agg = append(l.agg, false) 296 return selectKwd 297 298 {set} return set 299 {table} return tableKwd 300 {transaction} return transaction 301 {truncate} return truncate 302 {update} return update 303 {unique} return unique 304 {values} return values 305 {where} return where 306 307 {null} lval.item = nil 308 return null 309 310 {false} lval.item = false 311 return falseKwd 312 313 {true} lval.item = true 314 return trueKwd 315 316 {bigint} lval.item = qBigInt 317 return bigIntType 318 319 {bigrat} lval.item = qBigRat 320 return bigRatType 321 322 {blob} lval.item = qBlob 323 return blobType 324 325 {bool} lval.item = qBool 326 return boolType 327 328 {byte} lval.item = qUint8 329 return byteType 330 331 {complex}128 lval.item = qComplex128 332 return complex128Type 333 334 {complex}64 lval.item = qComplex64 335 return complex64Type 336 337 {duration} lval.item = qDuration 338 return durationType 339 340 {float} lval.item = qFloat64 341 return floatType 342 343 {float}32 lval.item = qFloat32 344 return float32Type 345 346 {float}64 lval.item = qFloat64 347 return float64Type 348 349 {int} lval.item = qInt64 350 return intType 351 352 {int}16 lval.item = qInt16 353 return int16Type 354 355 {int}32 lval.item = qInt32 356 return int32Type 357 358 {int}64 lval.item = qInt64 359 return int64Type 360 361 {int}8 lval.item = qInt8 362 return int8Type 363 364 {rune} lval.item = qInt32 365 return runeType 366 367 {string} lval.item = qString 368 return stringType 369 370 {time} lval.item = qTime 371 return timeType 372 373 {uint} lval.item = qUint64 374 return uintType 375 376 {uint}16 lval.item = qUint16 377 return uint16Type 378 379 {uint}32 lval.item = qUint32 380 return uint32Type 381 382 {uint}64 lval.item = qUint64 383 return uint64Type 384 385 {uint}8 lval.item = qUint8 386 return uint8Type 387 388 {ident} lval.item = string(l.val) 389 return identifier 390 391 ($|\?){D} lval.item, _ = strconv.Atoi(string(l.val[1:])) 392 return qlParam 393 394 . return c0 395 396 %% 397 return int(unicode.ReplacementChar) 398 } 399 400 func (l *lexer) npos() (line, col int) { 401 if line, col = l.nline, l.ncol; col == 0 { 402 line-- 403 col = l.lcol+1 404 } 405 return 406 } 407 408 func (l *lexer) str(lval *yySymType, pref string) int { 409 l.sc = 0 410 s := pref + string(l.val) 411 s, err := strconv.Unquote(s) 412 if err != nil { 413 l.err("string literal: %v", err) 414 return int(unicode.ReplacementChar) 415 } 416 417 lval.item = s 418 return stringLit 419 } 420 421 func (l *lexer) int(lval *yySymType, im bool) int { 422 if im { 423 l.val = l.val[:len(l.val)-1] 424 } 425 n, err := strconv.ParseUint(string(l.val), 0, 64) 426 if err != nil { 427 l.err("integer literal: %v", err) 428 return int(unicode.ReplacementChar) 429 } 430 431 if im { 432 lval.item = idealComplex(complex(0, float64(n))) 433 return imaginaryLit 434 } 435 436 switch { 437 case n < math.MaxInt64: 438 lval.item = idealInt(n) 439 default: 440 lval.item = idealUint(n) 441 } 442 return intLit 443 } 444 445 func (l *lexer) float(lval *yySymType, im bool) int { 446 if im { 447 l.val = l.val[:len(l.val)-1] 448 } 449 n, err := strconv.ParseFloat(string(l.val), 64) 450 if err != nil { 451 l.err("float literal: %v", err) 452 return int(unicode.ReplacementChar) 453 } 454 455 if im { 456 lval.item = idealComplex(complex(0, n)) 457 return imaginaryLit 458 } 459 460 lval.item = idealFloat(n) 461 return floatLit 462 }