Skip to content

Commit 0f1b3b8

Browse files
syntax: Pretty instance for ExprError (#1274)
1 parent 7ccc4d9 commit 0f1b3b8

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

crucible-syntax/src/Lang/Crucible/Syntax/Atoms.hs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{-# LANGUAGE LambdaCase, OverloadedStrings #-}
1+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+
{-# LANGUAGE LambdaCase #-}
3+
{-# LANGUAGE OverloadedStrings#-}
4+
25
-- | Atoms used by the Crucible CFG concrete syntax.
36
module Lang.Crucible.Syntax.Atoms
47
(
@@ -24,20 +27,21 @@ import qualified Data.Text as T
2427

2528
import Lang.Crucible.Syntax.SExpr
2629
import Numeric
30+
import qualified Prettyprinter as PP
2731

2832
import Text.Megaparsec as MP hiding (many, some)
2933
import Text.Megaparsec.Char
3034

3135
-- | The name of an atom (non-keyword identifier)
32-
newtype AtomName = AtomName Text deriving (Eq, Ord, Show)
36+
newtype AtomName = AtomName Text deriving (Eq, Ord, PP.Pretty, Show)
3337
-- | The name of a label (identifier followed by colon)
34-
newtype LabelName = LabelName Text deriving (Eq, Ord, Show)
38+
newtype LabelName = LabelName Text deriving (Eq, Ord, PP.Pretty, Show)
3539
-- | The name of a register (dollar sign followed by identifier)
36-
newtype RegName = RegName Text deriving (Eq, Ord, Show)
40+
newtype RegName = RegName Text deriving (Eq, Ord, PP.Pretty, Show)
3741
-- | The name of a function (at-sign followed by identifier)
38-
newtype FunName = FunName Text deriving (Eq, Ord, Show)
42+
newtype FunName = FunName Text deriving (Eq, Ord, PP.Pretty, Show)
3943
-- | The name of a global variable (two dollar signs followed by identifier)
40-
newtype GlobalName = GlobalName Text deriving (Eq, Ord, Show)
44+
newtype GlobalName = GlobalName Text deriving (Eq, Ord, PP.Pretty, Show)
4145

4246
-- | Individual language keywords (reserved identifiers)
4347
data Keyword = Defun | DefBlock | DefGlobal | Declare | Extern

crucible-syntax/src/Lang/Crucible/Syntax/Concrete.hs

+22
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import Data.Text (Text)
8787
import qualified Data.Text as T
8888
import qualified Data.Vector as V
8989
import Numeric.Natural
90+
import qualified Prettyprinter as PP
9091

9192
import Lang.Crucible.Syntax.ExprParse hiding (SyntaxError)
9293
import qualified Lang.Crucible.Syntax.ExprParse as SP
@@ -160,6 +161,27 @@ instance Semigroup (ExprErr s) where
160161
instance Monoid (ExprErr s) where
161162
mempty = TrivialErr (OtherPos "mempty")
162163

164+
instance PP.Pretty (ExprErr s) where
165+
pretty =
166+
\case
167+
TrivialErr p ->
168+
"Trivial error at" PP.<+> PP.viaShow p
169+
Errs e1 e2 ->
170+
PP.vcat ["Multiple errors:" , PP.pretty e1 , PP.pretty e2]
171+
DuplicateAtom p a ->
172+
PP.hsep ["Duplicate atom", backticks (PP.pretty a), "at", PP.viaShow p]
173+
DuplicateLabel p l ->
174+
PP.hsep ["Duplicate label", backticks (PP.pretty l), "at", PP.viaShow p]
175+
EmptyBlock p ->
176+
"Empty block at" PP.<+> PP.viaShow p
177+
NotGlobal p _ast ->
178+
"Expected a global at" PP.<+> PP.viaShow p
179+
InvalidRegister p _ast ->
180+
"Expected a register at" PP.<+> PP.viaShow p
181+
SyntaxParseError err ->
182+
PP.pretty (printSyntaxError err)
183+
where backticks = PP.enclose "`" "`"
184+
163185
-- | ParserHooks enables support for arbitrary syntax extensions by allowing
164186
-- users to supply their own parsers for types and syntax extensions.
165187
data ParserHooks ext = ParserHooks {

0 commit comments

Comments
 (0)