all repos

clerk @ dc54e906021a5d4e2caeca03c52769c41bd80b28

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
journal: dont lex */+ etc in transaction notes, 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() // TODO: why?
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
	var headerComments []*ast.Comment
228
	var postings []*ast.Posting
229
	for p.got(token.INDENT) || p.got(token.SEMICOLON) {
230
		if p.got(token.SEMICOLON) {
231
			c := p.parseComment()
232
			headerComments = append(headerComments, c)
233
			continue
234
		}
235
		posting := p.parsePosting()
236
		if posting != nil {
237
			postings = append(postings, posting)
238
		}
239
	}
240
241
	pt.HeaderComments = headerComments
242
	pt.Postings = postings
243
	pt.Comment = comment
244
	return pt
245
}
246
247
func (p *Parser) parseAutomatedTransaction() *ast.AutomatedTransaction {
248
	s := p.cur.Span
249
	p.expect(token.EQ)
250
	p.skipWhitespace()
251
252
	at := &ast.AutomatedTransaction{}
253
	at.Span = p.span(s)
254
255
	at.Expr = p.parseDirectiveExpr()
256
	at.Comment = p.parseOptInlineComment()
257
	p.expectNewline()
258
259
	// header comments
260
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
261
		p.advance()
262
		at.HeaderComments = append(at.HeaderComments, p.parseComment())
263
	}
264
265
	// postings
266
	for p.got(token.INDENT) {
267
		if p := p.parsePosting(); p != nil {
268
			at.Postings = append(at.Postings, p)
269
		}
270
	}
271
272
	return at
