/
parse.go
105 lines (86 loc) · 2.59 KB
/
parse.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package parse
import ("os"
"bufio"
"regexp"
)
// line reader for concurrent use
// filepath, line comment string, and an output chan containing lines
// of text. Comments and blank lines are stripped
// Closes the channel on EOF
func ReadLinesStripped(filepath string, line_comment string, lines chan<- string) {
// match comment string to end of line
com_re := regexp.MustCompile(line_comment + ".*$")
fd, err := os.Open(filepath)
if err != nil {
panic(err)
}
defer fd.Close()
scanln := bufio.NewScanner(fd)
for scanln.Scan() {
line := scanln.Text()
strip_line := com_re.ReplaceAllLiteralString(line, "")
if strip_line == "" { continue }
lines <- strip_line
}
if err := scanln.Err(); err != nil {
panic(err)
}
close(lines)
}
// given a channel of lines, split into tokens given an re object
// results go to an output chan of strings
func SplitTokenizer(split_re *regexp.Regexp, lines <-chan string, tokens chan<- string) {
for line := range lines {
for _, token := range split_re.Split(line, -1) {
if token == "" { continue }
tokens <- token
}
}
close(tokens)
}
// take n tokens from a string channel
func TakeN(n int, ch <-chan string) []string {
res := make([]string, n)
for i := 0 ; i < n ; i++ {
tok, ok := <- ch
if !ok { panic("parse error, premature end of file") }
res = append(res, tok)
}
return(res)
}
// remove tokens from a string channel until stop string is seen
func TakeUntil(stop string, ch <-chan string) []string {
res := []string{}
for {
tok, ok := <- ch
if ! ok { panic("parse error, premature end of file") }
res = append(res, tok)
if tok == stop { return(res) }
}
}
// remove tokens from a string channel until stop regexp is seen
func TakeUntilRE(stop_re *regexp.Regexp, ch <-chan string) []string {
res := []string{}
for {
tok, ok := <- ch
if ! ok { panic("parse error, premature end of file") }
res = append(res, tok)
if stop_re.MatchString(tok) { return(res) }
}
}
// map a slice of regexps against a slice of strings
// returning a boolean indicating whether all match
func MatchSyntax(tokens []string, template []*regexp.Regexp) bool {
//check for length match
size := len(template)
if size != len(tokens) {
return false
}
match := true
for i := 0 ; i < size ; i++ {
if ! template[i].MatchString(tokens[i]) {
match = false
}
}
return match
}