git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Parse module name with dots
34f0cd8Gary Burgess
examples/passing/DottedModules.pursnew file+16
@@ -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+3
@@ -16,6 +16,7 @@
16module Language.PureScript.CodeGen.Common where
16module Language.PureScript.CodeGen.Common where
17
17
18import Data.Char
18import Data.Char
19+import Data.List (intercalate)
19import Language.PureScript.Names
20import Language.PureScript.Names
20
21
21-- |
22-- |
@@ -127,3 +128,5 @@ nameIsJsReserved name =
127 , "with"
128 , "with"
128 , "yield" ]
129 , "yield" ]
129
130
131+moduleNameToJs :: ModuleName -> String
132+moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)
src/Language/PureScript/CodeGen/JS.hs+4−7
@@ -23,7 +23,7 @@ module Language.PureScript.CodeGen.JS (
23) where
23) where
24
24
25import Data.Maybe (fromMaybe, mapMaybe)
25import Data.Maybe (fromMaybe, mapMaybe)
26−import Data.List (sortBy, intercalate)
26+import Data.List (sortBy)
27import Data.Function (on)
27import Data.Function (on)
28import Data.Data (Data)
28import Data.Data (Data)
29import Data.Generics (mkQ, everything)
29import Data.Generics (mkQ, everything)
@@ -55,15 +55,12 @@ moduleToJs :: Options -> Module -> Environment -> Maybe JS
55moduleToJs opts (Module name decls) env =
55moduleToJs opts (Module name decls) env =
56 case jsDecls of
56 case jsDecls of
57 [] -> Nothing
57 [] -> Nothing
58− _ -> Just $ JSAssignment (JSAccessor (moduleNameToJS name) (JSVar "_ps")) $
58+ _ -> Just $ JSAssignment (JSAccessor (moduleNameToJs name) (JSVar "_ps")) $
59 JSApp (JSFunction Nothing ["module"] (JSBlock $ jsDecls ++ [JSReturn $ JSVar "module"]))
59 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 []))]
61 where
61 where
62 jsDecls = (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts name decl env) (decls))
62 jsDecls = (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts name decl env) (decls))
63
63
64−moduleNameToJS :: ModuleName -> String
65−moduleNameToJS (ModuleName pns) = intercalate "_" (runProperName `map` pns)
66−
67-- |
64-- |
68-- Generate code in the simplified Javascript intermediate representation for a declaration
65-- Generate code in the simplified Javascript intermediate representation for a declaration
69--
66--
@@ -215,7 +212,7 @@ varToJs m e qual@(Qualified _ ident) = go qual
215-- variable that may have a qualified name.
212-- variable that may have a qualified name.
216--
213--
217qualifiedToJS :: ModuleName -> (a -> Ident) -> Qualified a -> JS
214qualifiedToJS :: 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")
219qualifiedToJS m f (Qualified _ a) = JSVar $ identToJs (f a)
216qualifiedToJS m f (Qualified _ a) = JSVar $ identToJs (f a)
220
217
221-- |
218-- |
src/Language/PureScript/Parser/Common.hs+13−4
@@ -203,15 +203,24 @@ natural = PT.natural tokenParser
203properName :: P.Parsec String u ProperName
203properName :: P.Parsec String u ProperName
204properName = lexeme $ ProperName <$> P.try ((:) <$> P.upper <*> many P.alphaNum P.<?> "name")
204properName = lexeme $ ProperName <$> P.try ((:) <$> P.upper <*> many P.alphaNum P.<?> "name")
205
205
206+-- |
207+-- Parse a module name
208+--
209+moduleName :: P.Parsec String ParseState ModuleName
210+moduleName = ModuleName <$> P.try (sepBy properName dot)
211+
206-- |
212-- |
207-- Parse a qualified name, i.e. M.name or just name
213-- Parse a qualified name, i.e. M.name or just name
208--
214--
209parseQualified :: P.Parsec String ParseState a -> P.Parsec String ParseState (Qualified a)
215parseQualified :: P.Parsec String ParseState a -> P.Parsec String ParseState (Qualified a)
210−parseQualified parser = qual
216+parseQualified parser = part []
211 where
217 where
212− qual = (Qualified <$> (Just . ModuleName . pure <$> P.try (properName <* delimiter)) <*> parser)
218+ part path = (do name <- P.try (properName <* delimiter)
219+ part (updatePath path name))
213− <|> (Qualified Nothing <$> P.try parser)
220+ <|> (Qualified (qual path) <$> P.try parser)
214− delimiter = indented *> dot
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
215
224
216-- |
225-- |
217-- Parse an integer or floating point value
226-- Parse an integer or floating point value
src/Language/PureScript/Parser/Declarations.hs+3−3
@@ -89,7 +89,7 @@ parseImportDeclaration :: P.Parsec String ParseState Declaration
89parseImportDeclaration = do
89parseImportDeclaration = do
90 reserved "import"
90 reserved "import"
91 indented
91 indented
92− moduleName <- ModuleName . pure <$> properName
92+ moduleName <- moduleName
93 idents <- P.optionMaybe $ parens $ commaSep1 (Left <$> parseIdent <|> Right <$> properName)
93 idents <- P.optionMaybe $ parens $ commaSep1 (Left <$> parseIdent <|> Right <$> properName)
94 return $ ImportDeclaration moduleName idents
94 return $ ImportDeclaration moduleName idents
95
95
@@ -138,10 +138,10 @@ parseModule :: P.Parsec String ParseState Module
138parseModule = do
138parseModule = do
139 reserved "module"
139 reserved "module"
140 indented
140 indented
141− name <- properName
141+ name <- moduleName
142 _ <- lexeme $ P.string "where"
142 _ <- lexeme $ P.string "where"
143 decls <- mark (P.many (same *> parseDeclaration))
143 decls <- mark (P.many (same *> parseDeclaration))
144− return $ Module (ModuleName [name]) decls
144+ return $ Module name decls
145
145
146-- |
146-- |
147-- Parse a collection of modules
147-- Parse a collection of modules
src/Language/PureScript/Sugar/TypeClasses.hs+2−2
@@ -36,7 +36,7 @@ import Control.Arrow ((***))
36import Data.Maybe (fromMaybe)
36import Data.Maybe (fromMaybe)
37import Data.List (nub)
37import Data.List (nub)
38import Data.Generics (mkQ, everything)
38import Data.Generics (mkQ, everything)
39−import Language.PureScript.CodeGen.Common (identToJs)
39+import Language.PureScript.CodeGen.Common (identToJs, moduleNameToJs)
40
40
41type MemberMap = M.Map (ModuleName, ProperName) (String, [(String, Type)])
41type MemberMap = M.Map (ModuleName, ProperName) (String, [(String, Type)])
42
42
@@ -159,7 +159,7 @@ typeInstanceDictionaryEntryDeclaration _ _ _ _ _ = error "Invalid declaration in
159
159
160qualifiedToString :: ModuleName -> Qualified ProperName -> String
160qualifiedToString :: ModuleName -> Qualified ProperName -> String
161qualifiedToString mn (Qualified Nothing pn) = qualifiedToString mn (Qualified (Just mn) pn)
161qualifiedToString 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
163
163
164-- |
164-- |
165-- Generate a name for a type class dictionary, based on the module name, class name and type name
165-- Generate a name for a type class dictionary, based on the module name, class name and type name