You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

query_string_parser.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2014 Couchbase, 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. // 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. // as of Go 1.8 this requires the goyacc external tool
  15. // available from golang.org/x/tools/cmd/goyacc
  16. //go:generate goyacc -o query_string.y.go query_string.y
  17. //go:generate sed -i.tmp -e 1d query_string.y.go
  18. //go:generate rm query_string.y.go.tmp
  19. // note: OSX sed and gnu sed handle the -i (in-place) option differently.
  20. // using -i.tmp works on both, at the expense of having to remove
  21. // the unsightly .tmp files
  22. package query
  23. import (
  24. "fmt"
  25. "strings"
  26. )
  27. var debugParser bool
  28. var debugLexer bool
  29. func parseQuerySyntax(query string) (rq Query, err error) {
  30. if query == "" {
  31. return NewMatchNoneQuery(), nil
  32. }
  33. lex := newLexerWrapper(newQueryStringLex(strings.NewReader(query)))
  34. doParse(lex)
  35. if len(lex.errs) > 0 {
  36. return nil, fmt.Errorf(strings.Join(lex.errs, "\n"))
  37. }
  38. return lex.query, nil
  39. }
  40. func doParse(lex *lexerWrapper) {
  41. defer func() {
  42. r := recover()
  43. if r != nil {
  44. lex.errs = append(lex.errs, fmt.Sprintf("parse error: %v", r))
  45. }
  46. }()
  47. yyParse(lex)
  48. }
  49. const (
  50. queryShould = iota
  51. queryMust
  52. queryMustNot
  53. )
  54. type lexerWrapper struct {
  55. lex yyLexer
  56. errs []string
  57. query *BooleanQuery
  58. }
  59. func newLexerWrapper(lex yyLexer) *lexerWrapper {
  60. return &lexerWrapper{
  61. lex: lex,
  62. query: NewBooleanQueryForQueryString(nil, nil, nil),
  63. }
  64. }
  65. func (l *lexerWrapper) Lex(lval *yySymType) int {
  66. return l.lex.Lex(lval)
  67. }
  68. func (l *lexerWrapper) Error(s string) {
  69. l.errs = append(l.errs, s)
  70. }