on master help sign in
Add the possibility to "import Module as Alias".
commitf24a675b623fc3facb21fc6643a7eb7f2ef0d9ea authorMatus Tejiscak <ziman@functor.sk> dateMon 27 Jan 2014, 15:09 (13 years ago) parentfe3f03f
4 files changed+28−15
src/Idris/AbsSyntaxTree.hs+4−2 src/Idris/Chaser.hs+1−1 src/Idris/ParseHelpers.hs+9−4 src/Idris/Parser.hs+14−8
src/Idris/AbsSyntaxTree.hs+4−2
@@ -25,6 +25,7 @@ import Control.Monad.Trans.Error
2525import Data.List
2626import Data.Char
2727import qualified Data.Text as T
28+import qualified Data.Map as M
2829import Data.Either
2930import Data.Word (Word)
3031
@@ -135,7 +136,8 @@ data IState = IState {
135136 idris_colourTheme :: ColourTheme,
136137 idris_outh :: Handle,
137138 idris_errorhandlers :: [Name],
138− idris_nameIdx :: (Int, Ctxt (Int, Name))
139+ idris_nameIdx :: (Int, Ctxt (Int, Name)),
140+ module_aliases :: M.Map [T.Text] [T.Text]
139141 }
140142
141143data SizeChange = Smaller | Same | Bigger | Unknown
@@ -202,7 +204,7 @@ idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
202204 emptyContext
203205 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
204206 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
205− True defaultTheme stdout [] (0, emptyContext)
207+ True defaultTheme stdout [] (0, emptyContext) M.empty
206208
207209-- | The monad for the main REPL - reading and processing files and updating
208210-- global state (hence the IO inner monad).
src/Idris/Chaser.hs+1−1
@@ -132,7 +132,7 @@ buildTree built fp = btree [] fp
132132 file_in <- runIO $ readFile f
133133 file <- if lit then tclift $ unlit f file_in else return file_in
134134 (_, modules, _) <- parseImports f file
135− ms <- mapM (btree done) modules
135+ ms <- mapM (btree done . fst) modules
136136 return (concat ms)
137137 else return [] -- IBC with no source available
138138-- (\c -> return []) -- error, can't chase modules here
src/Idris/ParseHelpers.hs+9−4
@@ -25,6 +25,7 @@ import qualified Data.List.Split as Spl
2525import Data.List
2626import Data.Monoid
2727import Data.Char
28+import qualified Data.Map as M
2829import qualified Data.HashSet as HS
2930import qualified Data.Text as T
3031import qualified Data.ByteString.UTF8 as UTF8
@@ -210,10 +211,14 @@ maybeWithNS parser ascend bad = do
210211
211212-- | Parses a name
212213name :: IdrisParser Name
213−name = do i <- get
214− iName (syntax_keywords i)
215− <?> "name"
216−
214+name = (<?> "name") $ do
215+ keywords <- syntax_keywords <$> get
216+ aliases <- module_aliases <$> get
217+ unalias aliases <$> iName keywords
218+ where
219+ unalias :: M.Map [T.Text] [T.Text] -> Name -> Name
220+ unalias aliases (NS n ns) | Just ns' <- M.lookup ns aliases = NS n ns'
221+ unalias aliases name = name
217222
218223{- | List of all initial segments in ascending order of a list. Every such
219224 initial segment ends right before an element satisfying the given
src/Idris/Parser.hs+14−8
@@ -52,6 +52,7 @@ import qualified Data.List.Split as Spl
5252import Data.List
5353import Data.Monoid
5454import Data.Char
55+import qualified Data.Map as M
5556import qualified Data.HashSet as HS
5657import qualified Data.Text as T
5758import qualified Data.ByteString.UTF8 as UTF8
@@ -91,13 +92,14 @@ moduleHeader = try (do reserved "module"
9192{- | Parses an import statement
9293 Import ::= 'import' Identifier_t ';'?;
9394 -}
94−import_ :: IdrisParser String
95+import_ :: IdrisParser (String, Maybe String)
9596import_ = do reserved "import"
9697 id <- identifier
98+ newName <- optional (reserved "as" *> identifier)
9799 option ';' (lchar ';')
98− return (toPath id)
100+ return (toPath id, toPath <$> newName)
99101 <?> "import statement"
100− where toPath f = foldl1' (</>) (Spl.splitOn "." f)
102+ where toPath = foldl1' (</>) . Spl.splitOn "."
101103
102104{- | Parses program source
103105 Prog ::= Decl* EOF;
@@ -934,7 +936,7 @@ parseTactic :: IState -> String -> Result PTactic
934936parseTactic st = runparser (fullTactic defaultSyntax) st "(input)"
935937
936938-- | Parse module header and imports
937−parseImports :: FilePath -> String -> Idris ([String], [String], Maybe Delta)
939+parseImports :: FilePath -> String -> Idris ([String], [(String, Maybe String)], Maybe Delta)
938940parseImports fname input
939941 = do i <- getIState
940942 case parseString (runInnerParser (evalStateT imports i)) (Directed (UTF8.fromString fname) 0 0 0 0) input of
@@ -942,7 +944,7 @@ parseImports fname input
942944 Success (x, i) -> do -- Discard state updates (there should be
943945 -- none anyway)
944946 return x
945− where imports :: IdrisParser (([String], [String], Maybe Delta), IState)
947+ where imports :: IdrisParser (([String], [(String, Maybe String)], Maybe Delta), IState)
946948 imports = do whiteSpace
947949 mname <- moduleHeader
948950 ps <- many import_
@@ -1053,13 +1055,17 @@ loadSource h lidr f
10531055 let def_total = default_total i
10541056 file_in <- runIO $ readFile f
10551057 file <- if lidr then tclift $ unlit f file_in else return file_in
1056− (mname, modules, pos) <- parseImports f file
1058+ (mname, imports, pos) <- parseImports f file
1059+ let modules = map fst imports
1060+ modAliases = M.fromList
1061+ [(prep alias, prep realName)| (realName, Just alias) <- imports]
1062+ prep = map T.pack . Spl.splitOn "/"
10571063 i <- getIState
10581064 putIState (i { default_access = Hidden })
10591065 clearIBC -- start a new .ibc file
10601066 mapM_ (addIBC . IBCImport) modules
1061− ds' <- parseProg (defaultSyntax {syn_namespace = reverse mname })
1062− f file pos
1067+ let syntax = defaultSyntax{ syn_namespace = reverse mname }
1068+ ds' <- parseProg syntax f file pos
10631069 unless (null ds') $ do
10641070 let ds = namespaces mname ds'
10651071 logLvl 3 (showDecls True ds)