2012-06-10 04:21:16 +00:00
|
|
|
|
2012-11-23 04:15:59 +00:00
|
|
|
module Parse.Library where
|
2012-06-10 04:21:16 +00:00
|
|
|
|
|
|
|
import Ast
|
2013-05-29 23:20:38 +00:00
|
|
|
import Located
|
2012-06-10 04:21:16 +00:00
|
|
|
import Control.Applicative ((<$>),(<*>))
|
|
|
|
import Control.Monad
|
2012-11-25 04:49:56 +00:00
|
|
|
import Control.Monad.State
|
2013-04-25 05:26:01 +00:00
|
|
|
import Data.Char (isUpper)
|
2012-12-26 22:07:09 +00:00
|
|
|
import Rename (deprime)
|
2012-11-25 04:49:56 +00:00
|
|
|
import Text.Parsec hiding (newline,spaces,State)
|
|
|
|
import Text.Parsec.Indent
|
2012-06-10 04:21:16 +00:00
|
|
|
|
|
|
|
reserveds = [ "if", "then", "else"
|
2013-02-04 10:56:22 +00:00
|
|
|
, "case", "of"
|
2012-06-11 13:11:15 +00:00
|
|
|
, "let", "in"
|
2013-02-04 10:56:22 +00:00
|
|
|
, "data", "type"
|
2012-06-11 13:11:15 +00:00
|
|
|
, "module", "where"
|
2013-05-21 08:02:59 +00:00
|
|
|
, "import", "as", "hiding", "open"
|
2012-06-28 08:52:47 +00:00
|
|
|
, "export", "foreign" ]
|
2012-06-10 04:21:16 +00:00
|
|
|
|
|
|
|
expecting = flip (<?>)
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
type IParser a = ParsecT String () (State SourcePos) a
|
|
|
|
|
|
|
|
iParse :: IParser a -> SourceName -> String -> Either ParseError a
|
|
|
|
iParse aParser source_name input =
|
|
|
|
runIndent source_name $ runParserT aParser () source_name input
|
|
|
|
|
|
|
|
backslashed :: IParser Char
|
2012-06-10 04:21:16 +00:00
|
|
|
backslashed = do { char '\\'; c <- anyChar
|
|
|
|
; return . read $ ['\'','\\',c,'\''] }
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
var :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
var = makeVar (letter <|> char '_' <?> "variable")
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
lowVar :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
lowVar = makeVar (lower <?> "lower case variable")
|
2012-11-25 04:49:56 +00:00
|
|
|
capVar :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
capVar = makeVar (upper <?> "upper case variable")
|
|
|
|
|
2012-12-26 22:07:09 +00:00
|
|
|
rLabel :: IParser String
|
|
|
|
rLabel = deprime <$> lowVar
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
innerVarChar :: IParser Char
|
2012-06-10 04:21:16 +00:00
|
|
|
innerVarChar = alphaNum <|> char '_' <|> char '\'' <?> ""
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
makeVar :: IParser Char -> IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
makeVar p = do v <- (:) <$> p <*> many innerVarChar
|
|
|
|
guard (v `notElem` reserveds)
|
|
|
|
return v
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
reserved :: String -> IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
reserved word =
|
|
|
|
try (string word >> notFollowedBy innerVarChar) >> return word
|
|
|
|
<?> "reserved word '" ++ word ++ "'"
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
anyOp :: IParser String
|
2012-11-23 03:48:54 +00:00
|
|
|
anyOp = betwixt '`' '`' var <|> symOp <?> "infix operator (e.g. +, *, ||)"
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
symOp :: IParser String
|
2012-11-23 03:48:54 +00:00
|
|
|
symOp = do op <- many1 (satisfy isOp)
|
2013-02-04 10:56:22 +00:00
|
|
|
guard (op `notElem` [ "=", "..", "->", "--", "|", "\8594", ":" ])
|
2012-12-25 08:39:18 +00:00
|
|
|
case op of
|
|
|
|
"." -> notFollowedBy lower >> return op
|
2013-02-03 10:40:36 +00:00
|
|
|
"\8728" -> return "."
|
2012-12-25 08:39:18 +00:00
|
|
|
_ -> return op
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
arrow :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
arrow = string "->" <|> string "\8594" <?> "arrow (->)"
|
|
|
|
|
2013-02-04 10:56:22 +00:00
|
|
|
hasType :: IParser String
|
|
|
|
hasType = string ":" <?> "':' (a type annotation)'"
|
|
|
|
|
2012-06-12 06:28:45 +00:00
|
|
|
|
|
|
|
commitIf check p = commit <|> try p
|
|
|
|
where commit = do (try $ lookAhead check) >> p
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
spaceySepBy1 :: IParser b -> IParser a -> IParser [a]
|
2012-06-12 06:28:45 +00:00
|
|
|
spaceySepBy1 sep p = do
|
|
|
|
a <- p
|
|
|
|
(a:) <$> many (commitIf (whitespace >> sep) (whitespace >> sep >> whitespace >> p))
|
|
|
|
|
2012-06-11 13:11:15 +00:00
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
commaSep1 :: IParser a -> IParser [a]
|
2012-06-12 06:28:45 +00:00
|
|
|
commaSep1 = spaceySepBy1 (char ',' <?> "comma ','")
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
commaSep :: IParser a -> IParser [a]
|
2012-06-12 06:28:45 +00:00
|
|
|
commaSep = option [] . commaSep1
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
semiSep1 :: IParser a -> IParser [a]
|
2012-06-12 06:28:45 +00:00
|
|
|
semiSep1 = spaceySepBy1 (char ';' <?> "semicolon ';'")
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
pipeSep1 :: IParser a -> IParser [a]
|
2012-06-12 06:28:45 +00:00
|
|
|
pipeSep1 = spaceySepBy1 (char '|' <?> "type divider '|'")
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
consSep1 :: IParser a -> IParser [a]
|
2013-02-05 11:19:35 +00:00
|
|
|
consSep1 = spaceySepBy1 (string "::" <?> "cons operator '::'")
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
dotSep1 :: IParser a -> IParser [a]
|
2012-06-11 13:11:15 +00:00
|
|
|
dotSep1 p = (:) <$> p <*> many (try (char '.') >> p)
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
spaceSep1 :: IParser a -> IParser [a]
|
2012-06-10 04:21:16 +00:00
|
|
|
spaceSep1 p = (:) <$> p <*> spacePrefix p
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
spacePrefix p = many (try (whitespace >> indented >> p))
|
2012-06-12 06:28:45 +00:00
|
|
|
|
|
|
|
followedBy a b = do x <- a ; b ; return x
|
2012-06-10 04:21:16 +00:00
|
|
|
|
|
|
|
betwixt a b c = do char a ; out <- c
|
|
|
|
char b <?> "closing '" ++ [b] ++ "'" ; return out
|
|
|
|
|
|
|
|
surround a z name p = do
|
2012-11-24 21:00:56 +00:00
|
|
|
char a ; whitespace ; v <- p ; whitespace
|
2012-06-10 04:21:16 +00:00
|
|
|
char z <?> unwords ["closing", name, show z]
|
2012-11-24 21:00:56 +00:00
|
|
|
return v
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
braces :: IParser a -> IParser a
|
2012-06-10 04:21:16 +00:00
|
|
|
braces = surround '[' ']' "brace"
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
parens :: IParser a -> IParser a
|
2012-06-10 04:21:16 +00:00
|
|
|
parens = surround '(' ')' "paren"
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
brackets :: IParser a -> IParser a
|
2012-06-10 04:21:16 +00:00
|
|
|
brackets = surround '{' '}' "bracket"
|
|
|
|
|
2013-05-29 23:20:38 +00:00
|
|
|
addLocation :: IParser Expr -> IParser CExpr
|
|
|
|
addLocation expr = do
|
2012-12-25 08:39:18 +00:00
|
|
|
start <- getPosition
|
|
|
|
e <- expr
|
|
|
|
end <- getPosition
|
|
|
|
return (pos start end e)
|
|
|
|
|
|
|
|
accessible :: IParser CExpr -> IParser CExpr
|
2012-06-10 04:21:16 +00:00
|
|
|
accessible expr = do
|
2012-12-25 08:39:18 +00:00
|
|
|
start <- getPosition
|
2013-05-29 23:20:38 +00:00
|
|
|
ce@(L s t e) <- expr
|
2012-12-26 22:07:09 +00:00
|
|
|
let rest f = do
|
|
|
|
let dot = char '.' >> notFollowedBy (char '.')
|
|
|
|
access <- optionMaybe (try dot <?> "field access (e.g. List.map)")
|
|
|
|
case access of
|
|
|
|
Nothing -> return ce
|
|
|
|
Just _ -> accessible $ do
|
|
|
|
v <- var <?> "field access (e.g. List.map)"
|
|
|
|
end <- getPosition
|
|
|
|
return (pos start end (f v))
|
|
|
|
case e of Var (c:cs) | isUpper c -> rest (\v -> Var (c:cs ++ '.':v))
|
|
|
|
| otherwise -> rest (Access ce)
|
|
|
|
_ -> rest (Access ce)
|
2012-06-10 04:21:16 +00:00
|
|
|
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
spaces :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
spaces = many1 ((multiComment <|> string " " <?> "") >> return ' ') <?> "spaces"
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
forcedWS :: IParser [String]
|
2012-06-10 04:21:16 +00:00
|
|
|
forcedWS = try (do { spaces; many nl_space }) <|> try (many1 nl_space)
|
|
|
|
where nl_space = try $ many1 newline >> spaces
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
whitespace :: IParser ()
|
2012-06-10 04:21:16 +00:00
|
|
|
whitespace = optional forcedWS <?> ""
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
freshLine :: IParser [[String]]
|
2013-02-27 07:33:47 +00:00
|
|
|
freshLine = try (many1 newline >> many space_nl) <|> try (many1 space_nl) <?> ""
|
2012-06-10 04:21:16 +00:00
|
|
|
where space_nl = try $ spaces >> many1 newline
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
newline :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
newline = simpleNewline <|> lineComment <?> ""
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
simpleNewline :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
simpleNewline = try (string "\r\n") <|> string "\n"
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
lineComment :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
lineComment = do try (string "--")
|
|
|
|
manyTill anyChar $ simpleNewline <|> (eof >> return "\n")
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
multiComment :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
multiComment = do { try (string "{-"); closeComment }
|
|
|
|
|
2012-11-25 04:49:56 +00:00
|
|
|
closeComment :: IParser String
|
2012-06-10 04:21:16 +00:00
|
|
|
closeComment = manyTill anyChar . choice $
|
|
|
|
[ try (string "-}") <?> "close comment"
|
|
|
|
, do { try $ string "{-"; closeComment; closeComment }
|
|
|
|
]
|