all repos

clerk @ fbfbb0b

missing tooling for ledger/hledger

clerk/journal/parser/parser.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add missing transaction status, 1 month ago
1
package parser
2
3
import (
4
	"fmt"
5
	"strconv"
6
	"strings"
7
8
	"olexsmir.xyz/clerk/internal/decimal"
9
	"olexsmir.xyz/clerk/journal/ast"
10
	"olexsmir.xyz/clerk/journal/lexer"
11
	"olexsmir.xyz/clerk/journal/token"
12
)
13
14
type Parser struct {
15
	lexer  *lexer.Lexer
16
	errors []*ast.ParseError
17
	cur    token.Token
18
	peek   token.Token
19
}
20
21
func New(lex *lexer.Lexer) *Parser {
22
	p := &Parser{lexer: lex}
23
	p.advance() // populate .peek
24
	p.advance() // populate .cur
25
	return p
26
}
27
28
func (p *Parser) ParseJournal() *ast.Journal {
29
	f := &ast.Journal{}
30
	for p.cur.Type != token.EOF {
31
		if e := p.parseEntry(); e != nil {
32
			f.Entries = append(f.Entries, e)
33
		}
34
	}
35
	f.Errors = p.errors
36
	return f
37
}
38
39
func isDirectiveKeyword(t token.Type) bool {
40
	switch t {
41
	case token.COMMENTKW, token.ACCOUNT, token.COMMODITY, token.INCLUDE,
42
		token.ALIAS, token.PAYEE, token.TAG, token.APPLY, token.END,
43
		token.YEAR, token.DECIMALMARK, token.D, token.P, token.N, token.C:
44
		return true
45
	}
46
	return false
47
}
48
49
func (p *Parser) parseEntry() ast.Entry {
50
	if p.got(token.BANG) || p.got(token.AT) {
51
		if isDirectiveKeyword(p.peek.Type) {
52
			p.advance() // consume prefix
53
		}
54
	}
55
	switch p.cur.Type {
56
	case token.ILLEGAL:
57
		p.errorf("illegal character %q", p.cur.Literal)
58
		p.advance()
59
		return nil
60
	case token.INDENT:
61
		p.errorf("unexpected indent")
62
		p.syncToNextline()
63
		return nil
64
	case token.DATE:
65
		return p.parseTransaction()
66
	case token.TILDE:
67
		return p.parsePeriodicTransaction()
68
	case token.EQ:
69
		return p.parseAutomatedTransaction()
70
	case token.NEWLINE:
71
		return p.parseBlankLine()
72
	case token.SEMICOLON, token.HASH, token.PERCENT, token.STAR:
73
		return p.parseComment()
74
	case token.ACCOUNT:
75
		return p.parseAccountDirective()
76
	case token.COMMODITY:
77
		return p.parseCommodityDirective()
78
	case token.INCLUDE:
79
		return p.parseIncludeDirective()
80
	case token.ALIAS:
81
		return p.parseAliasDirective()
82
	case token.PAYEE:
83
		return p.parsePayeeDirective()
84
	case token.TAG:
85
		return p.parseTagDirective()
86
	case token.YEAR:
87
		return p.parseYearDirective()
88
	case token.DECIMALMARK:
89
		return p.parseDecimalMarkDirective()
90
	case token.D:
91
		return p.parseDefaultCommodityDirective()
92
	case token.P:
93
		return p.parseMarketPriceDirective()
94
	case token.N:
95
		return p.parseIgnoredDirective()
96
	case token.C:
97
		return p.parseConversionDirective()
98
	case token.APPLY:
99
		return p.parseApplyDirective()
100
	case token.END:
101
		return p.parseEndDirective()
102
	case token.COMMENTKW:
103
		return p.parseCommentBlockDirective()
104
	default:
105
		p.errorf("unexpected token %s", p.cur.Type)
106
		p.sync()
107
		return nil
108
	}
109
}
110
111
func (p *Parser) parseTransaction() *ast.Transaction {
112
	s := p.cur.Span
113
	tx := &ast.Transaction{}
114
115
	tx.Date = p.parseDate()
116
117
	p.skipWhitespace()
118
119
	// optional secondary date
120
	if p.got(token.EQ) {
121
		p.advance()
122
		p.skipWhitespace()
123
		d := p.parseDate()
124
		tx.SecondDate = &d
125
	}
126
127
	p.skipWhitespace()
128
129
	// optional status
130
	tx.Status = p.parseStatus()
131
132
	// optional code
133
	if p.got(token.LPAREN) {
134
		p.advance()
135
		var code strings.Builder
136
		for p.cur.Type != token.RPAREN {
137
			_, _ = code.WriteString(p.cur.Literal)
138
			p.advance()
139
		}
140
		tx.Code = new(code.String())
141
		p.advance()
142
		p.skipWhitespace()
143
	}
144
145
	// optional payee | note
146
	if p.got(token.TEXT) || p.got(token.STRING) {
147
		tx.Payee = p.parsePayee()
148
149
		// check for | separator
150
		if p.got(token.WHITESPACE) {
151
			p.skipWhitespace()
152
		}
153
154
		if p.got(token.PIPE) {
155
			p.advance()
156
			if p.got(token.TEXT) {
157
				n := p.cur.Literal
158
				p.advance()
159
				tx.Note = &n
160
			}
161
		}
162
	}
163
164
	tx.Comment = p.parseOptInlineComment()
165
	p.expectNewline()
166
167
	// header comments — indented ; lines before first posting
168
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
169
		p.advance() // consume indent
170
		c := p.parseComment()
171
		tx.HeaderComments = append(tx.HeaderComments, c)
172
	}
173
174
	// postings
