on master help sign in
Correct parsing of listings
404c442Evan Czaplicki
compiler/AST/Variable.hs+2−2
@@ -43,7 +43,7 @@ instance ToString Canonical where
43data Listing a = Listing
43data Listing a = Listing
44 { _explicits :: [a]
44 { _explicits :: [a]
45 , _open :: Bool
45 , _open :: Bool
46− } deriving (Eq,Ord)
46+ } deriving (Eq,Ord,Show)
47
47
48openListing :: Listing a
48openListing :: Listing a
49openListing = Listing [] True
49openListing = Listing [] True
@@ -53,7 +53,7 @@ data Value
53 = Value !String
53 = Value !String
54 | Alias !String
54 | Alias !String
55 | ADT !String !(Listing String)
55 | ADT !String !(Listing String)
56− deriving (Eq,Ord)
56+ deriving (Eq,Ord,Show)
57
57
58instance Pretty Raw where
58instance Pretty Raw where
59 pretty (Raw var) = variable var
59 pretty (Raw var) = variable var
compiler/Parse/Module.hs+21−11
@@ -6,7 +6,7 @@ import Text.Parsec hiding (newline,spaces)
6
6
7import Parse.Helpers
7import Parse.Helpers
8import AST.Module (ImportMethod(..))
8import AST.Module (ImportMethod(..))
9−import AST.Variable (Listing(..), Value(..))
9+import AST.Variable (Listing(..), Value(..), openListing)
10
10
11getModuleName :: String -> Maybe String
11getModuleName :: String -> Maybe String
12getModuleName source =
12getModuleName source =
@@ -41,12 +41,11 @@ import' =
41 (,) name <$> option (As name) method
41 (,) name <$> option (As name) method
42 where
42 where
43 method :: IParser ImportMethod
43 method :: IParser ImportMethod
44− method = try $ do whitespace
45− as' <|> importing'
44+ method = as' <|> importing'
46
45
47 as' :: IParser ImportMethod
46 as' :: IParser ImportMethod
48 as' = do
47 as' = do
49− reserved "as"
48+ try (whitespace >> reserved "as")
50 whitespace
49 whitespace
51 As <$> capVar <?> "alias for module"
50 As <$> capVar <?> "alias for module"
52
51
@@ -54,13 +53,24 @@ import' =
54 importing' = Open <$> listing value
53 importing' = Open <$> listing value
55
54
56listing :: IParser a -> IParser (Listing a)
55listing :: IParser a -> IParser (Listing a)
57−listing item =
56+listing item =
57+ do try (whitespace >> char '(')
58+ whitespace
58− parens (choice [ const (Listing [] True) <$> string ".."
59+ listing <- choice [ const openListing <$> string ".."
59− , Listing <$> commaSep1 item <*> return False
60+ , Listing <$> commaSep1 item <*> return False
60− ] <?> "listing of values (x,y,z)")
61+ ] <?> "listing of values (x,y,z)"
62+ whitespace
63+ char ')'
64+ return listing
61
65
62value :: IParser Value
66value :: IParser Value
67+value = val <|> tipe
68+ where
63−value = choice [ Value <$> (lowVar <|> parens symOp)
69+ val = Value <$> (lowVar <|> parens symOp)
64− , ADT <$> capVar <*> listing capVar
65− ]
66
70
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)