-
Notifications
You must be signed in to change notification settings - Fork 1
/
ini.peg
142 lines (103 loc) · 3.75 KB
/
ini.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// peg based parser for ini files
{
package parser
// Helper function taken from pigeon source / examples
func toIfaceSlice(v interface{}) []interface{} {
if v == nil {
return nil
}
return v.([]interface{})
}
}
File <- lines:Line* EOF {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf("\n\n\n>> File: %s // '%s'", c.pos, string(c.text))
// convert iface to []*Line
lsSlice := toIfaceSlice(lines)
ls := make([]*Line, len(lsSlice))
for i, l := range lsSlice {
ls[i] = l.(*Line)
}
return NewFile(ls), nil
}
Line <- ws:_ item:(Comment / Section / KeyValuePair / KeyOnly)? le:LineEnd {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> Line: %s // '%s'", c.pos, string(c.text))
it, _ := item.(Item)
return NewLine(c.pos, ws.(string), it, le.(string)), nil
}
Comment <- cs:(';' / '#') comment:CommentVal {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> Comment: %s // '%s'\n", c.pos, string(c.text))
return NewComment(c.pos, string(cs.([]byte)), comment.(string)), nil
}
Section <- '[' name:SectionName ']' ws:_ comment:Comment? {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> Section: %s // '%s'\n", c.pos, name)
com, _ := comment.(*Comment)
return NewSection(c.pos, name.(string), ws.(string), com), nil
}
KeyValuePair <- key:Key '=' ws:_ val:Value comment:Comment? {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> KeyValuePair: %s // '%s': '%s'\n", c.pos, key, val)
com, _ := comment.(*Comment)
v, _ := val.(string)
return NewKeyValuePair(c.pos, key.(string), ws.(string), &v, com), nil
}
KeyOnly <- key:Key ws:_ comment:Comment? {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> KeyOnly: %s // '%s'\n", c.pos, key)
com, _ := comment.(*Comment)
return NewKeyValuePair(c.pos, key.(string), ws.(string), nil, com), nil
}
CommentVal <- (!LineEnd .)* {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> CommentVal: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
SectionName <- [^#;\r\n[\]]+ {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> SectionName: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
Key <- [^#;=\r\n[\]]+ {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> Key: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
Value <- QuotedValue / SimpleValue {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> Value: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
QuotedValue <- '"' Char* '"' _ {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> QuotedValue: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
Char <- !('"' / '\\') . / '\\' ([\\/bfnrt"] / 'u' HexDigit HexDigit HexDigit HexDigit) { // " // ignore
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> Char: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
HexDigit <- [0-9a-f]i {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> HexDigit: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
SimpleValue <- [^;#\r\n]* {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> SimpleValue: %s // '%s'\n", c.pos, string(c.text))
return string(c.text), nil
}
LineEnd <- "\r\n" / '\n' {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> LineEnd: %s\n", c.pos)
return string(c.text), nil
}
_ "whitespace" <- [ \t]* {
lastPosition, lastText = c.pos, string(c.text)
//fmt.Printf(">> _ %s\n", c.pos)
return string(c.text), nil
}
EOF <- !.