on master help sign in
Correct parsing of listings
commit404c4429700239fa59c9a1cbd702232d1e864ff6 authorEvan Czaplicki <info@elm-lang.org> dateFri 23 May 2014, 16:06 (12 years ago) parent7e623f6
2 files changed+23−13
compiler/AST/Variable.hs+2−2 compiler/Parse/Module.hs+21−11
compiler/AST/Variable.hs+2−2
@@ -43,7 +43,7 @@ instance ToString Canonical where
4343data Listing a = Listing
4444 { _explicits :: [a]
4545 , _open :: Bool
46− } deriving (Eq,Ord)
46+ } deriving (Eq,Ord,Show)
4747
4848openListing :: Listing a
4949openListing = Listing [] True
@@ -53,7 +53,7 @@ data Value
5353 = Value !String
5454 | Alias !String
5555 | ADT !String !(Listing String)
56− deriving (Eq,Ord)
56+ deriving (Eq,Ord,Show)
5757
5858instance Pretty Raw where
5959 pretty (Raw var) = variable var
compiler/Parse/Module.hs+21−11
@@ -6,7 +6,7 @@ import Text.Parsec hiding (newline,spaces)
66
77import Parse.Helpers
88import AST.Module (ImportMethod(..))
9−import AST.Variable (Listing(..), Value(..))
9+import AST.Variable (Listing(..), Value(..), openListing)
1010
1111getModuleName :: String -> Maybe String
1212getModuleName source =
@@ -41,12 +41,11 @@ import' =
4141 (,) name <$> option (As name) method
4242 where
4343 method :: IParser ImportMethod
44− method = try $ do whitespace
45− as' <|> importing'
44+ method = as' <|> importing'
4645
4746 as' :: IParser ImportMethod
4847 as' = do
49− reserved "as"
48+ try (whitespace >> reserved "as")
5049 whitespace
5150 As <$> capVar <?> "alias for module"
5251
@@ -54,13 +53,24 @@ import' =
5453 importing' = Open <$> listing value
5554
5655listing :: IParser a -> IParser (Listing a)
57−listing item =
58− parens (choice [ const (Listing [] True) <$> string ".."
59− , Listing <$> commaSep1 item <*> return False
60− ] <?> "listing of values (x,y,z)")
56+listing item =
57+ do try (whitespace >> char '(')
58+ whitespace
59+ listing <- choice [ const openListing <$> string ".."
60+ , Listing <$> commaSep1 item <*> return False
61+ ] <?> "listing of values (x,y,z)"
62+ whitespace
63+ char ')'
64+ return listing
6165
6266value :: IParser Value
63−value = choice [ Value <$> (lowVar <|> parens symOp)
64− , ADT <$> capVar <*> listing capVar
65− ]
67+value = val <|> tipe
68+ where
69+ val = Value <$> (lowVar <|> parens symOp)
6670
71+ tipe = do
72+ name <- capVar
73+ maybeCtors <- optionMaybe (listing capVar)
74+ case maybeCtors of
75+ Nothing -> return (Alias name)
76+ Just ctors -> return (ADT name ctors)