175
	for p.got(token.INDENT) {
176
		if p := p.parsePosting(); p != nil {
177
			tx.Postings = append(tx.Postings, p)
178
		}
179
	}
180
181
	tx.Span = p.span(s)
182
	return tx
183
}
184
185
func unquote(s string) string {
186
	if len(s) >= 2 && ((s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'')) {
187
		return s[1 : len(s)-1]
188
	}
189
	return s
190
}
191
192
func (p *Parser) parsePayee() *ast.Payee {
193
	s := p.cur.Span
194
195
	if p.got(token.STRING) {
196
		name := unquote(p.cur.Literal)
197
		p.advance()
198
		return &ast.Payee{Name: name, Span: p.span(s)}
199
	}
200
201
	// keep spaces/tags between text tokens; stop before trailing whitespace
202
	var name strings.Builder
203
	for p.got(token.TEXT) || p.got(token.INT) || p.got(token.DECIMAL) || (p.got(token.WHITESPACE) && (p.willGet(token.TEXT) || p.willGet(token.INT) || p.willGet(token.DECIMAL))) {
204
		_, _ = name.WriteString(p.cur.Literal)
205
		p.advance()
206
	}
207
	return &ast.Payee{Name: unquote(name.String()), Span: p.span(s)}
208
}
209
210
func (p *Parser) parsePeriodicTransaction() *ast.PeriodicTransaction {
211
	s := p.cur.Span
212
	p.expect(token.TILDE)
213
	p.skipWhitespace()
214
215
	pt := &ast.PeriodicTransaction{}
216
217
	pt.Span = p.span(s)
218
	pt.Period = p.parsePeriod()
219
220
	if desc := p.parseOptPeriodicDescription(); desc != "" {
221
		pt.Description = &desc
222
	}
223
224
	comment := p.parseOptInlineComment()
225
	p.expectNewline()
226
227
	// header comment
228
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
229
		p.advance()
230
		pt.HeaderComments = append(pt.HeaderComments, p.parseComment())
231
	}
232
233
	// postings
234
	for p.got(token.INDENT) {
235
		if posting := p.parsePosting(); posting != nil {
236
			pt.Postings = append(pt.Postings, posting)
237
		}
238
	}
239
240
	pt.Comment = comment
241
	return pt
242
}
243
244
func (p *Parser) parseAutomatedTransaction() *ast.AutomatedTransaction {
245
	s := p.cur.Span
246
	p.expect(token.EQ)
247
	p.skipWhitespace()
248
249
	at := &ast.AutomatedTransaction{}
250
	at.Span = p.span(s)
251
252
	at.Expr = p.parseDirectiveExpr()
253
	at.Comment = p.parseOptInlineComment()
254
	p.expectNewline()
255
256
	// header comments
257
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
258
		p.advance()
259
		at.HeaderComments = append(at.HeaderComments, p.parseComment())
260
	}
261
262
	// postings
263
	for p.got(token.INDENT) {
264
		if p := p.parsePosting(); p != nil {
265
			at.Postings = append(at.Postings, p)
266
		}
267
	}
268
269
	return at
