forked from DataWraith/gopush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.go
73 lines (64 loc) · 1.24 KB
/
parsing.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
package gopush
import (
"errors"
"unicode"
)
func ignoreWhiteSpace(program string, includeNewline bool) string {
for i, r := range program {
if (r == '\n' && !includeNewline) || !(unicode.IsSpace(r)) {
return program[i:]
}
}
return ""
}
func ignoreComments(s string) string {
for {
s = ignoreWhiteSpace(s, true)
if s == "" {
return ""
}
if s[0] == '#' {
for i, r := range s {
if r == '\n' {
s = s[i+1:]
break
}
}
} else {
return s
}
}
}
func getToken(program string) (token, remainder string) {
for i, r := range program {
if unicode.IsSpace(r) {
return program[:i], program[i:]
}
}
return program, ""
}
func getToParen(program string) (subprogram, remainder string, err error) {
parenBalance := 1
for i, r := range program {
switch r {
case '(':
parenBalance++
case ')':
parenBalance--
}
if parenBalance == 0 {
return program[:i], program[i+1:], nil
}
}
return "", "", errors.New("unbalanced parentheses")
}
func getParameterSettingPair(s string) (parameter, setting, remainder string) {
s = ignoreComments(s)
if s == "" {
return "", "", ""
}
parameter, s = getToken(s)
s = ignoreWhiteSpace(s, false)
setting, s = getToken(s)
return parameter, setting, s
}