-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
491 lines (427 loc) · 10.9 KB
/
parse.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Package decksh is a little language that generates deck markup
// parsing
package decksh
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"text/scanner"
)
// eval evaluates an id string
func eval(s string) string {
v, ok := emap[s]
if ok {
return v
}
return s
}
// isaop tests for assignment operators
func isaop(s []string) bool {
if len(s) < 4 {
return false
}
op := s[1]
if (op == "+" || op == "-" || op == "*" || op == "/") && s[2] == "=" {
return true
}
return false
}
// parse takes a line of input and returns a string slice containing the parsed tokens
func parse(src string) []string {
var s scanner.Scanner
s.Init(strings.NewReader(src))
tokens := []string{}
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
tokens = append(tokens, s.TokenText())
}
for i := 1; i < len(tokens); i++ {
tokens[i] = eval(tokens[i])
}
return tokens
}
// filequote gets a name from a quoted string
func filequote(s string, linenumber int) (string, error) {
end := len(s) - 1
if len(s) < 3 || s[0] != doublequote && s[end] != doublequote {
return "", fmt.Errorf("line %d: %v is not a valid filename", linenumber, s)
}
return s[1:end], nil
}
// include inserts the contents of a file
// include "file"
func include(w io.Writer, s []string, linenumber int) error {
if len(s) != 2 {
return fmt.Errorf("line %d: include \"file\"", linenumber)
}
filearg, err := filequote(s[1], linenumber)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
r, err := os.Open(filearg)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
defer r.Close()
return Process(w, r)
}
// loadata creates a file using the data keyword
// data "file"...edata
func loadata(s []string, linenumber int, scanner *bufio.Scanner) error {
if len(s) != 2 {
return fmt.Errorf("line %d: data \"file\"...edata", linenumber)
}
filearg, err := filequote(s[1], linenumber)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
dataw, err := os.Create(filearg)
if err != nil {
return fmt.Errorf("line %d: %v (%v)", linenumber, s, err)
}
for scanner.Scan() {
t := scanner.Text()
if strings.TrimSpace(t) == "edata" {
break
}
f := strings.Fields(t)
if len(f) != 2 {
continue
}
fmt.Fprintf(dataw, "%v\t%v\n", f[0], f[1])
}
err = dataw.Close()
return err
}
// grid places objects read from a file in a grid
// grid "file" x y xint yint xlimit
func grid(w io.Writer, s []string, linenumber int) error {
if len(s) < 7 {
return fmt.Errorf("line %d: %s \"file\" x y xint yint xlimit", linenumber, s[0])
}
x, err := strconv.ParseFloat(eval(s[2]), 64)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
y, err := strconv.ParseFloat(eval(s[3]), 64)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
xint, err := strconv.ParseFloat(eval(s[4]), 64)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
yint, err := strconv.ParseFloat(eval(s[5]), 64)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
limit, err := strconv.ParseFloat(eval(s[6]), 64)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
filearg, err := filequote(s[1], linenumber)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
r, err := os.Open(filearg)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
defer r.Close()
scanner := bufio.NewScanner(r)
xp, yp := x, y
for scanner.Scan() {
if xp > limit {
xp = x
yp -= yint
}
t := scanner.Text()
if len(t) == 0 {
continue
}
keyparse(w, subxy(t, xp, yp), t, linenumber)
xp += xint
}
return scanner.Err()
}
// subxy replaces the "x" and "y" arguments with the named values
func subxy(s string, x, y float64) []string {
args := parse(s)
if len(args) < 3 {
return nil
}
for i := 0; i < len(args); i++ {
if args[i] == "x" || args[i] == "X" {
args[i] = ftoa(x)
}
if args[i] == "y" || args[i] == "Y" {
args[i] = ftoa(y)
}
}
return args
}
var funcmap = map[string]string{}
// import loads the definition of a function from a file
// import "file"
func importfunc(s []string, linenumber int) error {
if len(s) < 2 {
return fmt.Errorf("line %d: import \"file\"", linenumber)
}
filearg, err := filequote(s[1], linenumber)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
_, err = funcbody(filearg)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
return nil
}
// funcbody caches the body of function defintions
// avoiding reapeted open/read/close
func funcbody(s string) (string, error) {
_, loaded := funcmap[s]
var name string
// if the name is not loaded, read from the file
if !loaded {
data, err := os.ReadFile(s)
if err != nil {
return "", err
}
fs := string(data)
f := strings.Fields(fs)
if len(f) > 2 && f[0] == "def" {
name = f[1]
funcmap[name] = fs
}
}
return name, nil
}
// def reads and processes the function defintion
// def name arg1 arg2 ... argn
// ...
// edef
func def(scanner *bufio.Scanner, w io.Writer, s []string, filearg string, argoffset int, linenumber int) error {
n := 0
for scanner.Scan() {
t := scanner.Text()
if len(t) == 0 {
continue
}
if t == "edef" {
break
}
// the first line defines the arguments
// def name arg1 arg2 ... argn
if n == 0 {
fargs := strings.Fields(t)
if fargs[0] != "def" || len(fargs) < 3 {
return fmt.Errorf("line %d: %q, begin function definition with 'def name args...'", linenumber, filearg)
}
fargs = fargs[2:]
if len(fargs) != len(s)-argoffset {
return fmt.Errorf("line %d: the number of arguments do not match: (%v=%d and %v=%d)", linenumber, fargs, len(fargs), s[argoffset:], len(s)-argoffset)
}
// copy the callers arguments
for i := 0; i < len(fargs); i++ {
emap[fargs[i]] = eval(s[i+argoffset])
}
keyparse(w, fargs, t, linenumber)
} else {
tokens := parse(t)
if len(tokens) < 1 || t[0] == '#' {
continue
}
if tokens[0] == "for" {
parsefor(w, tokens, n, scanner)
continue
}
if tokens[0] == "if" {
parseif(w, t, n, scanner)
continue
}
keyparse(w, tokens, t, linenumber)
}
n++
}
return scanner.Err()
}
// subfunc handles argument substitution in a function
// func "file" arg1 [arg2] [argn]
func subfunc(w io.Writer, s []string, linenumber int) error {
if len(s) < 3 {
return fmt.Errorf("line %d: %s \"file\" arg1 arg2...argn", linenumber, s[0])
}
filearg, err := filequote(s[1], linenumber)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
name, ferr := funcbody(filearg)
if ferr != nil {
return fmt.Errorf("line %d: %v", linenumber, ferr)
}
scanner := bufio.NewScanner(strings.NewReader(funcmap[name]))
return def(scanner, w, s, filearg, 2, linenumber)
}
// directfunc calls a previously imported function
// function args...
func directfunc(w io.Writer, s []string, linenumber int) error {
//fmt.Fprintf(os.Stderr, "line %d, directfunc=%v\n", linenumber, s)
if len(s) < 2 {
return fmt.Errorf("line %d: %v, need at least one argument for a function", linenumber, s)
}
scanner := bufio.NewScanner(strings.NewReader(funcmap[s[0]]))
return def(scanner, w, s, s[0], 1, linenumber)
}
// evalif evaluates an if statement
func evalif(t string, n int) (bool, error) {
tk := strings.Fields(t)
r, err := condition(tk, n)
return r, err
}
// parseblock parses a block of code
func parseblock(w io.Writer, data []string, linenumber int) error {
for _, t := range data {
err := keyparse(w, parse(t), t, linenumber)
if err != nil {
return fmt.Errorf("line %d: %v", linenumber, err)
}
linenumber++
}
return nil
}
// parseif parses if -- else -- eif
func parseif(w io.Writer, t string, linenumber int, scanner *bufio.Scanner) error {
r, everr := evalif(t, linenumber)
if everr != nil {
return everr
}
ifblock, elseblock, blkerr := ifelse(scanner)
if blkerr != nil {
return blkerr
}
if r {
return parseblock(w, ifblock, linenumber)
} else {
return parseblock(w, elseblock, linenumber)
}
}
// ifelse returns if and (possibly empty) else blocks
func ifelse(scanner *bufio.Scanner) ([]string, []string, error) {
inelse := false
elseblock := []string{}
ifblock := []string{}
for scanner.Scan() {
t := scanner.Text()
s := strings.TrimSpace(t)
if s == "eif" {
break
}
if s == "else" {
inelse = true
continue
}
if inelse {
elseblock = append(elseblock, t)
} else {
ifblock = append(ifblock, t)
}
}
//fmt.Fprintf(os.Stderr, "ifblock=%v\telseblock=%v\n", ifblock, elseblock)
return ifblock, elseblock, scanner.Err()
}
// keyparse parses keywords and executes
func keyparse(w io.Writer, tokens []string, t string, n int) error {
// fmt.Fprintf(os.Stderr, "%v\n", len(tokens))
if len(tokens) < 1 {
return nil
}
if len(tokens) > 1 && tokens[1] == "=" {
return assign(tokens, n)
}
if isaop(tokens) {
return assignop(tokens, n)
}
switch tokens[0] {
case "deck", "doc":
return deck(w, tokens, n)
case "dump":
return Dump(t)
case "canvas":
return canvas(w, tokens, n)
case "include":
return include(w, tokens, n)
case "import":
return importfunc(tokens, n)
case "call", "func", "callfunc":
return subfunc(w, tokens, n)
case "slide", "page":
return slide(w, tokens, n)
case "grid":
return grid(w, tokens, n)
case "content":
return content(w, tokens, n)
case "text", "btext", "ctext", "etext", "textfile":
return text(w, tokens, n)
case "arctext":
return arctext(w, tokens, n)
case "rtext":
return rtext(w, tokens, n)
case "textblock", "textbox":
return textblock(w, tokens, n)
case "textblockfile", "textboxfile":
return textblockfile(w, tokens, n)
case "textcode":
return textcode(w, tokens, n)
case "image":
return image(w, tokens, n)
case "cimage":
return cimage(w, tokens, n)
case "list", "blist", "nlist", "clist":
return list(w, tokens, n)
case "elist", "eslide", "edeck", "edoc", "epage":
return endtag(w, tokens, n)
case "li":
return listitem(w, tokens, n)
case "ellipse", "rect":
return shapes(w, tokens, n)
case "circle", "square", "acircle":
return regshapes(w, tokens, n)
case "rrect", "roundrect":
return roundrect(w, tokens, n)
case "pill":
return pill(w, tokens, n)
case "star":
return star(w, tokens, n)
case "polygon", "poly":
return polygon(w, tokens, n)
case "polyline":
return polyline(w, tokens, n)
case "line":
return line(w, tokens, n)
case "arc":
return arc(w, tokens, n)
case "curve":
return curve(w, tokens, n)
case "legend":
return legend(w, tokens, n)
case "arrow":
return arrow(w, tokens, n)
case "lbrace", "rbrace", "ubrace", "dbrace":
return brace(w, tokens, n)
case "lbracket", "rbracket", "ubracket", "dbracket":
return bracket(w, tokens, n)
case "lcarrow", "rcarrow", "ucarrow", "dcarrow":
return carrow(w, tokens, n)
case "vline":
return vline(w, tokens, n)
case "hline":
return hline(w, tokens, n)
case "dchart":
return chart(w, t, n)
default: // not a keyword, direct function calls
return directfunc(w, tokens, n)
}
}