forked from chsc/gogl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmreader.go
70 lines (60 loc) · 1.35 KB
/
tmreader.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
// Copyright 2011 The GoGL Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.mkd file.
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
)
var (
tmEmptyOrCommentRE = regexp.MustCompile("^[ \\t]*(#.*)?$")
tmTypePairRE = regexp.MustCompile("^([_A-Za-z0-9]+),\\*,\\*,[\\t ]*([A-Za-z0-9\\*_ ]+),\\*,\\*")
)
func ReadTypeMapFromFiles(files []string) (TypeMap, error) {
allTypes := make(TypeMap)
for _, file := range files {
types, err := ReadTypeMapFromFile(file)
if err != nil {
return nil, err
}
for k, v := range types {
allTypes[k] = v
}
}
return allTypes, nil
}
func ReadTypeMapFromFile(name string) (TypeMap, error) {
file, err := os.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
return ReadTypeMap(file)
}
func ReadTypeMap(r io.Reader) (TypeMap, error) {
tm := make(TypeMap)
br := bufio.NewReader(r)
for {
line, err := br.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
line = strings.TrimRight(line, "\n")
if tmEmptyOrCommentRE.MatchString(line) {
continue
}
if typePair := tmTypePairRE.FindStringSubmatch(line); typePair != nil {
tm[typePair[1]] = typePair[2]
} else {
return tm, fmt.Errorf("Unable to parse line: '%v'", line)
}
}
return tm, nil
}