-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser_test.go
70 lines (53 loc) · 2.31 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
package parser_test
import (
"os"
"path"
"testing"
"github.com/antlr4-go/antlr/v4"
snowqlparser "github.com/bytebase/snowsql-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 TestSnowflakeSQLParser(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()
input, err := antlr.NewFileStream(filePath)
require.NoError(t, err)
lexer := snowqlparser.NewSnowflakeLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
p := snowqlparser.NewSnowflakeParser(stream)
lexerErrors := &CustomErrorListener{}
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := &CustomErrorListener{}
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
_ = p.Snowflake_file()
require.Equal(t, 0, lexerErrors.errors)
require.Equal(t, 0, parserErrors.errors)
})
}
}