on a3801ac help sign in
elk/compiler/Parse/Library.hs
183 lines 5.5 kB history raw
2module Parse.Library where
4import Ast
5import Located
6import Control.Applicative ((<$>),(<*>))
7import Control.Monad
8import Control.Monad.State
9import Data.Char (isUpper)
10import Rename (deprime)
11import Text.Parsec hiding (newline,spaces,State)
12import Text.Parsec.Indent
14reserveds = [ "if", "then", "else"
15 , "case", "of"
16 , "let", "in"
17 , "data", "type"
18 , "module", "where"
19 , "import", "as", "hiding", "open"
20 , "export", "foreign" ]
22expecting = flip (<?>)
24type IParser a = ParsecT String () (State SourcePos) a
26iParse :: IParser a -> SourceName -> String -> Either ParseError a
27iParse aParser source_name input =
28 runIndent source_name $ runParserT aParser () source_name input
30backslashed :: IParser Char
31backslashed = do { char '\\'; c <- anyChar
32 ; return . read $ ['\'','\\',c,'\''] }
34var :: IParser String
35var = makeVar (letter <|> char '_' <?> "variable")
37lowVar :: IParser String
38lowVar = makeVar (lower <?> "lower case variable")
39capVar :: IParser String
40capVar = makeVar (upper <?> "upper case variable")
42rLabel :: IParser String
43rLabel = deprime <$> lowVar
45innerVarChar :: IParser Char
46innerVarChar = alphaNum <|> char '_' <|> char '\'' <?> ""
48makeVar :: IParser Char -> IParser String
49makeVar p = do v <- (:) <$> p <*> many innerVarChar
50 guard (v `notElem` reserveds)
51 return v
53reserved :: String -> IParser String
54reserved word =
55 try (string word >> notFollowedBy innerVarChar) >> return word
56 <?> "reserved word '" ++ word ++ "'"
58anyOp :: IParser String
59anyOp = betwixt '`' '`' var <|> symOp <?> "infix operator (e.g. +, *, ||)"
61symOp :: IParser String
62symOp = do op <- many1 (satisfy isOp)
63 guard (op `notElem` [ "=", "..", "->", "--", "|", "\8594", ":" ])
64 case op of
65 "." -> notFollowedBy lower >> return op
66 "\8728" -> return "."
67 _ -> return op
69arrow :: IParser String
70arrow = string "->" <|> string "\8594" <?> "arrow (->)"
72hasType :: IParser String
73hasType = string ":" <?> "':' (a type annotation)'"
76commitIf check p = commit <|> try p
77 where commit = do (try $ lookAhead check) >> p
79spaceySepBy1 :: IParser b -> IParser a -> IParser [a]
80spaceySepBy1 sep p = do
81 a <- p
82 (a:) <$> many (commitIf (whitespace >> sep) (whitespace >> sep >> whitespace >> p))
85commaSep1 :: IParser a -> IParser [a]
86commaSep1 = spaceySepBy1 (char ',' <?> "comma ','")
88commaSep :: IParser a -> IParser [a]
89commaSep = option [] . commaSep1
91semiSep1 :: IParser a -> IParser [a]
92semiSep1 = spaceySepBy1 (char ';' <?> "semicolon ';'")
94pipeSep1 :: IParser a -> IParser [a]
95pipeSep1 = spaceySepBy1 (char '|' <?> "type divider '|'")
97consSep1 :: IParser a -> IParser [a]
98consSep1 = spaceySepBy1 (string "::" <?> "cons operator '::'")
100dotSep1 :: IParser a -> IParser [a]
101dotSep1 p = (:) <$> p <*> many (try (char '.') >> p)
103spaceSep1 :: IParser a -> IParser [a]
104spaceSep1 p = (:) <$> p <*> spacePrefix p
106spacePrefix p = many (try (whitespace >> indented >> p))
108followedBy a b = do x <- a ; b ; return x
110betwixt a b c = do char a ; out <- c
111 char b <?> "closing '" ++ [b] ++ "'" ; return out
113surround a z name p = do
114 char a ; whitespace ; v <- p ; whitespace
115 char z <?> unwords ["closing", name, show z]
116 return v
118braces :: IParser a -> IParser a
119braces = surround '[' ']' "brace"
121parens :: IParser a -> IParser a
122parens = surround '(' ')' "paren"
124brackets :: IParser a -> IParser a
125brackets = surround '{' '}' "bracket"
127addLocation :: IParser Expr -> IParser CExpr
128addLocation expr = do
129 start <- getPosition
130 e <- expr
131 end <- getPosition
132 return (pos start end e)
134accessible :: IParser CExpr -> IParser CExpr
135accessible expr = do
136 start <- getPosition
137 ce@(L s t e) <- expr
138 let rest f = do
139 let dot = char '.' >> notFollowedBy (char '.')
140 access <- optionMaybe (try dot <?> "field access (e.g. List.map)")
141 case access of
142 Nothing -> return ce
143 Just _ -> accessible $ do
144 v <- var <?> "field access (e.g. List.map)"
145 end <- getPosition
146 return (pos start end (f v))
147 case e of Var (c:cs) | isUpper c -> rest (\v -> Var (c:cs ++ '.':v))
148 | otherwise -> rest (Access ce)
149 _ -> rest (Access ce)
152spaces :: IParser String
153spaces = many1 ((multiComment <|> string " " <?> "") >> return ' ') <?> "spaces"
155forcedWS :: IParser [String]
156forcedWS = try (do { spaces; many nl_space }) <|> try (many1 nl_space)
157 where nl_space = try $ many1 newline >> spaces
159whitespace :: IParser ()
160whitespace = optional forcedWS <?> ""
162freshLine :: IParser [[String]]
163freshLine = try (many1 newline >> many space_nl) <|> try (many1 space_nl) <?> ""
164 where space_nl = try $ spaces >> many1 newline
166newline :: IParser String
167newline = simpleNewline <|> lineComment <?> ""
169simpleNewline :: IParser String
170simpleNewline = try (string "\r\n") <|> string "\n"
172lineComment :: IParser String
173lineComment = do try (string "--")
174 manyTill anyChar $ simpleNewline <|> (eof >> return "\n")
176multiComment :: IParser String
177multiComment = do { try (string "{-"); closeComment }
179closeComment :: IParser String
180closeComment = manyTill anyChar . choice $
181 [ try (string "-}") <?> "close comment"
182 , do { try $ string "{-"; closeComment; closeComment }
183 ]