forked from Zakay/gofigure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
180 lines (136 loc) · 4.4 KB
/
parser.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"os"
"strings"
"github.com/alecthomas/participle"
"github.com/alecthomas/participle/lexer"
)
// A valid indentifier part is one of the following:
// 1. an escaped character, like \"
// 2. a string of characters
var re_valid_ident_part = `(\\.|[a-zA-Z_][-a-zA-Z\d_]+)`
// GoFigureLexer - Contains the lexicographic rules for how gofigure is parsed
var GoFigureLexer = lexer.Must(lexer.Regexp(
`(?m)` +
`(\s+)` +
`|([#;].*$)|(/\*[.\s\n\r]*\*/)` + // Comments
`|(?P<MLString>("""(?:\\.|[^(""")])*""")|('''(?:\\.|[^(''')])*'''))` +
`|(?P<String>("(.|\\)*?")|('(.|\\)*?'))` +
`|(?P<Boolean>true|false)` +
`|(?P<Ident>` + re_valid_ident_part + `)` +
`|(?P<Float>-?\d+\.\d+)` +
`|(?P<Int>-?\d+)` +
`|(?P<SectionEnd>\[\])` +
`|(?P<Include>%include)` +
`|(?P<Expand>\.\.\.)` +
`|(?P<Special>[][{}.,:%@])`,
))
// FigureConfig - Structure capable of containing a full GoFigure configuration
type FigureConfig struct {
Entries []*Entry `(@@)*`
Pos lexer.Position
}
type Entry struct {
Include *Include `@@`
Section *Section `| "[" @@ (SectionEnd|EOF)?`
Field *Field `| @@`
Pos lexer.Position
}
type Include struct {
Includes []string `"%include" @String (","? @String)* `
Pos lexer.Position
}
/*
As SectionRoot and SectionChild must have different rules for how they are parsed,
they have to be separate structres.
*/
type Section struct {
Roots []SectionRoot `(@@)+ "]"`
Fields []*Field `(@@)*`
Pos lexer.Position
}
type SectionRoot struct {
Identifier []string `(@(Ident|"@") ("," " "*|" ")? | "%" "{" (@Ident ("," " "*|" ")?)* "}")`
Child *SectionChild `(@@)?`
Pos lexer.Position
}
type SectionChild struct {
Identifier []string `"." (@(Ident|"@"|Int) ("," " "*|" ")? | "%" "{" (@Int @"..." @Int | (@(Ident|Int) ("," " "*|" ")?)*) "}")`
Child *SectionChild `(@@)?`
Pos lexer.Position
}
type Field struct {
Key string `( (@Ident|@String) ` // Key
Child *ChildField ` ( "." @@` // When a child field should be created this is where it goes
Value *Value ` | ":" @@ )?)` // ? == allow empty values
ArrayIndex *int64
// ArrayIndex is not populated at parse-time,
// it's in this struct as childfields later get expanded to regular fields
Pos lexer.Position
}
type ChildField struct {
Key string `(( (@Ident|@String) ` // Key
ArrayIndex *int64 `|@Int)`
Child *ChildField `( "." @@` // When a child field should be created this is where it goes
Value *Value `| ":" @@ )?)` // ? == allow empty values
Pos lexer.Position
}
type UnprocessedString struct {
String *string `@MLString`
Pos lexer.Position
}
type Bool bool
func (b *Bool) Capture(v []string) error { *b = v[0] == "true"; return nil }
type Value struct {
String *string `@String`
MultilineString *UnprocessedString `| @@`
Integer *int64 `| @Int`
Float *float64 `| @Float`
Boolean *Bool `| (@"true" | @"false") `
Map []*Field `| "{" ((@@ ","?)* )? "}"`
ParsedArray []*Value `| "[" ((@@ ","?)* )? "]"`
Identifier *string `| @Ident @("." Ident)*`
// Here is where a sequential-number named map goes
FinalArray []*Value
Pos lexer.Position
}
func checkFileError(err error, filename string) {
if err != nil {
panic(strings.Replace(err.Error(), "<source>", filename, 1))
}
}
var _workingDirectory string
func setWorkingDirectory(workingDirectory string) {
_workingDirectory = workingDirectory
}
// BuildParser - Builds a new parser with GoFigureLexer as lexer
func BuildParser() (parser *participle.Parser) {
parser, err := participle.Build(
&FigureConfig{},
participle.Lexer(GoFigureLexer),
participle.Unquote("String"),
)
check(err)
return
}
var fileCache map[string]FigureConfig
// ParseFile - Parses a file with given filename and parser. If a nil argument is passed instead of a parser a new one is built
func ParseFile(filename string, parser *participle.Parser) (config FigureConfig) {
if config, exists := fileCache[filename]; exists {
return config
}
config = FigureConfig{}
if parser == nil {
parser = BuildParser()
}
if _workingDirectory != "" {
filename = _workingDirectory + "/" + filename
}
// Open a handle to file
file, err := os.Open(filename)
check(err)
err = parser.Parse(file, &config)
check(file.Close())
checkFileError(err, filename)
return
}