module Agda.Syntax.Parser.Monad
(
Parser
, ParseResult(..)
, ParseState(..)
, ParseError(..), ParseWarning(..)
, LexState
, LayoutBlock(..), LayoutContext, LayoutStatus(..)
, Column
, ParseFlags (..)
, initState
, defaultParseFlags
, parse
, parsePosString
, parseFromSrc
, setParsePos, setLastPos, getParseInterval
, setPrevToken
, getParseFlags
, getLexState, pushLexState, popLexState
, topBlock, popBlock, pushBlock
, getContext, setContext, modifyContext
, resetLayoutStatus
, parseWarning, parseWarningName
, parseError, parseErrorAt, parseError', parseErrorRange
, lexError
)
where
import Control.DeepSeq
import Control.Exception ( displayException )
import Control.Monad.Except
import Control.Monad.State
import Data.Int
import Data.Data ( Data )
import Data.Maybe ( listToMaybe )
import Agda.Interaction.Options.Warnings
import Agda.Syntax.Position
import Agda.Syntax.Parser.Tokens ( Keyword( KwMutual ) )
import Agda.Utils.FileName
import Agda.Utils.List ( tailWithDefault )
import qualified Agda.Utils.Maybe.Strict as Strict
import Agda.Utils.Pretty
import Agda.Utils.Impossible
newtype Parser a = P { forall a. Parser a -> StateT ParseState (Either ParseError) a
_runP :: StateT ParseState (Either ParseError) a }
deriving ((forall a b. (a -> b) -> Parser a -> Parser b)
-> (forall a b. a -> Parser b -> Parser a) -> Functor Parser
forall a b. a -> Parser b -> Parser a
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Parser b -> Parser a
$c<$ :: forall a b. a -> Parser b -> Parser a
fmap :: forall a b. (a -> b) -> Parser a -> Parser b
$cfmap :: forall a b. (a -> b) -> Parser a -> Parser b
Functor, Functor Parser
Functor Parser
-> (forall a. a -> Parser a)
-> (forall a b. Parser (a -> b) -> Parser a -> Parser b)
-> (forall a b c.
(a -> b -> c) -> Parser a -> Parser b -> Parser c)
-> (forall a b. Parser a -> Parser b -> Parser b)
-> (forall a b. Parser a -> Parser b -> Parser a)
-> Applicative Parser
forall a. a -> Parser a
forall a b. Parser a -> Parser b -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Parser a -> Parser b -> Parser a
$c<* :: forall a b. Parser a -> Parser b -> Parser a
*> :: forall a b. Parser a -> Parser b -> Parser b
$c*> :: forall a b. Parser a -> Parser b -> Parser b
liftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
$cliftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
$c<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
pure :: forall a. a -> Parser a
$cpure :: forall a. a -> Parser a
Applicative, Applicative Parser
Applicative Parser
-> (forall a b. Parser a -> (a -> Parser b) -> Parser b)
-> (forall a b. Parser a -> Parser b -> Parser b)
-> (forall a. a -> Parser a)
-> Monad Parser
forall a. a -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> Parser a
$creturn :: forall a. a -> Parser a
>> :: forall a b. Parser a -> Parser b -> Parser b
$c>> :: forall a b. Parser a -> Parser b -> Parser b
>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
$c>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
Monad, MonadState ParseState, MonadError ParseError)
data ParseState = PState
{ ParseState -> SrcFile
parseSrcFile :: !SrcFile
, ParseState -> PositionWithoutFile
parsePos :: !PositionWithoutFile
, ParseState -> PositionWithoutFile
parseLastPos :: !PositionWithoutFile
, ParseState -> String
parseInp :: String
, ParseState -> Char
parsePrevChar :: !Char
, ParseState -> String
parsePrevToken:: String
, ParseState -> LayoutContext
parseLayout :: LayoutContext
, ParseState -> LayoutStatus
parseLayStatus:: LayoutStatus
, ParseState -> Keyword
parseLayKw :: Keyword
, ParseState -> [LexState]
parseLexState :: [LexState]
, ParseState -> ParseFlags
parseFlags :: ParseFlags
, ParseState -> [ParseWarning]
parseWarnings :: ![ParseWarning]
}
deriving LexState -> ParseState -> ShowS
[ParseState] -> ShowS
ParseState -> String
(LexState -> ParseState -> ShowS)
-> (ParseState -> String)
-> ([ParseState] -> ShowS)
-> Show ParseState
forall a.
(LexState -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseState] -> ShowS
$cshowList :: [ParseState] -> ShowS
show :: ParseState -> String
$cshow :: ParseState -> String
showsPrec :: LexState -> ParseState -> ShowS
$cshowsPrec :: LexState -> ParseState -> ShowS
Show
type LexState = Int
type LayoutContext = [LayoutBlock]
data LayoutBlock
= Layout Keyword LayoutStatus Column
deriving LexState -> LayoutBlock -> ShowS
LayoutContext -> ShowS
LayoutBlock -> String
(LexState -> LayoutBlock -> ShowS)
-> (LayoutBlock -> String)
-> (LayoutContext -> ShowS)
-> Show LayoutBlock
forall a.
(LexState -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: LayoutContext -> ShowS
$cshowList :: LayoutContext -> ShowS
show :: LayoutBlock -> String
$cshow :: LayoutBlock -> String
showsPrec :: LexState -> LayoutBlock -> ShowS
$cshowsPrec :: LexState -> LayoutBlock -> ShowS
Show
type Column = Int32
data LayoutStatus
= Tentative
| Confirmed
deriving (LayoutStatus -> LayoutStatus -> Bool
(LayoutStatus -> LayoutStatus -> Bool)
-> (LayoutStatus -> LayoutStatus -> Bool) -> Eq LayoutStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LayoutStatus -> LayoutStatus -> Bool
$c/= :: LayoutStatus -> LayoutStatus -> Bool
== :: LayoutStatus -> LayoutStatus -> Bool
$c== :: LayoutStatus -> LayoutStatus -> Bool
Eq, LexState -> LayoutStatus -> ShowS
[LayoutStatus] -> ShowS
LayoutStatus -> String
(LexState -> LayoutStatus -> ShowS)
-> (LayoutStatus -> String)
-> ([LayoutStatus] -> ShowS)
-> Show LayoutStatus
forall a.
(LexState -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LayoutStatus] -> ShowS
$cshowList :: [LayoutStatus] -> ShowS
show :: LayoutStatus -> String
$cshow :: LayoutStatus -> String
showsPrec :: LexState -> LayoutStatus -> ShowS
$cshowsPrec :: LexState -> LayoutStatus -> ShowS
Show)
data ParseFlags = ParseFlags
{ :: Bool
}
deriving LexState -> ParseFlags -> ShowS
[ParseFlags] -> ShowS
ParseFlags -> String
(LexState -> ParseFlags -> ShowS)
-> (ParseFlags -> String)
-> ([ParseFlags] -> ShowS)
-> Show ParseFlags
forall a.
(LexState -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseFlags] -> ShowS
$cshowList :: [ParseFlags] -> ShowS
show :: ParseFlags -> String
$cshow :: ParseFlags -> String
showsPrec :: LexState -> ParseFlags -> ShowS
$cshowsPrec :: LexState -> ParseFlags -> ShowS
Show
data ParseError
= ParseError
{ ParseError -> SrcFile
errSrcFile :: !SrcFile
, ParseError -> PositionWithoutFile
errPos :: !PositionWithoutFile
, ParseError -> String
errInput :: String
, ParseError -> String
errPrevToken :: String
, ParseError -> String
errMsg :: String
}
| OverlappingTokensError
{ ParseError -> Range' SrcFile
errRange :: !(Range' SrcFile)
}
| InvalidExtensionError
{ ParseError -> AbsolutePath
errPath :: !AbsolutePath
, ParseError -> [String]
errValidExts :: [String]
}
| ReadFileError
{ errPath :: !AbsolutePath
, ParseError -> IOError
errIOError :: IOError
}
deriving LexState -> ParseError -> ShowS
[ParseError] -> ShowS
ParseError -> String
(LexState -> ParseError -> ShowS)
-> (ParseError -> String)
-> ([ParseError] -> ShowS)
-> Show ParseError
forall a.
(LexState -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseError] -> ShowS
$cshowList :: [ParseError] -> ShowS
show :: ParseError -> String
$cshow :: ParseError -> String
showsPrec :: LexState -> ParseError -> ShowS
$cshowsPrec :: LexState -> ParseError -> ShowS
Show
data ParseWarning
= OverlappingTokensWarning
{ ParseWarning -> Range' SrcFile
warnRange :: !(Range' SrcFile)
}
| UnsupportedAttribute Range !(Maybe String)
| MultipleAttributes Range !(Maybe String)
deriving (Typeable ParseWarning
Typeable ParseWarning
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ParseWarning -> c ParseWarning)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ParseWarning)
-> (ParseWarning -> Constr)
-> (ParseWarning -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ParseWarning))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ParseWarning))
-> ((forall b. Data b => b -> b) -> ParseWarning -> ParseWarning)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r)
-> (forall u. (forall d. Data d => d -> u) -> ParseWarning -> [u])
-> (forall u.
LexState -> (forall d. Data d => d -> u) -> ParseWarning -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning)
-> Data ParseWarning
ParseWarning -> DataType
ParseWarning -> Constr
(forall b. Data b => b -> b) -> ParseWarning -> ParseWarning
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. LexState -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
LexState -> (forall d. Data d => d -> u) -> ParseWarning -> u
forall u. (forall d. Data d => d -> u) -> ParseWarning -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ParseWarning
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ParseWarning -> c ParseWarning
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ParseWarning)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ParseWarning)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ParseWarning -> m ParseWarning
gmapQi :: forall u.
LexState -> (forall d. Data d => d -> u) -> ParseWarning -> u
$cgmapQi :: forall u.
LexState -> (forall d. Data d => d -> u) -> ParseWarning -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> ParseWarning -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ParseWarning -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ParseWarning -> r
gmapT :: (forall b. Data b => b -> b) -> ParseWarning -> ParseWarning
$cgmapT :: (forall b. Data b => b -> b) -> ParseWarning -> ParseWarning
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ParseWarning)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ParseWarning)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ParseWarning)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ParseWarning)
dataTypeOf :: ParseWarning -> DataType
$cdataTypeOf :: ParseWarning -> DataType
toConstr :: ParseWarning -> Constr
$ctoConstr :: ParseWarning -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ParseWarning
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ParseWarning
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ParseWarning -> c ParseWarning
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ParseWarning -> c ParseWarning
Data, LexState -> ParseWarning -> ShowS
[ParseWarning] -> ShowS
ParseWarning -> String
(LexState -> ParseWarning -> ShowS)
-> (ParseWarning -> String)
-> ([ParseWarning] -> ShowS)
-> Show ParseWarning
forall a.
(LexState -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseWarning] -> ShowS
$cshowList :: [ParseWarning] -> ShowS
show :: ParseWarning -> String
$cshow :: ParseWarning -> String
showsPrec :: LexState -> ParseWarning -> ShowS
$cshowsPrec :: LexState -> ParseWarning -> ShowS
Show)
instance NFData ParseWarning where
rnf :: ParseWarning -> ()
rnf (OverlappingTokensWarning Range' SrcFile
_) = ()
rnf (UnsupportedAttribute Range' SrcFile
_ Maybe String
s) = Maybe String -> ()
forall a. NFData a => a -> ()
rnf Maybe String
s
rnf (MultipleAttributes Range' SrcFile
_ Maybe String
s) = Maybe String -> ()
forall a. NFData a => a -> ()
rnf Maybe String
s
parseWarningName :: ParseWarning -> WarningName
parseWarningName :: ParseWarning -> WarningName
parseWarningName = \case
OverlappingTokensWarning{} -> WarningName
OverlappingTokensWarning_
UnsupportedAttribute{} -> WarningName
UnsupportedAttribute_
MultipleAttributes{} -> WarningName
MultipleAttributes_
data ParseResult a
= ParseOk ParseState a
| ParseFailed ParseError
deriving LexState -> ParseResult a -> ShowS
[ParseResult a] -> ShowS
ParseResult a -> String
(LexState -> ParseResult a -> ShowS)
-> (ParseResult a -> String)
-> ([ParseResult a] -> ShowS)
-> Show (ParseResult a)
forall a. Show a => LexState -> ParseResult a -> ShowS
forall a. Show a => [ParseResult a] -> ShowS
forall a. Show a => ParseResult a -> String
forall a.
(LexState -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseResult a] -> ShowS
$cshowList :: forall a. Show a => [ParseResult a] -> ShowS
show :: ParseResult a -> String
$cshow :: forall a. Show a => ParseResult a -> String
showsPrec :: LexState -> ParseResult a -> ShowS
$cshowsPrec :: forall a. Show a => LexState -> ParseResult a -> ShowS
Show
unP :: Parser a -> ParseState -> ParseResult a
unP :: forall a. Parser a -> ParseState -> ParseResult a
unP (P StateT ParseState (Either ParseError) a
m) ParseState
s = case StateT ParseState (Either ParseError) a
-> ParseState -> Either ParseError (a, ParseState)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT ParseState (Either ParseError) a
m ParseState
s of
Left ParseError
err -> ParseError -> ParseResult a
forall a. ParseError -> ParseResult a
ParseFailed ParseError
err
Right (a
a, ParseState
s) -> ParseState -> a -> ParseResult a
forall a. ParseState -> a -> ParseResult a
ParseOk ParseState
s a
a
parseError :: String -> Parser a
parseError :: forall a. String -> Parser a
parseError String
msg = do
ParseState
s <- Parser ParseState
forall s (m :: * -> *). MonadState s m => m s
get
ParseError -> Parser a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (ParseError -> Parser a) -> ParseError -> Parser a
forall a b. (a -> b) -> a -> b
$ ParseError
{ errSrcFile :: SrcFile
errSrcFile = ParseState -> SrcFile
parseSrcFile ParseState
s
, errPos :: PositionWithoutFile
errPos = ParseState -> PositionWithoutFile
parseLastPos ParseState
s
, errInput :: String
errInput = ParseState -> String
parseInp ParseState
s
, errPrevToken :: String
errPrevToken = ParseState -> String
parsePrevToken ParseState
s
, errMsg :: String
errMsg = String
msg
}
parseWarning :: ParseWarning -> Parser ()
parseWarning :: ParseWarning -> Parser ()
parseWarning ParseWarning
w =
(ParseState -> ParseState) -> Parser ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' ((ParseState -> ParseState) -> Parser ())
-> (ParseState -> ParseState) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \ParseState
s -> ParseState
s { parseWarnings :: [ParseWarning]
parseWarnings = ParseWarning
w ParseWarning -> [ParseWarning] -> [ParseWarning]
forall a. a -> [a] -> [a]
: ParseState -> [ParseWarning]
parseWarnings ParseState
s }
instance Pretty ParseError where
pretty :: ParseError -> Doc
pretty ParseError{PositionWithoutFile
errPos :: PositionWithoutFile
errPos :: ParseError -> PositionWithoutFile
errPos,SrcFile
errSrcFile :: SrcFile
errSrcFile :: ParseError -> SrcFile
errSrcFile,String
errMsg :: String
errMsg :: ParseError -> String
errMsg,String
errPrevToken :: String
errPrevToken :: ParseError -> String
errPrevToken,String
errInput :: String
errInput :: ParseError -> String
errInput} = [Doc] -> Doc
forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
[ (Position' SrcFile -> Doc
forall a. Pretty a => a -> Doc
pretty (PositionWithoutFile
errPos { srcFile :: SrcFile
srcFile = SrcFile
errSrcFile }) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon) Doc -> Doc -> Doc
<+>
String -> Doc
text String
errMsg
, String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ String
errPrevToken String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"<ERROR>"
, String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ LexState -> ShowS
forall a. LexState -> [a] -> [a]
take LexState
30 String
errInput String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"..."
]
pretty OverlappingTokensError{Range' SrcFile
errRange :: Range' SrcFile
errRange :: ParseError -> Range' SrcFile
errRange} = [Doc] -> Doc
forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
[ (Range' SrcFile -> Doc
forall a. Pretty a => a -> Doc
pretty Range' SrcFile
errRange Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon) Doc -> Doc -> Doc
<+>
Doc
"Multi-line comment spans one or more literate text blocks."
]
pretty InvalidExtensionError{AbsolutePath
errPath :: AbsolutePath
errPath :: ParseError -> AbsolutePath
errPath,[String]
errValidExts :: [String]
errValidExts :: ParseError -> [String]
errValidExts} = [Doc] -> Doc
forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
[ (AbsolutePath -> Doc
forall a. Pretty a => a -> Doc
pretty AbsolutePath
errPath Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon) Doc -> Doc -> Doc
<+>
Doc
"Unsupported extension."
, Doc
"Supported extensions are:" Doc -> Doc -> Doc
<+> [String] -> Doc
forall a. Pretty a => [a] -> Doc
prettyList_ [String]
errValidExts
]
pretty ReadFileError{AbsolutePath
errPath :: AbsolutePath
errPath :: ParseError -> AbsolutePath
errPath,IOError
errIOError :: IOError
errIOError :: ParseError -> IOError
errIOError} = [Doc] -> Doc
forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
[ Doc
"Cannot read file" Doc -> Doc -> Doc
<+> AbsolutePath -> Doc
forall a. Pretty a => a -> Doc
pretty AbsolutePath
errPath
, Doc
"Error:" Doc -> Doc -> Doc
<+> String -> Doc
text (IOError -> String
forall e. Exception e => e -> String
displayException IOError
errIOError)
]
instance HasRange ParseError where
getRange :: ParseError -> Range' SrcFile
getRange ParseError
err = case ParseError
err of
ParseError{ SrcFile
errSrcFile :: SrcFile
errSrcFile :: ParseError -> SrcFile
errSrcFile, errPos :: ParseError -> PositionWithoutFile
errPos = PositionWithoutFile
p } -> SrcFile
-> PositionWithoutFile -> PositionWithoutFile -> Range' SrcFile
forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Range' a
posToRange' SrcFile
errSrcFile PositionWithoutFile
p PositionWithoutFile
p
OverlappingTokensError{ Range' SrcFile
errRange :: Range' SrcFile
errRange :: ParseError -> Range' SrcFile
errRange } -> Range' SrcFile
errRange
InvalidExtensionError{} -> Range' SrcFile
errPathRange
ReadFileError{} -> Range' SrcFile
errPathRange
where
errPathRange :: Range' SrcFile
errPathRange = Position' SrcFile -> Position' SrcFile -> Range' SrcFile
forall a. Position' a -> Position' a -> Range' a
posToRange Position' SrcFile
p Position' SrcFile
p
where p :: Position' SrcFile
p = Maybe AbsolutePath -> Position' SrcFile
startPos (Maybe AbsolutePath -> Position' SrcFile)
-> Maybe AbsolutePath -> Position' SrcFile
forall a b. (a -> b) -> a -> b
$ AbsolutePath -> Maybe AbsolutePath
forall a. a -> Maybe a
Just (AbsolutePath -> Maybe AbsolutePath)
-> AbsolutePath -> Maybe AbsolutePath
forall a b. (a -> b) -> a -> b
$ ParseError -> AbsolutePath
errPath ParseError
err
instance Pretty ParseWarning where
pretty :: ParseWarning -> Doc
pretty OverlappingTokensWarning{Range' SrcFile
warnRange :: Range' SrcFile
warnRange :: ParseWarning -> Range' SrcFile
warnRange} = [Doc] -> Doc
forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
[ (Range' SrcFile -> Doc
forall a. Pretty a => a -> Doc
pretty Range' SrcFile
warnRange Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon) Doc -> Doc -> Doc
<+>
Doc
"Multi-line comment spans one or more literate text blocks."
]
pretty (UnsupportedAttribute Range' SrcFile
r Maybe String
s) = [Doc] -> Doc
forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
[ (Range' SrcFile -> Doc
forall a. Pretty a => a -> Doc
pretty Range' SrcFile
r Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon) Doc -> Doc -> Doc
<+>
(case Maybe String
s of
Maybe String
Nothing -> Doc
"Attributes"
Just String
s -> String -> Doc
text String
s Doc -> Doc -> Doc
<+> Doc
"attributes") Doc -> Doc -> Doc
<+>
Doc
"are not supported here."
]
pretty (MultipleAttributes Range' SrcFile
r Maybe String
s) = [Doc] -> Doc
forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
[ (Range' SrcFile -> Doc
forall a. Pretty a => a -> Doc
pretty Range' SrcFile
r Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon) Doc -> Doc -> Doc
<+>
Doc
"Multiple" Doc -> Doc -> Doc
<+>
(Doc -> Doc)
-> (String -> Doc -> Doc) -> Maybe String -> Doc -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc -> Doc
forall a. a -> a
id (\String
s -> (String -> Doc
text String
s Doc -> Doc -> Doc
<+>)) Maybe String
s Doc
"attributes (ignored)."
]
instance HasRange ParseWarning where
getRange :: ParseWarning -> Range' SrcFile
getRange OverlappingTokensWarning{Range' SrcFile
warnRange :: Range' SrcFile
warnRange :: ParseWarning -> Range' SrcFile
warnRange} = Range' SrcFile
warnRange
getRange (UnsupportedAttribute Range' SrcFile
r Maybe String
_) = Range' SrcFile
r
getRange (MultipleAttributes Range' SrcFile
r Maybe String
_) = Range' SrcFile
r
initStatePos :: Position -> ParseFlags -> String -> [LexState] -> ParseState
initStatePos :: Position' SrcFile
-> ParseFlags -> String -> [LexState] -> ParseState
initStatePos Position' SrcFile
pos ParseFlags
flags String
inp [LexState]
st =
PState { parseSrcFile :: SrcFile
parseSrcFile = Position' SrcFile -> SrcFile
forall a. Position' a -> a
srcFile Position' SrcFile
pos
, parsePos :: PositionWithoutFile
parsePos = PositionWithoutFile
pos'
, parseLastPos :: PositionWithoutFile
parseLastPos = PositionWithoutFile
pos'
, parseInp :: String
parseInp = String
inp
, parsePrevChar :: Char
parsePrevChar = Char
'\n'
, parsePrevToken :: String
parsePrevToken = String
""
, parseLexState :: [LexState]
parseLexState = [LexState]
st
, parseLayout :: LayoutContext
parseLayout = []
, parseLayStatus :: LayoutStatus
parseLayStatus = LayoutStatus
Confirmed
, parseLayKw :: Keyword
parseLayKw = Keyword
KwMutual
, parseFlags :: ParseFlags
parseFlags = ParseFlags
flags
, parseWarnings :: [ParseWarning]
parseWarnings = []
}
where
pos' :: PositionWithoutFile
pos' = Position' SrcFile
pos { srcFile :: ()
srcFile = () }
initState :: Maybe AbsolutePath -> ParseFlags -> String -> [LexState]
-> ParseState
initState :: Maybe AbsolutePath
-> ParseFlags -> String -> [LexState] -> ParseState
initState Maybe AbsolutePath
file = Position' SrcFile
-> ParseFlags -> String -> [LexState] -> ParseState
initStatePos (Maybe AbsolutePath -> Position' SrcFile
startPos Maybe AbsolutePath
file)
defaultParseFlags :: ParseFlags
defaultParseFlags :: ParseFlags
defaultParseFlags = ParseFlags { parseKeepComments :: Bool
parseKeepComments = Bool
False }
parse :: ParseFlags -> [LexState] -> Parser a -> String -> ParseResult a
parse :: forall a.
ParseFlags -> [LexState] -> Parser a -> String -> ParseResult a
parse ParseFlags
flags [LexState]
st Parser a
p String
input = ParseFlags
-> [LexState] -> Parser a -> SrcFile -> String -> ParseResult a
forall a.
ParseFlags
-> [LexState] -> Parser a -> SrcFile -> String -> ParseResult a
parseFromSrc ParseFlags
flags [LexState]
st Parser a
p SrcFile
forall a. Maybe a
Strict.Nothing String
input
parsePosString :: Position -> ParseFlags -> [LexState] -> Parser a -> String ->
ParseResult a
parsePosString :: forall a.
Position' SrcFile
-> ParseFlags -> [LexState] -> Parser a -> String -> ParseResult a
parsePosString Position' SrcFile
pos ParseFlags
flags [LexState]
st Parser a
p String
input = Parser a -> ParseState -> ParseResult a
forall a. Parser a -> ParseState -> ParseResult a
unP Parser a
p (Position' SrcFile
-> ParseFlags -> String -> [LexState] -> ParseState
initStatePos Position' SrcFile
pos ParseFlags
flags String
input [LexState]
st)
parseFromSrc :: ParseFlags -> [LexState] -> Parser a -> SrcFile -> String
-> ParseResult a
parseFromSrc :: forall a.
ParseFlags
-> [LexState] -> Parser a -> SrcFile -> String -> ParseResult a
parseFromSrc ParseFlags
flags [LexState]
st Parser a
p SrcFile
src String
input = Parser a -> ParseState -> ParseResult a
forall a. Parser a -> ParseState -> ParseResult a
unP Parser a
p (Maybe AbsolutePath
-> ParseFlags -> String -> [LexState] -> ParseState
initState (SrcFile -> Maybe AbsolutePath
forall a. Maybe a -> Maybe a
Strict.toLazy SrcFile
src) ParseFlags
flags String
input [LexState]
st)
setParsePos :: PositionWithoutFile -> Parser ()
setParsePos :: PositionWithoutFile -> Parser ()
setParsePos PositionWithoutFile
p = (ParseState -> ParseState) -> Parser ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ParseState -> ParseState) -> Parser ())
-> (ParseState -> ParseState) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \ParseState
s -> ParseState
s { parsePos :: PositionWithoutFile
parsePos = PositionWithoutFile
p }
setLastPos :: PositionWithoutFile -> Parser ()
setLastPos :: PositionWithoutFile -> Parser ()
setLastPos PositionWithoutFile
p = (ParseState -> ParseState) -> Parser ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ParseState -> ParseState) -> Parser ())
-> (ParseState -> ParseState) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \ParseState
s -> ParseState
s { parseLastPos :: PositionWithoutFile
parseLastPos = PositionWithoutFile
p }
setPrevToken :: String -> Parser ()
setPrevToken :: String -> Parser ()
setPrevToken String
t = (ParseState -> ParseState) -> Parser ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ParseState -> ParseState) -> Parser ())
-> (ParseState -> ParseState) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \ParseState
s -> ParseState
s { parsePrevToken :: String
parsePrevToken = String
t }
getLastPos :: Parser PositionWithoutFile
getLastPos :: Parser PositionWithoutFile
getLastPos = (ParseState -> PositionWithoutFile) -> Parser PositionWithoutFile
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ParseState -> PositionWithoutFile
parseLastPos
getParseInterval :: Parser Interval
getParseInterval :: Parser Interval
getParseInterval = do
ParseState
s <- Parser ParseState
forall s (m :: * -> *). MonadState s m => m s
get
Interval -> Parser Interval
forall (m :: * -> *) a. Monad m => a -> m a
return (Interval -> Parser Interval) -> Interval -> Parser Interval
forall a b. (a -> b) -> a -> b
$ SrcFile -> PositionWithoutFile -> PositionWithoutFile -> Interval
forall a.
a -> PositionWithoutFile -> PositionWithoutFile -> Interval' a
posToInterval (ParseState -> SrcFile
parseSrcFile ParseState
s) (ParseState -> PositionWithoutFile
parseLastPos ParseState
s) (ParseState -> PositionWithoutFile
parsePos ParseState
s)
getLexState :: Parser [LexState]
getLexState :: Parser [LexState]
getLexState = (ParseState -> [LexState]) -> Parser [LexState]
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ParseState -> [LexState]
parseLexState
modifyLexState :: ([LexState] -> [LexState]) -> Parser ()
modifyLexState :: ([LexState] -> [LexState]) -> Parser ()
modifyLexState [LexState] -> [LexState]
f = (ParseState -> ParseState) -> Parser ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ParseState -> ParseState) -> Parser ())
-> (ParseState -> ParseState) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \ ParseState
s -> ParseState
s { parseLexState :: [LexState]
parseLexState = [LexState] -> [LexState]
f (ParseState -> [LexState]
parseLexState ParseState
s) }
pushLexState :: LexState -> Parser ()
pushLexState :: LexState -> Parser ()
pushLexState LexState
l = ([LexState] -> [LexState]) -> Parser ()
modifyLexState (LexState
lLexState -> [LexState] -> [LexState]
forall a. a -> [a] -> [a]
:)
popLexState :: Parser ()
popLexState :: Parser ()
popLexState = ([LexState] -> [LexState]) -> Parser ()
modifyLexState (([LexState] -> [LexState]) -> Parser ())
-> ([LexState] -> [LexState]) -> Parser ()
forall a b. (a -> b) -> a -> b
$ [LexState] -> [LexState] -> [LexState]
forall a. [a] -> [a] -> [a]
tailWithDefault [LexState]
forall a. HasCallStack => a
__IMPOSSIBLE__
getParseFlags :: Parser ParseFlags
getParseFlags :: Parser ParseFlags
getParseFlags = (ParseState -> ParseFlags) -> Parser ParseFlags
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ParseState -> ParseFlags
parseFlags
parseErrorAt :: PositionWithoutFile -> String -> Parser a
parseErrorAt :: forall a. PositionWithoutFile -> String -> Parser a
parseErrorAt PositionWithoutFile
p String
msg =
do PositionWithoutFile -> Parser ()
setLastPos PositionWithoutFile
p
String -> Parser a
forall a. String -> Parser a
parseError String
msg
parseError' :: Maybe PositionWithoutFile -> String -> Parser a
parseError' :: forall a. Maybe PositionWithoutFile -> String -> Parser a
parseError' = (String -> Parser a)
-> (PositionWithoutFile -> String -> Parser a)
-> Maybe PositionWithoutFile
-> String
-> Parser a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String -> Parser a
forall a. String -> Parser a
parseError PositionWithoutFile -> String -> Parser a
forall a. PositionWithoutFile -> String -> Parser a
parseErrorAt
parseErrorRange :: HasRange r => r -> String -> Parser a
parseErrorRange :: forall r a. HasRange r => r -> String -> Parser a
parseErrorRange = Maybe PositionWithoutFile -> String -> Parser a
forall a. Maybe PositionWithoutFile -> String -> Parser a
parseError' (Maybe PositionWithoutFile -> String -> Parser a)
-> (r -> Maybe PositionWithoutFile) -> r -> String -> Parser a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Range' SrcFile -> Maybe PositionWithoutFile
forall a. Range' a -> Maybe PositionWithoutFile
rStart' (Range' SrcFile -> Maybe PositionWithoutFile)
-> (r -> Range' SrcFile) -> r -> Maybe PositionWithoutFile
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Range' SrcFile
forall a. HasRange a => a -> Range' SrcFile
getRange
lexError :: String -> Parser a
lexError :: forall a. String -> Parser a
lexError String
msg =
do PositionWithoutFile
p <- (ParseState -> PositionWithoutFile) -> Parser PositionWithoutFile
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ParseState -> PositionWithoutFile
parsePos
PositionWithoutFile -> String -> Parser a
forall a. PositionWithoutFile -> String -> Parser a
parseErrorAt PositionWithoutFile
p String
msg
getContext :: MonadState ParseState m => m LayoutContext
getContext :: forall (m :: * -> *). MonadState ParseState m => m LayoutContext
getContext = (ParseState -> LayoutContext) -> m LayoutContext
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ParseState -> LayoutContext
parseLayout
setContext :: LayoutContext -> Parser ()
setContext :: LayoutContext -> Parser ()
setContext = (LayoutContext -> LayoutContext) -> Parser ()
modifyContext ((LayoutContext -> LayoutContext) -> Parser ())
-> (LayoutContext -> LayoutContext -> LayoutContext)
-> LayoutContext
-> Parser ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutContext -> LayoutContext -> LayoutContext
forall a b. a -> b -> a
const
modifyContext :: (LayoutContext -> LayoutContext) -> Parser ()
modifyContext :: (LayoutContext -> LayoutContext) -> Parser ()
modifyContext LayoutContext -> LayoutContext
f = (ParseState -> ParseState) -> Parser ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ParseState -> ParseState) -> Parser ())
-> (ParseState -> ParseState) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \ ParseState
s -> ParseState
s { parseLayout :: LayoutContext
parseLayout = LayoutContext -> LayoutContext
f (ParseState -> LayoutContext
parseLayout ParseState
s) }
topBlock :: Parser (Maybe LayoutBlock)
topBlock :: Parser (Maybe LayoutBlock)
topBlock = LayoutContext -> Maybe LayoutBlock
forall a. [a] -> Maybe a
listToMaybe (LayoutContext -> Maybe LayoutBlock)
-> Parser LayoutContext -> Parser (Maybe LayoutBlock)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser LayoutContext
forall (m :: * -> *). MonadState ParseState m => m LayoutContext
getContext
popBlock :: Parser ()
popBlock :: Parser ()
popBlock =
do LayoutContext
ctx <- Parser LayoutContext
forall (m :: * -> *). MonadState ParseState m => m LayoutContext
getContext
case LayoutContext
ctx of
[] -> String -> Parser ()
forall a. String -> Parser a
parseError String
"There is no layout block to close at this point."
LayoutBlock
_:LayoutContext
ctx -> LayoutContext -> Parser ()
setContext LayoutContext
ctx
pushBlock :: LayoutBlock -> Parser ()
pushBlock :: LayoutBlock -> Parser ()
pushBlock LayoutBlock
l = (LayoutContext -> LayoutContext) -> Parser ()
modifyContext (LayoutBlock
l LayoutBlock -> LayoutContext -> LayoutContext
forall a. a -> [a] -> [a]
:)
resetLayoutStatus :: Parser ()
resetLayoutStatus :: Parser ()
resetLayoutStatus = (ParseState -> ParseState) -> Parser ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ParseState -> ParseState) -> Parser ())
-> (ParseState -> ParseState) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \ ParseState
s -> ParseState
s { parseLayStatus :: LayoutStatus
parseLayStatus = LayoutStatus
Tentative }