HasLocation

HasLocation
This commit is contained in:
crumbtoo
2024-02-16 17:22:24 -07:00
parent 953086d751
commit 709123d68e
4 changed files with 77 additions and 42 deletions

View File

@@ -1,12 +1,16 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE UndecidableInstances, QuantifiedConstraints #-}
module Compiler.Types module Compiler.Types
( SrcSpan(..) ( SrcSpan(..)
, srcSpanLine, srcSpanColumn, srcSpanAbs, srcSpanLen , srcSpanLine, srcSpanColumn, srcSpanAbs, srcSpanLen
, Located(..) , Located(..)
, HasLocation(location)
, _Located , _Located
, located
, nolo , nolo
, (<~>), (~>)
-- * Re-exports -- * Re-exports
, Comonad , Comonad
, Apply , Apply
@@ -14,19 +18,38 @@ module Compiler.Types
) )
where where
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Language.Haskell.TH.Syntax (Lift)
import Control.Comonad import Control.Comonad
import Control.Comonad.Cofree
import Data.Functor.Apply import Data.Functor.Apply
import Data.Functor.Bind import Data.Functor.Bind
import Data.Semigroup.Foldable
import Data.Kind
import Control.Lens hiding ((<<~)) import Control.Lens hiding ((<<~))
import Language.Haskell.TH.Syntax (Lift)
import Data.List.NonEmpty (NonEmpty)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- | Token wrapped with a span (line, column, absolute, length) -- | Token wrapped with a span (line, column, absolute, length)
data Located a = Located SrcSpan a data Located a = Located SrcSpan a
deriving (Show, Lift, Functor) deriving (Show, Lift, Functor)
located :: Lens (Located a) (Located b) a b class GetLocation s where
located = lens extract ($>) srcspan :: s -> SrcSpan
class HasLocation s where
location :: Lens' s SrcSpan
(<~>) :: a -> b -> SrcSpan
(<~>) = undefined
infixl 5 <~>
(~>) :: a -> b -> b
(~>) = undefined
infixl 4 ~>
instance Apply Located where instance Apply Located where
liftF2 f (Located sa p) (Located sb q) liftF2 f (Located sa p) (Located sb q)
@@ -78,3 +101,22 @@ instance Semigroup SrcSpan where
makePrisms ''Located makePrisms ''Located
--------------------------------------------------------------------------------
instance (GetLocation a) => GetLocation (NonEmpty a) where
srcspan = foldMap1 srcspan
instance GetLocation SrcSpan where
srcspan = id
instance (Functor f) => GetLocation (Cofree f SrcSpan) where
srcspan = extract
--------------------------------------------------------------------------------
instance HasLocation SrcSpan where
location = id
instance (Functor f) => HasLocation (Cofree f SrcSpan) where
location = _extract

View File

