cln!: sourcepos: Use megaparsec-supplied sourcePosPretty.

Change showSourcePos to sourcePosPretty, and showSourcePosPair to
sourcePosPairPretty.
This commit is contained in:
Stephen Morgan 2022-03-10 14:31:48 +11:00 committed by Simon Michael
parent f0e00b3a43
commit 19ed6d3f00
7 changed files with 20 additions and 25 deletions

View File

@ -169,7 +169,7 @@ transactionBalanceError t errs =
annotateErrorWithTransaction :: Transaction -> String -> String
annotateErrorWithTransaction t s =
unlines [ showSourcePosPair $ tsourcepos t, s
unlines [ sourcePosPairPretty $ tsourcepos t, s
, T.unpack . T.stripEnd $ showTransaction t
]
@ -586,7 +586,7 @@ checkBalanceAssertionOneCommodityB p@Posting{paccount=assertedacct} assertedamt
(case ptransaction p of
Nothing -> "?" -- shouldn't happen
Just t -> printf "%s\ntransaction:\n%s"
(showSourcePos pos)
(sourcePosPretty pos)
(textChomp $ showTransaction t)
:: String
where

View File

@ -374,7 +374,7 @@ journalCheckPayeesDeclared j = mapM_ checkpayee (jtxns j)
| otherwise = Left $
printf "undeclared payee \"%s\"\nat: %s\n\n%s"
(T.unpack p)
(showSourcePosPair $ tsourcepos t)
(sourcePosPairPretty $ tsourcepos t)
(linesPrepend2 "> " " " . (<>"\n") . textChomp $ showTransaction t)
where
p = transactionPayee t
@ -392,7 +392,7 @@ journalCheckAccountsDeclared j = mapM_ checkacct (journalPostings j)
++ case ptransaction of
Nothing -> ""
Just t -> printf "in transaction at: %s\n\n%s"
(showSourcePosPair $ tsourcepos t)
(sourcePosPairPretty $ tsourcepos t)
(linesPrepend " " . (<>"\n") . textChomp $ showTransaction t)
where
as = journalAccountNamesDeclared j
@ -411,7 +411,7 @@ journalCheckCommoditiesDeclared j =
++ case ptransaction of
Nothing -> ""
Just t -> printf "in transaction at: %s\n\n%s"
(showSourcePosPair $ tsourcepos t)
(sourcePosPairPretty $ tsourcepos t)
(linesPrepend " " . (<>"\n") . textChomp $ showTransaction t)
where
mfirstundeclaredcomm =

View File

@ -6,14 +6,13 @@ module Hledger.Utils.Parse (
SimpleTextParser,
TextParser,
-- * SourcePos
SourcePos(..),
mkPos,
unPos,
initialPos,
-- * SourcePos
showSourcePosPair,
showSourcePos,
sourcePosPretty,
sourcePosPairPretty,
choice',
choiceInState,
@ -63,16 +62,12 @@ type SimpleTextParser = Parsec CustomErr Text -- XXX an "a" argument breaks the
-- | A parser of text that runs in some monad.
type TextParser m a = ParsecT CustomErr Text m a
-- | Render source position in human-readable form.
showSourcePos :: SourcePos -> String
showSourcePos (SourcePos fp l c) =
show fp ++ " (line " ++ show (unPos l) ++ ", column " ++ show (unPos c) ++ ")"
-- | Render a pair of source position in human-readable form.
showSourcePosPair :: (SourcePos, SourcePos) -> String
showSourcePosPair (SourcePos fp l1 _, SourcePos _ l2 c2) =
show fp ++ " (lines " ++ show (unPos l1) ++ "-" ++ show l2' ++ ")"
where l2' = if unPos c2 == 1 then unPos l2 - 1 else unPos l2 -- might be at end of file withat last new-line
-- | Render a pair of source positions in human-readable form, only displaying the range of lines.
sourcePosPairPretty :: (SourcePos, SourcePos) -> String
sourcePosPairPretty (SourcePos fp l1 _, SourcePos _ l2 c2) =
fp ++ ":" ++ show (unPos l1) ++ "-" ++ show l2'
where
l2' = if unPos c2 == 1 then unPos l2 - 1 else unPos l2 -- might be at end of file with a final new line
-- | Backtracking choice, use this when alternatives share a prefix.
-- Consumes no input if all choices fail.

View File

@ -30,7 +30,7 @@ journalCheckOrdereddates CliOpts{reportspec_=rspec} j = do
let
datestr = if date2_ ropts then "2" else ""
uniquestr = if checkunique then " and/or not unique" else ""
positionstr = showSourcePosPair $ tsourcepos error
positionstr = sourcePosPairPretty $ tsourcepos error
txn1str = T.unpack . linesPrepend (T.pack " ") $ showTransaction previous
txn2str = T.unpack . linesPrepend2 (T.pack "> ") (T.pack " ") $ showTransaction error
Left $

View File

@ -49,5 +49,5 @@ checkposting leafandfullnames Posting{paccount,ptransaction} =
Nothing -> ""
Just t -> printf "\nseen in \"%s\" in transaction at: %s\n\n%s"
paccount
(showSourcePosPair $ tsourcepos t)
(sourcePosPairPretty $ tsourcepos t)
(linesPrepend "> " . (<>"\n") . textChomp $ showTransaction t) :: String)

View File

@ -56,7 +56,7 @@ $ hledger -f - stats
b $-1 = $-3
$ hledger -f - stats
>2 /balance assertion.*line 11, column 12/
>2 /balance assertion.*11:12/
>=1
# 4. should also work without commodity symbols
@ -314,7 +314,7 @@ $ hledger -f - stats
a 0 == $1
$ hledger -f - stats
>2 /balance assertion.*line 10, column 15/
>2 /balance assertion.*10:15/
>=1
# 18. Mix different commodities and total assignments

View File

@ -68,7 +68,7 @@ $ hledger -f journal:- print
[c]
$ hledger -f journal:- print
>2 /lines 1-4/
>2 /:1-4/
>=1
# 7. Balancing error messages show the recorded precision, not the display precision.
@ -123,7 +123,7 @@ $ hledger -f- print
b 1B
$ hledger -f- print
>2
hledger: "-" (lines 1-3)
hledger: -:1-3
could not balance this transaction:
real postings all have the same sign
2020-01-01