-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_state.go
237 lines (226 loc) · 6.14 KB
/
parser_state.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package tgff
import (
"errors"
"fmt"
"io"
)
type parState func(p *parser) parState
func parCompleteState(p *parser) parState {
p.success <- p.result
return nil
}
func parErrorState(err error) parState {
return func(p *parser) parState {
p.failure <- err
return nil
}
}
func parEndOrErrorState(err error) parState {
if err == io.EOF {
return parCompleteState
} else {
return parErrorState(err)
}
}
func parControlState(p *parser) parState {
name, err := p.receiveOne(controlToken)
if err != nil {
return parEndOrErrorState(err)
}
number, err := p.receiveOne(numberToken)
if err != nil {
return parErrorState(err)
}
if name.value == "HYPERPERIOD" {
p.result.Period = number.Uint()
return parControlState
}
if _, err := p.receiveOne(blockOpenToken); err != nil {
return parErrorState(err)
}
if token, err := p.peekOneOf(identToken, titleToken); err != nil {
return parErrorState(err)
} else if token.kind == identToken {
return parGraphState(p.result.addGraph(name.value, number.Uint()))
} else {
return parTableState(p.result.addTable(name.value, number.Uint()))
}
}
// parGraphState processes the body of a task graph declaration including
// PERIOD, TASK, ARC, and HARD_DEADLINE declarations.
func parGraphState(graph *Graph) parState {
return func(p *parser) parState {
token, err := p.receiveOneOf(identToken, blockCloseToken)
if err != nil {
return parErrorState(err)
}
if token.kind == blockCloseToken {
return parControlState
}
switch token.value {
case "PERIOD":
if token, err := p.receiveOneOf(numberToken); err != nil {
return parErrorState(err)
} else {
graph.Period = token.Uint()
return parGraphState(graph)
}
case "ARC":
return parArcState(graph)
case "TASK":
return parTaskState(graph)
case "HARD_DEADLINE":
return parDeadlineState(graph)
default:
return parErrorState(errors.New(fmt.Sprintf("unexpected %v", token)))
}
}
}
// parTaskState processes a TASK declaration in the following format:
//
// TASK <name> TYPE <number as unsigned int>
//
// The leading TASK is assumed to be already consumed.
func parTaskState(graph *Graph) parState {
return func(p *parser) parState {
task := graph.addTask()
if token, err := p.receiveOne(nameToken); err != nil {
return parErrorState(err)
} else {
task.Name = token.value
}
if _, err := p.receiveOneWith(identToken, "TYPE"); err != nil {
return parErrorState(err)
}
if token, err := p.receiveOne(numberToken); err != nil {
return parErrorState(err)
} else {
task.Type = token.Uint()
}
return parGraphState(graph)
}
}
// parArcState processes an ARC declaration in the following format:
//
// ARC <name> FROM <task name> TO <task name> TYPE <number as unsigned int>
//
// The leading ARC is assumed to be already consumed.
func parArcState(graph *Graph) parState {
return func(p *parser) parState {
arc := graph.addArc()
if token, err := p.receiveOne(nameToken); err != nil {
return parErrorState(err)
} else {
arc.Name = token.value
}
if _, err := p.receiveOneWith(identToken, "FROM"); err != nil {
return parErrorState(err)
}
if token, err := p.receiveOne(nameToken); err != nil {
return parErrorState(err)
} else {
arc.From = token.value
}
if _, err := p.receiveOneWith(identToken, "TO"); err != nil {
return parErrorState(err)
}
if token, err := p.receiveOne(nameToken); err != nil {
return parErrorState(err)
} else {
arc.To = token.value
}
if _, err := p.receiveOneWith(identToken, "TYPE"); err != nil {
return parErrorState(err)
}
if token, err := p.receiveOne(numberToken); err != nil {
return parErrorState(err)
} else {
arc.Type = token.Uint()
}
return parGraphState(graph)
}
}
// parDeadlineState processes a HARD_DEADLINE declaration in the following
// format:
//
// HARD_DEADLINE <deadline name> ON <task name> AT <time as unsigned int>
//
// The leading HARD_DEADLINE is assumed to be already consumed.
func parDeadlineState(graph *Graph) parState {
return func(p *parser) parState {
deadline := graph.addDeadline()
if token, err := p.receiveOne(nameToken); err != nil {
return parErrorState(err)
} else {
deadline.Name = token.value
}
if _, err := p.receiveOneWith(identToken, "ON"); err != nil {
return parErrorState(err)
}
if token, err := p.receiveOne(nameToken); err != nil {
return parErrorState(err)
} else {
deadline.On = token.value
}
if _, err := p.receiveOneWith(identToken, "AT"); err != nil {
return parErrorState(err)
}
if token, err := p.receiveOne(numberToken); err != nil {
return parErrorState(err)
} else {
deadline.At = token.Uint()
}
return parGraphState(graph)
}
}
// parTableState processes the body of a table declaration including two
// headers, one for the attributes of the table and one for the actual data,
// and the corresponding content.
func parTableState(table *Table) parState {
return func(p *parser) parState {
names, err := p.receiveAny(titleToken)
if err != nil {
return parErrorState(err)
}
values, err := p.receiveAny(numberToken)
if err != nil {
return parErrorState(err)
}
cols := len(names)
if cols != len(values) {
return parErrorState(errors.New(fmt.Sprintf(
"the attribute header of %v %v is invalid", table.Name, table.ID)))
}
table.Attributes = make(map[string]float64, cols)
for i := range names {
table.Attributes[names[i].value] = values[i].Float64()
}
names, err = p.receiveAny(titleToken)
if err != nil {
return parErrorState(err)
}
cols = len(names)
values, err = p.receiveAny(numberToken)
if err != nil {
return parErrorState(err)
}
size := len(values)
if size%cols != 0 {
return parErrorState(errors.New(fmt.Sprintf(
"the data header of %v %v is invalid", table.Name, table.ID)))
}
rows := size / cols
table.Columns = make([]Column, cols)
for i, name := range names {
table.Columns[i].Name = name.value
table.Columns[i].Data = make([]float64, rows)
for j := 0; j < rows; j++ {
table.Columns[i].Data[j] = values[j*cols+i].Float64()
}
}
if _, err := p.receiveOne(blockCloseToken); err != nil {
return parErrorState(err)
}
return parControlState
}
}