all repos

clerk @ 45890c9

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add unbalanced-transaction rule, 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.Period = p.parsePeriod()
218
219
	if desc := p.parseOptPeriodicDescription(); desc != "" {
220
		pt.Description = &desc
221
	}
222
223
	comment := p.parseOptInlineComment()
224
	p.expectNewline()
225
226
	// header comment
227
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
228
		p.advance()
229
		pt.HeaderComments = append(pt.HeaderComments, p.parseComment())
230
	}
231
232
	// postings
233
	for p.got(token.INDENT) {
234
		if posting := p.parsePosting(); posting != nil {
235
			pt.Postings = append(pt.Postings, posting)
236
		}
237
	}
238
239
	pt.Span = p.span(s)
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
251
	at.Expr = p.parseDirectiveExpr()
252
	at.Comment = p.parseOptInlineComment()
253
	p.expectNewline()
254
255
	// header comments
256
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
257
		p.advance()
258
		at.HeaderComments = append(at.HeaderComments, p.parseComment())
259
	}
260
261
	// postings
262
	for p.got(token.INDENT) {
263
		if p := p.parsePosting(); p != nil {
264
			at.Postings = append(at.Postings, p)
265
		}
266
	}
267
268
	at.Span = p.span(s)
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
	} else if p.got(token.STRING) {
500
		name = unquote(p.cur.Literal)
501
		p.advance()
502
	}
503
504
	comment := p.parseOptInlineComment()
505
	p.expectNewline()
506
507
	return &ast.TagDirective{
508
		Name:    name,
509
		Comment: comment,
510
		Span:    p.span(s),
511
	}
