-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_test.go
72 lines (57 loc) · 1.54 KB
/
match_test.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
package tpl
import (
"strconv"
"strings"
"testing"
)
// -----------------------------------------------------------------------------
func TestMatch(t *testing.T) {
/* term = factor *('*' factor/mul | '/' factor/div)
expr = term *('+' term/add | '-' term/sub)
factor =
FLOAT/push |
'-' factor/neg |
'(' expr ')' |
(IDENT '(' expr % ','/arity ')')/call |
'+' factor
*/
var stk []string
var args Grammar
scanner := new(Scanner)
push := func(tokens []Token, g Grammar) {
v := tokens[0].Literal
if v == "" {
v = scanner.Ttol(tokens[0].Kind)
} else if tokens[0].Kind == IDENT {
v += "/" + strconv.Itoa(args.Len())
}
stk = append(stk, v)
}
factor := Var("factor")
term := And(factor, Repeat0(Or(
Action(And(Gr('*'), factor), push),
Action(And(Gr('/'), factor), push),
)))
expr := And(term, Repeat0(Or(
Action(And(Gr('+'), term), push),
Action(And(Gr('-'), term), push),
)))
args = List(expr, Gr(','))
factor.Assign(Or(
Action(Gr(FLOAT), push),
Action(And(Gr('-'), factor), push),
And(Gr('('), expr, Gr(')')),
Action(And(Gr(IDENT), Gr('('), args, Gr(')')), push),
And(Gr('+'), factor),
))
m := Matcher{Grammar: expr, Scanner: scanner}
err := m.MatchExactly([]byte(`max(1.2 + sin(.3) * 2, cos(3), pow(5, 6), 7)`), "")
if err != nil {
t.Fatal("MatchExactly failed:", err)
}
text := strings.Join(stk, " ")
if text != "1.2 .3 sin/1 2 '*' '+' 3 cos/1 5 6 pow/2 7 max/4" {
t.Fatal("MatchExactly failed:", text)
}
}
// -----------------------------------------------------------------------------