parsing: make the leading ! in directives optional, like ledger

This commit is contained in:
Simon Michael 2011-08-02 23:28:53 +00:00
parent d025daa8a9
commit 75f0886ad4
2 changed files with 17 additions and 17 deletions

View File

@ -110,7 +110,7 @@ module Hledger.Read.JournalReader (
ledgeraccountname, ledgeraccountname,
ledgerdatetime, ledgerdatetime,
ledgerDefaultYear, ledgerDefaultYear,
ledgerExclamationDirective, ledgerDirective,
ledgerHistoricalPrice, ledgerHistoricalPrice,
reader, reader,
someamount, someamount,
@ -167,7 +167,7 @@ journalFile = do
-- As all journal line types can be distinguished by the first -- As all journal line types can be distinguished by the first
-- character, excepting transactions versus empty (blank or -- character, excepting transactions versus empty (blank or
-- comment-only) lines, can use choice w/o try -- comment-only) lines, can use choice w/o try
journalItem = choice [ ledgerExclamationDirective journalItem = choice [ ledgerDirective
, liftM (return . addTransaction) ledgerTransaction , liftM (return . addTransaction) ledgerTransaction
, liftM (return . addModifierTransaction) ledgerModifierTransaction , liftM (return . addModifierTransaction) ledgerModifierTransaction
, liftM (return . addPeriodicTransaction) ledgerPeriodicTransaction , liftM (return . addPeriodicTransaction) ledgerPeriodicTransaction
@ -206,15 +206,15 @@ ledgercommentline = do
return s return s
<?> "comment" <?> "comment"
ledgerExclamationDirective :: GenParser Char JournalContext JournalUpdate ledgerDirective :: GenParser Char JournalContext JournalUpdate
ledgerExclamationDirective = do ledgerDirective = do
char '!' <?> "directive" optional $ char '!'
directive <- many nonspace choice' [
case directive of string "include" >> ledgerInclude
"include" -> ledgerInclude ,string "account" >> ledgerAccountBegin
"account" -> ledgerAccountBegin ,string "end" >> ledgerAccountEnd
"end" -> ledgerAccountEnd ]
_ -> mzero <?> "directive"
ledgerInclude :: GenParser Char JournalContext JournalUpdate ledgerInclude :: GenParser Char JournalContext JournalUpdate
ledgerInclude = do ledgerInclude = do
@ -677,10 +677,10 @@ tests_Hledger_Read_JournalReader = TestList [
,"ledgerPeriodicTransaction" ~: do ,"ledgerPeriodicTransaction" ~: do
assertParse (parseWithCtx nullctx ledgerPeriodicTransaction "~ (some period expr)\n some:postings 1\n") assertParse (parseWithCtx nullctx ledgerPeriodicTransaction "~ (some period expr)\n some:postings 1\n")
,"ledgerExclamationDirective" ~: do ,"ledgerDirective" ~: do
assertParse (parseWithCtx nullctx ledgerExclamationDirective "!include /some/file.x\n") assertParse (parseWithCtx nullctx ledgerDirective "!include /some/file.x\n")
assertParse (parseWithCtx nullctx ledgerExclamationDirective "!account some:account\n") assertParse (parseWithCtx nullctx ledgerDirective "account some:account\n")
assertParse (parseWithCtx nullctx (ledgerExclamationDirective >> ledgerExclamationDirective) "!account a\n!end\n") assertParse (parseWithCtx nullctx (ledgerDirective >> ledgerDirective) "!account a\nend\n")
,"ledgercommentline" ~: do ,"ledgercommentline" ~: do
assertParse (parseWithCtx nullctx ledgercommentline "; some comment \n") assertParse (parseWithCtx nullctx ledgercommentline "; some comment \n")

View File

@ -54,7 +54,7 @@ import Text.ParserCombinators.Parsec hiding (parse)
import Hledger.Data import Hledger.Data
import Hledger.Read.Utils import Hledger.Read.Utils
import Hledger.Read.JournalReader (ledgerExclamationDirective, ledgerHistoricalPrice, import Hledger.Read.JournalReader (ledgerDirective, ledgerHistoricalPrice,
ledgerDefaultYear, emptyLine, ledgerdatetime) ledgerDefaultYear, emptyLine, ledgerdatetime)
import Hledger.Utils import Hledger.Utils
@ -84,7 +84,7 @@ timelogFile = do items <- many timelogItem
-- As all ledger line types can be distinguished by the first -- As all ledger line types can be distinguished by the first
-- character, excepting transactions versus empty (blank or -- character, excepting transactions versus empty (blank or
-- comment-only) lines, can use choice w/o try -- comment-only) lines, can use choice w/o try
timelogItem = choice [ ledgerExclamationDirective timelogItem = choice [ ledgerDirective
, liftM (return . addHistoricalPrice) ledgerHistoricalPrice , liftM (return . addHistoricalPrice) ledgerHistoricalPrice
, ledgerDefaultYear , ledgerDefaultYear
, emptyLine >> return (return id) , emptyLine >> return (return id)