1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 00:42:33 +03:00

File holds a Reference.

This commit is contained in:
Rob Rix 2021-07-28 14:47:38 -04:00
parent 2422a57215
commit 4115251fff
No known key found for this signature in database
GPG Key ID: 2BE643E01DC032AE
5 changed files with 36 additions and 15 deletions

View File

@ -8,7 +8,8 @@ module Analysis.Blob
, nullBlob , nullBlob
) where ) where
import Analysis.File import Analysis.File as A
import Analysis.Reference as A
import Data.Aeson import Data.Aeson
import Source.Language as Language import Source.Language as Language
import Source.Source as Source import Source.Source as Source
@ -34,13 +35,13 @@ instance FromJSON Blob where
-- The resulting Blob's span is taken from the 'totalSpan' of the source. -- The resulting Blob's span is taken from the 'totalSpan' of the source.
fromSource :: Path.PartClass.AbsRel ar => Path.File ar -> Language -> Source -> Blob fromSource :: Path.PartClass.AbsRel ar => Path.File ar -> Language -> Source -> Blob
fromSource filepath language source fromSource filepath language source
= Blob source (Analysis.File.File (Path.toAbsRel filepath) (totalSpan source) language) = Blob source (A.File (A.Reference (Path.toAbsRel filepath) (totalSpan source)) language)
blobLanguage :: Blob -> Language blobLanguage :: Blob -> Language
blobLanguage = Analysis.File.fileBody . blobFile blobLanguage = A.fileBody . blobFile
blobPath :: Blob -> Path.AbsRelFile blobPath :: Blob -> Path.AbsRelFile
blobPath = Analysis.File.filePath . blobFile blobPath = A.refPath . A.fileRef . blobFile
-- | Show FilePath for error or json outputs. -- | Show FilePath for error or json outputs.
blobFilePath :: Blob -> String blobFilePath :: Blob -> String

View File

@ -148,7 +148,7 @@ runFile
-> File term -> File term
-> m (File (Either (Reference, String) Concrete)) -> m (File (Either (Reference, String) Concrete))
runFile eval file = traverse run file runFile eval file = traverse run file
where run = runReader (Reference (filePath file) (fileSpan file)) where run = runReader (fileRef file)
. runFail . runFail
. runReader @Env mempty . runReader @Env mempty
. A.runEnv . A.runEnv

View File

@ -1,11 +1,15 @@
{-# LANGUAGE DeriveTraversable #-} {-# LANGUAGE DeriveTraversable #-}
module Analysis.File module Analysis.File
( File(..) ( -- * Files
, fileLanguage File(..)
-- * Constructors
, fromBody , fromBody
, fromPath , fromPath
-- * Eliminators
, fileLanguage
) where ) where
import qualified Analysis.Reference as A
import Data.Maybe (fromJust, listToMaybe) import Data.Maybe (fromJust, listToMaybe)
import GHC.Stack import GHC.Stack
import Source.Language as Language import Source.Language as Language
@ -13,20 +17,27 @@ import Source.Span
import qualified System.Path as Path import qualified System.Path as Path
import qualified System.Path.PartClass as Path.PartClass import qualified System.Path.PartClass as Path.PartClass
-- Files
data File a = File data File a = File
{ filePath :: !Path.AbsRelFile { fileRef :: !A.Reference
, fileSpan :: Span
, fileBody :: !a , fileBody :: !a
} }
deriving (Eq, Foldable, Functor, Ord, Show, Traversable) deriving (Eq, Foldable, Functor, Ord, Show, Traversable)
-- Constructors
fromBody :: HasCallStack => a -> File a fromBody :: HasCallStack => a -> File a
fromBody body = File (Path.absRel (srcLocFile srcLoc)) (spanFromSrcLoc srcLoc) body where fromBody body = File (A.Reference (Path.absRel (srcLocFile srcLoc)) (spanFromSrcLoc srcLoc)) body where
srcLoc = snd (fromJust (listToMaybe (getCallStack callStack))) srcLoc = snd (fromJust (listToMaybe (getCallStack callStack)))
fromPath :: Path.PartClass.AbsRel ar => Path.File ar -> File Language
fromPath p = File (A.fromPath p) (Language.forPath p)
-- Eliminators
-- | The language of the provided file, as inferred by 'Language.forPath'. -- | The language of the provided file, as inferred by 'Language.forPath'.
fileLanguage :: File a -> Language fileLanguage :: File a -> Language
fileLanguage = Language.forPath . filePath fileLanguage = Language.forPath . A.refPath . fileRef
fromPath :: Path.PartClass.AbsRel ar => Path.File ar -> File Language
fromPath p = File (Path.toAbsRel p) (point (Pos 0 0)) (Language.forPath p)

View File

@ -1,10 +1,13 @@
module Analysis.Reference module Analysis.Reference
( -- * Reference ( -- * Reference
Reference(..) Reference(..)
-- * Constructors
, fromPath
) where ) where
import Source.Span import Source.Span
import System.Path as Path import System.Path as Path
import System.Path.PartClass as Path.PartClass
-- Reference -- Reference
@ -14,3 +17,9 @@ data Reference = Reference
} }
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
-- FIXME: add this to some sort of static context carried in analyses -- FIXME: add this to some sort of static context carried in analyses
-- Constructors
fromPath :: Path.PartClass.AbsRel ar => Path.File ar -> Reference
fromPath p = Reference (Path.toAbsRel p) (point (Pos 0 0))

View File

@ -148,7 +148,7 @@ runFile eval file = traverse run file
modify @(Heap Type) (fmap (Set.map (substAll subst))) modify @(Heap Type) (fmap (Set.map (substAll subst)))
pure (substAll subst <$> t)) pure (substAll subst <$> t))
. runState @Substitution mempty . runState @Substitution mempty
. runReader (Reference (filePath file) (fileSpan file)) . runReader (fileRef file)
. runEnv . runEnv
. runFail . runFail
. (\ m -> do . (\ m -> do