@@ -72,32 +72,21 @@ import Compiler.Types
%% %%
StandaloneProgram :: { Program RlpcPs } StandaloneProgram :: { Program RlpcPs }
StandaloneProgram : '{' Decls '}' { undefined } StandaloneProgram : layout0(Decl) { Program $1 }
| VL DeclsV VR { undefined }
StandaloneExpr :: { Expr RlpcPs } StandaloneExpr :: { Expr RlpcPs }
: VL Expr VR { undefined } : VL Expr VR { $2 }
VL :: { () } VL :: { () }
VL : vlbrace { undefined } VL : vlbrace { () }
VR :: { () } VR :: { () }
VR : vrbrace { undefined } VR : vrbrace { () }
| error { undefined } | error { () }
Decls :: { [Decl RlpcPs] } VS :: { () }
Decls : Decl ';' Decls { undefined } VS : ';' { () }
| Decl ';' { undefined } | vsemi { () }
| Decl { undefined }
DeclsV :: { [Decl RlpcPs] }
DeclsV : Decl VS DeclsV { undefined }
| Decl VS { undefined }
| Decl { undefined }
VS :: { Located RlpToken }
VS : ';' { undefined }
| vsemi { undefined }
Decl :: { Decl RlpcPs } Decl :: { Decl RlpcPs }
: FunDecl { undefined } : FunDecl { undefined }
@@ -148,7 +137,7 @@ TypeApp :: { Ty RlpcPs }
| TypeApp Type1 { undefined } | TypeApp Type1 { undefined }
FunDecl :: { Decl RlpcPs } FunDecl :: { Decl RlpcPs }
FunDecl : Var Params '=' Expr { undefined } FunDecl : Var Params '=' Expr { FunD $1 $2 $4 Nothing }
Params :: { [Pat RlpcPs] } Params :: { [Pat RlpcPs] }
Params : {- epsilon -} { undefined } Params : {- epsilon -} { undefined }
@@ -199,21 +188,21 @@ Alt :: { Alt RlpcPs }
: Pat '->' Expr { undefined } : Pat '->' Expr { undefined }
-- layout0(p : β) :: [β] -- layout0(p : β) :: [β]
layout0(p) : '{' layout_list0(';',p) '}' { undefined } layout0(p) : '{' layout_list0(';',p) '}' { $2 }
| VL layout_list0(VS,p) VR { undefined } | VL layout_list0(VS,p) VR { $2 }
-- layout_list0(sep : α, p : β) :: [β] -- layout_list0(sep : α, p : β) :: [β]
layout_list0(sep,p) : p { undefined } layout_list0(sep,p) : p { [$1] }
| layout_list1(sep,p) sep p { undefined } | layout_list1(sep,p) sep p { $1 `snoc` $3 }
| {- epsilon -} { undefined } | {- epsilon -} { [] }
-- layout1(p : β) :: [β] -- layout1(p : β) :: [β]
layout1(p) : '{' layout_list1(';',p) '}' { undefined } layout1(p) : '{' layout_list1(';',p) '}' { $2 }
| VL layout_list1(VS,p) VR { undefined } | VL layout_list1(VS,p) VR { $2 }
-- layout_list1(sep : α, p : β) :: [β] -- layout_list1(sep : α, p : β) :: [β]
layout_list1(sep,p) : p { undefined } layout_list1(sep,p) : p { [$1] }
| layout_list1(sep,p) sep p { undefined } | layout_list1(sep,p) sep p { $1 `snoc` $3 }
Binding :: { Binding RlpcPs } Binding :: { Binding RlpcPs }
: Pat '=' Expr { undefined } : Pat '=' Expr { undefined }
@@ -230,17 +219,17 @@ InfixOp :: { Located PsName }
-- TODO: microlens-pro save me microlens-pro (rewrite this with prisms) -- TODO: microlens-pro save me microlens-pro (rewrite this with prisms)
Lit :: { Lit RlpcPs } Lit :: { Lit RlpcPs }
: litint { undefined } : litint { $1 ^. to extract
. singular _TokenLitInt
. to IntL }
Var :: { Located PsName } Var :: { PsName }
Var : varname { undefined } Var : varname { undefined }
| varsym { undefined } | varsym { undefined }
Con :: { Located PsName } Con :: { PsName }
: conname { undefined } : conname { undefined }
--}
{ {
parseRlpProgR = undefined parseRlpProgR = undefined

View File

@@ -17,7 +17,7 @@ module Rlp.Parse.Types
, RlpToken(..), AlexInput(..), Position(..), spanFromPos, LexerAction , RlpToken(..), AlexInput(..), Position(..), spanFromPos, LexerAction
, Located(..), PsName , Located(..), PsName
-- ** Lenses -- ** Lenses
, aiPrevChar, aiSource, aiBytes, aiPos, posLine, posColumn , _TokenLitInt, aiPrevChar, aiSource, aiBytes, aiPos, posLine, posColumn
-- * Error handling -- * Error handling
, MsgEnvelope(..), RlpcError(..), RlpParseError(..) , MsgEnvelope(..), RlpcError(..), RlpParseError(..)
@@ -53,7 +53,7 @@ data RlpcPs
type instance NameP RlpcPs = PsName type instance NameP RlpcPs = PsName
type PsName = Text type PsName = Located Text
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@@ -125,6 +125,11 @@ data RlpToken
| TokenEOF | TokenEOF
deriving (Show) deriving (Show)
_TokenLitInt :: Prism' RlpToken Int
_TokenLitInt = prism TokenLitInt $ \case
TokenLitInt n -> Right n
x -> Left x
newtype P a = P { newtype P a = P {
runP :: ParseState runP :: ParseState
-> (ParseState, [MsgEnvelope RlpParseError], Maybe a) -> (ParseState, [MsgEnvelope RlpParseError], Maybe a)

View File

@@ -40,12 +40,11 @@ import Control.Comonad.Cofree
import Data.Functor.Foldable import Data.Functor.Foldable
import Data.Functor.Foldable.TH (makeBaseFunctor) import Data.Functor.Foldable.TH (makeBaseFunctor)
import Compiler.Types (SrcSpan(..)) import Compiler.Types (SrcSpan(..), Located(..))
import Core.Syntax qualified as Core import Core.Syntax qualified as Core
import Core (Rec(..), HasRHS(..), HasLHS(..)) import Core (Rec(..), HasRHS(..), HasLHS(..))
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
type PsName = Text
type family NameP p type family NameP p
data Program p = Program data Program p = Program