diff options
author | Michael Muré <batolettre@gmail.com> | 2020-08-20 12:00:34 +0200 |
---|---|---|
committer | Mike Goldin <mike.goldin@protonmail.ch> | 2020-11-17 08:09:51 -0500 |
commit | b285c57dc62caac2c1f09e74eeece406b0e7cc00 (patch) | |
tree | aa2f15c8e3a250c8bd8d8f4a7b11a5c9819453a8 /query/lexer.go | |
parent | 902997f53771babb2f9ea1bb6288c2ec295c4c9e (diff) | |
download | git-bug-b285c57dc62caac2c1f09e74eeece406b0e7cc00.tar.gz |
query: expand the tokenizer/parser to parse arbitrary search terms
Diffstat (limited to 'query/lexer.go')
-rw-r--r-- | query/lexer.go | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/query/lexer.go b/query/lexer.go index ca67d641..5ca700c7 100644 --- a/query/lexer.go +++ b/query/lexer.go @@ -6,9 +6,38 @@ import ( "unicode" ) +type tokenKind int + +const ( + _ tokenKind = iota + tokenKindKV + tokenKindSearch +) + type token struct { + kind tokenKind + + // KV qualifier string value string + + // Search + term string +} + +func newTokenKV(qualifier, value string) token { + return token{ + kind: tokenKindKV, + qualifier: qualifier, + value: value, + } +} + +func newTokenSearch(term string) token { + return token{ + kind: tokenKindSearch, + term: term, + } } // tokenize parse and break a input into tokens ready to be @@ -22,6 +51,13 @@ func tokenize(query string) ([]token, error) { var tokens []token for _, field := range fields { split := strings.Split(field, ":") + + // full text search + if len(split) == 1 { + tokens = append(tokens, newTokenSearch(removeQuote(field))) + continue + } + if len(split) != 2 { return nil, fmt.Errorf("can't tokenize \"%s\"", field) } @@ -33,14 +69,13 @@ func tokenize(query string) ([]token, error) { return nil, fmt.Errorf("empty value for qualifier \"%s\"", split[0]) } - tokens = append(tokens, token{ - qualifier: split[0], - value: removeQuote(split[1]), - }) + tokens = append(tokens, newTokenKV(split[0], removeQuote(split[1]))) } return tokens, nil } +// split the query into chunks by splitting on whitespaces but respecting +// quotes func splitQuery(query string) ([]string, error) { lastQuote := rune(0) inQuote := false |