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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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