forked from akrennmair/epos
/
expression.go
135 lines (110 loc) · 2.98 KB
/
expression.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
package epos
import (
"errors"
"fmt"
"github.com/feyeleanor/atomiser"
"github.com/feyeleanor/chain"
"strconv"
"strings"
)
// Expression converts a S-Expr-based query to a structure of Condition objects.
// The following symbols are available for queries:
//
// (id 1) query entry with ID 1
// (eq field-name value) query all entries where field-name equals value
// (or expr...) OR all query sub-expressions
// (and expr...) AND all query sub-expressions
func Expression(s string) (Condition, error) {
expr := atomiser.NewAtomiser(strings.NewReader(s)).ReadList()
if expr == nil {
return nil, fmt.Errorf("parsing '%s' failed", s)
}
return parseExpressionToCondition(expr)
}
func parseExpressionToCondition(expr *chain.Cell) (Condition, error) {
sym, ok := expr.Car().(atomiser.Symbol)
if !ok {
return nil, fmt.Errorf("expected symbol, got '%v' instead", expr.Car())
}
sym = atomiser.Symbol(strings.ToLower(string(sym)))
switch sym {
case "and":
return parseAnd(expr.Cdr())
case "or":
return parseOr(expr.Cdr())
case "eq":
return parseEqual(expr.Cdr())
case "id":
return parseId(expr.Cdr())
}
return nil, fmt.Errorf("unknown symbol '%s'", sym)
}
func parseAnd(expr *chain.Cell) (Condition, error) {
cond := &And{}
cur := expr
for cur != nil {
if subexpr, ok := cur.Car().(*chain.Cell); ok {
if subcond, err := parseExpressionToCondition(subexpr); err != nil {
return nil, err
} else {
*cond = append(*cond, subcond)
}
}
cur = cur.Cdr()
}
if len(*cond) == 0 {
return nil, errors.New("empty and expression")
}
return cond, nil
}
func parseOr(expr *chain.Cell) (Condition, error) {
cond := &Or{}
cur := expr
for cur != nil {
if subexpr, ok := cur.Car().(*chain.Cell); ok {
if subcond, err := parseExpressionToCondition(subexpr); err != nil {
return nil, err
} else {
*cond = append(*cond, subcond)
}
}
cur = cur.Cdr()
}
if len(*cond) == 0 {
return nil, errors.New("empty or expression")
}
return cond, nil
}
func parseEqual(expr *chain.Cell) (Condition, error) {
cond := &Equals{}
if expr == nil {
return nil, errors.New("missing arguments in eq")
}
if field, ok := expr.Car().(atomiser.Symbol); !ok {
return nil, fmt.Errorf("expected field name, got '%#v' instead", expr.Car())
} else {
cond.Field = string(field)
}
expr = expr.Cdr()
if expr == nil {
return nil, fmt.Errorf("missing value in (eq %s) expression", cond.Field)
}
cond.Value = fmt.Sprintf("%v", expr.Car())
return cond, nil
}
func parseId(expr *chain.Cell) (Condition, error) {
if expr == nil {
return nil, fmt.Errorf("missing ID value in (id) expression")
}
id := new(Id)
if id_str, ok := expr.Car().(atomiser.Symbol); !ok {
return nil, fmt.Errorf("expected ID, got '%#v' instead", expr.Car())
} else {
parsed_id, err := strconv.ParseInt(string(id_str), 10, 64)
if err != nil {
return nil, fmt.Errorf("couldn't parse numeric ID in id expression")
}
*id = Id(parsed_id)
}
return id, nil
}