-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparser_line.go
44 lines (36 loc) · 874 Bytes
/
parser_line.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
package goyesql
import (
"regexp"
"strings"
)
// A line may be blank, a tag, a comment or a query
const (
lineBlank = iota
lineQuery
lineComment
lineTag
)
// ParsedLine stores line type and value
//
// For example: parsedLine{Type=lineTag, Value="foo"}
type parsedLine struct {
Type int
Value string
}
var (
// -- name: $tag
reTag = regexp.MustCompile("^\\s*--\\s*name\\s*:\\s*(.+)")
// -- $comment
reComment = regexp.MustCompile("^\\s*--\\s*(.+)")
)
func parseLine(line string) parsedLine {
line = strings.Trim(line, " ")
if line == "" {
return parsedLine{lineBlank, ""}
} else if matches := reTag.FindStringSubmatch(line); len(matches) > 0 {
return parsedLine{lineTag, matches[1]}
} else if matches := reComment.FindStringSubmatch(line); len(matches) > 0 {
return parsedLine{lineComment, matches[1]}
}
return parsedLine{lineQuery, line}
}