-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
144 lines (121 loc) · 3.88 KB
/
main_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
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
package main_test
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/conflowio/parsley/ast"
"github.com/conflowio/parsley/combinator"
"github.com/conflowio/parsley/parser"
"github.com/conflowio/parsley/parsley"
"github.com/conflowio/parsley/text"
"github.com/conflowio/parsley/text/terminal"
)
// Let's define a simple parser which is able to parse adding two integers.
func ExampleParse() {
sum := ast.InterpreterFunc(func(userCtx interface{}, node parsley.NonTerminalNode) (interface{}, parsley.Error) {
nodes := node.Children()
value0, _ := parsley.EvaluateNode(userCtx, nodes[0])
value1, _ := parsley.EvaluateNode(userCtx, nodes[2])
return value0.(int64) + value1.(int64), nil
})
p := combinator.SeqOf(
terminal.Integer("integer"),
terminal.Rune('+'),
terminal.Integer("integer"),
).Bind(sum)
r := text.NewReader(text.NewFile("example.file", []byte("1+2")))
ctx := parsley.NewContext(parsley.NewFileSet(), r)
node, err := parsley.Parse(ctx, combinator.Sentence(p))
if err != nil {
panic(err)
}
value, err2 := parsley.EvaluateNode(nil, node)
if err2 != nil {
panic(err2)
}
fmt.Printf("Result: %d\n", value.(int64))
// Output: Result: 3
}
// Let's define a simple parser which is able to parse adding two integers.
func ExampleEvaluate() {
sum := ast.InterpreterFunc(func(userCtx interface{}, node parsley.NonTerminalNode) (interface{}, parsley.Error) {
nodes := node.Children()
value0, _ := parsley.EvaluateNode(userCtx, nodes[0])
value1, _ := parsley.EvaluateNode(userCtx, nodes[2])
return value0.(int64) + value1.(int64), nil
})
p := combinator.SeqOf(
terminal.Integer("integer"),
terminal.Rune('+'),
terminal.Integer("integer"),
).Bind(sum)
r := text.NewReader(text.NewFile("example.file", []byte("1+2")))
ctx := parsley.NewContext(parsley.NewFileSet(), r)
value, err := parsley.Evaluate(ctx, combinator.Sentence(p))
if err != nil {
panic(err)
}
fmt.Printf("Result: %d\n", value.(int64))
// Output: Result: 3
}
var _ = Describe("Parsley", func() {
It("should handle direct left recursion", func() {
input := "abbbbbbbbbbbbbbbbbbb"
concat := ast.InterpreterFunc(func(userCtx interface{}, node parsley.NonTerminalNode) (interface{}, parsley.Error) {
s := ""
for _, node := range node.Children() {
val, err := parsley.EvaluateNode(userCtx, node)
if err != nil {
return nil, err
}
if runeVal, ok := val.(rune); ok {
s = s + string(runeVal)
} else {
s = s + val.(string)
}
}
return s, nil
})
var p parser.Func
p = combinator.Memoize(combinator.Any(
combinator.SeqOf(
&p,
terminal.Rune('b'),
).Bind(concat),
terminal.Rune('a'),
))
f := text.NewFile("testfile", []byte(input))
r := text.NewReader(f)
ctx := parsley.NewContext(parsley.NewFileSet(), r)
result, err := parsley.Evaluate(ctx, combinator.Sentence(&p))
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(input))
Expect(ctx.CallCount()).To(Equal(298))
})
It("should handle highly ambiguous left-recursive grammar", func() {
input := "1+2+3+4+5+6+7+8+9+10"
add := ast.InterpreterFunc(func(userCtx interface{}, node parsley.NonTerminalNode) (interface{}, parsley.Error) {
nodes := node.Children()
value0, _ := parsley.EvaluateNode(userCtx, nodes[0])
value1, _ := parsley.EvaluateNode(userCtx, nodes[2])
return value0.(int64) + value1.(int64), nil
})
var p parser.Func
value := combinator.Memoize(combinator.Any(
terminal.Integer("integer"),
&p,
))
p = combinator.Memoize(combinator.SeqOf(
value,
terminal.Rune('+'),
value,
).Bind(add))
f := text.NewFile("testfile", []byte(input))
r := text.NewReader(f)
ctx := parsley.NewContext(parsley.NewFileSet(), r)
result, err := parsley.Evaluate(ctx, combinator.Sentence(&p))
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(int64(55)))
Expect(ctx.CallCount()).To(Equal(237769))
})
})