all repos

clerk @ b7de91e0945ef9cb014ac30d74c2291f76a8c87e

missing tooling for ledger/hledger
5 files changed, 35 insertions(+), 6 deletions(-)
parser: support quote commodities in N and P directives
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-08-19 14:11:06 +0300
Authored at: 2026-08-19 13:52:43 +0300
Change ID: zknmzyunxwoxqnmsvwxsslkwrqtnxpxu
Parent: a925423
M journal/parser/parser.go
···
        663
        663
         	p.skipWhitespace()

      
        664
        664
         

      
        665
        665
         	id := &ast.IgnoredDirective{}

      
        666
        
        -	if p.got(token.TEXT) || p.got(token.COMMODITYMARK) {

      
        667
        
        -		id.Text = p.cur.Literal

      
        
        666
        +	if p.got(token.TEXT) || p.got(token.COMMODITYMARK) || p.got(token.STRING) {

      
        
        667
        +		id.Text = unquote(p.cur.Literal)

      
        668
        668
         		p.advance()

      
        669
        669
         	}

      
        670
        670
         	id.Comment = p.parseOptInlineComment()

      ···
        688
        688
         		p.skipWhitespace()

      
        689
        689
         	}

      
        690
        690
         

      
        691
        
        -	tok, _ := p.expect(token.COMMODITYMARK)

      
        692
        
        -	mp.Commodity = tok.Literal

      
        
        691
        +	if p.got(token.COMMODITYMARK) || p.got(token.STRING) {

      
        
        692
        +		mp.Commodity = unquote(p.cur.Literal)

      
        
        693
        +		p.advance()

      
        
        694
        +	} else {

      
        
        695
        +		p.errorf("expected commodity symbol, got %s", p.cur.Type)

      
        
        696
        +	}

      
        693
        697
         	p.skipWhitespace()

      
        694
        698
         

      
        695
        699
         	mp.Amount = *p.parseAmount()

      
M journal/parser/parser_test.go
···
        25
        25
         		"D directive",

      
        26
        26
         		"P directive",

      
        27
        27
         		"P directive with time",

      
        
        28
        +		"P directive quoted commodity",

      
        28
        29
         		"N directive",

      
        
        30
        +		"N directive quoted",

      
        29
        31
         		"inclue directive",

      
        30
        32
         		"inclue directive with comment",

      
        31
        33
         		"inclue directive digit path",

      
A journal/parser/testdata/N_directive_quoted.txtar
···
        
        1
        +-- input --

      
        
        2
        +N "my fund"

      
        
        3
        +

      
        
        4
        +-- expect --

      
        
        5
        +Journal

      
        
        6
        +  IgnoredDirective j:1:1-3:0

      
        
        7
        +    Text: my fund

      
        
        8
        +  BlankLine j:3:0-3:1

      
A journal/parser/testdata/P_directive_quoted_commodity.txtar
···
        
        1
        +-- input --

      
        
        2
        +P 2024-01-01 "my fund" $100

      
        
        3
        +

      
        
        4
        +-- expect --

      
        
        5
        +Journal

      
        
        6
        +  MarketPriceDirective j:1:1-3:0

      
        
        7
        +    Date: 2024-01-01

      
        
        8
        +    Commodity: "my fund"

      
        
        9
        +    Amount j:1:24-2:0

      
        
        10
        +      Quantity: 100

      
        
        11
        +      Commodity: "$" j:1:24-1:25

      
        
        12
        +      CommodityPos: Before

      
        
        13
        +      HasSpace: false

      
        
        14
        +      Precision: 0

      
        
        15
        +  BlankLine j:3:0-3:1

      
M journal/printer/directives.go
···
        138
        138
         	p.writeDate(m.DateTime.Date)

      
        139
        139
         	p.writeTime(m.DateTime.Time)

      
        140
        140
         	p.buf.WriteByte(' ')

      
        141
        
        -	p.buf.WriteString(m.Commodity)

      
        
        141
        +	p.buf.WriteString(quoteString(m.Commodity))

      
        142
        142
         	p.buf.WriteByte(' ')

      
        143
        143
         	p.writeAmount(&m.Amount)

      
        144
        144
         	p.writeInlineComment(m.Comment)

      ···
        177
        177
         

      
        178
        178
         func (p *printer) writeIgnoredDirective(e *ast.IgnoredDirective) {

      
        179
        179
         	p.buf.WriteString("N ")

      
        180
        
        -	p.buf.WriteString(e.Text)

      
        
        180
        +	p.buf.WriteString(quoteString(e.Text))

      
        181
        181
         	p.writeInlineComment(e.Comment)

      
        182
        182
         }

      
        183
        183