From 43c9f018dc0c19dc439f2f61c9e9ec0612f82138 Mon Sep 17 00:00:00 2001 From: Chris Lemaire Date: Tue, 24 Jan 2023 21:04:48 +0100 Subject: [PATCH] journal: Account directives reject account names with brackets Previously, the accounts passed to account directives would be stripped of their surrounding brackets, but the required behaviour is to have account directives plain reject bracketed accounts. This change ensures that accounts in account directives may not start with a bracket character. --- hledger-lib/Hledger/Read/JournalReader.hs | 5 ++- hledger/hledger.m4.md | 6 ++- hledger/test/journal/directive-account.test | 43 ++++++++++++--------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 281014b4c..d1143b7de 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -381,7 +381,8 @@ accountdirectivep = do lift skipNonNewlineSpaces1 -- the account name, possibly modified by preceding alias or apply account directives - acct <- modifiedaccountnamep + acct <- (notFollowedBy (char '(' <|> char '[') "account name without brackets") >> + modifiedaccountnamep -- maybe a comment, on this and/or following lines (cmt, tags) <- lift transactioncommentp @@ -433,7 +434,7 @@ addAccountDeclaration (a,cmt,tags,pos) = do modify' (\j -> let decls = jdeclaredaccounts j - d = (textUnbracket a, nullaccountdeclarationinfo{ + d = (a, nullaccountdeclarationinfo{ adicomment = cmt ,aditags = tags ,adideclarationorder = length decls + 1 -- gets renumbered when Journals are finalised or merged diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 4490f74e2..cef692958 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -1801,8 +1801,10 @@ They are written as the word `account` followed by a hledger-style [account name account assets:bank:checking ``` -Note, however, that account names declared in the account directive are stripped of surrounding brackets and parentheses. -The above directive is thus equivalent to this: +Note, however, that accounts declared in account directives are not allowed to have surrounding +brackets and parentheses, unlike accounts used in postings. +So the following journal will not parse: + ```journal account (assets:bank:checking) ``` diff --git a/hledger/test/journal/directive-account.test b/hledger/test/journal/directive-account.test index 94abceaeb..eec26e06b 100644 --- a/hledger/test/journal/directive-account.test +++ b/hledger/test/journal/directive-account.test @@ -68,28 +68,35 @@ account Expenses:Food $ hledger -f- accounts Expenses:Food -# 5. It unbrackets account names. +# 5. It does not allow parentheses in names. < account (a) -account (a:aa) -account (a:(aaa)) -account [b] -account [b:bb] -account [b:[bbb]] -account [([c])] -account [([c:cc])] -account [([c:[ccc]])] $ hledger -f- accounts -a -a:aa -a:(aaa) -b -b:bb -b:[bbb] -c -c:cc -c:[ccc] +>2 +hledger: Error: -:1:9: + | +1 | account (a) + | ^ +unexpected '(' +expecting account name without brackets + +>=1 + +# 6. It does not allow brackets in names. +< +account [a] + +$ hledger -f- accounts +>2 +hledger: Error: -:1:9: + | +1 | account [a] + | ^ +unexpected '[' +expecting account name without brackets + +>=1 # TODO # a trailing : should give a clear error