-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsplit.go
64 lines (58 loc) · 1.56 KB
/
split.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
package memefish
import "github.com/cloudspannerecosystem/memefish/token"
type RawStatement struct {
Pos, End token.Pos
Statement string
}
// SplitRawStatements splits an input string to statement strings at terminating semicolons without parsing.
// It preserves all comments.
// Statements are terminated by `;`, `<eof>` or `;<eof>` and the minimum output will be []string{""}.
// See [terminating semicolons].
// This function won't panic but return error if lexer become error state.
// filepath can be empty, it is only used in error message.
//
// [terminating semicolons]: https://cloud.google.com/spanner/docs/reference/standard-sql/lexical#terminating_semicolons
func SplitRawStatements(filepath, s string) ([]*RawStatement, error) {
lex := &Lexer{
File: &token.File{
FilePath: filepath,
Buffer: s,
},
}
var result []*RawStatement
var firstPos token.Pos
for {
if lex.Token.Kind == ";" {
result = append(result, &RawStatement{
Pos: firstPos,
End: lex.Token.Pos,
Statement: s[firstPos:lex.Token.Pos],
})
if err := lex.NextToken(); err != nil {
return nil, err
}
firstPos = lex.Token.Pos
continue
}
err := lex.NextToken()
if err != nil {
return nil, err
}
if lex.Token.Kind == token.TokenEOF {
if lex.Token.Pos != firstPos {
result = append(result, &RawStatement{
Pos: firstPos,
End: lex.Token.Pos,
Statement: s[firstPos:lex.Token.Pos],
})
}
break
}
}
if len(result) == 0 {
return []*RawStatement{
{Statement: ""},
}, nil
}
return result, nil
}