270
}
271
272
func (p *Parser) parsePeriod() ast.Period {
273
	s := p.cur.Span
274
275
	var periodBuf strings.Builder
276
277
	for !p.got(token.NEWLINE) && !p.got(token.EOF) &&
278
		!p.got(token.SEMICOLON) && !p.got(token.HASH) && !p.got(token.PERCENT) && !p.got(token.STAR) {
279
280
		if p.got(token.WHITESPACE) {
281
			if len(p.cur.Literal) >= 2 {
282
				break
283
			}
284
			if p.willGet(token.NEWLINE) || p.willGet(token.EOF) ||
285
				p.willGet(token.SEMICOLON) || p.willGet(token.HASH) ||
286
				p.willGet(token.PERCENT) || p.willGet(token.STAR) {
287
				p.advance()
288
				continue
289
			}
290
		}
291
292
		periodBuf.WriteString(p.cur.Literal)
293
		p.advance()
294
	}
295
296
	str := periodBuf.String()
297
	period := ast.Period{Raw: str, Span: p.span(s)}
298
299
	if _, after, ok := strings.Cut(str, " from "); ok {
300
		end := strings.Index(after, " ")
301
		dateStr := after
302
		if end >= 0 {
303
			dateStr = after[:end]
304
		}
305
		if d := parseSimpleDate(dateStr); d.Year > 0 {
306
			period.From = &d
307
			rest := after
308
			if end >= 0 {
309
				rest = after[end:]
310
			}
311
			if _, toAfter, ok := strings.Cut(rest, " to "); ok {
312
				if toEnd := strings.Index(toAfter, " "); toEnd >= 0 {
313
					toAfter = toAfter[:toEnd]
314
				}
315
				if d := parseSimpleDate(toAfter); d.Year > 0 {
316
					period.To = &d
317
				}
318
			}
319
		}
320
	}
321
	return period
322
}
323
324
func (p *Parser) parseComment() *ast.Comment {
325
	s := p.cur.Span
326
	marker := p.cur.Literal[0]
327
	p.advance()
328
	p.skipWhitespace()
329
330
	var text string
331
	if p.got(token.TEXT) {
332
		text = p.cur.Literal
333
		p.advance()
334
	}
335
336
	p.expectNewline()
337
338
	return &ast.Comment{
339
		Marker: marker,
340
		Text:   text,
341
		Span:   p.span(s),
342
	}
343
}
344
345
func (p *Parser) parseAccountDirective() *ast.AccountDirective {
346
	s := p.cur.Span
347
	p.expect(token.ACCOUNT)
348
	p.skipWhitespace()
349
350
	account := p.parseAccount()
351
	comment := p.parseOptInlineComment()
352
	p.expectNewline()
353
354
	for p.got(token.INDENT) {
355
		p.advance()
356
		for !p.got(token.NEWLINE) && !p.got(token.EOF) {
357
			p.advance()
358
		}
359
		p.expectNewline()
360
	}
361
362
	return &ast.AccountDirective{
363
		Account: account,
364
		Comment: comment,
365
		Span:    p.span(s),
366
	}
367
}
368
369
func (p *Parser) parseCommodityDirective() *ast.CommodityDirective {
370
	s := p.cur.Span
371
	p.expect(token.COMMODITY)
372
	p.skipWhitespace()
373
374
	var commodity string
375
	var format *ast.Amount
376
377
	switch p.cur.Type {
378
	case token.COMMODITYMARK, token.TEXT, token.STRING:
379
		commodity = p.cur.Literal
380
		if p.got(token.STRING) {
381
			commodity = unquote(commodity)
382
		}
383
		p.advance()
384
		hadSpace := p.got(token.WHITESPACE)
385
		p.skipWhitespace()
386
		if p.got(token.INT) || p.got(token.DECIMAL) || p.got(token.TEXT) {
387
			format = p.parseAmount()
388
			format.Commodity = commodity
389
			format.CommodityPos = ast.CommodityBefore
390
			format.HasSpace = hadSpace
391
		}
392
	case token.INT, token.DECIMAL:
393
		format = p.parseAmount()
394
		commodity = format.Commodity
395
	default:
396
		p.errorf("expected commodity name or amount, got %s", p.cur.Type)
397
	}
398
399
	if commodity == "" {
400
		p.errorf("expected commodity name, got %s", p.cur.Type)
401
	}
402
403
	comment := p.parseOptInlineComment()
404
	p.expectNewline()
405
406
	for p.got(token.INDENT) {
407
		p.advance()
408
		p.skipWhitespace()
409
		if p.got(token.COMMODITYMARK) && p.cur.Literal == "format" {
410
			p.advance()
411
			p.skipWhitespace()
412
			format = p.parseAmount()
413
		} else {
414
			for !p.got(token.NEWLINE) && !p.got(token.EOF) {
415
				p.advance()
416
			}
417
		}
418
		p.expectNewline()
419
	}
420
421
	cd := &ast.CommodityDirective{
422
		Commodity: commodity,
423
		Comment:   comment,
424
		Span:      p.span(s),
425
	}
426
	if format != nil {
427
		cd.Format = *format
428
	}
429
	return cd
430
}
431
432
func (p *Parser) parseIncludeDirective() *ast.IncludeDirective {
433
	s := p.cur.Span
434
	p.expect(token.INCLUDE)
435
	p.skipWhitespace()
436
437
	id := &ast.IncludeDirective{}
438
439
	if p.got(token.TEXT) {
440
		id.Path = p.cur.Literal
441
		p.advance()
442
	} else {
443
		p.errorf("expected file path, got %s", p.cur.Type)
444
	}
445
446
	p.skipWhitespace()
447
	id.Comment = p.parseOptInlineComment()
448
	p.expectNewline()
449
	id.Span = p.span(s)
450
	return id
451
}
452
453
func (p *Parser) parseAliasDirective() *ast.AliasDirective {
454
	s := p.cur.Span
455
	alias := &ast.AliasDirective{}
456
	p.expect(token.ALIAS)
457
	p.skipWhitespace()
458
	alias.From = p.parseAccount()
459
	p.skipWhitespace()
460
	p.expect(token.EQ)
461
	p.skipWhitespace()
462
	alias.To = p.parseAccount()
463
	p.skipWhitespace()
464
	alias.Comment = p.parseOptInlineComment()
465
	p.expectNewline()
466
	alias.Span = p.span(s)
467
	return alias
468
}
469
470
func (p *Parser) parsePayeeDirective() *ast.PayeeDirective {
471
	s := p.cur.Span
472
	p.expect(token.PAYEE)
473
	p.skipWhitespace()
474
475
	name := ""
476
	if p.got(token.TEXT) || p.got(token.STRING) {
477
		name = p.parsePayee().Name
478
	}
479
480
	comment := p.parseOptInlineComment()
481
	p.expectNewline()
482
483
	return &ast.PayeeDirective{
484
		Name:    name,
485
		Comment: comment,
486
		Span:    p.span(s),
487
	}
488
}
489
490
func (p *Parser) parseTagDirective() *ast.TagDirective {
491
	s := p.cur.Span
492
	p.expect(token.TAG)
493
	p.skipWhitespace()
494
495
	name := ""
496
	if p.got(token.TEXT) {
497
		name = p.cur.Literal
498
		p.advance()
499
	}
500
501
	comment := p.parseOptInlineComment()
502
	p.expectNewline()
503
504
	return &ast.TagDirective{
505
		Name:    name,
506
		Comment: comment,
507
		Span:    p.span(s),
508
	}
509
}
510
511
func (p *Parser) parseYearDirective() *ast.YearDirective {
512
	s := p.cur.Span
513
	year := &ast.YearDirective{}
514
	p.expect(token.YEAR)
515
	p.skipWhitespace()
516
517
	if p.got(token.INT) {
518
		year.Year, _ = strconv.Atoi(p.cur.Literal)
519
		p.advance()
520
	} else {
521
		p.errorf("expected year, got %s", p.cur.Type)
522
	}
523
524
	p.skipWhitespace()
525
	year.Comment = p.parseOptInlineComment()
526
	p.expectNewline()
527
	year.Span = p.span(s)
528
	return year
529
}
530
531
func (p *Parser) parseDecimalMarkDirective() *ast.DecimalMarkDirective {
532
	s := p.cur.Span
533
	mark := &ast.DecimalMarkDirective{}
534
	p.expect(token.DECIMALMARK)
535
	p.skipWhitespace()
536
537
	mark.Mark = byte('.')
538
	if p.got(token.TEXT) {
539
		if len(p.cur.Literal) > 0 {
540
			mark.Mark = p.cur.Literal[0]
541
		}
542
		p.advance()
543
	}
544
545
	p.skipWhitespace()
546
	mark.Comment = p.parseOptInlineComment()
547
	p.expectNewline()
548
	mark.Span = p.span(s)
549
	return mark
550
}
551
552
func (p *Parser) parseDefaultCommodityDirective() *ast.DefaultCommodityDirective {
553
	s := p.cur.Span
554
	com := &ast.DefaultCommodityDirective{}
555
	p.expect(token.D)
556
	p.skipWhitespace()
557
	com.Amount = *p.parseAmount()
558
	p.skipWhitespace()
559
	com.Comment = p.parseOptInlineComment()
560
	p.expectNewline()
561
	com.Span = p.span(s)
562
	return com
563
}
564
565
func (p *Parser) parseConversionDirective() *ast.ConversionDirective {
566
	s := p.cur.Span
567
	cd := &ast.ConversionDirective{}
568
	p.expect(token.C)
569
	p.skipWhitespace()
570
571
	if p.isAmountStart() {
572
		cd.From = *p.parseAmount()
573
	} else {
574
		p.errorf("expected amount, got %s", p.cur.Type)
575
	}
576
577
	p.skipWhitespace()
578
	if p.got(token.EQ) {
579
		p.advance()
580
		p.skipWhitespace()
581
		if p.isAmountStart() {
582
			cd.To = *p.parseAmount()
583
		} else {
584
			p.errorf("expected amount, got %s", p.cur.Type)
585
		}
586
	}
587
588
	p.skipWhitespace()
589
	cd.Comment = p.parseOptInlineComment()
590
	p.expectNewline()
591
	cd.Span = p.span(s)
592
	return cd
593
}
594
595
func (p *Parser) parseIgnoredDirective() *ast.IgnoredDirective {
596
	s := p.cur.Span
597
	p.expect(token.N)
598
	p.skipWhitespace()
599
600
	id := &ast.IgnoredDirective{}
601
	if p.got(token.TEXT) || p.got(token.COMMODITYMARK) {
602
		id.Text = p.cur.Literal
603
		p.advance()
604
	}
605
	p.skipWhitespace()
606
	id.Comment = p.parseOptInlineComment()
607
608
	p.expectNewline()
609
	id.Span = p.span(s)
610
	return id
611
}
612
613
func (p *Parser) parseMarketPriceDirective() *ast.MarketPriceDirective {
614
	s := p.cur.Span
615
	p.expect(token.P)
616
	p.skipWhitespace()
617
618
	mp := &ast.MarketPriceDirective{}
619
	mp.DateTime.Date = p.parseDate()
620
	p.skipWhitespace()
621
622
	if p.got(token.TIME) {
623
		mp.DateTime.Time = new(p.parseTime())
624
		p.skipWhitespace()
625
	}
626
627
	tok, _ := p.expect(token.COMMODITYMARK)
628
	mp.Commodity = tok.Literal
629
	p.advance()
630
631
	mp.Amount = *p.parseAmount()
632
633
	p.skipWhitespace()
634
	mp.Comment = p.parseOptInlineComment()
635
636
	p.expectNewline()
637
	mp.Span = p.span(s)
638
	return mp
639
}
640
641
func (p *Parser) parseTime() ast.Time {
642
	s := p.cur.Span
643
	tok, _ := p.expect(token.TIME)
644
	lit := tok.Literal
645
646
	parts := strings.Split(lit, ":")
647
	if len(parts) < 2 {
648
		p.errorf("invalid time format: %q", lit)
649
		return ast.Time{Span: p.span(s)}
650
	}
651
652
	hour, _ := strconv.Atoi(parts[0])
653
	minute, _ := strconv.Atoi(parts[1])
654
	second := 0
655
	if len(parts) > 2 {
656
		second, _ = strconv.Atoi(parts[2])
657
	}
658
659
	if hour < 0 || hour > 23 {
660
		p.errorf("invalid hour %d in time %q", hour, lit)
661
	}
662
	if minute < 0 || minute > 59 {
663
		p.errorf("invalid minute %d in time %q", minute, lit)
664
	}
665
	if second < 0 || second > 59 {
666
		p.errorf("invalid second %d in time %q", second, lit)
667
	}
668
669
	return ast.Time{
670
		Hour:   hour,
671
		Minute: minute,
672
		Second: second,
673
		Span:   p.span(s),
674
	}
675
}
676
677
func (p *Parser) parseApplyDirective() *ast.ApplyDirective {
678
	s := p.cur.Span
679
	p.expect(token.APPLY)
680
	p.skipWhitespace()
681
682
	expr := p.parseDirectiveExpr()
683
	comment := p.parseOptInlineComment()
684
	p.expectNewline()
685
686
	return &ast.ApplyDirective{
687
		Expr:    expr,
688
		Comment: comment,
689
		Span:    p.span(s),
690
	}
691
}
692
693
func (p *Parser) parseEndDirective() *ast.EndDirective {
694
	s := p.cur.Span
695
	p.expect(token.END)
696
	p.skipWhitespace()
697
698
	expr := p.parseDirectiveExpr()
699
	comment := p.parseOptInlineComment()
700
	p.expectNewline()
701
702
	return &ast.EndDirective{
703
		Expr:    expr,
704
		Comment: comment,
705
		Span:    p.span(s),
706
	}
707
}
708
709
func (p *Parser) parseCommentBlockDirective() *ast.CommentBlockDirective {
710
	start := p.cur.Span
711
	p.expect(token.COMMENTKW)
712
	p.skipWhitespace()
713
714
	header := p.parseDirectiveExpr()
715
	comment := p.parseOptInlineComment()
716
	p.expectNewline()
717
718
	var content strings.Builder
719
	for p.cur.Type != token.EOF {
720
		if p.got(token.END) {
721
			if p.willGet(token.NEWLINE) || p.willGet(token.EOF) {
722
				p.advance()
723
				p.expectNewline()
724
				break
725
			}
726
			if p.willGet(token.WHITESPACE) {
727
				endTok := p.cur
728
				p.advance()
729
				wsTok := p.cur
730
				p.advance()
731
				if p.got(token.TEXT) && p.cur.Literal == "comment" { // todo: this should check if it's an actual COMMENTKW token
732
					p.advance()
733
					p.parseDirectiveExpr()
734
					p.parseOptInlineComment()
735
					p.expectNewline()
736
					break
737
				}
738
				content.WriteString(endTok.Literal)
739
				content.WriteString(wsTok.Literal)
740
				continue
741
			}
742
		}
743
		content.WriteString(p.cur.Literal)
744
		p.advance()
745
	}
746
747
	return &ast.CommentBlockDirective{
748
		Header:  header,
749
		Content: content.String(),
750
		Comment: comment,
751
		Span:    p.span(start),
752
	}
753
}
754
755
func (p *Parser) parseStatus() ast.Status {
756
	s := p.cur.Span
757
	st := ast.Status{}
758
	switch p.cur.Type {
759
	case token.STAR:
760
		p.advance()
761
		p.skipWhitespace()
762
		st.Value = ast.StatusCleared
763
	case token.BANG:
764
		p.advance()
765
		p.skipWhitespace()
766
		st.Value = ast.StatusPending
767
	default:
768
		st.Value = ast.StatusNone
769
	}
770
	st.Span = p.span(s)
771
	return st
772
}
773
774
func (p *Parser) isAmountStart() bool {
775
	switch p.cur.Type {
776
	default:
777
		return false
778
	case token.COMMODITYMARK, token.STRING, token.INT, token.DECIMAL, token.MINUS, token.PLUS, token.PARENEXPR:
779
		return true
780
	}
781
}
782
783
func (p *Parser) parseAmount() *ast.Amount {
784
	s := p.cur.Span
785
	amt := &ast.Amount{
786
		QuantityFmt: ast.QuantityFormat{Decimal: '.'},
787
		Span:        p.span(s),
788
	}
789
790
	// commodity before quantity: $10.00, eur 10.00
791
	if p.got(token.COMMODITYMARK) || p.got(token.TEXT) || p.got(token.STRING) {
792
		amt.Commodity = unquote(p.cur.Literal)
793
		amt.CommodityPos = ast.CommodityBefore
794
		p.advance()
795
		if p.got(token.WHITESPACE) {
796
			amt.HasSpace = true
797
			p.skipWhitespace()
798
		}
799
		switch p.cur.Type {
800
		case token.MINUS:
801
			amt.IsNegative = true
802
			p.advance()
803
		case token.PLUS:
804
			p.advance()
805
		}
806
		p.parseQuantityInto(amt)
807
	} else {
808
		// optional sign
809
		switch p.cur.Type {
810
		case token.MINUS:
811
			amt.IsNegative = true
812
			p.advance()
813
		case token.PLUS:
814
			p.advance()
815
		}
816
817
		// commodity before quantity: -$120, -eur 120:
818
		if p.got(token.COMMODITYMARK) || p.got(token.TEXT) || p.got(token.STRING) {
819
			amt.Commodity = unquote(p.cur.Literal)
820
			amt.CommodityPos = ast.CommodityBefore
821
			p.advance()
822
			if p.got(token.WHITESPACE) {
823
				amt.HasSpace = true
824
				p.skipWhitespace()
825
			}
826
		}
827
828
		p.parseQuantityInto(amt)
829
830
		// commodity after quantity: 10.00 UAH, 10.00 "EUR" (only if not set)
831
		if amt.Commodity == "" {
832
			switch p.cur.Type {
833
			case token.WHITESPACE:
834
				p.skipWhitespace()
835
				if p.got(token.COMMODITYMARK) || p.got(token.TEXT) || p.got(token.STRING) {
836
					amt.HasSpace = true
837
					amt.Commodity = unquote(p.cur.Literal)
838
					amt.CommodityPos = ast.CommodityAfter
839
					p.advance()
840
				}
841
			case token.COMMODITYMARK, token.TEXT, token.STRING:
842
				amt.Commodity = unquote(p.cur.Literal)
843
				amt.CommodityPos = ast.CommodityAfter
844
				p.advance()
845
			}
846
		}
847
	}
848
849
	return amt
850
}
851
852
func (p *Parser) parseAmountWithOptExpr() *ast.Amount {
853
	if p.got(token.STAR) {
854
		p.advance()
855
		p.skipWhitespace()
856
		amt := p.parseAmount()
857
		if amt != nil {
858
			amt.IsExpr = true
859
		}
860
		return amt
861
	}
862
	if p.got(token.PARENEXPR) {
863
		lit := p.cur.Literal
864
		amt := &ast.Amount{
865
			IsExpr:      true,
866
			QuantityFmt: ast.QuantityFormat{Decimal: '.'},
867
		}
868
		if len(lit) >= 2 && lit[0] == '(' && lit[len(lit)-1] == ')' {
869
			inner := lit[1 : len(lit)-1]
870
			i := 0
871
			for i < len(inner) && (inner[i] == ' ' || inner[i] == '\t') {
872
				i++
873
			}
874
			j := len(inner)
875
			for j > i && (inner[j-1] == ' ' || inner[j-1] == '\t') {
876
				j--
877
			}
878
			amt.Expr = inner[i:j]
879
		}
880
		amt.Span = p.cur.Span
881
		p.advance()
882
		return amt
883
	}
884
	return p.parseAmount()
885
}
886
887
func (p *Parser) parsePosting() *ast.Posting {
888
	s := p.cur.Span
889
	posting := &ast.Posting{}
890
	p.expect(token.INDENT)
891
892
	// exit if it's empty line
893
	if p.got(token.NEWLINE) || p.got(token.EOF) {
894
		p.syncToNextline()
895
		return nil
896
	}
897
898
	// optional status, outside of brackets, '! (account)'
899
	posting.Status = p.parseStatus()
900
901
	// detect virtual posting brackets
902
	switch p.cur.Type {
903
	case token.LPAREN:
904
		posting.Type = ast.PostingVirtualUnbalanced
905
		p.advance()
906
	case token.LBRACKET:
907
		posting.Type = ast.PostingVirtualBalanced
908
		p.advance()
909
	}
910
911
	// optional status, inside of brackets, '(* account)'
912
	if p.got(token.STAR) || p.got(token.BANG) {
913
		posting.Status = p.parseStatus()
914
	}
915
916
	// validate, must be account text
917
	if p.cur.Type != token.TEXT {
918
		p.errorf("expected account name, got %s", p.cur.Type)
919
		p.syncToNextline()
920
		return nil
921
	}
922
923
	posting.Account = p.parseAccount()
924
925
	// consume closing bracket
926
	switch p.cur.Type {
927
	case token.RPAREN:
928
		p.advance()
929
	case token.RBRACKET:
930
		p.advance()
931
	}
932
933
	// optional amount - after two spaces
934
	if p.got(token.WHITESPACE) {
935
		p.skipWhitespace()
936
		if p.isAmountStart() || p.got(token.STAR) {
937
			posting.Amount = p.parseAmountWithOptExpr()
938
		}
939
	}
940
941
	// optional cost '@' or '@@'
942
	if p.got(token.WHITESPACE) {
943
		p.skipWhitespace()
944
	}
945
	if p.got(token.AT) || p.got(token.ATAT) {
946
		posting.Cost = p.parseCost()
947
	}
948
949
	// optional balance assertion
950
	if p.got(token.WHITESPACE) {
951
		p.skipWhitespace()
952
	}
953
	if p.got(token.EQ) || p.got(token.EQEQ) || p.got(token.EQEQEQ) {
954
		posting.Balance = p.parseBalanceAssertion()
955
	}
956
957
	posting.Comment = p.parseOptInlineComment()
958
	p.expectNewline()
959
960
	// continuation comments
961
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
962
		p.advance()
963
		c := p.parseComment()
964
		posting.Comments = append(posting.Comments, *c)
965
	}
