This repository was archived by the owner on Nov 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
113 lines (99 loc) · 2.3 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
const (
runeTab = '\t'
runeSpace = ' '
)
// ParserOptions contains the parser related options
type ParserOptions struct {
commentChar uint8
}
// NewDefaultParserOptions returns the default set of parser options
func NewDefaultParserOptions() *ParserOptions {
return &ParserOptions{'#'}
}
// Parser is the parser data structure
type Parser struct {
parserOptions *ParserOptions
nodes chan *Node
errors chan *BreakingError
done chan bool
}
// NewParser returns new parser
func NewParser(parserOptions *ParserOptions) *Parser {
return &Parser{
parserOptions,
make(chan *Node),
make(chan *BreakingError),
make(chan bool),
}
}
func (p *Parser) parseFile(fileName string) {
f, err := os.Open(fileName)
if err != nil {
p.errors <- NewBreakingError(err.Error(), exitErrorOpeningFile)
return
}
defer f.Close()
p.parseStream(f)
}
func (p *Parser) parseStream(reader io.Reader) {
var node *Node
lineNumber := 0
lineScanner := bufio.NewScanner(reader)
for lineScanner.Scan() {
lineNumber++
line := lineScanner.Text()
trimmedLine := mytrim(line)
//skip empty lines and lines starting with #
if trimmedLine == "" || line[0] == p.parserOptions.commentChar {
continue
}
//new nodes start at the beginning of the line
if line[0] != runeSpace && line[0] != runeTab {
if node != nil {
p.nodes <- node
}
node = NewNode(trimmedLine)
continue
}
if node != nil {
separator := strings.LastIndexAny(trimmedLine, "\t ")
if separator == -1 {
p.errors <- NewBreakingError(
fmt.Sprintf("Bad syntax on line %d, \"%s\".", lineNumber, line),
exitErrorBadSyntax,
)
return
}
ename := mytrim(trimmedLine[0:separator])
//get element value
snum := mytrim(trimmedLine[separator:])
enum, err := strconv.ParseFloat(snum, 32)
if err != nil {
p.errors <- NewBreakingError(
fmt.Sprintf("Error converting \"%s\" to float on line %d \"%s\".", snum, lineNumber, line),
exitErrorConversion,
)
return
}
if ndx, exists := node.elements.index(ename); exists {
(*node.elements)[ndx].val += float32(enum)
} else {
node.elements.add(ename, float32(enum))
}
}
}
// push last node
if node != nil {
p.nodes <- node
}
p.done <- true
}