@@ -20,15 +20,18 @@ module Database.Persist.Quasi.Internal.ModelParser
2020 , parseSource
2121 , memberBlockAttrs
2222 , ParseResult
23- , CumulativeParseResult ( .. )
23+ , CumulativeParseResult
2424 , toCumulativeParseResult
25+ , cumulativeData
2526 , renderErrors
2627 , runConfiguredParser
2728 , initialExtraState
2829 ) where
2930
3031import Control.Monad (void )
3132import Control.Monad.Trans.State
33+ import Data.Either (partitionEithers )
34+ import Data.Foldable (fold )
3235import Data.List (intercalate )
3336import Data.List.NonEmpty (NonEmpty (.. ))
3437import qualified Data.List.NonEmpty as NEL
@@ -62,11 +65,19 @@ initialExtraState =
6265type Parser a =
6366 StateT
6467 ExtraState
65- (ParsecT Void String ( Either ( ParseErrorBundle String Void )) )
68+ (Parsec Void String )
6669 a
67- type InternalParseResult a =
68- Either (ParseErrorBundle String Void ) (a , ExtraState )
69- type ParseResult a = Either (ParseErrorBundle String Void ) a
70+
71+ type EntityParseError = ParseErrorBundle String Void
72+ type InternalParseResult a = Either EntityParseError (a , ExtraState )
73+
74+ type ParseResult a = Either EntityParseError a
75+ type CumulativeParseResult a = Either [EntityParseError ] a
76+
77+ cumulativeData :: (Monoid a ) => CumulativeParseResult a -> a
78+ cumulativeData cpr = case cpr of
79+ Left _ -> mempty
80+ Right r -> r
7081
7182-- | Run a parser using a provided ExtraState
7283-- @since 2.16.0.0
@@ -76,11 +87,9 @@ runConfiguredParser
7687 -> String
7788 -> String
7889 -> InternalParseResult a
79- runConfiguredParser acc parser fp s = do
80- (_internalState, parseResult) <-
81- runParserT' (runStateT parser acc) initialInternalState
82- parseResult
90+ runConfiguredParser acc parser fp s = parseResult
8391 where
92+ (_internalState, parseResult) = runParser' (runStateT parser acc) initialInternalState
8493 initialSourcePos =
8594 SourcePos
8695 { sourceName = fp
@@ -104,50 +113,18 @@ runConfiguredParser acc parser fp s = do
104113 , stateParseErrors = []
105114 }
106115
107- -- @since 2.16.0.0
108- data CumulativeParseResult a = CumulativeParseResult
109- { cumulativeErrors :: [ParseErrorBundle String Void ]
110- , cumulativeData :: a
111- }
112-
113- -- | Populates a CumulativeParseResult with a single error or datum
114- -- @since 2.16.0.0
115- toCumulativeParseResult
116- :: (Monoid a )
117- => ParseResult a
118- -> CumulativeParseResult a
119- toCumulativeParseResult (Left peb) =
120- CumulativeParseResult
121- { cumulativeErrors = [peb]
122- , cumulativeData = mempty
123- }
124- toCumulativeParseResult (Right res) =
125- CumulativeParseResult
126- { cumulativeErrors = []
127- , cumulativeData = res
128- }
129-
130116-- | Converts the errors in a CumulativeParseResult to a String
131117-- @since 2.16.0.0
132118renderErrors :: CumulativeParseResult a -> Maybe String
133- renderErrors cpr =
134- case cumulativeErrors cpr of
135- [] -> Nothing
136- pebs -> Just $ intercalate " \n " $ fmap errorBundlePretty pebs
137-
138- instance (Semigroup a ) => Semigroup (CumulativeParseResult a ) where
139- (<>) l r =
140- CumulativeParseResult
141- { cumulativeErrors = cumulativeErrors l ++ cumulativeErrors r
142- , cumulativeData = cumulativeData l <> cumulativeData r
143- }
119+ renderErrors cpr = case cpr of
120+ Right _ -> Nothing
121+ Left errs -> Just $ intercalate " \n " $ fmap errorBundlePretty errs
144122
145- instance (Monoid a ) => Monoid (CumulativeParseResult a ) where
146- mempty =
147- CumulativeParseResult
148- { cumulativeErrors = mempty
149- , cumulativeData = mempty
150- }
123+ toCumulativeParseResult
124+ :: (Monoid a ) => [ParseResult a ] -> CumulativeParseResult a
125+ toCumulativeParseResult prs = case partitionEithers prs of
126+ ([] , results) -> Right $ fold results
127+ (errs, _) -> Left errs
151128
152129-- | Source location: file and line/col information. This is half of a 'SourceSpan'.
153130--
@@ -350,39 +327,6 @@ anyToken =
350327 , ptext
351328 ]
352329
353- class Block a where
354- blockFirstPos :: a -> SourcePos
355- blockMembers :: a -> [Member ]
356-
357- instance Block EntityBlock where
358- blockFirstPos = entityHeaderPos . entityBlockEntityHeader
359- blockMembers = entityBlockMembers
360-
361- instance Block ExtraBlock where
362- blockFirstPos = extraBlockHeaderPos . extraBlockExtraBlockHeader
363- blockMembers = NEL. toList . extraBlockMembers
364-
365- blockLastPos :: (Block a ) => a -> SourcePos
366- blockLastPos b = case blockMembers b of
367- [] -> blockFirstPos b
368- members -> maximum $ fmap memberEndPos members
369-
370- blockBlockAttrs :: (Block a ) => a -> [BlockAttr ]
371- blockBlockAttrs eb =
372- foldMap f (blockMembers eb)
373- where
374- f = \ case
375- MemberBlockAttr fs -> [fs]
376- _ -> []
377-
378- blockExtraBlocks :: (Block a ) => a -> [ExtraBlock ]
379- blockExtraBlocks eb =
380- foldMap f (blockMembers eb)
381- where
382- f = \ case
383- MemberExtraBlock ex -> [ex]
384- _ -> []
385-
386330data ParsedEntityDef = ParsedEntityDef
387331 { parsedEntityDefComments :: [Text ]
388332 , parsedEntityDefEntityName :: EntityNameHS
@@ -415,6 +359,28 @@ data EntityBlock = EntityBlock
415359 }
416360 deriving (Show )
417361
362+ entityBlockFirstPos :: EntityBlock -> SourcePos
363+ entityBlockFirstPos = entityHeaderPos . entityBlockEntityHeader
364+
365+ entityBlockLastPos :: EntityBlock -> SourcePos
366+ entityBlockLastPos eb = case entityBlockMembers eb of
367+ [] -> entityBlockFirstPos eb
368+ members -> maximum $ fmap memberEndPos members
369+
370+ entityBlockBlockAttrs :: EntityBlock -> [BlockAttr ]
371+ entityBlockBlockAttrs = foldMap f <$> entityBlockMembers
372+ where
373+ f m = case m of
374+ MemberExtraBlock _ -> []
375+ MemberBlockAttr ba -> [ba]
376+
377+ entityBlockExtraBlocks :: EntityBlock -> [ExtraBlock ]
378+ entityBlockExtraBlocks = foldMap f <$> entityBlockMembers
379+ where
380+ f m = case m of
381+ MemberExtraBlock eb -> [eb]
382+ MemberBlockAttr _ -> []
383+
418384data ExtraBlockHeader = ExtraBlockHeader
419385 { extraBlockHeaderKey :: Text
420386 , extraBlockHeaderRemainingTokens :: [Token ]
@@ -480,7 +446,7 @@ appendCommentToState ptok = do
480446 es <- get
481447 let
482448 comments = esPositionedCommentTokens es
483- void $ put es{esPositionedCommentTokens = comments ++ [ptok] }
449+ void $ put es{esPositionedCommentTokens = ptok : comments }
484450
485451setLastDocumentablePosition :: Parser ()
486452setLastDocumentablePosition = do
@@ -492,11 +458,10 @@ getDcb :: Parser (Maybe DocCommentBlock)
492458getDcb = do
493459 es <- get
494460 let
495- comments = esPositionedCommentTokens es
461+ comments = reverse $ esPositionedCommentTokens es
496462 _ <- put es{esPositionedCommentTokens = [] }
497463 let
498464 candidates = dropWhile (\ (_sp, ct) -> not (isDocComment ct)) comments
499- let
500465 filteredCandidates = dropWhile (commentIsIncorrectlyPositioned es) candidates
501466 pure $ docCommentBlockFromPositionedTokens filteredCandidates
502467 where
@@ -625,9 +590,9 @@ toParsedEntityDef mSourceLoc eb =
625590 entityNameHS = EntityNameHS . entityHeaderTableName . entityBlockEntityHeader $ eb
626591
627592 attributePair a = (blockAttrTokens a, docCommentBlockText <$> blockAttrDocCommentBlock a)
628- parsedFieldAttributes = fmap attributePair (blockBlockAttrs eb)
593+ parsedFieldAttributes = fmap attributePair (entityBlockBlockAttrs eb)
629594
630- extras = extraBlocksAsMap (blockExtraBlocks eb)
595+ extras = extraBlocksAsMap (entityBlockExtraBlocks eb)
631596 filepath = maybe " " locFile mSourceLoc
632597 relativeStartLine = maybe 0 locStartLine mSourceLoc
633598 relativeStartCol = maybe 0 locStartCol mSourceLoc
@@ -636,11 +601,11 @@ toParsedEntityDef mSourceLoc eb =
636601 SourceSpan
637602 { spanFile = filepath
638603 , spanStartLine =
639- relativeStartLine + (unPos . sourceLine $ blockFirstPos eb)
640- , spanEndLine = relativeStartLine + (unPos . sourceLine $ blockLastPos eb)
604+ relativeStartLine + (unPos . sourceLine $ entityBlockFirstPos eb)
605+ , spanEndLine = relativeStartLine + (unPos . sourceLine $ entityBlockLastPos eb)
641606 , spanStartCol =
642- relativeStartCol + (unPos . sourceColumn $ blockFirstPos eb)
643- , spanEndCol = unPos . sourceColumn $ blockLastPos eb
607+ relativeStartCol + (unPos . sourceColumn $ entityBlockFirstPos eb)
608+ , spanEndCol = unPos . sourceColumn $ entityBlockLastPos eb
644609 }
645610
646611parseSource :: Maybe SourceLoc -> Text -> ParseResult [ParsedEntityDef ]
0 commit comments