-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser_test.go
85 lines (63 loc) · 2.69 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
package parser_test
import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/antlr4-go/antlr/v4"
mysqlparser "github.com/bytebase/mysql-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 TestMySQLDBSQLParser(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 := ioutil.ReadFile(filePath)
require.NoError(t, err)
dataString := strings.TrimRight(string(data), " \t\r\n;") + "\n;"
// dataString := string(data)
// antlr.ConfigureRuntime(antlr.WithParserATNSimulatorDebug(true))
input := antlr.NewInputStream(dataString)
lexer := mysqlparser.NewMySQLLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
p := mysqlparser.NewMySQLParser(stream)
lexerErrors := &CustomErrorListener{}
lexer.RemoveErrorListeners()
lexer.AddErrorListener(lexerErrors)
parserErrors := &CustomErrorListener{}
p.RemoveErrorListeners()
p.AddErrorListener(parserErrors)
p.BuildParseTrees = true
tree := p.Script()
// tree := p.FromClause()
// fmt.Println(stream.GetAllText())
require.Equal(t, 0, lexerErrors.errors)
require.Equal(t, 0, parserErrors.errors)
require.Equal(t, dataString, stream.GetTextFromRuleContext(tree))
})
}
}