512
}
513
514
func (p *Parser) parseYearDirective() *ast.YearDirective {
515
	s := p.cur.Span
516
	year := &ast.YearDirective{}
517
	p.expect(token.YEAR)
518
	p.skipWhitespace()
519
520
	if p.got(token.INT) {
521
		year.Year, _ = strconv.Atoi(p.cur.Literal)
522
		p.advance()
523
	} else {
524
		p.errorf("expected year, got %s", p.cur.Type)
525
	}
526
527
	p.skipWhitespace()
528
	year.Comment = p.parseOptInlineComment()
529
	p.expectNewline()
530
	year.Span = p.span(s)
531
	return year
532
}
533
534
func (p *Parser) parseDecimalMarkDirective() *ast.DecimalMarkDirective {
535
	s := p.cur.Span
536
	mark := &ast.DecimalMarkDirective{}
537
	p.expect(token.DECIMALMARK)
538
	p.skipWhitespace()
539
540
	mark.Mark = byte('.')
541
	if p.got(token.TEXT) {
542
		if len(p.cur.Literal) > 0 {
543
			mark.Mark = p.cur.Literal[0]
544
		}
545
		p.advance()
546
	}
547
548
	p.skipWhitespace()
549
	mark.Comment = p.parseOptInlineComment()
550
	p.expectNewline()
551
	mark.Span = p.span(s)
552
	return mark
553
}
554
555
func (p *Parser) parseDefaultCommodityDirective() *ast.DefaultCommodityDirective {
556
	s := p.cur.Span
557
	com := &ast.DefaultCommodityDirective{}
558
	p.expect(token.D)
559
	p.skipWhitespace()
560
	com.Amount = *p.parseAmount()
561
	p.skipWhitespace()
562
	com.Comment = p.parseOptInlineComment()
563
	p.expectNewline()
564
	com.Span = p.span(s)
565
	return com
566
}
567
568
func (p *Parser) parseConversionDirective() *ast.ConversionDirective {
569
	s := p.cur.Span
570
	cd := &ast.ConversionDirective{}
571
	p.expect(token.C)
572
	p.skipWhitespace()
573
574
	if p.isAmountStart() {
575
		cd.From = *p.parseAmount()
576
	} else {
577
		p.errorf("expected amount, got %s", p.cur.Type)
578
	}
579
580
	p.skipWhitespace()
581
	if p.got(token.EQ) {
582
		p.advance()
583
		p.skipWhitespace()
584
		if p.isAmountStart() {
585
			cd.To = *p.parseAmount()
586
		} else {
587
			p.errorf("expected amount, got %s", p.cur.Type)
588
		}
589
	}
590
591
	p.skipWhitespace()
592
	cd.Comment = p.parseOptInlineComment()
593
	p.expectNewline()
594
	cd.Span = p.span(s)
595
	return cd
596
}
597
598
func (p *Parser) parseIgnoredDirective() *ast.IgnoredDirective {
599
	s := p.cur.Span
600
	p.expect(token.N)
601
	p.skipWhitespace()
602
603
	id := &ast.IgnoredDirective{}
604
	if p.got(token.TEXT) || p.got(token.COMMODITYMARK) {
605
		id.Text = p.cur.Literal
606
		p.advance()
607
	}
608
	p.skipWhitespace()
609
	id.Comment = p.parseOptInlineComment()
610
611
	p.expectNewline()
612
	id.Span = p.span(s)
613
	return id
614
}
615
616
func (p *Parser) parseMarketPriceDirective() *ast.MarketPriceDirective {
617
	s := p.cur.Span
618
	p.expect(token.P)
619
	p.skipWhitespace()
620
621
	mp := &ast.MarketPriceDirective{}
622
	mp.DateTime.Date = p.parseDate()
623
	p.skipWhitespace()
624
625
	if p.got(token.TIME) {
626
		mp.DateTime.Time = new(p.parseTime())
627
		p.skipWhitespace()
628
	}
629
630
	tok, _ := p.expect(token.COMMODITYMARK)
631
	mp.Commodity = tok.Literal
632
	p.advance()
633
634
	mp.Amount = *p.parseAmount()
635
636
	p.skipWhitespace()
637
	mp.Comment = p.parseOptInlineComment()
638
639
	p.expectNewline()
640
	mp.Span = p.span(s)
641
	return mp
642
}
643
644
func (p *Parser) parseTime() ast.Time {
645
	s := p.cur.Span
646
	tok, _ := p.expect(token.TIME)
647
	lit := tok.Literal
648
649
	parts := strings.Split(lit, ":")
650
	if len(parts) < 2 {
651
		p.errorf("invalid time format: %q", lit)
652
		return ast.Time{Span: p.span(s)}
653
	}
654
655
	hour, _ := strconv.Atoi(parts[0])
656
	minute, _ := strconv.Atoi(parts[1])
657
	second := 0
658
	if len(parts) > 2 {
659
		second, _ = strconv.Atoi(parts[2])
660
	}
661
662
	if hour < 0 || hour > 23 {
663
		p.errorf("invalid hour %d in time %q", hour, lit)
664
	}
665
	if minute < 0 || minute > 59 {
666
		p.errorf("invalid minute %d in time %q", minute, lit)
667
	}
668
	if second < 0 || second > 59 {
669
		p.errorf("invalid second %d in time %q", second, lit)
670
	}
671
672
	return ast.Time{
673
		Hour:   hour,
674
		Minute: minute,
675
		Second: second,
676
		Span:   p.span(s),
677
	}
678
}
679
680
func (p *Parser) parseApplyDirective() *ast.ApplyDirective {
681
	s := p.cur.Span
682
	p.expect(token.APPLY)
683
	p.skipWhitespace()
684
685
	expr := p.parseDirectiveExpr()
686
	comment := p.parseOptInlineComment()
687
	p.expectNewline()
688
689
	return &ast.ApplyDirective{
690
		Expr:    expr,
691
		Comment: comment,
692
		Span:    p.span(s),
693
	}
694
}
695
696
func (p *Parser) parseEndDirective() *ast.EndDirective {
697
	s := p.cur.Span
698
	p.expect(token.END)
699
	p.skipWhitespace()
700
701
	expr := p.parseDirectiveExpr()
702
	comment := p.parseOptInlineComment()
703
	p.expectNewline()
704
705
	return &ast.EndDirective{
706
		Expr:    expr,
707
		Comment: comment,
708
		Span:    p.span(s),
709
	}
710
}
711
712
func (p *Parser) parseCommentBlockDirective() *ast.CommentBlockDirective {
713
	start := p.cur.Span
714
	p.expect(token.COMMENTKW)
715
	p.skipWhitespace()
716
717
	header := p.parseDirectiveExpr()
718
	comment := p.parseOptInlineComment()
719
	p.expectNewline()
720
721
	var content strings.Builder
722
	for p.cur.Type != token.EOF {
723
		if p.got(token.END) {
724
			if p.willGet(token.NEWLINE) || p.willGet(token.EOF) {
725
				p.advance()
726
				p.expectNewline()
727
				break
728
			}
729
			if p.willGet(token.WHITESPACE) {
730
				endTok := p.cur
731
				p.advance()
732
				wsTok := p.cur
733
				p.advance()
734
				if p.got(token.TEXT) && p.cur.Literal == "comment" { // todo: this should check if it's an actual COMMENTKW token
735
					p.advance()
736
					p.parseDirectiveExpr()
737
					p.parseOptInlineComment()
738
					p.expectNewline()
739
					break
740
				}
741
				content.WriteString(endTok.Literal)
742
				content.WriteString(wsTok.Literal)
743
				continue
744
			}
745
		}
746
		content.WriteString(p.cur.Literal)
747
		p.advance()
748
	}
749
750
	return &ast.CommentBlockDirective{
751
		Header:  header,
752
		Content: content.String(),
753
		Comment: comment,
754
		Span:    p.span(start),
755
	}
756
}
757
758
func (p *Parser) parseStatus() ast.Status {
759
	s := p.cur.Span
760
	st := ast.Status{}
761
	switch p.cur.Type {
762
	case token.STAR:
763
		p.advance()
764
		p.skipWhitespace()
765
		st.Value = ast.StatusCleared
766
	case token.BANG:
767
		p.advance()
768
		p.skipWhitespace()
769
		st.Value = ast.StatusPending
770
	default:
771
		st.Value = ast.StatusNone
772
	}
773
	st.Span = p.span(s)
774
	return st
775
}
776
777
func (p *Parser) isAmountStart() bool {
778
	switch p.cur.Type {
779
	default:
780
		return false
781
	case token.COMMODITYMARK, token.STRING, token.INT, token.DECIMAL, token.MINUS, token.PLUS, token.PARENEXPR:
782
		return true
783
	}
784
}
785
786
func (p *Parser) parseAmount() *ast.Amount {
787
	s := p.cur.Span
788
	amt := &ast.Amount{
789
		QuantityFmt: ast.QuantityFormat{Decimal: '.'},
790
		Span:        p.span(s),
791
	}
792
793
	// commodity before quantity: $10.00, eur 10.00
794
	if p.got(token.COMMODITYMARK) || p.got(token.TEXT) || p.got(token.STRING) {
795
		amt.Commodity = unquote(p.cur.Literal)
796
		amt.CommodityPos = ast.CommodityBefore
797
		p.advance()
798
		if p.got(token.WHITESPACE) {
799
			amt.HasSpace = true
800
			p.skipWhitespace()
801
		}
802
		switch p.cur.Type {
803
		case token.MINUS:
804
			amt.IsNegative = true
805
			p.advance()
806
		case token.PLUS:
807
			p.advance()
808
		}
809
		p.parseQuantityInto(amt)
810
	} else {
811
		// optional sign
812
		switch p.cur.Type {
813
		case token.MINUS:
814
			amt.IsNegative = true
815
			p.advance()
816
		case token.PLUS:
817
			p.advance()
818
		}
819
820
		// commodity before quantity: -$120, -eur 120:
821
		if p.got(token.COMMODITYMARK) || p.got(token.TEXT) || p.got(token.STRING) {
822
			amt.Commodity = unquote(p.cur.Literal)
823
			amt.CommodityPos = ast.CommodityBefore
824
			p.advance()
825
			if p.got(token.WHITESPACE) {
826
				amt.HasSpace = true
827
				p.skipWhitespace()
828
			}
829
		}
830
831
		p.parseQuantityInto(amt)
832
833
		// commodity after quantity: 10.00 UAH, 10.00 "EUR" (only if not set)
834
		if amt.Commodity == "" {
835
			switch p.cur.Type {
836
			case token.WHITESPACE:
837
				p.skipWhitespace()
838
				if p.got(token.COMMODITYMARK) || p.got(token.TEXT) || p.got(token.STRING) {
839
					amt.HasSpace = true
840
					amt.Commodity = unquote(p.cur.Literal)
841
					amt.CommodityPos = ast.CommodityAfter
842
					p.advance()
843
				}
844
			case token.COMMODITYMARK, token.TEXT, token.STRING:
845
				amt.Commodity = unquote(p.cur.Literal)
846
				amt.CommodityPos = ast.CommodityAfter
847
				p.advance()
848
			}
849
		}
850
	}
851
852
	return amt
853
}
854
855
func (p *Parser) parseAmountWithOptExpr() *ast.Amount {
856
	if p.got(token.STAR) {
857
		p.advance()
858
		p.skipWhitespace()
859
		amt := p.parseAmount()
860
		if amt != nil {
861
			amt.IsExpr = true
862
		}
863
		return amt
864
	}
865
	if p.got(token.PARENEXPR) {
866
		lit := p.cur.Literal
867
		amt := &ast.Amount{
868
			IsExpr:      true,
869
			QuantityFmt: ast.QuantityFormat{Decimal: '.'},
870
		}
871
		if len(lit) >= 2 && lit[0] == '(' && lit[len(lit)-1] == ')' {
872
			inner := lit[1 : len(lit)-1]
873
			i := 0
874
			for i < len(inner) && (inner[i] == ' ' || inner[i] == '\t') {
875
				i++
876
			}
877
			j := len(inner)
878
			for j > i && (inner[j-1] == ' ' || inner[j-1] == '\t') {
879
				j--
880
			}
881
			amt.Expr = inner[i:j]
882
		}
883
		amt.Span = p.cur.Span
884
		p.advance()
885
		return amt
886
	}
887
	return p.parseAmount()
888
}
889
890
func (p *Parser) parsePosting() *ast.Posting {
891
	s := p.cur.Span
892
	posting := &ast.Posting{}
893
	p.expect(token.INDENT)
894
895
	// exit if it's empty line
896
	if p.got(token.NEWLINE) || p.got(token.EOF) {
897
		p.syncToNextline()
898
		return nil
899
	}
900
901
	// optional status, outside of brackets, '! (account)'
902
	posting.Status = p.parseStatus()
903
904
	// detect virtual posting brackets
905
	switch p.cur.Type {
906
	case token.LPAREN:
907
		posting.Type = ast.PostingVirtualUnbalanced
908
		p.advance()
909
	case token.LBRACKET:
910
		posting.Type = ast.PostingVirtualBalanced
911
		p.advance()
912
	}
913
914
	// optional status, inside of brackets, '(* account)'
915
	if p.got(token.STAR) || p.got(token.BANG) {
916
		posting.Status = p.parseStatus()
917
	}
918
919
	// validate, must be account text
920
	if p.cur.Type != token.TEXT {
921
		p.errorf("expected account name, got %s", p.cur.Type)
922
		p.syncToNextline()
923
		return nil
924
	}
925
926
	posting.Account = p.parseAccount()
927
928
	// consume closing bracket
929
	switch p.cur.Type {
930
	case token.RPAREN:
931
		p.advance()
932
	case token.RBRACKET:
933
		p.advance()
934
	}
935
936
	// optional amount - after two spaces
937
	if p.got(token.WHITESPACE) {
938
		p.skipWhitespace()
939
		if p.isAmountStart() || p.got(token.STAR) {
940
			posting.Amount = p.parseAmountWithOptExpr()
941
		}
942
	}
943
944
	// optional cost '@' or '@@'
945
	if p.got(token.WHITESPACE) {
946
		p.skipWhitespace()
947
	}
948
	if p.got(token.AT) || p.got(token.ATAT) {
949
		posting.Cost = p.parseCost()
950
	}
951
952
	// optional balance assertion
953
	if p.got(token.WHITESPACE) {
954
		p.skipWhitespace()
955
	}
956
	if p.got(token.EQ) || p.got(token.EQEQ) || p.got(token.EQEQEQ) {
957
		posting.Balance = p.parseBalanceAssertion()
958
	}
959
960
	posting.Comment = p.parseOptInlineComment()
961
	p.expectNewline()
962
963
	// continuation comments
964
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
965
		p.advance()
966
		c := p.parseComment()
967
		posting.Comments = append(posting.Comments, *c)
968
	}
