hintparserimpl.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2020 PingCAP, Inc.
  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. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package parser
  14. import (
  15. "strconv"
  16. "strings"
  17. "unicode"
  18. "github.com/pingcap/parser/ast"
  19. "github.com/pingcap/parser/mysql"
  20. "github.com/pingcap/parser/terror"
  21. )
  22. var (
  23. ErrWarnOptimizerHintUnsupportedHint = terror.ClassParser.NewStd(mysql.ErrWarnOptimizerHintUnsupportedHint)
  24. ErrWarnOptimizerHintInvalidToken = terror.ClassParser.NewStd(mysql.ErrWarnOptimizerHintInvalidToken)
  25. ErrWarnMemoryQuotaOverflow = terror.ClassParser.NewStd(mysql.ErrWarnMemoryQuotaOverflow)
  26. ErrWarnOptimizerHintParseError = terror.ClassParser.NewStd(mysql.ErrWarnOptimizerHintParseError)
  27. ErrWarnOptimizerHintInvalidInteger = terror.ClassParser.NewStd(mysql.ErrWarnOptimizerHintInvalidInteger)
  28. )
  29. // hintScanner implements the yyhintLexer interface
  30. type hintScanner struct {
  31. Scanner
  32. }
  33. func (hs *hintScanner) Errorf(format string, args ...interface{}) error {
  34. inner := hs.Scanner.Errorf(format, args...)
  35. return ErrWarnOptimizerHintParseError.GenWithStackByArgs(inner)
  36. }
  37. func (hs *hintScanner) Lex(lval *yyhintSymType) int {
  38. tok, pos, lit := hs.scan()
  39. hs.lastScanOffset = pos.Offset
  40. var errorTokenType string
  41. switch tok {
  42. case intLit:
  43. n, e := strconv.ParseUint(lit, 10, 64)
  44. if e != nil {
  45. hs.AppendError(ErrWarnOptimizerHintInvalidInteger.GenWithStackByArgs(lit))
  46. return int(unicode.ReplacementChar)
  47. }
  48. lval.number = n
  49. return hintIntLit
  50. case singleAtIdentifier:
  51. lval.ident = lit
  52. return hintSingleAtIdentifier
  53. case identifier:
  54. lval.ident = lit
  55. if tok1, ok := hintTokenMap[strings.ToUpper(lit)]; ok {
  56. return tok1
  57. }
  58. return hintIdentifier
  59. case stringLit:
  60. lval.ident = lit
  61. if hs.sqlMode.HasANSIQuotesMode() && hs.r.s[pos.Offset] == '"' {
  62. return hintIdentifier
  63. }
  64. return hintStringLit
  65. case bitLit:
  66. if strings.HasPrefix(lit, "0b") {
  67. lval.ident = lit
  68. return hintIdentifier
  69. }
  70. errorTokenType = "bit-value literal"
  71. case hexLit:
  72. if strings.HasPrefix(lit, "0x") {
  73. lval.ident = lit
  74. return hintIdentifier
  75. }
  76. errorTokenType = "hexadecimal literal"
  77. case quotedIdentifier:
  78. lval.ident = lit
  79. return hintIdentifier
  80. case eq:
  81. return '='
  82. case floatLit:
  83. errorTokenType = "floating point number"
  84. case decLit:
  85. errorTokenType = "decimal number"
  86. default:
  87. if tok <= 0x7f {
  88. return tok
  89. }
  90. errorTokenType = "unknown token"
  91. }
  92. hs.AppendError(ErrWarnOptimizerHintInvalidToken.GenWithStackByArgs(errorTokenType, lit, tok))
  93. return int(unicode.ReplacementChar)
  94. }
  95. type hintParser struct {
  96. lexer hintScanner
  97. result []*ast.TableOptimizerHint
  98. // the following fields are used by yyParse to reduce allocation.
  99. cache []yyhintSymType
  100. yylval yyhintSymType
  101. yyVAL *yyhintSymType
  102. }
  103. func newHintParser() *hintParser {
  104. return &hintParser{cache: make([]yyhintSymType, 50)}
  105. }
  106. func (hp *hintParser) parse(input string, sqlMode mysql.SQLMode, initPos Pos) ([]*ast.TableOptimizerHint, []error) {
  107. hp.result = nil
  108. hp.lexer.reset(input[3:])
  109. hp.lexer.SetSQLMode(sqlMode)
  110. hp.lexer.r.p = Pos{
  111. Line: initPos.Line,
  112. Col: initPos.Col + 3, // skipped the initial '/*+'
  113. Offset: 0,
  114. }
  115. hp.lexer.inBangComment = true // skip the final '*/' (we need the '*/' for reporting warnings)
  116. yyhintParse(&hp.lexer, hp)
  117. warns, errs := hp.lexer.Errors()
  118. if len(errs) == 0 {
  119. errs = warns
  120. }
  121. return hp.result, errs
  122. }
  123. // ParseHint parses an optimizer hint (the interior of `/*+ ... */`).
  124. func ParseHint(input string, sqlMode mysql.SQLMode, initPos Pos) ([]*ast.TableOptimizerHint, []error) {
  125. hp := newHintParser()
  126. return hp.parse(input, sqlMode, initPos)
  127. }
  128. func (hp *hintParser) warnUnsupportedHint(name string) {
  129. warn := ErrWarnOptimizerHintUnsupportedHint.GenWithStackByArgs(name)
  130. hp.lexer.warns = append(hp.lexer.warns, warn)
  131. }
  132. func (hp *hintParser) lastErrorAsWarn() {
  133. hp.lexer.lastErrorAsWarn()
  134. }