-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser_test.go
223 lines (171 loc) · 6.05 KB
/
parser_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package parser_test
import (
"fmt"
"os"
"path"
"testing"
"time"
"github.com/antlr4-go/antlr/v4"
pgparser "github.com/bytebase/postgresql-parser"
"github.com/stretchr/testify/require"
)
type CustomErrorListener struct {
errors int
}
func NewCustomErrorListener() *CustomErrorListener {
return new(CustomErrorListener)
}
func (l *CustomErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {
l.errors += 1
antlr.ConsoleErrorListenerINSTANCE.SyntaxError(recognizer, offendingSymbol, line, column, msg, e)
}
func (l *CustomErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs *antlr.ATNConfigSet) {
antlr.ConsoleErrorListenerINSTANCE.ReportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs)
}
func (l *CustomErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs *antlr.ATNConfigSet) {
antlr.ConsoleErrorListenerINSTANCE.ReportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs)
}
func (l *CustomErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex, prediction int, configs *antlr.ATNConfigSet) {
antlr.ConsoleErrorListenerINSTANCE.ReportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs)
}
func TestPostgreSQLParser(t *testing.T) {
examples, err := os.ReadDir("examples")
require.NoError(t, err)
for _, file := range examples {
filePath := path.Join("examples", file.Name())
t.Run(filePath, func(t *testing.T) {
t.Parallel()
// read all the bytes from the file
data, err := os.ReadFile(filePath)
require.NoError(t, err)
input := antlr.NewInputStream(string(data))
lexer := pgparser.NewPostgreSQLLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
p := pgparser.NewPostgreSQLParser(stream)
lexerErrors := &CustomErrorListener{}
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := &CustomErrorListener{}
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
tree := p.Root()
require.Equal(t, 0, lexerErrors.errors)
require.Equal(t, 0, parserErrors.errors)
start := 0
stop := stream.Size() - 1
for i := 0; i < stream.Size(); i++ {
if stream.Get(i).GetChannel() == antlr.TokenDefaultChannel {
start = i
break
}
}
for i := stream.Size() - 1; i >= 0; i-- {
if stream.Get(i).GetChannel() == antlr.TokenDefaultChannel && stream.Get(i).GetTokenType() != antlr.TokenEOF {
stop = i
break
}
}
require.Equal(t, start, tree.GetStart().GetTokenIndex())
require.Equal(t, stop, tree.GetStop().GetTokenIndex())
// require.Equal(t, string(data), stream.GetTextFromTokens(stream.Get(0), stream.Get(stream.Size()-1)))
})
}
}
type ParseService struct {
lexer *pgparser.PostgreSQLLexer
parser *pgparser.PostgreSQLParser
tokens antlr.TokenStream
input antlr.CharStream
errors int
}
func NewParseService() *ParseService {
s := &ParseService{
input: antlr.NewInputStream(""),
}
s.lexer = pgparser.NewPostgreSQLLexer(s.input)
s.tokens = antlr.NewCommonTokenStream(s.lexer, 0)
s.parser = pgparser.NewPostgreSQLParser(s.tokens)
return s
}
func (s *ParseService) Parse(script string, buildTree bool) antlr.RuleContext {
s.errors = 0
s.input = antlr.NewInputStream(script)
s.lexer.Reset()
s.lexer.SetInputStream(s.input)
s.tokens.SetTokenSource(s.lexer)
// set token stream and reset parser
s.parser.SetTokenStream(s.tokens)
lexerErrors := &CustomErrorListener{}
s.lexer.RemoveErrorListeners()
s.lexer.AddErrorListener(lexerErrors)
parserErrors := &CustomErrorListener{}
s.parser.RemoveErrorListeners()
s.parser.AddErrorListener(parserErrors)
// Set the parser's error strategy to bail on the first error encountered
s.parser.SetErrorHandler(antlr.NewBailErrorStrategy())
s.parser.BuildParseTrees = buildTree
tree := s.parser.Root()
s.errors += lexerErrors.errors
s.errors += parserErrors.errors
return tree
}
func TestBenchmarkParser(t *testing.T) {
s := NewParseService()
examples, err := os.ReadDir("examples")
require.NoError(t, err)
var files []string
for i, file := range examples {
if i > 20 {
break
}
filePath := path.Join("examples", file.Name())
data, err := os.ReadFile(filePath)
require.NoError(t, err)
files = append(files, string(data))
}
fmt.Printf("Total files: %d\n", len(files))
newParserEachFileStartTime := time.Now()
for _, file := range files {
input := antlr.NewInputStream(file)
lexer := pgparser.NewPostgreSQLLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
p := pgparser.NewPostgreSQLParser(stream)
lexerErrors := &CustomErrorListener{}
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := &CustomErrorListener{}
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
_ = p.Root()
require.Equal(t, 0, lexerErrors.errors)
require.Equal(t, 0, parserErrors.errors)
}
fmt.Printf("New parser each file took %s\n", time.Since(newParserEachFileStartTime).String())
coldStartTime := time.Now()
for _, file := range files {
_ = s.Parse(string(file), true)
require.Zero(t, s.errors)
}
fmt.Printf("Parse with cold start took %s\n", time.Since(coldStartTime).String())
for i := 0; i < 5; i++ {
for _, file := range files {
_ = s.Parse(string(file), true)
require.Zero(t, s.errors)
}
}
fmt.Println("Finish warm up")
warmStartTime := time.Now()
for _, file := range files {
_ = s.Parse(string(file), true)
require.Zero(t, s.errors)
}
fmt.Printf("Parse with warm start took %s\n", time.Since(warmStartTime).String())
withoutParseTreeStartTime := time.Now()
for _, file := range files {
_ = s.Parse(string(file), false)
require.Zero(t, s.errors)
}
fmt.Printf("Parse without parse tree took %s\n", time.Since(withoutParseTreeStartTime).String())
}