diff options
author | Pacien TRAN-GIRARD | 2014-09-30 22:07:44 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-09-30 22:07:44 +0200 |
commit | 1be0cb5a3fa5b2abe5f5d10ec827f06617861d4b (patch) | |
tree | 4ef98c2982834af152cd4b57a16d310e8635be0e /src/vparse | |
parent | 67b03424e3aee09078dff50cbf9d3e9e4221bc93 (diff) | |
download | go-vparse-master.tar.gz |
Diffstat (limited to 'src/vparse')
-rw-r--r-- | src/vparse/vparse.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/vparse/vparse.go b/src/vparse/vparse.go new file mode 100644 index 0000000..624d0a5 --- /dev/null +++ b/src/vparse/vparse.go | |||
@@ -0,0 +1,69 @@ | |||
1 | package vparse | ||
2 | |||
3 | import ( | ||
4 | "bufio" | ||
5 | "io" | ||
6 | "strings" | ||
7 | ) | ||
8 | |||
9 | type Node struct { | ||
10 | Type string | ||
11 | Properties map[string]string | ||
12 | Children []Node | ||
13 | } | ||
14 | |||
15 | func parseNode(scanner *bufio.Scanner, nodeType string) Node { | ||
16 | node := Node{nodeType, make(map[string]string), make([]Node, 0)} | ||
17 | |||
18 | var lastProperty string | ||
19 | var expectColon bool = false | ||
20 | |||
21 | SC: | ||
22 | for scanner.Scan() { | ||
23 | |||
24 | line := scanner.Text() | ||
25 | |||
26 | if strings.HasPrefix(line, " ") { | ||
27 | value := line[1:] | ||
28 | if expectColon { | ||
29 | value = strings.SplitN(value, ":", 2)[1] | ||
30 | } | ||
31 | |||
32 | node.Properties[lastProperty] += value | ||
33 | |||
34 | expectColon = false | ||
35 | continue | ||
36 | } | ||
37 | |||
38 | if !strings.Contains(line, ":") { | ||
39 | lastProperty = line | ||
40 | expectColon = true | ||
41 | continue | ||
42 | } | ||
43 | |||
44 | splitLine := strings.SplitN(line, ":", 2) | ||
45 | |||
46 | switch splitLine[0] { | ||
47 | |||
48 | case "END": | ||
49 | break SC | ||
50 | |||
51 | case "BEGIN": | ||
52 | node.Children = append(node.Children, parseNode(scanner, splitLine[1])) | ||
53 | break | ||
54 | |||
55 | default: | ||
56 | node.Properties[splitLine[0]] = splitLine[1] | ||
57 | lastProperty = splitLine[0] | ||
58 | |||
59 | } | ||
60 | |||
61 | } | ||
62 | |||
63 | return node | ||
64 | } | ||
65 | |||
66 | func Parse(reader io.Reader) []Node { | ||
67 | scanner := bufio.NewScanner(reader) | ||
68 | return parseNode(scanner, "").Children | ||
69 | } | ||