-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparser.go
724 lines (660 loc) · 18 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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
// Package cooklang provides a parser for .cook defined recipes as defined in
// https://cooklang.org/docs/spec/
package cooklang
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"slices"
"strconv"
"strings"
"unicode/utf8"
"gopkg.in/yaml.v3"
)
const (
commentsLinePrefix = "--"
metadataLinePrefix = ">>"
metadataValueSeparator = ":"
prefixIngredient = '@'
prefixCookware = '#'
prefixTimer = '~'
prefixBlockComment = '['
prefixInlineComment = '-'
ItemTypeText ItemType = "text"
ItemTypeComment ItemType = "comment"
ItemTypeCookware ItemType = "cookware"
ItemTypeIngredient ItemType = "ingredient"
ItemTypeTimer ItemType = "timer"
CommentTypeLine CommentType = 1
CommentTypeBlock CommentType = 2
CommentTypeEndLine CommentType = 3
)
type ItemType string
// CommentType defines what type is the comment
type CommentType int
// Cookware represents a cookware item
type Cookware struct {
IsNumeric bool // true if the amount is numeric
Name string // cookware name
Quantity float64 // quantity of the cookware
QuantityRaw string // quantity of the cookware as raw text
}
type CookwareV2 struct {
Type ItemType `json:"type"`
Name string `json:"name"`
Quantity float64 `json:"quantity"`
}
func (c Cookware) asCookwareV2() CookwareV2 {
return CookwareV2{
Type: ItemTypeCookware,
Name: c.Name,
Quantity: c.Quantity,
}
}
// IngredientAmount represents the amount required of an ingredient
type IngredientAmount struct {
IsNumeric bool // true if the amount is numeric
Quantity float64 // quantity of the ingredient
QuantityRaw string // quantity of the ingredient as raw text
Unit string // optional ingredient unit
}
// Ingredient represents a recipe ingredient
type Ingredient struct {
Name string // name of the ingredient
Amount IngredientAmount // optional ingredient amount (default: 1)
}
type IngredientV2 struct {
Type ItemType `json:"type"`
Name string `json:"name"`
Quantity float64 `json:"quantity"`
Units string `json:"units,omitempty"`
}
func (i Ingredient) asIngredientV2() IngredientV2 {
return IngredientV2{
Type: ItemTypeIngredient,
Name: i.Name,
Quantity: i.Amount.Quantity,
Units: i.Amount.Unit,
}
}
// Timer represents a time duration
type Timer struct {
Name string // name of the timer
Duration float64 // duration of the timer
Unit string // time unit of the duration
}
type TimerV2 struct {
Type ItemType `json:"type"`
Name string `json:"name,omitempty"`
Quantity float64 `json:"quantity"`
Unit string `json:"units"`
}
func (t Timer) asTimerV2() TimerV2 {
return TimerV2{
Type: ItemTypeTimer,
Name: t.Name,
Quantity: t.Duration,
Unit: t.Unit,
}
}
// Comment represents comment text
type Comment struct {
Type CommentType
Value string
}
type Text struct {
Value string
}
type TextV2 struct {
Type ItemType `json:"type"`
Value string `json:"value"`
}
func (t Text) asTextV2() TextV2 {
return TextV2{ItemTypeText, t.Value}
}
type jsonStep struct {
Type string `json:"type"`
Value string `json:"value,omitempty"`
Name string `json:"name,omitempty"`
Quantity any `json:"quantity,omitempty"`
Units string `json:"units,omitempty"`
}
func (t *Text) MarshalJson() ([]byte, error) {
return json.Marshal(&jsonStep{
Type: "text",
Value: t.Value,
})
}
func newText(v string) Text {
return Text{v}
}
// Step represents a recipe step
type Step struct {
Directions string // step directions as plain text
Timers []Timer // list of timers in the step
Ingredients []Ingredient // list of ingredients used in the step
Cookware []Cookware // list of cookware used in the step
Comments []string // list of comments
}
// Metadata contains key value map of metadata
type Metadata = map[string]any
// Recipe contains a cooklang defined recipe
type Recipe struct {
Steps []Step // list of steps for the recipe
Metadata Metadata // metadata of the recipe
}
type ParseV2Config struct {
IgnoreTypes []ItemType
}
type StepV2 []any
// RecipeV2 contains a cooklang defined recipe
type RecipeV2 struct {
Steps []StepV2 `json:"steps"` // list of steps for the recipe
Metadata Metadata `json:"metadata"` // metadata of the recipe
}
type ParserV2 struct {
config *ParseV2Config
inFrontMatter bool
pastFirstLine bool
frontMatter strings.Builder
}
func (r Recipe) String() string {
var sb strings.Builder
for k, v := range r.Metadata {
sb.WriteString(fmt.Sprintf("%s %s: %s\n", metadataLinePrefix, k, v))
}
if len(r.Metadata) > 0 {
sb.WriteString("\n")
}
steps := len(r.Steps)
for i, s := range r.Steps {
sb.WriteString(fmt.Sprintln(s.Directions))
if i != steps-1 {
sb.WriteString("\n")
}
}
return sb.String()
}
// ParseFile parses a cooklang recipe file and returns the recipe or an error
func ParseFile(fileName string) (*Recipe, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
return ParseStream(bufio.NewReader(f))
}
func (p *ParserV2) ParseFile(fileName string) (*RecipeV2, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
return p.ParseStream(bufio.NewReader(f))
}
// ParseString parses a cooklang recipe string and returns the recipe or an error
func ParseString(s string) (*Recipe, error) {
if s == "" {
return nil, fmt.Errorf("recipe string must not be empty")
}
return ParseStream(strings.NewReader(s))
}
func (p *ParserV2) ParseString(s string) (*RecipeV2, error) {
if s == "" {
return nil, fmt.Errorf("recipe string must not be empty")
}
return p.ParseStream(strings.NewReader(s))
}
func NewParserV2(config *ParseV2Config) *ParserV2 {
return &ParserV2{
config: config,
}
}
// ParseStream parses a cooklang recipe text stream and returns the recipe or an error
func ParseStream(s io.Reader) (*Recipe, error) {
scanner := bufio.NewScanner(s)
recipe := Recipe{
make([]Step, 0),
make(map[string]any),
}
var line string
lineNumber := 0
for scanner.Scan() {
lineNumber++
line = scanner.Text()
if strings.TrimSpace(line) != "" {
err := parseLine(line, &recipe)
if err != nil {
return nil, fmt.Errorf("line %d: %w", lineNumber, err)
}
}
}
return &recipe, nil
}
// ParseStream parses a cooklang recipe text stream and returns the recipe or an error
func (p *ParserV2) ParseStream(s io.Reader) (*RecipeV2, error) {
scanner := bufio.NewScanner(s)
recipe := RecipeV2{
make([]StepV2, 0),
make(map[string]any),
}
var line string
lineNumber := 0
for scanner.Scan() {
lineNumber++
line = scanner.Text()
if strings.TrimSpace(line) != "" {
err := p.parseLine(line, &recipe)
if err != nil {
return nil, fmt.Errorf("line %d: %w", lineNumber, err)
}
}
}
return &recipe, nil
}
func parseLine(line string, recipe *Recipe) error {
if strings.HasPrefix(line, commentsLinePrefix) {
commentLine, err := parseSingleLineComment(line)
if err != nil {
return err
}
recipe.Steps = append(recipe.Steps, Step{
Comments: []string{commentLine},
})
} else if strings.HasPrefix(line, metadataLinePrefix) {
key, value, err := parseMetadata(line)
if err != nil {
return err
}
recipe.Metadata[key] = value
} else {
step, err := parseRecipeLine(line)
if err != nil {
return err
}
recipe.Steps = append(recipe.Steps, *step)
}
return nil
}
func (p *ParserV2) parseLine(line string, recipe *RecipeV2) error {
// be lenient with trailing spaces when detecting the front matter
// header/footer
rightTrimmedLine := strings.TrimRight(line, " ")
if !p.pastFirstLine && rightTrimmedLine == "---" && !p.inFrontMatter {
p.inFrontMatter = true
} else if rightTrimmedLine == "---" && p.inFrontMatter {
p.inFrontMatter = false
y := strings.NewReader(p.frontMatter.String())
err := yaml.NewDecoder(y).Decode(recipe.Metadata)
if err != nil {
return fmt.Errorf("decoding yaml front matter: %w", err)
}
} else if p.inFrontMatter {
p.frontMatter.WriteString(line)
p.frontMatter.WriteString("\n")
} else if strings.HasPrefix(line, commentsLinePrefix) {
commentLine, err := parseSingleLineComment(line)
if err != nil {
return err
}
if !slices.Contains(p.config.IgnoreTypes, ItemTypeComment) {
recipe.Steps = append(recipe.Steps, StepV2{Comment{CommentTypeLine, commentLine}})
}
} else if strings.HasPrefix(line, metadataLinePrefix) {
key, value, err := parseMetadata(line)
if err != nil {
return err
}
recipe.Metadata[key] = value
} else {
step, err := p.parseRecipeLine(line)
if err != nil {
return err
}
recipe.Steps = append(recipe.Steps, *step)
}
p.pastFirstLine = true
return nil
}
func parseSingleLineComment(line string) (string, error) {
return strings.TrimSpace(line[2:]), nil
}
func parseMetadata(line string) (string, string, error) {
metadataLine := strings.TrimSpace(line[2:])
index := strings.Index(metadataLine, metadataValueSeparator)
if index < 1 {
return "", "", fmt.Errorf("invalid metadata: %s", metadataLine)
}
return strings.TrimSpace(metadataLine[:index]), strings.TrimSpace(metadataLine[index+1:]), nil
}
func peek(s string) rune {
r, _ := utf8.DecodeRuneInString(s)
return r
}
func parseStepCB(line string, cb func(item any) (bool, error)) (string, error) {
skipIndex := -1
var directions strings.Builder
var err error
var skipNext int
var ingredient *Ingredient
var cookware *Cookware
var timer *Timer
var comment string
var buffer strings.Builder
for index, ch := range line {
if skipIndex > index {
continue
}
if ch == prefixIngredient {
nextRune := peek(line[index+1:])
if nextRune != ' ' {
if buffer.Len() > 0 {
if stop, err := cb(newText(buffer.String())); err != nil || stop {
return directions.String(), err
}
buffer.Reset()
}
// ingredient ahead
ingredient, skipNext, err = getIngredient(line[index:])
if err != nil {
return directions.String(), err
}
skipIndex = index + skipNext
directions.WriteString((*ingredient).Name)
if stop, err := cb(*ingredient); err != nil || stop {
return directions.String(), err
}
continue
}
}
if ch == prefixCookware {
nextRune := peek(line[index+1:])
if nextRune != ' ' {
if buffer.Len() > 0 {
if stop, err := cb(newText(buffer.String())); err != nil || stop {
return directions.String(), err
}
buffer.Reset()
}
// Cookware ahead
cookware, skipNext, err = getCookware(line[index:])
if err != nil {
return directions.String(), err
}
skipIndex = index + skipNext
directions.WriteString((*cookware).Name)
if stop, err := cb(*cookware); err != nil || stop {
return directions.String(), err
}
continue
}
}
if ch == prefixTimer {
nextRune := peek(line[index+1:])
if nextRune != ' ' {
if buffer.Len() > 0 {
if stop, err := cb(newText(buffer.String())); err != nil || stop {
return directions.String(), err
}
buffer.Reset()
}
//timer ahead
timer, skipNext, err = getTimer(line[index:])
if err != nil {
return directions.String(), err
}
skipIndex = index + skipNext
directions.WriteString(fmt.Sprintf("%v %s", (*timer).Duration, (*timer).Unit))
if stop, err := cb(*timer); err != nil || stop {
return directions.String(), err
}
continue
}
}
if ch == prefixBlockComment {
nextRune := peek(line[index+1:])
if nextRune == '-' {
if buffer.Len() > 0 {
if stop, err := cb(newText(buffer.String())); err != nil || stop {
return directions.String(), err
}
buffer.Reset()
}
// block comment ahead
comment, skipNext, err = getBlockComment(line[index:])
if err != nil {
return directions.String(), err
}
skipIndex = index + skipNext
if stop, err := cb(Comment{CommentTypeBlock, comment}); err != nil || stop {
return directions.String(), err
}
continue
}
}
if ch == prefixInlineComment {
nextRune := peek(line[index+1:])
if nextRune == prefixInlineComment {
if buffer.Len() > 0 {
if stop, err := cb(newText(buffer.String())); err != nil || stop {
return directions.String(), err
}
buffer.Reset()
}
// end-line comment ahead
comment = strings.TrimSpace(line[index+len(commentsLinePrefix):])
if err != nil {
return directions.String(), err
}
if stop, err := cb(Comment{CommentTypeEndLine, comment}); err != nil || stop {
return directions.String(), err
}
break
}
}
// raw string
buffer.WriteRune(ch)
directions.WriteRune(ch)
}
if buffer.Len() > 0 {
if stop, err := cb(newText(buffer.String())); err != nil || stop {
return directions.String(), err
}
buffer.Reset()
}
return strings.TrimSpace(directions.String()), nil
}
func parseRecipeLine(line string) (*Step, error) {
step := Step{
Timers: make([]Timer, 0),
Ingredients: make([]Ingredient, 0),
Cookware: make([]Cookware, 0),
}
var err error
step.Directions, err = parseStepCB(line, func(item any) (bool, error) {
switch v := item.(type) {
case Timer:
step.Timers = append(step.Timers, v)
case Ingredient:
step.Ingredients = append(step.Ingredients, v)
case Cookware:
step.Cookware = append(step.Cookware, v)
case Text:
//
case Comment:
step.Comments = append(step.Comments, v.Value)
default:
return true, fmt.Errorf("unknown type %T", v)
}
return false, nil
})
if err != nil {
return nil, err
}
return &step, nil
}
func (p *ParserV2) parseRecipeLine(line string) (*StepV2, error) {
step := StepV2{}
var err error
_, err = parseStepCB(line, func(item any) (bool, error) {
switch v := item.(type) {
case Timer:
if !slices.Contains(p.config.IgnoreTypes, ItemTypeTimer) {
step = append(step, v.asTimerV2())
}
case Ingredient:
if !slices.Contains(p.config.IgnoreTypes, ItemTypeIngredient) {
step = append(step, v.asIngredientV2())
}
case Cookware:
if !slices.Contains(p.config.IgnoreTypes, ItemTypeCookware) {
step = append(step, v.asCookwareV2())
}
case Text:
if !slices.Contains(p.config.IgnoreTypes, ItemTypeText) {
step = append(step, v.asTextV2())
}
case Comment:
if !slices.Contains(p.config.IgnoreTypes, ItemTypeComment) {
step = append(step, v)
}
default:
return true, fmt.Errorf("unknown type %T", v)
}
return false, nil
})
if err != nil {
return nil, err
}
return &step, nil
}
func getCookware(line string) (*Cookware, int, error) {
endIndex := findNodeEndIndex(line)
Cookware, err := getCookwareFromRawString(line[1:endIndex])
return Cookware, endIndex, err
}
func getIngredient(line string) (*Ingredient, int, error) {
endIndex := findNodeEndIndex(line)
ingredient, err := getIngredientFromRawString(line[1:endIndex])
return ingredient, endIndex, err
}
func getTimer(line string) (*Timer, int, error) {
endIndex := findNodeEndIndex(line)
timer, err := getTimerFromRawString(line[1:endIndex])
return timer, endIndex, err
}
func getBlockComment(s string) (string, int, error) {
index := strings.Index(s, "-]")
if index == -1 {
return "", 0, fmt.Errorf("invalid block comment")
}
return strings.TrimSpace(s[2:index]), index + 2, nil
}
func getFloat(s string) (bool, float64, error) {
var fl float64
var err error
trimmedValue := strings.TrimSpace(s)
if trimmedValue == "" {
return false, 0, nil
}
index := strings.Index(trimmedValue, "/")
if index == -1 {
fl, err = strconv.ParseFloat(trimmedValue, 64)
return err == nil, fl, err
}
var numerator int
var denominator int
numerator, err = strconv.Atoi(strings.TrimSpace(trimmedValue[:index]))
if err != nil {
return false, 0, err
}
denominator, err = strconv.Atoi(strings.TrimSpace(trimmedValue[index+1:]))
if err != nil {
return false, 0, err
}
return true, float64(numerator) / float64(denominator), nil
}
func findNodeEndIndex(line string) int {
endIndex := -1
for index, ch := range line {
if index == 0 {
continue
}
if (ch == prefixCookware || ch == prefixIngredient || ch == prefixTimer || ch == prefixBlockComment) && endIndex == -1 {
break
}
if ch == '}' {
endIndex = index + 1
break
}
}
if endIndex == -1 {
endIndex = strings.Index(line, " ")
if endIndex == -1 {
endIndex = len(line)
}
}
return endIndex
}
func getIngredientFromRawString(s string) (*Ingredient, error) {
index := strings.Index(s, "{")
if index == -1 {
return &Ingredient{Name: s, Amount: IngredientAmount{Quantity: 1}}, nil
}
amount, err := getAmount(s[index+1:len(s)-1], 0)
if err != nil {
return nil, err
}
return &Ingredient{Name: s[:index], Amount: *amount}, nil
}
func getAmount(s string, defaultValue float64) (*IngredientAmount, error) {
if s == "" {
return &IngredientAmount{Quantity: defaultValue, QuantityRaw: "", IsNumeric: false}, nil
}
index := strings.Index(s, "%")
if index == -1 {
isNumeric, f, _ := getFloat(s)
if !isNumeric {
f = defaultValue
}
return &IngredientAmount{Quantity: f, QuantityRaw: strings.TrimSpace(s), IsNumeric: isNumeric}, nil
}
isNumeric, f, _ := getFloat(s[:index])
if !isNumeric {
f = defaultValue
}
return &IngredientAmount{Quantity: f, QuantityRaw: strings.TrimSpace(s[:index]), Unit: strings.TrimSpace(s[index+1:]), IsNumeric: isNumeric}, nil
}
func getCookwareFromRawString(s string) (*Cookware, error) {
index := strings.Index(s, "{")
if index == -1 {
return &Cookware{Name: s, Quantity: 1}, nil
}
amount, err := getAmount(s[index+1:len(s)-1], 1)
if err != nil {
return nil, err
}
return &Cookware{Name: s[:index], Quantity: amount.Quantity, IsNumeric: amount.IsNumeric, QuantityRaw: amount.QuantityRaw}, nil
}
func getTimerFromRawString(s string) (*Timer, error) {
name := ""
index := strings.Index(s, "{")
if index > -1 {
name = strings.TrimSpace(s[:index])
s = s[index+1:]
}
index = strings.Index(s, "%")
if index == -1 {
return &Timer{Name: s, Duration: 0, Unit: ""}, nil
}
isNumeric, f, err := getFloat(s[:index])
if err != nil {
return nil, err
}
if !isNumeric {
return &Timer{Name: name, Duration: 0, Unit: s[index+1 : len(s)-1]}, nil
}
return &Timer{Name: name, Duration: f, Unit: s[index+1 : len(s)-1]}, nil
}