-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipeline.go
85 lines (76 loc) · 2.11 KB
/
pipeline.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
package pipeline
import (
"fmt"
"github.com/hscells/transmute/backend"
"github.com/hscells/transmute/lexer"
"github.com/hscells/transmute/parser"
"log"
"strings"
)
// TransmutePipeline contains the information needed to execute a full compilation.
type TransmutePipeline struct {
Parser parser.QueryParser
Compiler backend.Compiler
Options TransmutePipelineOptions
}
// TransmutePipelineOptions contains additional optional components relating to the pipeline.
type TransmutePipelineOptions struct {
LexOptions lexer.LexOptions
FieldMapping map[string][]string
AddRedundantParenthesis bool
RequiresLexing bool
}
// NewPipeline creates a new transmute pipeline.
func NewPipeline(parser parser.QueryParser, compiler backend.Compiler, options TransmutePipelineOptions) TransmutePipeline {
return TransmutePipeline{
Parser: parser,
Compiler: compiler,
Options: options,
}
}
// Execute takes a pipeline and a query and will fully lex, parse, and compile the query.
func (p TransmutePipeline) Execute(query string) (backend.BooleanQuery, error) {
// Set the field mapping on the parser if it is defined separately in the pipeline.
// Otherwise, the default field mapping will be used for the parser.
if p.Options.FieldMapping != nil || len(p.Options.FieldMapping) > 0 {
p.Parser.FieldMapping = p.Options.FieldMapping
}
// Lex.
var ast lexer.Node
var err error
query = strings.TrimSpace(query)
if p.Options.AddRedundantParenthesis {
if strings.Count(query, "\n") == 0 {
n := 0
for i, c := range query {
if c == '(' {
n++
} else if c == ')' {
n--
}
if i > 0 && i < len(query)-1 && n == 0 {
log.Println("adding a redundant set of parens to query")
query = fmt.Sprintf("(%s)", query)
break
}
}
}
}
if p.Options.RequiresLexing {
ast, err = lexer.Lex(query, p.Options.LexOptions)
if err != nil {
return nil, err
}
} else {
ast = lexer.Node{
Value: query,
Children: nil,
Operator: "",
Reference: 1,
}
}
// Parse.
boolQuery := p.Parser.Parse(ast)
// Compile.
return p.Compiler.Compile(boolQuery)
}