diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2024-10-03 18:23:50 -0600 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2024-10-03 18:23:50 -0600 |
commit | 407e41489cc22fbf0518fd370530f8857b8c3ed0 (patch) | |
tree | 8c5f3fceb7c9e083033e06c818556eba1dcf9a06 | |
parent | 72eeba5fd6178409b4aab5eb8dbfaf4460f6841c (diff) | |
download | fiddle-407e41489cc22fbf0518fd370530f8857b8c3ed0.tar.gz fiddle-407e41489cc22fbf0518fd370530f8857b8c3ed0.tar.bz2 fiddle-407e41489cc22fbf0518fd370530f8857b8c3ed0.zip |
Clean up warnings and remove unused files.
22 files changed, 137 insertions, 247 deletions
diff --git a/package.yaml b/package.yaml index 62bbdf8..8ea485f 100644 --- a/package.yaml +++ b/package.yaml @@ -33,6 +33,8 @@ ghc-options: - -XViewPatterns - -Wall - -fno-warn-orphans + - -fno-warn-name-shadowing + - -fno-warn-missing-local-signatures dependencies: - base >= 4.0.0 diff --git a/src/Language/Fiddle/Ast.hs b/src/Language/Fiddle/Ast.hs index 7ef12da..a6ea87a 100644 --- a/src/Language/Fiddle/Ast.hs +++ b/src/Language/Fiddle/Ast.hs @@ -4,20 +4,6 @@ module Language.Fiddle.Ast (module X) where -import Data.Coerce -import Data.Functor.Identity -import Data.Kind (Type) -import Data.List.NonEmpty -import Data.Proxy -import Data.Text (Text) -import Data.Traversable -import Data.Type.Bool -import Data.Type.Equality -import Data.Typeable -import Data.Void (Void, absurd) -import GHC.Generics -import qualified GHC.TypeError as TypeError -import GHC.TypeLits import Language.Fiddle.Ast.Internal.Instances as X import Language.Fiddle.Ast.Internal.Kinds as X import Language.Fiddle.Ast.Internal.Stage as X diff --git a/src/Language/Fiddle/Ast/FileInterface.hs b/src/Language/Fiddle/Ast/FileInterface.hs deleted file mode 100644 index c1cfac8..0000000 --- a/src/Language/Fiddle/Ast/FileInterface.hs +++ /dev/null @@ -1,38 +0,0 @@ -module Language.Fiddle.Ast.FileInterface where - --- Definitions for file interfaces. These interfaces contain an abstract --- representation of the symbols and information exported by a fiddle file. --- These interfaces are also serializable, and when compiling a fiddle file, all --- the import statements should supply an fdi (fiddle interface) file to speed --- up subsequent compilations. - -import Data.Text -import Data.Word - -data ObjectType = ObjectType - { objectTypeSize :: Word32 - } - -data Metatype - = Object - { objectLocation :: Word64, - objectType :: Text - } - | Type - { typeSizeBytes :: Word32 - } - -data Element a = Element - { elementFullyQualifiedSymbol :: Text, - elementDocumentation :: Maybe Text, - elementMetatype :: Metatype, - elementAnnotation :: a - } - -data ResolvedImport a = ResolvedImport { - dependencies :: [String] -} - -data FileInterface a = FiddleInterface - { exportedElements :: [Element a] - } diff --git a/src/Language/Fiddle/Ast/Internal/Generic.hs b/src/Language/Fiddle/Ast/Internal/Generic.hs deleted file mode 100644 index cb075cc..0000000 --- a/src/Language/Fiddle/Ast/Internal/Generic.hs +++ /dev/null @@ -1,5 +0,0 @@ -{-# LANGUAGE TypeOperators #-} -module Language.Fiddle.Ast.Internal.Generic where - -import GHC.Generics -import GHC.TypeError as TypeError diff --git a/src/Language/Fiddle/Ast/Internal/Instances.hs b/src/Language/Fiddle/Ast/Internal/Instances.hs index 3380ccd..232d5c0 100644 --- a/src/Language/Fiddle/Ast/Internal/Instances.hs +++ b/src/Language/Fiddle/Ast/Internal/Instances.hs @@ -1,5 +1,3 @@ -{-# LANGUAGE FunctionalDependencies #-} - module Language.Fiddle.Ast.Internal.Instances ( module X, Alter (..), @@ -7,18 +5,14 @@ module Language.Fiddle.Ast.Internal.Instances CompilationStage (..), Annotated (..), GAnnot (..), - TreeType (..), + TreeType, ) where import Data.Functor.Identity import Data.Kind -import Data.Type.Bool -import Data.Type.Equality import Data.Typeable import GHC.Generics -import GHC.TypeError as TypeError -import GHC.TypeLits import Language.Fiddle.Ast.Internal.Instances.Walk as X import Language.Fiddle.Ast.Internal.Kinds import Language.Fiddle.Ast.Internal.Stage @@ -312,7 +306,7 @@ instance (Traversable f1, Alter u) => GAlter t f1 f2 a1 a2 (Rec0 (f1 (u f1 a1))) (Rec0 (f2 (u f2 a2))) where - galter proxy ffn fn k1 = do + galter _ ffn fn k1 = do newK <- mapM (alter ffn fn) (unK1 k1) K1 <$> ffn newK @@ -419,7 +413,7 @@ instance ) => GAdvanceStage stage s m (Rec0 (t' stage f a)) (Rec0 (t' stage' f a)) where - gAdvanceStage pxy st (K1 val) = K1 <$> advanceStage st val + gAdvanceStage _ st (K1 val) = K1 <$> advanceStage st val -- | 'GAdvanceStage' instance for record fields ('Rec0') containing a functor -- ('func') of AST elements ('t'). This handles cases where the field is a @@ -438,7 +432,7 @@ instance ) => GAdvanceStage stage s m (Rec0 (func (t' stage f a))) (Rec0 (func (t' stage' f a))) where - gAdvanceStage pxy st (K1 val) = K1 <$> mapM (advanceStage st) val + gAdvanceStage _ st (K1 val) = K1 <$> mapM (advanceStage st) val -- | 'GAdvanceStage' instance for record fields ('Rec0') containing a -- functor ('f') wrapping an AST element. This handles cases where the field @@ -456,14 +450,14 @@ instance ) => GAdvanceStage stage s m (Rec0 (f (t' stage f a))) (Rec0 (f (t' stage' f a))) where - gAdvanceStage pxy st (K1 val) = K1 <$> mapM (advanceStage st) val + gAdvanceStage _ st (K1 val) = K1 <$> mapM (advanceStage st) val -- | 'GAdvanceStage' instance for simple record fields ('Rec0') that do not -- need to change between stages. This is used for fields that are not AST -- nodes and remain the same when advancing the stage (e.g., primitive -- types like 'Int', 'Bool', etc.). instance (Monad m) => GAdvanceStage stage s m (Rec0 a) (Rec0 a) where - gAdvanceStage pxy _ (K1 val) = return (K1 val) + gAdvanceStage _ _ (K1 val) = return (K1 val) -- | 'GAdvanceStage' instance for records which can be converted to eathother -- for the current stage.. diff --git a/src/Language/Fiddle/Ast/Internal/Instances/Walk.hs b/src/Language/Fiddle/Ast/Internal/Instances/Walk.hs index 6feaff3..d80963d 100644 --- a/src/Language/Fiddle/Ast/Internal/Instances/Walk.hs +++ b/src/Language/Fiddle/Ast/Internal/Instances/Walk.hs @@ -1,6 +1,5 @@ module Language.Fiddle.Ast.Internal.Instances.Walk (Walk (..), GWalk (..)) where -import Data.Foldable (foldlM) import Data.Typeable import GHC.Generics diff --git a/src/Language/Fiddle/Ast/Internal/Stage.hs b/src/Language/Fiddle/Ast/Internal/Stage.hs index f175fc4..985dfae 100644 --- a/src/Language/Fiddle/Ast/Internal/Stage.hs +++ b/src/Language/Fiddle/Ast/Internal/Stage.hs @@ -6,10 +6,8 @@ module Language.Fiddle.Ast.Internal.Stage where -import Data.Type.Bool import Data.Type.Equality import Data.Typeable -import qualified GHC.TypeError as TypeError import GHC.TypeLits -- | Represents the different stages of the compilation process. diff --git a/src/Language/Fiddle/Ast/Internal/SyntaxTree.hs b/src/Language/Fiddle/Ast/Internal/SyntaxTree.hs index 6aa0793..8e3cd6c 100644 --- a/src/Language/Fiddle/Ast/Internal/SyntaxTree.hs +++ b/src/Language/Fiddle/Ast/Internal/SyntaxTree.hs @@ -55,25 +55,19 @@ module Language.Fiddle.Ast.Internal.SyntaxTree where import Control.Monad (forM_) -import Data.Coerce import Data.Functor.Identity import Data.Kind (Type) import Data.List.NonEmpty hiding (map) -import Data.Proxy import Data.Text (Text) -import Data.Traversable +import qualified Data.Text as Text import Data.Type.Bool -import Data.Type.Equality import Data.Typeable -import Data.Void (Void, absurd) +import Data.Void (Void) import GHC.Generics -import GHC.TypeLits -import Language.Fiddle.Ast.Internal.Generic import Language.Fiddle.Ast.Internal.Instances import Language.Fiddle.Ast.Internal.Kinds import Language.Fiddle.Ast.Internal.Stage import Language.Fiddle.Internal.UnitInterface (UnitInterface) -import qualified Data.Text as Text -- | Common data for each qualified element. newtype CommonQualifcationData diff --git a/src/Language/Fiddle/Ast/Internal/SyntaxTreeKinds.hs b/src/Language/Fiddle/Ast/Internal/SyntaxTreeKinds.hs deleted file mode 100644 index bfdfb13..0000000 --- a/src/Language/Fiddle/Ast/Internal/SyntaxTreeKinds.hs +++ /dev/null @@ -1 +0,0 @@ -module Language.Fiddle.Ast.Internal.SyntaxTreeKinds where diff --git a/src/Language/Fiddle/Compiler.hs b/src/Language/Fiddle/Compiler.hs index a7b07ea..c029765 100644 --- a/src/Language/Fiddle/Compiler.hs +++ b/src/Language/Fiddle/Compiler.hs @@ -4,11 +4,9 @@ module Language.Fiddle.Compiler where -import Control.Arrow (Arrow (first, second)) +import Control.Arrow import Control.Monad (when) -import Control.Monad.Identity (Identity) -import Control.Monad.RWS (RWS, RWST, evalRWS, mapRWS, runRWS) -import Control.Monad.State +import Control.Monad.RWS import Control.Monad.Trans.Maybe import Control.Monad.Writer import Data.Default @@ -16,7 +14,7 @@ import Language.Fiddle.Ast import Language.Fiddle.Types import Options.Applicative import System.IO (hPutStrLn, stderr) -import Text.Parsec (SourcePos, sourceColumn, sourceLine, sourceName) +import Text.Parsec (sourceColumn, sourceLine, sourceName) data Level = Error | Warning | Info deriving (Eq, Ord, Show, Read, Enum) @@ -209,10 +207,10 @@ execCompilationPipelineWithCmdline :: ) ) execCompilationPipelineWithCmdline - (CompilationPhase flagsParser ioAction rest) = do + (CompilationPhase flagsParser ioAct rest) = do fmap ( \opts ast -> do - (diags, ms) <- ioAction opts ast + (diags, ms) <- ioAct opts ast case ms of Just s -> return $ first (diags ++) $ compile_ $ rest opts s ast Nothing -> return (diags, Nothing) diff --git a/src/Language/Fiddle/Compiler/ConsistencyCheck.hs b/src/Language/Fiddle/Compiler/ConsistencyCheck.hs index 79ac9e7..3bdae4a 100644 --- a/src/Language/Fiddle/Compiler/ConsistencyCheck.hs +++ b/src/Language/Fiddle/Compiler/ConsistencyCheck.hs @@ -11,36 +11,29 @@ module Language.Fiddle.Compiler.ConsistencyCheck ) where -import Control.Monad (forM, forM_, unless, when) -import Control.Monad.Identity (Identity (Identity)) +import Control.Monad (forM_, unless, when) import Control.Monad.RWS (MonadState (get, put), MonadWriter (tell), gets, modify') -import Data.Foldable (Foldable (toList), foldlM) +import Data.Foldable (foldlM) import Data.Functor.Identity -import qualified Data.IntMap as IntMap -import Data.Kind (Type) -import Data.List (inits, intercalate) -import Data.List.NonEmpty (NonEmpty (..)) -import qualified Data.List.NonEmpty as NonEmpty -import Data.Map (Map) -import qualified Data.Map as Map -import Data.Maybe (fromMaybe, mapMaybe) -import qualified Data.Set as Set -import qualified Data.Text as Text -import Data.Void +import Data.List (intercalate) +import Data.Maybe (mapMaybe) import Data.Word (Word32) import GHC.TypeError as TypeError -import GHC.TypeLits import Language.Fiddle.Ast import Language.Fiddle.Compiler import Language.Fiddle.Internal.Scopes import Language.Fiddle.Internal.UnitInterface as UnitInterface import Language.Fiddle.Types (Comment (DocComment), Commented (Commented, unCommented), SourceSpan) -import Text.Printf (printf) import Prelude hiding (unzip) +import Text.Printf (printf) + +import qualified Data.IntMap as IntMap +import qualified Data.List.NonEmpty as NonEmpty +import qualified Data.Text as Text data GlobalState = GlobalState { globalScope :: Scope String (Either SizeBits SizeBytes), - fileDependencies :: [FilePath], + _fileDependencies :: [FilePath], unitInterface :: UnitInterface } @@ -158,7 +151,7 @@ instance AdvanceStage CurrentStage ObjTypeBody where deriving instance AdvanceStage CurrentStage FiddleDecl instance AdvanceStage CurrentStage (Directed FiddleDecl) where - modifyState (Directed directives t _) s = case t of + modifyState (Directed _ t _) s = case t of (BitsDecl _ id typ annotation) -> do typeSize <- getTypeSize typ insertTypeSize annotation s id typeSize @@ -345,9 +338,6 @@ objTypeBodyToStage3 st (ObjTypeBody bodyType decls a) startOff = do ) _ -> return () fUnzip xs = (fst <$> xs, snd <$> xs) - pushApply :: Maybe (a, b) -> (Maybe a, Maybe b) - pushApply (Just (a, b)) = (Just a, Just b) - pushApply Nothing = (Nothing, Nothing) registerBodyToStage3 :: LocalState -> @@ -531,6 +521,7 @@ lookupTypeSize (LocalState scopePath) (Name idents a) = do (unCommented a) ] compilationFailure + _ -> compilationFailure getTypeSize :: BitType CurrentStage I Annot -> Compile s SizeBits getTypeSize (RawBits expr _) = fromIntegral <$> exprToSize expr @@ -581,9 +572,12 @@ getTypeSize (EnumBitType expr (Identity (EnumBody constants _)) ann) = do diagnosticError :: String -> Annot -> Compile a () diagnosticError str a = tell [Diagnostic Error str (unCommented a)] -diagnosticInfo :: String -> Annot -> Compile a () -diagnosticInfo str a = tell [Diagnostic Info str (unCommented a)] - +insertIntoUnitInterface :: + NonEmpty.NonEmpty String -> + UnitInterface -> + Commented SourceSpan -> + ExportedValue -> + UnitInterface insertIntoUnitInterface path ui (Commented comments srcspan) val = let docComments = mconcat diff --git a/src/Language/Fiddle/Compiler/Expansion.hs b/src/Language/Fiddle/Compiler/Expansion.hs index 202717f..1c4df45 100644 --- a/src/Language/Fiddle/Compiler/Expansion.hs +++ b/src/Language/Fiddle/Compiler/Expansion.hs @@ -7,20 +7,17 @@ module Language.Fiddle.Compiler.Expansion (expandAst, expansionPhase) where import Control.Monad.Identity (Identity (..)) -import Control.Monad.State (get, gets, modify, put) -import qualified Data.Char as Char +import Control.Monad.State (get, modify, put) import Data.List (intercalate) -import qualified Data.List.NonEmpty as NonEmpty import Data.Text (Text) -import qualified Data.Text as Text -import Data.Type.Bool -import Debug.Trace -import GHC.TypeLits import Language.Fiddle.Ast import Language.Fiddle.Compiler -import Language.Fiddle.Compiler.Qualification +import Language.Fiddle.Compiler.Qualification () import Language.Fiddle.Types -import Text.Printf (printf) + +import qualified Data.Char as Char +import qualified Data.List.NonEmpty as NonEmpty +import qualified Data.Text as Text type M = Compile State diff --git a/src/Language/Fiddle/Compiler/ImportResolution.hs b/src/Language/Fiddle/Compiler/ImportResolution.hs index aacf27e..4d4bd32 100644 --- a/src/Language/Fiddle/Compiler/ImportResolution.hs +++ b/src/Language/Fiddle/Compiler/ImportResolution.hs @@ -8,33 +8,32 @@ module Language.Fiddle.Compiler.ImportResolution ) where -import qualified Codec.Compression.GZip as GZip import Control.Arrow (Arrow (second)) import Control.Monad (filterM) import Control.Monad.Identity (Identity) import Control.Monad.Trans.Maybe (MaybeT (MaybeT, runMaybeT)) -import Control.Monad.Writer.Lazy (MonadTrans (lift), MonadWriter (tell), WriterT (WriterT, runWriterT), execWriterT) -import Data.Aeson (decode, eitherDecode, encode) -import qualified Data.ByteString.Lazy as BL +import Control.Monad.Writer.Lazy (MonadTrans (lift), MonadWriter (tell), WriterT (..), execWriterT) +import Data.Aeson (eitherDecode, encode) import Data.Map (Map) -import qualified Data.Map as Map import Data.Maybe (fromMaybe) import Data.Text (Text) -import qualified Data.Text as Text import Data.Tuple (swap) import Data.Typeable import Language.Fiddle.Ast -import Language.Fiddle.Ast.FileInterface (ResolvedImport) import Language.Fiddle.Compiler -import Language.Fiddle.Compiler.Expansion +import Language.Fiddle.Compiler.Expansion () import Language.Fiddle.Internal.UnitInterface import Language.Fiddle.Types import Options.Applicative import System.Directory import System.FilePath -import System.IO import Text.Printf (printf) +import qualified Codec.Compression.GZip as GZip +import qualified Data.ByteString.Lazy as BL +import qualified Data.Map as Map +import qualified Data.Text as Text + newtype Flags = Flags { importDirectories :: [FilePath] } @@ -70,8 +69,6 @@ type M = Compile GlobalState type Annot = Commented SourceSpan -newtype ImportError = ImportError [Diagnostic] - newtype ResolvedImports = ResolvedImports { importMap :: Map Text ([Diagnostic], Maybe UnitInterface) } diff --git a/src/Language/Fiddle/Compiler/Qualification.hs b/src/Language/Fiddle/Compiler/Qualification.hs index 27c0911..eddb3cb 100644 --- a/src/Language/Fiddle/Compiler/Qualification.hs +++ b/src/Language/Fiddle/Compiler/Qualification.hs @@ -10,14 +10,12 @@ -- removed, as they become unnecessary once references are fully qualified. module Language.Fiddle.Compiler.Qualification (qualificationPhase) where -import Control.Monad (forM) import Control.Monad.Identity import Data.Foldable (foldlM) -import Data.Maybe (catMaybes) import Data.Word import Language.Fiddle.Ast import Language.Fiddle.Compiler -import Language.Fiddle.Compiler.ConsistencyCheck +import Language.Fiddle.Compiler.ConsistencyCheck () import Language.Fiddle.Internal.Scopes import Language.Fiddle.Internal.UnitInterface import Language.Fiddle.Types @@ -25,9 +23,9 @@ import Language.Fiddle.Types type CurrentStage = Expanded data GlobalState = GlobalState - { globalScope :: Scope String (Either SizeBits SizeBytes), - fileDependencies :: [FilePath], - unitInterface :: UnitInterface + { _globalScope :: Scope String (Either SizeBits SizeBytes), + _fileDependencies :: [FilePath], + _unitInterface :: UnitInterface } newtype LocalState = LocalState (ScopePath String) @@ -44,8 +42,8 @@ instance CompilationStage Expanded where type StageAfter Expanded = Qualified type StageMonad Expanded = Compile GlobalState type StageState Expanded = LocalState - type StageFunctor Expanded = Identity - type StageAnnotation Expanded = Commented SourceSpan + type StageFunctor Expanded = I + type StageAnnotation Expanded = Annot qualificationPhase :: CompilationPhase Expanded Qualified qualificationPhase = @@ -85,12 +83,12 @@ instance AdvanceStage CurrentStage ObjType where deriving instance (AdvanceStage CurrentStage t) => AdvanceStage CurrentStage (Directed t) instance AdvanceStage CurrentStage PackageBody where - advanceStage localState (PackageBody decls annot) = - PackageBody <$> advanceFiddleDecls localState decls <*> pure annot + advanceStage localState (PackageBody decls a) = + PackageBody <$> advanceFiddleDecls localState decls <*> pure a instance AdvanceStage CurrentStage FiddleUnit where - advanceStage localState (FiddleUnit () decls annot) = - FiddleUnit () <$> advanceFiddleDecls localState decls <*> pure annot + advanceStage localState (FiddleUnit () decls a) = + FiddleUnit () <$> advanceFiddleDecls localState decls <*> pure a advanceFiddleDecls :: LocalState -> @@ -102,6 +100,7 @@ advanceFiddleDecls (LocalState scopePath) decls = fmap (reverse . fst) $ do ( \(declsRet, scopePath') -> \case Directed {directedSubtree = UsingDecl {usingName = name}} -> return (declsRet, addUsingPath (nameToList name) scopePath') + _ -> undefined ) ([], scopePath) decls diff --git a/src/Language/Fiddle/Compiler/Stage0.hs b/src/Language/Fiddle/Compiler/Stage0.hs index 96ee539..cd17140 100644 --- a/src/Language/Fiddle/Compiler/Stage0.hs +++ b/src/Language/Fiddle/Compiler/Stage0.hs @@ -6,12 +6,10 @@ import qualified Data.Text import Language.Fiddle.Ast import Language.Fiddle.Compiler import qualified Language.Fiddle.Parser -import Language.Fiddle.Types (Commented, SourceSpan(..)) +import Language.Fiddle.Types (Commented, SourceSpan (..)) import Text.Parsec (ParseError, errorPos) import Text.Parsec.Error (errorMessages, showErrorMessages) -newtype Stage0Diagnostic = SyntaxError String - toStage0 :: String -> Data.Text.Text -> @@ -31,15 +29,16 @@ toStage1 :: FiddleUnit Parsed (Either ParseError) a -> Compile () (FiddleUnit Parsed Identity a) toStage1 ast = do - alter - ( \case - (Left l) -> do - tell [parseErrorToDiagnostic l] - return (Left l) - r -> return r - ) - return - ast + _ <- + alter + ( \case + (Left l) -> do + tell [parseErrorToDiagnostic l] + return (Left l) + r -> return r + ) + return + ast hoistMaybe $ case squeeze ast of diff --git a/src/Language/Fiddle/GenericTree.hs b/src/Language/Fiddle/GenericTree.hs index 95a730a..f0ac96a 100644 --- a/src/Language/Fiddle/GenericTree.hs +++ b/src/Language/Fiddle/GenericTree.hs @@ -10,14 +10,9 @@ module Language.Fiddle.GenericTree where -import Control.Monad.Writer (execWriter, tell) -import Data.Aeson (Value (..), foldable, object, toEncoding, toJSON) import Data.Aeson.Encoding (text) import Data.Aeson.Types as Aeson import Data.Foldable (Foldable (toList)) -import qualified Data.Foldable -import Data.Functor.Classes (Show1, liftShowsPrec) -import Data.Kind (Type) import Data.Proxy import qualified Data.Text import Data.Typeable @@ -26,8 +21,6 @@ import GHC.Generics import GHC.TypeLits (KnownSymbol, symbolVal) import Language.Fiddle.Ast import Language.Fiddle.Types -import Text.Parsec.Pos -import Text.Printf (printf) type Context stage = ( Show (NumberType stage), @@ -61,11 +54,11 @@ alterGenericSyntaxTree fn genericTree | (Just newGenericTree) <- fn genericTree = newGenericTree | otherwise = case genericTree of - SyntaxTreeObject str members annot tree -> + SyntaxTreeObject str members a tree -> SyntaxTreeObject str (map (alterGenericSyntaxTree fn) members) - annot + a tree SyntaxTreeList members -> SyntaxTreeList $ map (alterGenericSyntaxTree fn) members @@ -81,18 +74,6 @@ instance (ToJSON a) => ToJSON (Commented a) where toJSON (Commented comment a) = object ["comment" .= comment, "annot" .= a] --- instance ToJSON SourceSpan where --- toJSON (SourceSpan start end) = --- object ["start" .= toJSON start, "end" .= toJSON end] --- --- instance ToJSON SourcePos where --- toJSON sourcePos = --- object --- [ "name" .= sourceName sourcePos, --- "row" .= sourceLine sourcePos, --- "col" .= sourceColumn sourcePos --- ] - instance (Foldable f) => ToJSON (GenericSyntaxTree f (Maybe Value)) where toJSON = \case (SyntaxTreeObject typ membs Nothing _) -> diff --git a/src/Language/Fiddle/Internal/Scopes.hs b/src/Language/Fiddle/Internal/Scopes.hs index 02c9a5a..70cadee 100644 --- a/src/Language/Fiddle/Internal/Scopes.hs +++ b/src/Language/Fiddle/Internal/Scopes.hs @@ -2,10 +2,9 @@ module Language.Fiddle.Internal.Scopes where -import Control.Monad (forM) import Data.Aeson import Data.Aeson.Key -import Data.List (inits, intercalate) +import Data.List (inits) import Data.List.NonEmpty (NonEmpty (..), prependList) import Data.Map (Map) import qualified Data.Map as Map @@ -129,10 +128,10 @@ lookupScopeWithPath (ScopePath current others) key scope = Nothing -> [] instance (ToJSONKey k, ToJSON v, Ord k) => ToJSON (Scope k v) where - toJSON (Scope subScopes scopeValues) = + toJSON scope = object - [ fromString "subScopes" .= toJSON subScopes, - fromString "scopeValues" .= toJSON scopeValues + [ fromString "subScopes" .= toJSON (subScopes scope), + fromString "scopeValues" .= toJSON (scopeValues scope) ] instance (FromJSONKey k, FromJSON v, Ord k) => FromJSON (Scope k v) where diff --git a/src/Language/Fiddle/Internal/UnitInterface.hs b/src/Language/Fiddle/Internal/UnitInterface.hs index 4244121..aacb71d 100644 --- a/src/Language/Fiddle/Internal/UnitInterface.hs +++ b/src/Language/Fiddle/Internal/UnitInterface.hs @@ -67,10 +67,10 @@ data ExportedValue where deriving (Show, Eq, Ord, Generic, FromJSON, ToJSON) instance ToJSON UnitInterface where - toJSON (UnitInterface rootScope dependencies) = + toJSON ui = object - [ "rootScope" .= rootScope, - "dependencies" .= dependencies + [ "rootScope" .= rootScope ui, + "dependencies" .= dependencies ui ] instance FromJSON UnitInterface where diff --git a/src/Language/Fiddle/Parser.hs b/src/Language/Fiddle/Parser.hs index a1c7a0e..00cce27 100644 --- a/src/Language/Fiddle/Parser.hs +++ b/src/Language/Fiddle/Parser.hs @@ -12,8 +12,6 @@ import Data.Functor.Identity import Data.Kind (Type) import Data.List.NonEmpty (NonEmpty (..)) import Data.Text (Text) -import qualified Data.Text -import Debug.Trace import Language.Fiddle.Ast import Language.Fiddle.Tokenizer import Language.Fiddle.Types @@ -54,12 +52,12 @@ stripTrailingComments = reverse . dropWhile isComment . reverse directedP :: (Annotated (t Parsed)) => Pa t -> PaS (Directed t 'Parsed) directedP subparser = withMetaLeaveComments $ do - comments <- many commentP - Directed <$> many directiveP <*> pushComments comments subparser + coms <- many commentP + Directed <$> many directiveP <*> pushComments coms subparser pushComments :: (Annotated t) => [Comment] -> PaS t -> PaS t -pushComments comments subparse = do - setAnnot (\(Commented coms a) -> Commented (comments ++ coms) a) <$> subparse +pushComments coms subparse = do + setAnnot (\(Commented coms' a) -> Commented (coms ++ coms') a) <$> subparse directiveP :: PaS Directive directiveP = @@ -75,18 +73,18 @@ directiveElementP = withMeta $ do identifier1 <- nextTextP choice [ do - tok TokColon + tok_ TokColon let backend = identifier1 key <- nextTextP choice [ do - tok TokEq + tok_ TokEq DirectiveElementKeyValue (Just backend) key <$> directiveExpressionP, do return (DirectiveElementKey (Just backend) key) ], do - tok TokEq + tok_ TokEq let key = identifier1 DirectiveElementKeyValue Nothing key <$> directiveExpressionP, return $ DirectiveElementKey Nothing identifier1 @@ -122,7 +120,7 @@ stringTokenP = importListP :: PaS ImportList importListP = withMeta $ do - tok TokLParen + tok_ TokLParen ImportList <$> many (ident <* (tok TokComma <|> lookAhead (tok TokRParen))) <* tok TokRParen @@ -150,7 +148,7 @@ fiddleDeclP = do ObjTypeDecl () <$> ident <*> ( do - tok TokColon + tok_ TokColon bt <- bodyTypeP defer body (objTypeBodyP bt) ) @@ -199,11 +197,11 @@ objTypeDeclP :: Pa ObjTypeDecl objTypeDeclP = withMeta $ ( do - tok KWAssertPos + tok_ KWAssertPos AssertPosStatement (Witness ()) <$> exprInParenP ) <|> ( do - tok KWReserved + tok_ KWReserved ReservedDecl <$> exprInParenP ) <|> ( do @@ -211,9 +209,9 @@ objTypeDeclP = TypeSubStructure <$> defer body (objTypeBodyP bt) <*> optionMaybe ident ) <|> ( do - mod <- optionMaybe modifierP - tok KWReg - RegisterDecl mod + modifier <- optionMaybe modifierP + tok_ KWReg + RegisterDecl modifier <$> optionMaybe ident <*> exprInParenP <*> optionMaybe (tok TokColon *> registerBodyP) @@ -285,7 +283,7 @@ registerBitsTypeRefP = do anonymousBitsTypeP :: Pa AnonymousBitsType anonymousBitsTypeP = withMeta $ do - tok KWEnum + tok_ KWEnum AnonymousEnumBody <$> exprInParenP <*> defer body enumBodyP bitTypeP :: Pa BitType @@ -293,7 +291,7 @@ bitTypeP = withMeta $ rawBits <|> enumType where rawBits = RawBits <$> (tok TokLParen *> expressionP <* tok TokRParen) enumType = do - tok KWEnum + tok_ KWEnum expr <- exprInParenP EnumBitType expr <$> defer body enumBodyP @@ -323,9 +321,9 @@ body = do directiveBodyTokens :: P [Token SourceSpan] directiveBodyTokens = do - tokKeepComment TokDirectiveStart + _ <- tokKeepComment TokDirectiveStart ret <- concat <$> manyTill ((: []) <$> anyToken) (lookAhead $ tokKeepComment TokDirectiveEnd) - tokKeepComment TokDirectiveEnd + _ <- tokKeepComment TokDirectiveEnd return ret body' :: P (Token SourceSpan, [Token SourceSpan], Token SourceSpan) @@ -339,7 +337,7 @@ body' = do (lookAhead $ tokKeepComment TokRBrace) r <- tokKeepComment TokRBrace - next <- lookAhead anyToken + _ <- lookAhead anyToken return (l, stripTrailingComments ret, r) @@ -373,24 +371,18 @@ packageBodyP = ) ) -printNext :: P () -printNext = do - t <- lookAhead anyToken - traceM $ "NextToken: " ++ show t - return () - ident :: PaS Identifier ident = withMeta $ token $ \case - (TokIdent id) -> Just (Identifier id) + (TokIdent identTok) -> Just (Identifier identTok) _ -> Nothing name :: PaS Name name = withMeta $ do i <- ident is <- many $ do - tok TokDot + tok_ TokDot ident return $ Name (i :| is) @@ -398,11 +390,11 @@ name = withMeta $ do -- and after and sets the positions and adds it to the annotation. withMeta :: P (Commented SourceSpan -> b) -> P b withMeta p = do - comments <- many commentP + comments' <- many commentP start <- getPosition fn <- p end <- getPosition - return $ fn (Commented comments (SourceSpan start end)) + return $ fn (Commented comments' (SourceSpan start end)) -- Takes a some parsable thing p and automatically parses the comments before -- and after and sets the positions and adds it to the annotation. @@ -425,22 +417,25 @@ tokKeepComment t' = do Text.Parsec.token (\(Token t _) -> show t) (\(Token _ (SourceSpan s1 _)) -> s1) - (\tok@(Token t _) -> if t == t' then Just tok else Nothing) + (\aToken@(Token t _) -> if t == t' then Just aToken else Nothing) + +tok_ :: T -> P () +tok_ = void . tok tok :: T -> P (Token SourceSpan) tok t' = do - many commentP + _ <- many commentP Text.Parsec.token (\(Token t _) -> show t) (\(Token _ (SourceSpan s1 _)) -> s1) - (\tok@(Token t _) -> if t == t' then Just tok else Nothing) + (\tt@(Token t _) -> if t == t' then Just tt else Nothing) parseFiddleText :: String -> Text -> F (FiddleUnit 'Parsed F (Commented SourceSpan)) -parseFiddleText sourceName txt = +parseFiddleText srcName txt = runIdentity . Text.Parsec.runParserT (fiddleUnit <* eof) () - sourceName + srcName . stripTrailingComments - =<< tokenize sourceName txt + =<< tokenize srcName txt diff --git a/src/Language/Fiddle/Tokenizer.hs b/src/Language/Fiddle/Tokenizer.hs index 007009f..d2e5cf8 100644 --- a/src/Language/Fiddle/Tokenizer.hs +++ b/src/Language/Fiddle/Tokenizer.hs @@ -8,7 +8,6 @@ import Data.Text (Text) import qualified Data.Text import Language.Fiddle.Types import Text.Parsec -import qualified Text.Parsec data T = KWAssertPos @@ -111,7 +110,7 @@ parseToken = spaces *> tok parseToken' <* spaces ident -> TokIdent ident parseString = fmap (TokString . Data.Text.pack . concat) $ do - char '"' + _ <- char '"' manyTill ( do c <- anyChar @@ -126,12 +125,12 @@ parseToken = spaces *> tok parseToken' <* spaces parseComment = try ( do - string "//" + _ <- string "//" TokComment . Data.Text.pack <$> manyTill anyChar (char '\n') ) <|> try ( do - string "/**" + _ <- string "/**" TokDocComment . Data.Text.pack <$> manyTill anyChar (try $ string "*/") ) diff --git a/src/Language/Fiddle/Types.hs b/src/Language/Fiddle/Types.hs index 047ace4..8b022cf 100644 --- a/src/Language/Fiddle/Types.hs +++ b/src/Language/Fiddle/Types.hs @@ -4,7 +4,6 @@ module Language.Fiddle.Types where import Data.Aeson import Data.Text (Text, pack, splitOn, unpack) -import Text.Parsec (SourcePos) import Text.Parsec.Pos data Comment = NormalComment Text | DocComment Text @@ -41,9 +40,9 @@ parseSpan str = do -- ToJSON and FromJSON instances instance ToJSON SourceSpan where - toJSON span = + toJSON srcSpan = object - ["span" .= formatSpan span] + ["span" .= formatSpan srcSpan] instance FromJSON SourceSpan where parseJSON = withObject "SourceSpan" $ \v -> do diff --git a/src/Main.hs b/src/Main.hs index fb2a1f2..4da2295 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -2,7 +2,7 @@ module Main where import Control.Monad (forM_) import Control.Monad.Identity (Identity) -import Data.Aeson (Value (Null, String), encode) +import Data.Aeson (Value (..), encode) import qualified Data.ByteString.Lazy.Char8 as BL import qualified Data.Text as Text import qualified Data.Text.IO as TextIO @@ -16,19 +16,28 @@ import Language.Fiddle.Compiler.ImportResolution import Language.Fiddle.Compiler.Qualification import Language.Fiddle.Compiler.Stage0 import Language.Fiddle.GenericTree - ( GenericSyntaxTree (..), - ToGenericSyntaxTree (toGenericSyntaxTree), - alterGenericSyntaxTree, - ) -import qualified Language.Fiddle.Parser -import qualified Language.Fiddle.Tokenizer -import Language.Fiddle.Types (Commented (unCommented)) +import Language.Fiddle.Types (Commented (unCommented), SourceSpan) import Options.Applicative import qualified System.Environment as System import System.Exit (exitWith) -import System.IO --- compilationPipeline :: _ -> _ -> CompilationPhase Parsed Checked +compilationPipeline :: + ( FilePath -> + IO + ( [Diagnostic], + Maybe + ( FiddleUnit Parsed Identity (Commented SourceSpan) + ) + ) + ) -> + ( FiddleUnit Parsed Identity (Commented SourceSpan) -> + IO + ( [Diagnostic], + Maybe + (FiddleUnit Checked Identity (Commented SourceSpan)) + ) + ) -> + CompilationPhase Parsed Checked compilationPipeline parse compile = importResolutionPhase parse compile >>> expansionPhase @@ -56,12 +65,12 @@ runCompilationPipeline :: IO ([Diagnostic], Maybe (TreeType FiddleUnit Checked)) runCompilationPipeline argv tree = case fromArgs argv of - Failure failure -> + Success (_, pipelineAction) -> pipelineAction tree + _ -> return ( [Diagnostic Error "Internal parsing failure (this is a bug)." (unCommented $ annot tree)], Nothing ) - Success (_, pipelineAction) -> pipelineAction tree -- | Parse command-line arguments into global flags and a compilation action. fromArgs :: @@ -111,12 +120,7 @@ parseCommandLineArgs :: TreeType FiddleUnit Parsed -> IO ([Diagnostic], Maybe (TreeType FiddleUnit Checked)) ) -parseCommandLineArgs argv = - case fromArgs argv of - Failure failure -> do - hPutStrLn stderr (fst $ renderFailure failure "") - exitWith (ExitFailure 1) - Success v -> return v +parseCommandLineArgs argv = handleParseResult (fromArgs argv) -- | Parse the input file into the initial AST. parseInputFile :: String -> IO ([Diagnostic], Maybe (TreeType FiddleUnit Parsed)) |