273
}
274
275
func (p *Parser) parsePeriod() ast.Period {
276
	s := p.cur.Span
277
278
	var periodBuf strings.Builder
279
280
	for !p.got(token.NEWLINE) && !p.got(token.EOF) &&
281
		!p.got(token.SEMICOLON) && !p.got(token.HASH) && !p.got(token.PERCENT) && !p.got(token.STAR) {
282
283
		if p.got(token.WHITESPACE) {
284
			if len(p.cur.Literal) >= 2 {
285
				break
286
			}
287
			if p.willGet(token.NEWLINE) || p.willGet(token.EOF) ||
288
				p.willGet(token.SEMICOLON) || p.willGet(token.HASH) ||
289
				p.willGet(token.PERCENT) || p.willGet(token.STAR) {
290
				p.advance()
291
				continue
292
			}
293
		}
294
295
		periodBuf.WriteString(p.cur.Literal)
296
		p.advance()
297
	}
298
299
	str := periodBuf.String()
300
	period := ast.Period{Raw: str, Span: p.span(s)}
301
302
	if _, after, ok := strings.Cut(str, " from "); ok {
303
		end := strings.Index(after, " ")
304
		dateStr := after
305
		if end >= 0 {
306
			dateStr = after[:end]
307
		}
308
		if d := parseSimpleDate(dateStr); d.Year > 0 {
309
			period.From = &d
310
			rest := after
311
			if end >= 0 {
312
				rest = after[end:]
313
			}
314
			if _, toAfter, ok := strings.Cut(rest, " to "); ok {
315
				if toEnd := strings.Index(toAfter, " "); toEnd >= 0 {
316
					toAfter = toAfter[:toEnd]
317
				}
318
				if d := parseSimpleDate(toAfter); d.Year > 0 {
319
					period.To = &d
320
				}
321
			}
322
		}
323
	}
324
	return period
325
}
326
327
func (p *Parser) parseComment() *ast.Comment {
328
	s := p.cur.Span
329
	marker := p.cur.Literal[0]
330
	p.advance()
331
	p.skipWhitespace()
332
333
	var text string
334
	if p.got(token.TEXT) {
335
		text = p.cur.Literal
336
		p.advance()
337
	}
338
339
	p.expectNewline()
340
341
	return &ast.Comment{
342
		Marker: marker,
343
		Text:   text,
344
		Span:   p.span(s),
345
	}
346
}
347
348
func (p *Parser) parseAccountDirective() *ast.AccountDirective {
349
	s := p.cur.Span
350
	p.expect(token.ACCOUNT)
351
	p.skipWhitespace()
352
353
	account := p.parseAccount()
354
	comment := p.parseOptInlineComment()
355
	p.expectNewline()
356
357
	for p.got(token.INDENT) {
358
		p.advance()
359
		for !p.got(token.NEWLINE) && !p.got(token.EOF) {
360
			p.advance()
361
		}
362
		p.expectNewline()
363
	}
364
365
	return &ast.AccountDirective{
366
		Account: account,
367
		Comment: comment,
368
		Span:    p.span(s),
369
	}
370
}
371
372
func (p *Parser) parseCommodityDirective() *ast.CommodityDirective {
373
	s := p.cur.Span
374
	p.expect(token.COMMODITY)
375
	p.skipWhitespace()
376
377
	var commodity string
378
	var format *ast.Amount
379
380
	switch p.cur.Type {
381
	case token.TEXT, token.INT, token.DECIMAL:
382
		format = p.parseAmount()
383
		commodity = format.Commodity
384
	case token.COMMODITYMARK:
385
		commodity = p.cur.Literal
386
		p.advance()
387
		hadSpace := p.got(token.WHITESPACE)
388
		p.skipWhitespace()
389
		if p.got(token.INT) || p.got(token.DECIMAL) || p.got(token.TEXT) {
390
			format = p.parseAmount()
391
			format.Commodity = commodity
392
			format.CommodityPos = ast.CommodityBefore
393
			format.HasSpace = hadSpace
394
		}
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
	path := ""
438
	if p.got(token.TEXT) {
439
		path = p.cur.Literal
440
		p.advance()
441
	} else {
442
		p.errorf("expected file path, got %s", p.cur.Type)
443
	}
444
445
	comment := p.parseOptInlineComment()
446
	p.expectNewline()
447
448
	return &ast.IncludeDirective{
449
		Path:    path,
450
		Comment: comment,
451
		Span:    p.span(s),
452
	}
453
}
454
455
func (p *Parser) parseAliasDirective() *ast.AliasDirective {
456
	s := p.cur.Span
457
	alias := &ast.AliasDirective{}
458
	p.expect(token.ALIAS)
459
	p.skipWhitespace()
460
	alias.From = p.parseAccount().Name
461
	p.skipWhitespace()
462
	p.expect(token.EQ)
463
	p.skipWhitespace()
464
	alias.To = p.parseAccount().Name
465
	p.skipWhitespace()
466
	alias.Comment = p.parseOptInlineComment()
467
	p.expectNewline()
468
	alias.Span = p.span(s)
469
	return alias
470
}
471
472
func (p *Parser) parsePayeeDirective() *ast.PayeeDirective {
473
	s := p.cur.Span
474
	p.expect(token.PAYEE)
475
	p.skipWhitespace()
476
477
	name := ""
478
	if p.got(token.TEXT) || p.got(token.STRING) {
479
		name = p.parsePayee().Name
480
	}
481
482
	comment := p.parseOptInlineComment()
483
	p.expectNewline()
484
485
	return &ast.PayeeDirective{
486
		Name:    name,
487
		Comment: comment,
488
		Span:    p.span(s),
489
	}
490
}
491
492
func (p *Parser) parseTagDirective() *ast.TagDirective {
493
	s := p.cur.Span
494
	p.expect(token.TAG)
495
	p.skipWhitespace()
496
497
	name := ""
498
	if p.got(token.TEXT) {
499
		name = p.cur.Literal
500
		p.advance()
501
	}
502
503
	comment := p.parseOptInlineComment()
504
	p.expectNewline()
505
506
	return &ast.TagDirective{
507
		Name:    name,
508
		Comment: comment,
509
		Span:    p.span(s),
510
	}
511
}
512
513
func (p *Parser) parseYearDirective() *ast.YearDirective {
514
	s := p.cur.Span
515
	year := &ast.YearDirective{}
516
	p.expect(token.YEAR)
517
	p.skipWhitespace()
518
519
	if p.got(token.INT) {
520
		year.Year, _ = strconv.Atoi(p.cur.Literal)
521
		p.advance()
522
	} else {
523
		p.errorf("expected year, got %s", p.cur.Type)
524
	}
525
526
	p.skipWhitespace()
527
	year.Comment = p.parseOptInlineComment()
528
	p.expectNewline()
529
	year.Span = p.span(s)
530
	return year
531
}
532
533
func (p *Parser) parseDecimalMarkDirective() *ast.DecimalMarkDirective {
534
	s := p.cur.Span
535
	mark := &ast.DecimalMarkDirective{}
536
	p.expect(token.DECIMALMARK)
537
	p.skipWhitespace()
538
539
	mark.Mark = byte('.')
540
	if p.got(token.TEXT) {
541
		if len(p.cur.Literal) > 0 {
542
			mark.Mark = p.cur.Literal[0]
543
		}
544
		p.advance()
545
	}
546
547
	p.skipWhitespace()
548
	mark.Comment = p.parseOptInlineComment()
549
	p.expectNewline()
550
	mark.Span = p.span(s)
551
	return mark
552
}
553
554
func (p *Parser) parseDefaultCommodityDirective() *ast.DefaultCommodityDirective {
555
	s := p.cur.Span
556
	com := &ast.DefaultCommodityDirective{}
557
	p.expect(token.D)
558
	p.skipWhitespace()
559
	com.Amount = *p.parseAmount()
560
	p.skipWhitespace()
561
	com.Comment = p.parseOptInlineComment()
562
	p.expectNewline()
563
	com.Span = p.span(s)
564
	return com
565
}
566
567
func (p *Parser) parseConversionDirective() *ast.ConversionDirective {
568
	s := p.cur.Span
569
	cd := &ast.ConversionDirective{}
570
	p.expect(token.C)
571
	p.skipWhitespace()
572
573
	if p.isAmountStart() {
574
		cd.From = *p.parseAmount()
575
	} else {
576
		p.errorf("expected amount, got %s", p.cur.Type)
577
	}
578
579
	p.skipWhitespace()
580
	if p.got(token.EQ) {
581
		p.advance()
582
		p.skipWhitespace()
583
		if p.isAmountStart() {
584
			cd.To = *p.parseAmount()
585
		} else {
586
			p.errorf("expected amount, got %s", p.cur.Type)
587
		}
588
	}
589
590
	p.skipWhitespace()
591
	cd.Comment = p.parseOptInlineComment()
592
	p.expectNewline()
593
	cd.Span = p.span(s)
594
	return cd
595
}
596
597
func (p *Parser) parseIgnoredDirective() *ast.IgnoredDirective {
598
	s := p.cur.Span
599
	p.expect(token.N)
600
	p.skipWhitespace()
601
602
	id := &ast.IgnoredDirective{}
603
	if p.got(token.TEXT) || p.got(token.COMMODITYMARK) {
604
		id.Text = p.cur.Literal
605
		p.advance()
606
	}
607
	p.skipWhitespace()
608
	id.Comment = p.parseOptInlineComment()
609
610
	p.expectNewline()
611
	id.Span = p.span(s)
612
	return id
613
}
614
615
func (p *Parser) parseMarketPriceDirective() *ast.MarketPriceDirective {
616
	s := p.cur.Span
617
	p.expect(token.P)
618
	p.skipWhitespace()
619
620
	mp := &ast.MarketPriceDirective{}
621
	mp.DateTime.Date = p.parseDate()
622
	p.skipWhitespace()
623
624
	if p.got(token.TIME) {
625
		mp.DateTime.Time = new(p.parseTime())
626
		p.skipWhitespace()
627
	}
628
629
	tok, _ := p.expect(token.COMMODITYMARK)
630
	mp.Commodity = tok.Literal
631
	p.advance()
632
	p.skipWhitespace()
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
	if p.got(token.STAR) || p.got(token.BANG) {
760
		status := ast.StatusPending
761
		if p.cur.Literal[0] == '*' {
762
			status = ast.StatusCleared
763
		}
764
		st := &ast.Status{Value: status, Span: p.cur.Span}
765
		p.advance()
766
		p.skipWhitespace()
767
		return st
768
	}
769
	return nil
770
}
771
772
func (p *Parser) isAmountStart() bool {
773
	switch p.cur.Type {
774
	default:
775
		return false
776
	case token.COMMODITYMARK, token.INT, token.DECIMAL, token.MINUS, token.PLUS, token.PARENEXPR:
777
		return true
778
	case token.TEXT:
779
		return len(p.cur.Literal) > 0 && p.cur.Literal[0] >= '0' && p.cur.Literal[0] <= '9'
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
791
	if p.got(token.COMMODITYMARK) {
792
		amt.Commodity = 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:
818
		if p.got(token.COMMODITYMARK) {
819
			amt.Commodity = 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 (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) {
836
					amt.HasSpace = true
837
					amt.Commodity = p.cur.Literal
838
					amt.CommodityPos = ast.CommodityAfter
839
					p.advance()
840
				}
841
			case token.COMMODITYMARK, token.TEXT:
842
				amt.Commodity = 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
		p.skipWhitespace()
956
		if p.got(token.AT) || p.got(token.ATAT) {
957
			p.parseCost()
958
		}
959
	}
960
961
	posting.Comment = p.parseOptInlineComment()
962
	p.expectNewline()
963
964
	// continuation comments
965
	for p.got(token.INDENT) && p.willGet(token.SEMICOLON) {
966
		p.advance()
967
		c := p.parseComment()
968
		posting.Comments = append(posting.Comments, *c)
969
	}
970
971
	posting.Span = p.span(s)
972
	return posting
973
}
974
975
func (p *Parser) parseCost() *ast.Cost {
976
	s := p.cur.Span
977
	isTotal := p.got(token.ATAT)
978
	p.advance() // consume '@' '@@'
979
	p.skipWhitespace()
980
	return &ast.Cost{
981
		IsTotal: isTotal,
982
		Amount:  *p.parseAmount(),
983
		Span:    p.span(s),
984
	}
985
}
986
987
func (p *Parser) parseBalanceAssertion() *ast.BalanceAssertion {
988
	s := p.cur.Span
989
990
	ba := &ast.BalanceAssertion{}
991
	switch p.cur.Type {
992
	case token.EQ: // basic assertion
993
	case token.EQEQ:
994
		ba.IsStrict = true
995
	case token.EQEQEQ:
996
		ba.IsStrict = true
997
		ba.IsInclusive = true
998
	}
999
	p.advance()
1000
	p.skipWhitespace()
1001
1002
	ba.Amount = *p.parseAmount()
1003
	ba.Span = p.span(s)
1004
	return ba
1005
}
1006
1007
func (p *Parser) parseAccount() ast.Account {
1008
	s := p.cur.Span
1009
	var name strings.Builder
1010
1011
	switch p.cur.Type {
1012
	case token.TEXT:
1013
		_, _ = name.WriteString(p.cur.Literal)
1014
		p.advance()
1015
		if p.got(token.WHITESPACE) && p.willGet(token.TEXT) && p.peek.Literal[0] != '(' {
1016
			_, _ = name.WriteString(" ")
1017
			p.advance()
1018
			_, _ = name.WriteString(p.cur.Literal)
1019
			p.advance()
1020
		}
1021
	case token.COMMODITYMARK:
1022
		_, _ = name.WriteString(p.cur.Literal)
1023
		p.advance()
1024
		for p.got(token.TEXT) {
1025
			_, _ = name.WriteString(p.cur.Literal)
1026
			p.advance()
1027
		}
1028
	}
1029
	return ast.Account{Name: name.String(), Span: p.span(s)}
1030
}
1031
1032
func (p *Parser) parseDate() ast.Date {
1033
	s := p.cur.Span
1034
	tok, ok := p.expect(token.DATE)
1035
	if !ok {
1036
		return ast.Date{Span: p.span(s)}
1037
	}
1038
1039
	sep := byte(0)
1040
	lit := tok.Literal
1041
	for i := 0; i < len(lit); i++ {
1042
		if lit[i] == '/' || lit[i] == '-' || lit[i] == '.' {
1043
			sep = lit[i]
1044
			break
1045
		}
1046
	}
1047
	if sep == 0 {
1048
		p.errorf("invalid date format: %q", lit)
1049
		return ast.Date{Span: p.span(s)}
1050
	}
1051
1052
	parts := strings.Split(lit, string(sep))
1053
1054
	// M/D or MM/DD (year inferred)
1055
	if len(parts) == 2 {
1056
		month, err := strconv.Atoi(parts[0])
1057
		day, err2 := strconv.Atoi(parts[1])
1058
		if err != nil || err2 != nil {
1059
			p.errorf("invalid date literal: %q", lit)
1060
			return ast.Date{Span: p.span(s)}
1061
		}
1062
		if month < 1 || month > 12 {
1063
			p.errorf("invalid month %d in %q", month, lit)
1064
			return ast.Date{Span: p.span(s)}
1065
		}
1066
		if day < 1 || day > 31 {
1067
			p.errorf("invalid day %d in %q", day, lit)
1068
			return ast.Date{Span: p.span(s)}
1069
		}
1070
		return ast.Date{Month: month, Day: day, Sep: sep, Span: p.span(s)}
1071
	}
1072
1073
	if len(parts) != 3 {
1074
		p.errorf("invalid date format: %q", lit)
1075
		return ast.Date{Span: p.span(s)}
1076
	}
1077
1078
	year, err := strconv.Atoi(parts[0])
1079
	month, err2 := strconv.Atoi(parts[1])
1080
	day, err3 := strconv.Atoi(parts[2])
1081
	if err != nil || err2 != nil || err3 != nil {
1082
		p.errorf("invalid date literal: %q", lit)
1083
		return ast.Date{Span: p.span(s)}
1084
	}
1085
	if month < 1 || month > 12 {
1086
		p.errorf("invalid month %d in %q", month, lit)
1087
		return ast.Date{Span: p.span(s)}
1088
	}
1089
	if day < 1 || day > 31 {
1090
		p.errorf("invalid day %d in %q", day, lit)
1091
		return ast.Date{Span: p.span(s)}
1092
	}
1093
1094
	return ast.Date{
1095
		Year:  year,
1096
		Month: month,
1097
		Day:   day,
1098
		Sep:   sep,
1099
		Span:  p.span(s),
1100
	}
1101
}
1102
1103
func (p *Parser) parseOptInlineComment() *ast.Comment {
1104
	p.skipWhitespace() // todo:
1105
	if p.cur.Type != token.SEMICOLON {
1106
		return nil
1107
	}
1108
1109
	s := p.cur.Span
1110
	marker := p.cur.Literal[0]
1111
	p.advance() // consume marker
1112
	p.skipWhitespace()
1113
1114
	text := ""
1115
	if p.got(token.TEXT) {
1116
		text = p.cur.Literal
1117
		p.advance()
1118
	}
1119
1120
	return &ast.Comment{
1121
		Marker: marker,
1122
		Text:   text,
1123
		Span:   p.span(s),
1124
	}
1125
}
1126
1127
func (p *Parser) parseOptPeriodicDescription() string {
1128
	if p.cur.Type != token.WHITESPACE || len(p.cur.Literal) < 2 {
1129
		return ""
1130
	}
1131
1132
	p.skipWhitespace()
1133
1134
	if p.cur.Type != token.TEXT {
1135
		return ""
1136
	}
1137
1138
	return p.parseDescription()
1139
}
1140
1141
func (p *Parser) parseDescription() string {
1142
	var desc strings.Builder
1143
	for p.got(token.TEXT) || (p.got(token.WHITESPACE) && p.willGet(token.TEXT)) {
1144
		_, _ = desc.WriteString(p.cur.Literal)
1145
		p.advance()
1146
	}
1147
	return desc.String()
1148
}
1149
1150
func (p *Parser) parseDirectiveExpr() string {
1151
	var b strings.Builder
1152
	for p.cur.Type != token.NEWLINE && p.cur.Type != token.EOF && p.cur.Type != token.SEMICOLON {
1153
		_, _ = b.WriteString(p.cur.Literal)
1154
		p.advance()
1155
	}
1156
	return b.String()
1157
}
1158
1159
func (p *Parser) parseQuantityInto(amt *ast.Amount) {
1160
	if p.cur.Type != token.INT && p.cur.Type != token.DECIMAL && p.cur.Type != token.TEXT {
1161
		p.errorf("expected quantity, got %s", p.cur.Type)
1162
		return
1163
	}
1164
1165
	lit := p.cur.Literal
1166
	p.advance()
1167
1168
	// detect format metadata before normalizing
1169
	amt.QuantityFmt = detectFormat(lit)
1170
1171
	// normalize for decimal.NewFromString
1172
	// remove thousands separators, replace decimal mark with '.'
1173
	normalized := normalizeLiteral(lit, amt.QuantityFmt.Thousands, amt.QuantityFmt.Decimal)
1174
1175
	q, err := decimal.FromString(normalized)
1176
	if err != nil {
1177
		p.errorf("invalid quantity %q: %v", lit, err)
1178
		return
1179
	}
1180
1181
	if amt.IsNegative {
1182
		q = q.Neg()
1183
	}
1184
	amt.Quantity = q
1185
}
1186
1187
func (p *Parser) parseBlankLine() *ast.BlankLine {
1188
	s := p.cur.Span
1189
	p.expectNewline()
1190
	return &ast.BlankLine{Span: s}
1191
}
1192
1193
func (p *Parser) expectNewline() {
1194
	if p.got(token.NEWLINE) || p.got(token.EOF) {
1195
		if p.got(token.NEWLINE) {
1196
			p.advance()
1197
		}
1198
		return
1199
	}
1200
	p.errorf("expected %s, got %s", token.NEWLINE, p.cur.Type)
1201
}
1202
1203
func (p *Parser) advance() token.Token {
1204
	prev := p.cur
1205
	p.cur = p.peek
1206
	p.peek = p.lexer.Next()
1207
	return prev
1208
}
1209
1210
func (p *Parser) got(kind token.Type) bool     { return p.cur.Type == kind }
1211
func (p *Parser) willGet(kind token.Type) bool { return p.peek.Type == kind }
1212
1213
func (p *Parser) expect(kind token.Type) (token.Token, bool) {
1214
	if p.got(kind) {
1215
		return p.advance(), true
1216
	}
1217
	p.errorf("expected %s, got %s", kind, p.cur.Type)
1218
	return p.cur, false
1219
}
1220
1221
func (p *Parser) errorf(format string, args ...any) {
1222
	p.errors = append(p.errors, &ast.ParseError{
1223
		Span:    p.cur.Span,
1224
		Message: fmt.Sprintf(format, args...),
1225
	})
1226
}
1227
1228
func (p *Parser) sync() {
1229
	for {
1230
		switch p.cur.Type {
1231
		case token.EOF:
1232
			return
1233
		case token.NEWLINE:
1234
			p.advance()
1235
			switch p.cur.Type {
1236
			case token.DATE, token.ACCOUNT, token.COMMODITY,
1237
				token.INCLUDE, token.ALIAS, token.PAYEE,
1238
				token.TAG, token.YEAR, token.D, token.P,
1239
				token.APPLY, token.END, token.COMMENTKW,
1240
				token.DECIMALMARK, token.TILDE, token.N, token.EQ:
1241
				return
1242
			}
1243
		default:
1244
			p.advance()
1245
		}
1246
	}
1247
}
1248
1249
func (p *Parser) syncToNextline() {
1250
	for p.cur.Type != token.NEWLINE && p.cur.Type != token.EOF {
1251
		p.advance()
1252
	}
1253
	if p.got(token.NEWLINE) {
1254
		p.advance()
1255
	}
1256
}
1257
1258
func (p *Parser) skipWhitespace() {
1259
	for p.got(token.WHITESPACE) {
1260
		p.advance()
1261
	}
1262
}
1263
1264
func (p *Parser) span(s token.Span) token.Span {
1265
	return token.Span{Start: s.Start, End: p.cur.Span.Start}
1266
}
1267
1268
func normalizeLiteral(lit string, thousands, decimal byte) string {
1269
	var b strings.Builder
1270
	for _, ch := range []byte(lit) {
1271
		if thousands != 0 && ch == thousands {
1272
			continue // skip thousands separator
1273
		}
1274
		if ch == decimal {
1275
			b.WriteByte('.')
1276
		} else {
1277
			b.WriteByte(ch)
1278
		}
1279
	}
1280
	return b.String()
1281
}
1282
1283
func detectFormat(lit string) ast.QuantityFormat {
1284
	var separators []int
1285
	for i, ch := range []byte(lit) {
1286
		if ch == '.' || ch == ',' || ch == ' ' || ch == '_' || ch == '\'' {
1287
			separators = append(separators, i)
1288
		}
1289
	}
1290
1291
	if len(separators) == 0 {
1292
		return ast.QuantityFormat{Decimal: '.', Thousands: 0, Precision: 0}
1293
	}
1294
1295
	var decimal byte
1296
	thousands := byte(0)
1297
	precision := 0
1298
1299
	if len(separators) == 1 {
1300
		pos := separators[0]
1301
		sepChar := lit[pos]
1302
		if sepChar == ' ' || sepChar == '_' || sepChar == '\'' {
1303
			thousands = sepChar
1304
			decimal = '.' // default
1305
			precision = 0
1306
		} else {
1307
			decimal = sepChar
1308
			precision = len(lit) - pos - 1
1309
		}
1310
	} else {
1311
		last := separators[len(separators)-1]
1312
		decimal = lit[last]
1313
		thousands = lit[separators[0]]
1314
		precision = len(lit) - last - 1
1315
	}
1316
1317
	return ast.QuantityFormat{
1318
		Decimal:   decimal,
1319
		Thousands: thousands,
1320
		Precision: precision,
1321
	}
1322
}
1323
1324
func parseSimpleDate(s string) ast.Date {
1325
	if len(s) < 8 {
1326
		return ast.Date{}
1327
	}
1328
	sep := byte('-')
1329
	if strings.Contains(s, "/") {
1330
		sep = byte('/')
1331
	} else if strings.Contains(s, ".") {
1332
		sep = byte('.')
1333
	}
1334
	parts := strings.Split(s, string(sep))
1335
	if len(parts) != 3 {
1336
		return ast.Date{}
1337
	}
1338
	year, _ := strconv.Atoi(parts[0])
1339
	month, _ := strconv.Atoi(parts[1])
1340
	day, _ := strconv.Atoi(parts[2])
1341
	return ast.Date{Year: year, Month: month, Day: day, Sep: sep}
1342
}