-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser_test.go
84 lines (66 loc) · 2.63 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
package parser_test
import (
"os"
"path"
"strings"
"testing"
"github.com/antlr4-go/antlr/v4"
plsqlparser "github.com/bytebase/plsql-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 TestPLSQLParser(t *testing.T) {
examples, err := os.ReadDir("examples")
require.NoError(t, err)
examplesSQLScript, err := os.ReadDir("examples-sql-script")
require.NoError(t, err)
files := append(examples, examplesSQLScript...)
for i, file := range files {
if strings.HasSuffix(file.Name(), ".sql.tree") {
continue
}
var filePath string
if i < len(examples) {
filePath = path.Join("examples", file.Name())
} else {
filePath = path.Join("examples-sql-script", file.Name())
}
t.Run(filePath, func(t *testing.T) {
t.Parallel()
input, err := antlr.NewFileStream(filePath)
require.NoError(t, err)
lexer := plsqlparser.NewPlSqlLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
p := plsqlparser.NewPlSqlParser(stream)
p.SetVersion12(true)
lexerErrors := &CustomErrorListener{}
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := &CustomErrorListener{}
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
_ = p.Sql_script()
require.Equal(t, 0, lexerErrors.errors)
require.Equal(t, 0, parserErrors.errors)
})
}
}