From 1be0cb5a3fa5b2abe5f5d10ec827f06617861d4b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 30 Sep 2014 22:07:44 +0200 Subject: First version --- src/vparse/vparse.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/vparse/vparse.go (limited to 'src/vparse/vparse.go') 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 @@ +package vparse + +import ( + "bufio" + "io" + "strings" +) + +type Node struct { + Type string + Properties map[string]string + Children []Node +} + +func parseNode(scanner *bufio.Scanner, nodeType string) Node { + node := Node{nodeType, make(map[string]string), make([]Node, 0)} + + var lastProperty string + var expectColon bool = false + +SC: + for scanner.Scan() { + + line := scanner.Text() + + if strings.HasPrefix(line, " ") { + value := line[1:] + if expectColon { + value = strings.SplitN(value, ":", 2)[1] + } + + node.Properties[lastProperty] += value + + expectColon = false + continue + } + + if !strings.Contains(line, ":") { + lastProperty = line + expectColon = true + continue + } + + splitLine := strings.SplitN(line, ":", 2) + + switch splitLine[0] { + + case "END": + break SC + + case "BEGIN": + node.Children = append(node.Children, parseNode(scanner, splitLine[1])) + break + + default: + node.Properties[splitLine[0]] = splitLine[1] + lastProperty = splitLine[0] + + } + + } + + return node +} + +func Parse(reader io.Reader) []Node { + scanner := bufio.NewScanner(reader) + return parseNode(scanner, "").Children +} -- cgit v1.2.3