-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.peg
116 lines (89 loc) · 1.76 KB
/
host.peg
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
106
107
108
109
110
111
112
113
114
115
116
{
package main
import (
"log"
"strings"
"net"
)
}
hosts <- items:(( host / blanks / comment )+) {
lns := toASTHostLineSlice(items)
return ASTHosts(lns),nil
}
host <- _* ip:ip _+ a:aliases _* com:comment? {
var com_val ASTComment
if com != nil {
com_val = com.(ASTComment)
}
return ASTHost{
IP: ip.(net.IP),
Aliases: a.([]string),
line: c.pos.line,
comment: &com_val,
},nil
}
comment <- hash comment:(NOTEOL*) NLEOF {
cmt := toIfaceSlice(comment)
str := make([]byte,0,len(cmt))
for i := range cmt {
bs := cmt[i].([]byte)
for j := range bs {
str = append(str, bs[j])
}
}
return ASTComment{
Comment: string(str),
line: c.pos.line,
}, nil
}
blanks <- blanks:(blank+) {
lns := toIfaceSlice(blanks)
return ASTBlank{
Count: len(lns),
line: c.pos.line,
},nil
}
blank <- _* NL { return "\n", nil }
aliases <- first:alias rest:(op_alias*) {
rst := toStringSlice(rest)
strs := make([]string,0,len(rst)+1)
strs = append(strs,first.(string))
for _,s := range rst {
if s == "" {
continue
}
strs = append(strs,s)
}
return strs,nil
}
op_alias <- _+ a:alias { return a, nil }
alias <- (word / digit / dot / dash)+ {
return string(c.text), nil
}
ip <- ipv4 / ipv6
ipv6 <- (hex* colon? colon hex*)+ {
return net.ParseIP(string(c.text)),nil
}
ipv4 <- (quadpair dot quadpair dot quadpair dot quadpair) {
return net.ParseIP(string(c.text)),nil
}
quadpair <- [0-9]+
colon <- ':'
digit <- [0-9]
hex <- [0-9a-f]i+
dash <- "-"
dot <- "."
word <- [\pL]+
hash <- "#"
NOTEOL <- ch:(.) &{
char := string(ch.([]byte))
switch char {
case "\n","\r":
return false, nil
}
return true, nil
} { return c.text, nil }
NLEOF <- NL / EOF
NL <- ( "\r\n" / "\n\r" / "\r" / "\n" )
_ "space" <- ( "\t" / [\pZ] )
EOF <- !.