-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
152 lines (137 loc) · 2.98 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2020 by Chris Palmer (https://noncombatant.org)
// SPDX-License-Identifier: GPL-3.0
package main
import (
"fmt"
"unicode"
)
type Query struct {
Keyword string
Term string
Negated bool
}
func (q Query) String() string {
return fmt.Sprintf("{Keyword: %q, Term: %q, Negated: %t}", q.Keyword, q.Term, q.Negated)
}
func parseTerms(query string) []string {
const (
Start = iota
Bareword
Quoted
Boundary
)
state := Start
currentTerm := ""
var terms []string
for _, r := range query {
if state == Bareword {
if unicode.IsSpace(r) {
state = Boundary
if currentTerm != "" {
terms = append(terms, currentTerm)
currentTerm = ""
}
} else if r == ':' {
state = Boundary
if currentTerm != "" {
terms = append(terms, currentTerm)
currentTerm = ""
}
terms = append(terms, string(r))
} else {
currentTerm += string(r)
}
} else if state == Quoted {
if r == '"' {
state = Boundary
if currentTerm != "" {
terms = append(terms, currentTerm)
currentTerm = ""
}
} else {
currentTerm += string(r)
}
} else if state == Boundary {
if r == '"' {
state = Quoted
} else if r == '-' || r == ':' {
if currentTerm != "" {
terms = append(terms, currentTerm)
currentTerm = ""
}
terms = append(terms, string(r))
} else if !unicode.IsSpace(r) {
state = Bareword
currentTerm += string(r)
}
} else {
if unicode.IsSpace(r) {
state = Boundary
if currentTerm != "" {
terms = append(terms, currentTerm)
currentTerm = ""
}
} else if r == '"' {
state = Quoted
if currentTerm != "" {
terms = append(terms, currentTerm)
currentTerm = ""
}
} else if r == '-' || r == ':' {
state = Boundary
if currentTerm != "" {
terms = append(terms, currentTerm)
currentTerm = ""
}
terms = append(terms, string(r))
} else {
state = Bareword
currentTerm += string(r)
}
}
}
if currentTerm != "" {
terms = append(terms, currentTerm)
}
return terms
}
// Consumes some of `terms`; return a `Query` and remainder of `terms`.
func getQuery(terms []string) (Query, []string) {
if terms[0] == "-" {
if len(terms) > 1 {
return Query{"", terms[1], true}, terms[2:]
} else {
// Don't return `Query{"", "-", ...}`, which is meaningless.
return Query{}, []string{}
}
}
if len(terms) > 1 {
if terms[1] == ":" {
if len(terms) > 3 {
if terms[2] == "-" {
return Query{terms[0], terms[3], true}, terms[4:]
} else {
return Query{terms[0], terms[2], false}, terms[3:]
}
} else {
return Query{terms[0], terms[2], false}, terms[3:]
}
} else {
return Query{"", terms[0], false}, terms[1:]
}
} else {
return Query{"", terms[0], false}, []string{}
}
}
func reconstructQueries(terms []string) []Query {
queries := make([]Query, 0)
for {
if len(terms) == 0 {
break
}
var q Query
q, terms = getQuery(terms)
queries = append(queries, q)
}
return queries
}