rc #13
@@ -18,6 +18,7 @@ module Compiler.RLPC
|
|||||||
, RLPCOptions(RLPCOptions)
|
, RLPCOptions(RLPCOptions)
|
||||||
, IsRlpcError(..)
|
, IsRlpcError(..)
|
||||||
, RlpcError(..)
|
, RlpcError(..)
|
||||||
|
, MsgEnvelope(..)
|
||||||
, addFatal
|
, addFatal
|
||||||
, addWound
|
, addWound
|
||||||
, MonadErrorful
|
, MonadErrorful
|
||||||
|
|||||||
@@ -3,13 +3,14 @@
|
|||||||
module Compiler.RlpcError
|
module Compiler.RlpcError
|
||||||
( IsRlpcError(..)
|
( IsRlpcError(..)
|
||||||
, MsgEnvelope(..)
|
, MsgEnvelope(..)
|
||||||
, Severity
|
, Severity(..)
|
||||||
, RlpcError(..)
|
, RlpcError(..)
|
||||||
, SrcSpan(..)
|
, SrcSpan(..)
|
||||||
, msgSpan
|
, msgSpan
|
||||||
, msgDiagnostic
|
, msgDiagnostic
|
||||||
, msgSeverity
|
, msgSeverity
|
||||||
, liftRlpcErrors
|
, liftRlpcErrors
|
||||||
|
, errorMsg
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
@@ -60,3 +61,10 @@ liftRlpcErrors = mapErrorful liftRlpcError
|
|||||||
instance (IsRlpcError e) => IsRlpcError (MsgEnvelope e) where
|
instance (IsRlpcError e) => IsRlpcError (MsgEnvelope e) where
|
||||||
liftRlpcError msg = msg ^. msgDiagnostic & liftRlpcError
|
liftRlpcError msg = msg ^. msgDiagnostic & liftRlpcError
|
||||||
|
|
||||||
|
errorMsg :: SrcSpan -> e -> MsgEnvelope e
|
||||||
|
errorMsg s e = MsgEnvelope
|
||||||
|
{ _msgSpan = s
|
||||||
|
, _msgDiagnostic = e
|
||||||
|
, _msgSeverity = SevError
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ $asciisym = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~\:]
|
|||||||
|
|
||||||
@reservedname =
|
@reservedname =
|
||||||
case|data|do|import|in|let|letrec|module|of|where
|
case|data|do|import|in|let|letrec|module|of|where
|
||||||
|
|infixr|infixl|infix
|
||||||
|
|
||||||
@reservedop =
|
@reservedop =
|
||||||
"=" | \\ | "->" | "|"
|
"=" | \\ | "->" | "|"
|
||||||
@@ -125,6 +126,9 @@ lexReservedName = \case
|
|||||||
"of" -> TokenOf
|
"of" -> TokenOf
|
||||||
"let" -> TokenLet
|
"let" -> TokenLet
|
||||||
"in" -> TokenIn
|
"in" -> TokenIn
|
||||||
|
"infix" -> TokenInfix
|
||||||
|
"infixl" -> TokenInfixL
|
||||||
|
"infixr" -> TokenInfixR
|
||||||
|
|
||||||
lexReservedOp :: Text -> RlpToken
|
lexReservedOp :: Text -> RlpToken
|
||||||
lexReservedOp = \case
|
lexReservedOp = \case
|
||||||
@@ -223,7 +227,7 @@ initAlexInput s = AlexInput
|
|||||||
, _aiPos = (1,1)
|
, _aiPos = (1,1)
|
||||||
}
|
}
|
||||||
|
|
||||||
runP' :: P a -> Text -> (ParseState, [RlpParseError], Maybe a)
|
runP' :: P a -> Text -> (ParseState, [MsgEnvelope RlpParseError], Maybe a)
|
||||||
runP' p s = runP p st where
|
runP' p s = runP p st where
|
||||||
st = initParseState s
|
st = initParseState s
|
||||||
|
|
||||||
@@ -241,7 +245,7 @@ lexToken = do
|
|||||||
AlexToken inp' l act -> do
|
AlexToken inp' l act -> do
|
||||||
psInput .= inp'
|
psInput .= inp'
|
||||||
act inp l
|
act inp l
|
||||||
AlexError inp' -> addFatal RlpParErrLexical
|
AlexError inp' -> addFatalHere 1 RlpParErrLexical
|
||||||
|
|
||||||
lexCont :: (Located RlpToken -> P a) -> P a
|
lexCont :: (Located RlpToken -> P a) -> P a
|
||||||
lexCont = (lexToken >>=)
|
lexCont = (lexToken >>=)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ module Rlp.Parse
|
|||||||
( parseRlpProg
|
( parseRlpProg
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
import Compiler.RlpcError
|
||||||
import Rlp.Lex
|
import Rlp.Lex
|
||||||
import Rlp.Syntax
|
import Rlp.Syntax
|
||||||
import Rlp.Parse.Types
|
import Rlp.Parse.Types
|
||||||
@@ -14,6 +15,7 @@ import Lens.Micro.Platform ()
|
|||||||
import Data.List.Extra
|
import Data.List.Extra
|
||||||
import Data.Fix
|
import Data.Fix
|
||||||
import Data.Functor.Const
|
import Data.Functor.Const
|
||||||
|
import Data.Text qualified as T
|
||||||
}
|
}
|
||||||
|
|
||||||
%name parseRlpProg StandaloneProgram
|
%name parseRlpProg StandaloneProgram
|
||||||
@@ -161,16 +163,19 @@ mkProgram ds = do
|
|||||||
pure $ RlpProgram (associate pt <$> ds)
|
pure $ RlpProgram (associate pt <$> ds)
|
||||||
|
|
||||||
parseError :: Located RlpToken -> P a
|
parseError :: Located RlpToken -> P a
|
||||||
parseError (Located ((l,c),s) t) = addFatal RlpParErrUnexpectedToken
|
parseError (Located ((l,c),s) t) = addFatal $
|
||||||
|
errorMsg (SrcSpan l c s) RlpParErrUnexpectedToken
|
||||||
|
|
||||||
mkInfixD :: Assoc -> Int -> Name -> P PartialDecl'
|
mkInfixD :: Assoc -> Int -> Name -> P PartialDecl'
|
||||||
mkInfixD a p n = do
|
mkInfixD a p n = do
|
||||||
let opl :: Lens' ParseState (Maybe OpInfo)
|
let opl :: Lens' ParseState (Maybe OpInfo)
|
||||||
opl = psOpTable . at n
|
opl = psOpTable . at n
|
||||||
opl <~ (use opl >>= \case
|
opl <~ (use opl >>= \case
|
||||||
-- TODO: non-fatal error
|
Just o -> addWoundHere l e >> pure (Just o) where
|
||||||
Just o -> pure (Just o)
|
e = RlpParErrDuplicateInfixD n
|
||||||
|
l = T.length n
|
||||||
Nothing -> pure (Just (a,p))
|
Nothing -> pure (Just (a,p))
|
||||||
)
|
)
|
||||||
pure $ InfixD a p n
|
pure $ InfixD a p n
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
{-# LANGUAGE LambdaCase #-}
|
{-# LANGUAGE LambdaCase #-}
|
||||||
module Rlp.Parse.Types
|
module Rlp.Parse.Types
|
||||||
( LexerAction
|
( LexerAction
|
||||||
|
, MsgEnvelope(..)
|
||||||
|
, RlpcError(..)
|
||||||
, AlexInput(..)
|
, AlexInput(..)
|
||||||
, Position(..)
|
, Position(..)
|
||||||
, RlpToken(..)
|
, RlpToken(..)
|
||||||
@@ -30,6 +32,8 @@ module Rlp.Parse.Types
|
|||||||
, aiPos
|
, aiPos
|
||||||
, addFatal
|
, addFatal
|
||||||
, addWound
|
, addWound
|
||||||
|
, addFatalHere
|
||||||
|
, addWoundHere
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
@@ -66,6 +70,12 @@ type Position =
|
|||||||
, Int -- column
|
, Int -- column
|
||||||
)
|
)
|
||||||
|
|
||||||
|
posLine :: Lens' Position Int
|
||||||
|
posLine = _1
|
||||||
|
|
||||||
|
posColumn :: Lens' Position Int
|
||||||
|
posColumn = _2
|
||||||
|
|
||||||
data RlpToken
|
data RlpToken
|
||||||
-- literals
|
-- literals
|
||||||
= TokenLitInt Int
|
= TokenLitInt Int
|
||||||
@@ -103,7 +113,10 @@ data RlpToken
|
|||||||
| TokenEOF
|
| TokenEOF
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
newtype P a = P { runP :: ParseState -> (ParseState, [RlpParseError], Maybe a) }
|
newtype P a = P {
|
||||||
|
runP :: ParseState
|
||||||
|
-> (ParseState, [MsgEnvelope RlpParseError], Maybe a)
|
||||||
|
}
|
||||||
deriving (Functor)
|
deriving (Functor)
|
||||||
|
|
||||||
instance Applicative P where
|
instance Applicative P where
|
||||||
@@ -125,7 +138,7 @@ instance MonadState ParseState P where
|
|||||||
let (a,st') = f st
|
let (a,st') = f st
|
||||||
in (st', [], Just a)
|
in (st', [], Just a)
|
||||||
|
|
||||||
instance MonadErrorful RlpParseError P where
|
instance MonadErrorful (MsgEnvelope RlpParseError) P where
|
||||||
addWound e = P $ \st -> (st, [e], Just ())
|
addWound e = P $ \st -> (st, [e], Just ())
|
||||||
addFatal e = P $ \st -> (st, [e], Nothing)
|
addFatal e = P $ \st -> (st, [e], Nothing)
|
||||||
|
|
||||||
@@ -150,7 +163,7 @@ type OpInfo = (Assoc, Int)
|
|||||||
-- data WithLocation a = WithLocation [String] a
|
-- data WithLocation a = WithLocation [String] a
|
||||||
|
|
||||||
data RlpParseError = RlpParErrOutOfBoundsPrecedence Int
|
data RlpParseError = RlpParErrOutOfBoundsPrecedence Int
|
||||||
| RlpParErrDuplicateInfixD
|
| RlpParErrDuplicateInfixD Name
|
||||||
| RlpParErrLexical
|
| RlpParErrLexical
|
||||||
| RlpParErrUnexpectedToken
|
| RlpParErrUnexpectedToken
|
||||||
deriving (Eq, Ord, Show)
|
deriving (Eq, Ord, Show)
|
||||||
@@ -158,7 +171,6 @@ data RlpParseError = RlpParErrOutOfBoundsPrecedence Int
|
|||||||
instance IsRlpcError RlpParseError where
|
instance IsRlpcError RlpParseError where
|
||||||
|
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- absolute psycho shit (partial ASTs)
|
-- absolute psycho shit (partial ASTs)
|
||||||
|
|
||||||
type PartialDecl' = Decl (Const PartialExpr') Name
|
type PartialDecl' = Decl (Const PartialExpr') Name
|
||||||
@@ -204,3 +216,27 @@ type PartialExpr' = Fix Partial
|
|||||||
makeLenses ''AlexInput
|
makeLenses ''AlexInput
|
||||||
makeLenses ''ParseState
|
makeLenses ''ParseState
|
||||||
|
|
||||||
|
addWoundHere :: Int -> RlpParseError -> P ()
|
||||||
|
addWoundHere l e = P $ \st ->
|
||||||
|
let e' = MsgEnvelope
|
||||||
|
{ _msgSpan = let pos = psInput . aiPos
|
||||||
|
in SrcSpan (st ^. pos . posLine)
|
||||||
|
(st ^. pos . posColumn)
|
||||||
|
l
|
||||||
|
, _msgDiagnostic = e
|
||||||
|
, _msgSeverity = SevError
|
||||||
|
}
|
||||||
|
in (st, [e'], Just ())
|
||||||
|
|
||||||
|
addFatalHere :: Int -> RlpParseError -> P a
|
||||||
|
addFatalHere l e = P $ \st ->
|
||||||
|
let e' = MsgEnvelope
|
||||||
|
{ _msgSpan = let pos = psInput . aiPos
|
||||||
|
in SrcSpan (st ^. pos . posLine)
|
||||||
|
(st ^. pos . posColumn)
|
||||||
|
l
|
||||||
|
, _msgDiagnostic = e
|
||||||
|
, _msgSeverity = SevError
|
||||||
|
}
|
||||||
|
in (st, [e'], Nothing)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user