969
970
	posting.Span = p.span(s)
971
	return posting
972
}
973
974
func (p *Parser) parseCost() *ast.Cost {
975
	s := p.cur.Span
976
	isTotal := p.got(token.ATAT)
977
	p.advance() // consume '@' '@@'
978
	p.skipWhitespace()
979
	return &ast.Cost{
980
		IsTotal: isTotal,
981
		Amount:  *p.parseAmount(),
982
		Span:    p.span(s),
983
	}
984
}
985
986
func (p *Parser) parseBalanceAssertion() *ast.BalanceAssertion {
987
	s := p.cur.Span
988
989
	ba := &ast.BalanceAssertion{}
990
	switch p.cur.Type {
991
	case token.EQ: // basic assertion
992
	case token.EQEQ:
993
		ba.IsStrict = true
994
	case token.EQEQEQ:
995
		ba.IsStrict = true
996
		ba.IsInclusive = true
997
	}
998
	p.advance()
999
	p.skipWhitespace()
1000
1001
	ba.Amount = *p.parseAmount()
1002
	p.skipWhitespace()
1003
	if p.got(token.AT) || p.got(token.ATAT) {
1004
		c := p.parseCost()
1005
		ba.Cost = c
1006
	}
1007
	ba.Span = p.span(s)
1008
	return ba
1009
}
1010
1011
func (p *Parser) readAccountSegment() (ast.SubAccount, bool) {
1012
	switch p.cur.Type {
1013
	case token.TEXT:
1014
		sub := ast.SubAccount{Name: p.cur.Literal, Span: p.cur.Span}
1015
		p.advance()
1016
1017
		// handle multi work segment, e.g: "credit card"
1018
		if p.got(token.WHITESPACE) && p.willGet(token.TEXT) && len(p.peek.Literal) > 0 && p.peek.Literal[0] != '(' {
1019
			sub.Name += " "
1020
			p.advance()
1021
			sub.Name += p.cur.Literal
1022
			p.advance()
1023
		}
1024
		return sub, true
1025
1026
	case token.COMMODITYMARK:
1027
		sub := ast.SubAccount{Name: p.cur.Literal, Span: p.cur.Span}
1028
		p.advance()
1029
		// merge "EUR" + "-HRK" to "EUR-HRK"
1030
		for p.got(token.TEXT) {
1031
			sub.Name += p.cur.Literal
1032
			p.advance()
1033
		}
1034
		return sub, true
1035
1036
	default:
1037
		return ast.SubAccount{}, false
1038
	}
1039
}
1040
1041
func (p *Parser) parseAccount() ast.Account {
1042
	s := p.cur.Span
1043
	acc := ast.Account{}
1044
1045
	sub, ok := p.readAccountSegment()
1046
	if !ok {
1047
		p.errorf("expected account, got %s", p.cur.Type)
1048
		return ast.Account{}
1049
	}
1050
	acc.Name = append(acc.Name, sub)
1051
1052
	for p.got(token.COLON) {
1053
		p.advance()
1054
		sub, ok := p.readAccountSegment()
1055
		if !ok {
1056
			break
1057
		}
1058
		acc.Name = append(acc.Name, sub)
1059
	}
1060
1061
	acc.Span = p.span(s)
1062
	return acc
1063
}
1064
1065
func (p *Parser) parseDate() ast.Date {
1066
	s := p.cur.Span
1067
	tok, ok := p.expect(token.DATE)
1068
	if !ok {
1069
		return ast.Date{Span: p.span(s)}
1070
	}
1071
1072
	sep := byte(0)
1073
	lit := tok.Literal
1074
	for i := 0; i < len(lit); i++ {
1075
		if lit[i] == '/' || lit[i] == '-' || lit[i] == '.' {
1076
			sep = lit[i]
1077
			break
1078
		}
1079
	}
1080
	if sep == 0 {
1081
		p.errorf("invalid date format: %q", lit)
1082
		return ast.Date{Span: p.span(s)}
1083
	}
1084
1085
	parts := strings.Split(lit, string(sep))
1086
1087
	// M/D or MM/DD (year inferred)
1088
	if len(parts) == 2 {
1089
		month, err := strconv.Atoi(parts[0])
1090
		day, err2 := strconv.Atoi(parts[1])
1091
		if err != nil || err2 != nil {
1092
			p.errorf("invalid date literal: %q", lit)
1093
			return ast.Date{Span: p.span(s)}
1094
		}
1095
		if month < 1 || month > 12 {
1096
			p.errorf("invalid month %d in %q", month, lit)
1097
			return ast.Date{Span: p.span(s)}
1098
		}
1099
		if day < 1 || day > 31 {
1100
			p.errorf("invalid day %d in %q", day, lit)
1101
			return ast.Date{Span: p.span(s)}
1102
		}
1103
		return ast.Date{Month: month, Day: day, Sep: sep, Span: p.span(s)}
1104
	}
1105
1106
	if len(parts) != 3 {
1107
		p.errorf("invalid date format: %q", lit)
1108
		return ast.Date{Span: p.span(s)}
1109
	}
1110
1111
	year, err := strconv.Atoi(parts[0])
1112
	month, err2 := strconv.Atoi(parts[1])
1113
	day, err3 := strconv.Atoi(parts[2])
1114
	if err != nil || err2 != nil || err3 != nil {
1115
		p.errorf("invalid date literal: %q", lit)
1116
		return ast.Date{Span: p.span(s)}
1117
	}
1118
	if month < 1 || month > 12 {
1119
		p.errorf("invalid month %d in %q", month, lit)
1120
		return ast.Date{Span: p.span(s)}
1121
	}
1122
	if day < 1 || day > 31 {
1123
		p.errorf("invalid day %d in %q", day, lit)
1124
		return ast.Date{Span: p.span(s)}
1125
	}
1126
1127
	return ast.Date{
1128
		Year:  year,
1129
		Month: month,
1130
		Day:   day,
1131
		Sep:   sep,
1132
		Span:  p.span(s),
1133
	}
1134
}
1135
1136
func (p *Parser) parseOptInlineComment() *ast.Comment {
1137
	p.skipWhitespace()
1138
	if p.cur.Type != token.SEMICOLON {
1139
		return nil
1140
	}
1141
1142
	s := p.cur.Span
1143
	marker := p.cur.Literal[0]
1144
	p.advance() // consume marker
1145
	p.skipWhitespace()
1146
1147
	text := ""
1148
	if p.got(token.TEXT) {
1149
		text = p.cur.Literal
1150
		p.advance()
1151
	}
1152
1153
	return &ast.Comment{
1154
		Marker: marker,
1155
		Text:   text,
1156
		Span:   p.span(s),
1157
	}
1158
}
1159
1160
func (p *Parser) parseOptPeriodicDescription() string {
1161
	if p.cur.Type != token.WHITESPACE || len(p.cur.Literal) < 2 {
1162
		return ""
1163
	}
1164
1165
	p.skipWhitespace()
1166
1167
	if p.cur.Type != token.TEXT {
1168
		return ""
1169
	}
1170
1171
	return p.parseDescription()
1172
}
1173
1174
func (p *Parser) parseDescription() string {
1175
	var desc strings.Builder
1176
	for p.got(token.TEXT) || (p.got(token.WHITESPACE) && p.willGet(token.TEXT)) {
1177
		_, _ = desc.WriteString(p.cur.Literal)
1178
		p.advance()
1179
	}
1180
	return desc.String()
1181
}
1182
1183
func (p *Parser) parseDirectiveExpr() string {
1184
	var b strings.Builder
1185
	for p.cur.Type != token.NEWLINE && p.cur.Type != token.EOF && p.cur.Type != token.SEMICOLON {
1186
		_, _ = b.WriteString(p.cur.Literal)
1187
		p.advance()
1188
	}
1189
	return b.String()
1190
}
1191
1192
func (p *Parser) parseQuantityInto(amt *ast.Amount) {
1193
	if p.cur.Type != token.INT && p.cur.Type != token.DECIMAL && p.cur.Type != token.TEXT {
1194
		p.errorf("expected quantity, got %s", p.cur.Type)
1195
		return
1196
	}
1197
1198
	lit := p.cur.Literal
1199
	p.advance()
1200
1201
	// detect format metadata before normalizing
1202
	amt.QuantityFmt = detectFormat(lit)
1203
1204
	// normalize for decimal.NewFromString
1205
	// remove thousands separators, replace decimal mark with '.'
1206
	normalized := normalizeLiteral(lit, amt.QuantityFmt.Thousands, amt.QuantityFmt.Decimal)
1207
1208
	q, err := decimal.FromString(normalized)
1209
	if err != nil {
1210
		p.errorf("invalid quantity %q: %v", lit, err)
1211
		return
1212
	}
1213
1214
	if amt.IsNegative {
1215
		q = q.Neg()
1216
	}
1217
	amt.Quantity = q
1218
}
1219
1220
func (p *Parser) parseBlankLine() *ast.BlankLine {
1221
	s := p.cur.Span
1222
	p.expectNewline()
1223
	return &ast.BlankLine{Span: s}
1224
}
1225
1226
func (p *Parser) expectNewline() {
1227
	if p.got(token.NEWLINE) || p.got(token.EOF) {
1228
		if p.got(token.NEWLINE) {
1229
			p.advance()
1230
		}
1231
		return
1232
	}
1233
	p.errorf("expected %s, got %s", token.NEWLINE, p.cur.Type)
1234
}
1235
1236
func (p *Parser) advance() token.Token {
1237
	prev := p.cur
1238
	p.cur = p.peek
1239
	p.peek = p.lexer.Next()
1240
	return prev
1241
}
1242
1243
func (p *Parser) got(kind token.Type) bool     { return p.cur.Type == kind }
1244
func (p *Parser) willGet(kind token.Type) bool { return p.peek.Type == kind }
1245
1246
func (p *Parser) expect(kind token.Type) (token.Token, bool) {
1247
	if p.got(kind) {
1248
		return p.advance(), true
1249
	}
1250
	p.errorf("expected %s, got %s", kind, p.cur.Type)
1251
	return p.cur, false
1252
}
1253
1254
func (p *Parser) errorf(format string, args ...any) {
1255
	p.errors = append(p.errors, &ast.ParseError{
1256
		Span:    p.cur.Span,
1257
		Message: fmt.Sprintf(format, args...),
1258
	})
1259
}
1260
1261
func (p *Parser) sync() {
1262
	for {
1263
		switch p.cur.Type {
1264
		case token.EOF:
1265
			return
1266
		case token.NEWLINE:
1267
			p.advance()
1268
			switch p.cur.Type {
1269
			case token.DATE, token.ACCOUNT, token.COMMODITY,
1270
				token.INCLUDE, token.ALIAS, token.PAYEE,
1271
				token.TAG, token.YEAR, token.D, token.P,
1272
				token.APPLY, token.END, token.COMMENTKW,
1273
				token.DECIMALMARK, token.TILDE, token.N, token.EQ:
1274
				return
1275
			}
1276
		default:
1277
			p.advance()
1278
		}
1279
	}
1280
}
1281
1282
func (p *Parser) syncToNextline() {
1283
	for p.cur.Type != token.NEWLINE && p.cur.Type != token.EOF {
1284
		p.advance()
1285
	}
1286
	if p.got(token.NEWLINE) {
1287
		p.advance()
1288
	}
1289
}
1290
1291
func (p *Parser) skipWhitespace() {
1292
	for p.got(token.WHITESPACE) {
1293
		p.advance()
1294
	}
1295
}
1296
1297
func (p *Parser) span(s token.Span) token.Span {
1298
	return token.Span{Start: s.Start, End: p.cur.Span.Start}
1299
}
1300
1301
func normalizeLiteral(lit string, thousands, decimal byte) string {
1302
	var b strings.Builder
1303
	for _, ch := range []byte(lit) {
1304
		if thousands != 0 && ch == thousands {
1305
			continue // skip thousands separator
1306
		}
1307
		if ch == decimal {
1308
			b.WriteByte('.')
1309
		} else {
1310
			b.WriteByte(ch)
1311
		}
1312
	}
1313
	return b.String()
1314
}
1315
1316
func detectFormat(lit string) ast.QuantityFormat {
1317
	var seps []int
1318
	for i, ch := range []byte(lit) {
1319
		if ch == '.' || ch == ',' || ch == ' ' || ch == '_' || ch == '\'' {
1320
			seps = append(seps, i)
1321
		}
1322
	}
1323
1324
	if len(seps) == 0 {
1325
		return ast.QuantityFormat{Decimal: '.', Thousands: 0, Precision: 0}
1326
	}
1327
1328
	last := seps[len(seps)-1]
1329
	dec := lit[last]
1330
	var thou byte
1331
	if len(seps) > 1 {
1332
		thou = lit[seps[0]]
1333
	} else if dec == ' ' || dec == '_' || dec == '\'' {
1334
		// single space/underscore/apostrophe is always thousands
1335
		thou = dec
1336
		dec = '.'
1337
	}
1338
1339
	// calculate precision when the last separator is a real decimal
1340
	prec := 0
1341
	if thou == 0 || len(seps) > 1 {
1342
		prec = len(lit) - last - 1
1343
	}
1344
1345
	return ast.QuantityFormat{Decimal: dec, Thousands: thou, Precision: prec}
1346
}
1347
1348
func parseSimpleDate(s string) ast.Date {
1349
	if len(s) < 8 {
1350
		return ast.Date{}
1351
	}
1352
	sep := byte('-')
1353
	if strings.Contains(s, "/") {
1354
		sep = byte('/')
1355
	} else if strings.Contains(s, ".") {
1356
		sep = byte('.')
1357
	}
1358
	parts := strings.Split(s, string(sep))
1359
	if len(parts) != 3 {
1360
		return ast.Date{}
1361
	}
1362
	year, _ := strconv.Atoi(parts[0])
1363
	month, _ := strconv.Atoi(parts[1])
1364
	day, _ := strconv.Atoi(parts[2])
1365
	return ast.Date{Year: year, Month: month, Day: day, Sep: sep}
1366
}