Skip to content

Commit 34f0cd8

Browse files
committed
Parse module name with dots
1 parent d3f368b commit 34f0cd8

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Some.Module.Name where
2+
3+
class Foo a where
4+
foo :: a -> a
5+
6+
x = "Done"
7+
8+
module Main where
9+
10+
import Prelude
11+
import Some.Module.Name
12+
13+
instance Some.Module.Name.Foo String where
14+
foo s = s
15+
16+
main = Trace.print $ foo x

src/Language/PureScript/CodeGen/Common.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
module Language.PureScript.CodeGen.Common where
1717

1818
import Data.Char
19+
import Data.List (intercalate)
1920
import Language.PureScript.Names
2021

2122
-- |
@@ -127,3 +128,5 @@ nameIsJsReserved name =
127128
, "with"
128129
, "yield" ]
129130

131+
moduleNameToJs :: ModuleName -> String
132+
moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)

src/Language/PureScript/CodeGen/JS.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Language.PureScript.CodeGen.JS (
2323
) where
2424

2525
import Data.Maybe (fromMaybe, mapMaybe)
26-
import Data.List (sortBy, intercalate)
26+
import Data.List (sortBy)
2727
import Data.Function (on)
2828
import Data.Data (Data)
2929
import Data.Generics (mkQ, everything)
@@ -55,15 +55,12 @@ moduleToJs :: Options -> Module -> Environment -> Maybe JS
5555
moduleToJs opts (Module name decls) env =
5656
case jsDecls of
5757
[] -> Nothing
58-
_ -> Just $ JSAssignment (JSAccessor (moduleNameToJS name) (JSVar "_ps")) $
58+
_ -> Just $ JSAssignment (JSAccessor (moduleNameToJs name) (JSVar "_ps")) $
5959
JSApp (JSFunction Nothing ["module"] (JSBlock $ jsDecls ++ [JSReturn $ JSVar "module"]))
60-
[(JSBinary Or (JSAccessor (moduleNameToJS name) (JSVar "_ps")) (JSObjectLiteral []))]
60+
[(JSBinary Or (JSAccessor (moduleNameToJs name) (JSVar "_ps")) (JSObjectLiteral []))]
6161
where
6262
jsDecls = (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts name decl env) (decls))
6363

64-
moduleNameToJS :: ModuleName -> String
65-
moduleNameToJS (ModuleName pns) = intercalate "_" (runProperName `map` pns)
66-
6764
-- |
6865
-- Generate code in the simplified Javascript intermediate representation for a declaration
6966
--
@@ -215,7 +212,7 @@ varToJs m e qual@(Qualified _ ident) = go qual
215212
-- variable that may have a qualified name.
216213
--
217214
qualifiedToJS :: ModuleName -> (a -> Ident) -> Qualified a -> JS
218-
qualifiedToJS m f (Qualified (Just m') a) | m /= m' = accessor (f a) (JSAccessor (moduleNameToJS m') $ JSVar "_ps")
215+
qualifiedToJS m f (Qualified (Just m') a) | m /= m' = accessor (f a) (JSAccessor (moduleNameToJs m') $ JSVar "_ps")
219216
qualifiedToJS m f (Qualified _ a) = JSVar $ identToJs (f a)
220217

221218
-- |

src/Language/PureScript/Parser/Common.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,24 @@ natural = PT.natural tokenParser
203203
properName :: P.Parsec String u ProperName
204204
properName = lexeme $ ProperName <$> P.try ((:) <$> P.upper <*> many P.alphaNum P.<?> "name")
205205

206+
-- |
207+
-- Parse a module name
208+
--
209+
moduleName :: P.Parsec String ParseState ModuleName
210+
moduleName = ModuleName <$> P.try (sepBy properName dot)
211+
206212
-- |
207213
-- Parse a qualified name, i.e. M.name or just name
208214
--
209215
parseQualified :: P.Parsec String ParseState a -> P.Parsec String ParseState (Qualified a)
210-
parseQualified parser = qual
216+
parseQualified parser = part []
211217
where
212-
qual = (Qualified <$> (Just . ModuleName . pure <$> P.try (properName <* delimiter)) <*> parser)
213-
<|> (Qualified Nothing <$> P.try parser)
214-
delimiter = indented *> dot
218+
part path = (do name <- P.try (properName <* delimiter)
219+
part (updatePath path name))
220+
<|> (Qualified (qual path) <$> P.try parser)
221+
delimiter = indented *> dot <* P.notFollowedBy dot
222+
updatePath path name = path ++ [name]
223+
qual path = if null path then Nothing else Just $ ModuleName path
215224

216225
-- |
217226
-- Parse an integer or floating point value

src/Language/PureScript/Parser/Declarations.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ parseImportDeclaration :: P.Parsec String ParseState Declaration
8989
parseImportDeclaration = do
9090
reserved "import"
9191
indented
92-
moduleName <- ModuleName . pure <$> properName
92+
moduleName <- moduleName
9393
idents <- P.optionMaybe $ parens $ commaSep1 (Left <$> parseIdent <|> Right <$> properName)
9494
return $ ImportDeclaration moduleName idents
9595

@@ -138,10 +138,10 @@ parseModule :: P.Parsec String ParseState Module
138138
parseModule = do
139139
reserved "module"
140140
indented
141-
name <- properName
141+
name <- moduleName
142142
_ <- lexeme $ P.string "where"
143143
decls <- mark (P.many (same *> parseDeclaration))
144-
return $ Module (ModuleName [name]) decls
144+
return $ Module name decls
145145

146146
-- |
147147
-- Parse a collection of modules

src/Language/PureScript/Sugar/TypeClasses.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Control.Arrow ((***))
3636
import Data.Maybe (fromMaybe)
3737
import Data.List (nub)
3838
import Data.Generics (mkQ, everything)
39-
import Language.PureScript.CodeGen.Common (identToJs)
39+
import Language.PureScript.CodeGen.Common (identToJs, moduleNameToJs)
4040

4141
type MemberMap = M.Map (ModuleName, ProperName) (String, [(String, Type)])
4242

@@ -159,7 +159,7 @@ typeInstanceDictionaryEntryDeclaration _ _ _ _ _ = error "Invalid declaration in
159159

160160
qualifiedToString :: ModuleName -> Qualified ProperName -> String
161161
qualifiedToString mn (Qualified Nothing pn) = qualifiedToString mn (Qualified (Just mn) pn)
162-
qualifiedToString _ (Qualified (Just mn) pn) = runModuleName mn ++ "_" ++ runProperName pn
162+
qualifiedToString _ (Qualified (Just mn) pn) = moduleNameToJs mn ++ "_" ++ runProperName pn
163163

164164
-- |
165165
-- Generate a name for a type class dictionary, based on the module name, class name and type name

0 commit comments

Comments
 (0)