Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 57 lines (47 sloc) 1.106 kB
fffe53c @chsc Copyright added to each source file.
authored
1 // Copyright 2011 The GoGL Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE.mkd file.
4
88ece00 @chsc initial commit
authored
5 package main
6
7 import (
0149fb8 Implemented ReadTypeMap
J. Bates authored
8 "bufio"
2af3170 @chsc Compatibility fixes for Go 1
authored
9 "fmt"
10 "io"
11 "os"
0149fb8 Implemented ReadTypeMap
J. Bates authored
12 "regexp"
3c41569 @chsc Typemap reader refactored + unit test
authored
13 "strings"
0149fb8 Implemented ReadTypeMap
J. Bates authored
14 )
15
16 var (
3c41569 @chsc Typemap reader refactored + unit test
authored
17 tmEmptyOrCommentRE = regexp.MustCompile("^[ \\t]*(#.*)?$")
fcec1a5 @chsc Type map parsing fixed + better error handling
authored
18 tmTypePairRE = regexp.MustCompile("^([_A-Za-z0-9]+),\\*,\\*,[\\t ]*([A-Za-z0-9\\*_ ]+),\\*,\\*")
88ece00 @chsc initial commit
authored
19 )
20
2af3170 @chsc Compatibility fixes for Go 1
authored
21 func ReadTypeMapFromFile(name string) (TypeMap, error) {
88ece00 @chsc initial commit
authored
22 file, err := os.Open(name)
23 if err != nil {
24 return nil, err
25 }
26 defer file.Close()
27 return ReadTypeMap(file)
28 }
29
2af3170 @chsc Compatibility fixes for Go 1
authored
30 func ReadTypeMap(r io.Reader) (TypeMap, error) {
0149fb8 Implemented ReadTypeMap
J. Bates authored
31 tm := make(TypeMap)
32 br := bufio.NewReader(r)
e44fdf2 @chsc Gofmt
authored
33
3c41569 @chsc Typemap reader refactored + unit test
authored
34 for {
35 line, err := br.ReadString('\n')
2af3170 @chsc Compatibility fixes for Go 1
authored
36 if err == io.EOF {
3c41569 @chsc Typemap reader refactored + unit test
authored
37 break
0149fb8 Implemented ReadTypeMap
J. Bates authored
38 }
3c41569 @chsc Typemap reader refactored + unit test
authored
39 if err != nil {
40 return nil, err
0149fb8 Implemented ReadTypeMap
J. Bates authored
41 }
3c41569 @chsc Typemap reader refactored + unit test
authored
42 line = strings.TrimRight(line, "\n")
e44fdf2 @chsc Gofmt
authored
43
3c41569 @chsc Typemap reader refactored + unit test
authored
44 if tmEmptyOrCommentRE.MatchString(line) {
74fac7d @chsc Change warning in tmreader.go to error.
authored
45 continue
46 }
47
48 if typePair := tmTypePairRE.FindStringSubmatch(line); typePair != nil {
0149fb8 Implemented ReadTypeMap
J. Bates authored
49 tm[typePair[1]] = typePair[2]
3c41569 @chsc Typemap reader refactored + unit test
authored
50 } else {
74fac7d @chsc Change warning in tmreader.go to error.
authored
51 return tm, fmt.Errorf("Unable to parse line: '%v'", line)
0149fb8 Implemented ReadTypeMap
J. Bates authored
52 }
53 }
e44fdf2 @chsc Gofmt
authored
54
0149fb8 Implemented ReadTypeMap
J. Bates authored
55 return tm, nil
88ece00 @chsc initial commit
authored
56 }
Something went wrong with that request. Please try again.