966
967
	posting.Span = p.span(s)
968
	return posting
969
}
970
971
func (p *Parser) parseCost() *ast.Cost {
972
	s := p.cur.Span
973
	isTotal := p.got(token.ATAT)
974
	p.advance() // consume '@' '@@'
975
	p.skipWhitespace()
976
	return &ast.Cost{
977
		IsTotal: isTotal,
978
		Amount:  *p.parseAmount(),
979
		Span:    p.span(s),
980
	}
981
}
982
983
func (p *Parser) parseBalanceAssertion() *ast.BalanceAssertion {
984
	s := p.cur.Span
985
986
	ba := &ast.BalanceAssertion{}
987
	switch p.cur.Type {
988
	case token.EQ: // basic assertion
989
	case token.EQEQ:
990
		ba.IsStrict = true
991
	case token.EQEQEQ:
992
		ba.IsStrict = true
993
		ba.IsInclusive = true
994
	}
995
	p.advance()
996
	p.skipWhitespace()
997
998
	ba.Amount = *p.parseAmount()
999
	p.skipWhitespace()
1000
	if p.got(token.AT) || p.got(token.ATAT) {
1001
		c := p.parseCost()
1002
		ba.Cost = c
1003
	}
1004
	ba.Span = p.span(s)
1005
	return ba
1006
}
1007
1008
func (p *Parser) readAccountSegment() (ast.SubAccount, bool) {
1009
	switch p.cur.Type {
1010
	case token.TEXT:
1011
		sub := ast.SubAccount{Name: p.cur.Literal, Span: p.cur.Span}
1012
		p.advance()
1013
1014
		// handle multi work segment, e.g: "credit card"
1015
		if p.got(token.WHITESPACE) && p.willGet(token.TEXT) && len(p.peek.Literal) > 0 && p.peek.Literal[0] != '(' {
1016
			sub.Name += " "
1017
			p.advance()
1018
			sub.Name += p.cur.Literal
1019
			p.advance()
1020
		}
1021
		return sub, true
1022
1023
	case token.COMMODITYMARK:
1024
		sub := ast.SubAccount{Name: p.cur.Literal, Span: p.cur.Span}
1025
		p.advance()
1026
		// merge "EUR" + "-HRK" to "EUR-HRK"
1027
		for p.got(token.TEXT) {
1028
			sub.Name += p.cur.Literal
1029
			p.advance()
1030
		}
1031
		return sub, true
1032
1033
	default:
1034
		return ast.SubAccount{}, false
1035
	}
1036
}
1037
1038
func (p *Parser) parseAccount() ast.Account {
1039
	s := p.cur.Span
1040
	acc := ast.Account{}
1041
1042
	sub, ok := p.readAccountSegment()
1043
	if !ok {
1044
		p.errorf("expected account, got %s", p.cur.Type)
1045
		return ast.Account{}
1046
	}
1047
	acc.Name = append(acc.Name, sub)
1048
1049
	for p.got(token.COLON) {
1050
		p.advance()
1051
		sub, ok := p.readAccountSegment()
1052
		if !ok {
1053
			break
1054
		}
1055
		acc.Name = append(acc.Name, sub)
1056
	}
1057
1058
	acc.Span = p.span(s)
1059
	return acc
1060
}
1061
1062
func (p *Parser) parseDate() ast.Date {
1063
	s := p.cur.Span
1064
	tok, ok := p.expect(token.DATE)
1065
	if !ok {
1066
		return ast.Date{Span: p.span(s)}
1067
	}
1068
1069
	sep := byte(0)
1070
	lit := tok.Literal
1071
	for i := 0; i < len(lit); i++ {
1072
		if lit[i] == '/' || lit[i] == '-' || lit[i] == '.' {
1073
			sep = lit[i]
1074
			break
1075
		}
1076
	}
1077
	if sep == 0 {
1078
		p.errorf("invalid date format: %q", lit)
1079
		return ast.Date{Span: p.span(s)}
1080
	}
1081
1082
	parts := strings.Split(lit, string(sep))
1083
1084
	// M/D or MM/DD (year inferred)
1085
	if len(parts) == 2 {
1086
		month, err := strconv.Atoi(parts[0])
1087
		day, err2 := strconv.Atoi(parts[1])
1088
		if err != nil || err2 != nil {
1089
			p.errorf("invalid date literal: %q", lit)
1090
			return ast.Date{Span: p.span(s)}
1091
		}
1092
		if month < 1 || month > 12 {
1093
			p.errorf("invalid month %d in %q", month, lit)
1094
			return ast.Date{Span: p.span(s)}
1095
		}
1096
		if day < 1 || day > 31 {
1097
			p.errorf("invalid day %d in %q", day, lit)
1098
			return ast.Date{Span: p.span(s)}
1099
		}
1100
		return ast.Date{Month: month, Day: day, Sep: sep, Span: p.span(s)}
1101
	}
1102
1103
	if len(parts) != 3 {
1104
		p.errorf("invalid date format: %q", lit)
1105
		return ast.Date{Span: p.span(s)}
1106
	}
1107
1108
	year, err := strconv.Atoi(parts[0])
1109
	month, err2 := strconv.Atoi(parts[1])
1110
	day, err3 := strconv.Atoi(parts[2])
1111
	if err != nil || err2 != nil || err3 != nil {
1112
		p.errorf("invalid date literal: %q", lit)
1113
		return ast.Date{Span: p.span(s)}
1114
	}
1115
	if month < 1 || month > 12 {
1116
		p.errorf("invalid month %d in %q", month, lit)
1117
		return ast.Date{Span: p.span(s)}
1118
	}
1119
	if day < 1 || day > 31 {
1120
		p.errorf("invalid day %d in %q", day, lit)
1121
		return ast.Date{Span: p.span(s)}
1122
	}
1123
1124
	return ast.Date{
1125
		Year:  year,
1126
		Month: month,
1127
		Day:   day,
1128
		Sep:   sep,
1129
		Span:  p.span(s),
1130
	}
1131
}
1132
1133
func (p *Parser) parseOptInlineComment() *ast.Comment {
1134
	p.skipWhitespace()
1135
	if p.cur.Type != token.SEMICOLON {
1136
		return nil
1137
	}
1138
1139
	s := p.cur.Span
1140
	marker := p.cur.Literal[0]
1141
	p.advance() // consume marker
1142
	p.skipWhitespace()
1143
1144
	text := ""
1145
	if p.got(token.TEXT) {
1146
		text = p.cur.Literal
1147
		p.advance()
1148
	}
1149
1150
	return &ast.Comment{
1151
		Marker: marker,
1152
		Text:   text,
1153
		Span:   p.span(s),
1154
	}
1155
}
1156
1157
func (p *Parser) parseOptPeriodicDescription() string {
1158
	if p.cur.Type != token.WHITESPACE || len(p.cur.Literal) < 2 {
1159
		return ""
1160
	}
1161
1162
	p.skipWhitespace()
1163
1164
	if p.cur.Type != token.TEXT {
1165
		return ""
1166
	}
1167
1168
	return p.parseDescription()
1169
}
1170
1171
func (p *Parser) parseDescription() string {
1172
	var desc strings.Builder
1173
	for p.got(token.TEXT) || (p.got(token.WHITESPACE) && p.willGet(token.TEXT)) {
1174
		_, _ = desc.WriteString(p.cur.Literal)
1175
		p.advance()
1176
	}
1177
	return desc.String()
1178
}
1179
1180
func (p *Parser) parseDirectiveExpr() string {
1181
	var b strings.Builder
1182
	for p.cur.Type != token.NEWLINE && p.cur.Type != token.EOF && p.cur.Type != token.SEMICOLON {
1183
		_, _ = b.WriteString(p.cur.Literal)
1184
		p.advance()
1185
	}
1186
	return b.String()
1187
}
1188
1189
func (p *Parser) parseQuantityInto(amt *ast.Amount) {
1190
	if p.cur.Type != token.INT && p.cur.Type != token.DECIMAL && p.cur.Type != token.TEXT {
1191
		p.errorf("expected quantity, got %s", p.cur.Type)
1192
		return
1193
	}
1194
1195
	lit := p.cur.Literal
1196
	p.advance()
1197
1198
	// detect format metadata before normalizing
1199
	amt.QuantityFmt = detectFormat(lit)
1200
1201
	// normalize for decimal.NewFromString
1202
	// remove thousands separators, replace decimal mark with '.'
1203
	normalized := normalizeLiteral(lit, amt.QuantityFmt.Thousands, amt.QuantityFmt.Decimal)
1204
1205
	q, err := decimal.FromString(normalized)
1206
	if err != nil {
1207
		p.errorf("invalid quantity %q: %v", lit, err)
1208
		return
1209
	}
1210
1211
	if amt.IsNegative {
1212
		q = q.Neg()
1213
	}
1214
	amt.Quantity = q
1215
}
1216
1217
func (p *Parser) parseBlankLine() *ast.BlankLine {
1218
	s := p.cur.Span
1219
	p.expectNewline()
1220
	return &ast.BlankLine{Span: s}
1221
}
1222
1223
func (p *Parser) expectNewline() {
1224
	if p.got(token.NEWLINE) || p.got(token.EOF) {
1225
		if p.got(token.NEWLINE) {
1226
			p.advance()
1227
		}
1228
		return
1229
	}
1230
	p.errorf("expected %s, got %s", token.NEWLINE, p.cur.Type)
1231
}
1232
1233
func (p *Parser) advance() token.Token {
1234
	prev := p.cur
1235
	p.cur = p.peek
1236
	p.peek = p.lexer.Next()
1237
	return prev
1238
}
1239
1240
func (p *Parser) got(kind token.Type) bool     { return p.cur.Type == kind }
1241
func (p *Parser) willGet(kind token.Type) bool { return p.peek.Type == kind }
1242
1243
func (p *Parser) expect(kind token.Type) (token.Token, bool) {
1244
	if p.got(kind) {
1245
		return p.advance(), true
1246
	}
1247
	p.errorf("expected %s, got %s", kind, p.cur.Type)
1248
	return p.cur, false
1249
}
1250
1251
func (p *Parser) errorf(format string, args ...any) {
1252
	p.errors = append(p.errors, &ast.ParseError{
1253
		Span:    p.cur.Span,
1254
		Message: fmt.Sprintf(format, args...),
1255
	})
1256
}
1257
1258
func (p *Parser) sync() {
1259
	for {
1260
		switch p.cur.Type {
1261
		case token.EOF:
1262
			return
1263
		case token.NEWLINE:
1264
			p.advance()
1265
			switch p.cur.Type {
1266
			case token.DATE, token.ACCOUNT, token.COMMODITY,
1267
				token.INCLUDE, token.ALIAS, token.PAYEE,
1268
				token.TAG, token.YEAR, token.D, token.P,
1269
				token.APPLY, token.END, token.COMMENTKW,
1270
				token.DECIMALMARK, token.TILDE, token.N, token.EQ:
1271
				return
1272
			}
1273
		default:
1274
			p.advance()
1275
		}
1276
	}
1277
}
1278
1279
func (p *Parser) syncToNextline() {
1280
	for p.cur.Type != token.NEWLINE && p.cur.Type != token.EOF {
1281
		p.advance()
1282
	}
1283
	if p.got(token.NEWLINE) {
1284
		p.advance()
1285
	}
1286
}
1287
1288
func (p *Parser) skipWhitespace() {
1289
	for p.got(token.WHITESPACE) {
1290
		p.advance()
1291
	}
1292
}
1293
1294
func (p *Parser) span(s token.Span) token.Span {
1295
	return token.Span{Start: s.Start, End: p.cur.Span.Start}
1296
}
1297
1298
func normalizeLiteral(lit string, thousands, decimal byte) string {
1299
	var b strings.Builder
1300
	for _, ch := range []byte(lit) {
1301
		if thousands != 0 && ch == thousands {
1302
			continue // skip thousands separator
1303
		}
1304
		if ch == decimal {
1305
			b.WriteByte('.')
1306
		} else {
1307
			b.WriteByte(ch)
1308
		}
1309
	}
1310
	return b.String()
1311
}
1312
1313
func detectFormat(lit string) ast.QuantityFormat {
1314
	var seps []int
1315
	for i, ch := range []byte(lit) {
1316
		if ch == '.' || ch == ',' || ch == ' ' || ch == '_' || ch == '\'' {
1317
			seps = append(seps, i)
1318
		}
1319
	}
1320
1321
	if len(seps) == 0 {
1322
		return ast.QuantityFormat{Decimal: '.', Thousands: 0, Precision: 0}
1323
	}
1324
1325
	last := seps[len(seps)-1]
1326
	dec := lit[last]
1327
	var thou byte
1328
	if len(seps) > 1 {
1329
		thou = lit[seps[0]]
1330
	} else if dec == ' ' || dec == '_' || dec == '\'' {
1331
		// single space/underscore/apostrophe is always thousands
1332
		thou = dec
1333
		dec = '.'
1334
	}
1335
1336
	// calculate precision when the last separator is a real decimal
1337
	prec := 0
1338
	if thou == 0 || len(seps) > 1 {
1339
		prec = len(lit) - last - 1
1340
	}
1341
1342
	return ast.QuantityFormat{Decimal: dec, Thousands: thou, Precision: prec}
1343
}
1344
1345
func parseSimpleDate(s string) ast.Date {
1346
	if len(s) < 8 {
1347
		return ast.Date{}
1348
	}
1349
	sep := byte('-')
1350
	if strings.Contains(s, "/") {
1351
		sep = byte('/')
1352
	} else if strings.Contains(s, ".") {
1353
		sep = byte('.')
1354
	}
1355
	parts := strings.Split(s, string(sep))
1356
	if len(parts) != 3 {
1357
		return ast.Date{}
1358
	}
1359
	year, _ := strconv.Atoi(parts[0])
1360
	month, _ := strconv.Atoi(parts[1])
1361
	day, _ := strconv.Atoi(parts[2])
1362
	return ast.Date{Year: year, Month: month, Day: day, Sep: sep}
1363
}