aboutsummaryrefslogtreecommitdiff
path: root/parser/CC/Parser.hs
blob: 66bc6cfe06e9f6c55f2dc9727c1bcb80bc16c56e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
module CC.Parser(runPass, parseProgram) where

import Control.Monad
import qualified Data.Set as Set
import Text.Parsec hiding (SourcePos, getPosition, token)
import qualified Text.Parsec

import CC.AST.Source
import CC.Context
import CC.Pretty


type Parser a = Parsec String () a

runPass :: Context -> RawString -> Either (PrettyShow ParseError) Program
runPass (Context path (Builtins _ prelude)) (RawString src) = do
    prog1 <- fmapLeft PrettyShow (parseProgram "<prelude>" prelude)
    prog2 <- fmapLeft PrettyShow (parseProgram path src)
    return (prog1 <> prog2)
  where fmapLeft f (Left x) = Left (f x)
        fmapLeft _ (Right x) = Right x

parseProgram :: FilePath -> String -> Either ParseError Program
parseProgram fname src = parse pProgram fname src

pProgram :: Parser Program
pProgram = do
    prog <- Program <$> many pDecl
    emptyLines
    eof
    return prog

pDecl :: Parser Decl
pDecl = choice
    [ DeclType <$> pDeclType
    , DeclAlias <$> pDeclAlias 
    , DeclFunc <$> pDeclFunc ]

pDeclFunc :: Parser FuncDef
pDeclFunc = do
    func <- try $ do
        emptyLines
        name <- pName0 LowerCase <?> "declaration head name"
        return name
    mtyp <- optionMaybe $ do
        symbol "::"
        typ <- pType
        whitespace >> void newline
        emptyLines
        (func', _) <- pName0 LowerCase
        guard (fst func == func')
        return typ
    args <- many (pName LowerCase)
    symbol "="
    expr <- pExpr
    return (FuncDef mtyp func args expr)

pDeclType :: Parser TypeDef
pDeclType = (\(n, a, t) -> TypeDef n a t) <$> pTypedefLike "type"

pDeclAlias :: Parser AliasDef
pDeclAlias = (\(n, a, t) -> AliasDef n a t) <$> pTypedefLike "alias"

pTypedefLike :: String -> Parser ((Name, SourceRange), [(Name, SourceRange)], Type)
pTypedefLike keyword = do
    try (emptyLines >> string keyword >> whitespace1)
    name <- pName0 UpperCase
    args <- many (pName LowerCase)
    symbol "="
    ty <- pType
    return (name, args, ty)

pType :: Parser Type
pType = chainr1 pTypeTerm (symbol "->" >> return TFun)

pTypeTerm :: Parser Type
pTypeTerm = pTypeAtom <|> pTypeCall

pTypeAtom :: Parser Type
pTypeAtom = choice
    [ wordToken "Int" >> return TInt
    , TyVar . fst <$> pName LowerCase
    , pParenType
    , pUnionType ]

pTypeCall :: Parser Type
pTypeCall = do
    (constr, _) <- pName UpperCase
    args <- many pTypeAtom
    return (TNamed constr args)

pParenType :: Parser Type
pParenType = do
    token "("
    tys <- pType `sepBy` token ","
    token ")"
    case tys of
        [ty] -> return ty
        _ -> return (TTup tys)

pUnionType :: Parser Type
pUnionType = do
    token "{"
    tys <- pType `sepBy` token "|"
    token "}"
    case tys of
        [] -> unexpected "empty union type"
        [ty] -> return ty
        _ -> let tyset = Set.fromList tys
             in if Set.size tyset == length tys
                    then return (TUnion tyset)
                    else unexpected "duplicate types in union"

pExpr :: Parser Expr
pExpr = label (pLam <|> pLet <|> pCall) "expression"
  where
    pCall = do
        atoms <- many1 pExprAtom
        annot <- optionMaybe (do symbol "::"
                                 p1 <- getPosition
                                 ty <- pType
                                 p2 <- getPosition
                                 return (ty, SourceRange p1 p2))
        let call = foldl1 (\a b -> Call (mergeRange (range a) (range b)) a b) atoms
        case annot of
            Just (ty, sr) -> return (Annot (mergeRange (range call) sr) call ty)
            Nothing -> return call

    pLam = do
        p1 <- try $ do
            whitespace
            p <- getPosition
            void (char '\\')
            return p
        names <- many1 (pName LowerCase)
        symbol "->"
        body <- pExpr
        p2 <- getPosition
        return (Lam (SourceRange p1 p2) names body)

    pLet = do
        p1 <- try $ do
            whitespace
            p <- getPosition
            void (string "let")
            return p
        afterKeyword p1
      where
        afterKeyword p1 = do
            whitespace1
            lhs <- pName0 LowerCase
            symbol "="
            rhs <- pExpr
            let fullRange rest = mergeRange (SourceRange p1 p1) (range rest)
            choice [ do p1' <- try $ do
                            whitespace
                            p1' <- getPosition
                            void (string "let")
                            return p1'
                        rest <- afterKeyword p1'
                        return (Let (fullRange rest) lhs rhs rest)
                   , do symbol "in"
                        body <- pExpr
                        return (Let (fullRange body) lhs rhs body) ]

pExprAtom :: Parser Expr
pExprAtom =
    choice [ uncurry (flip Int) <$> pInt
           , uncurry (flip Var) <$> pName LowerCase
           , uncurry (flip Constr) <$> pName UpperCase
           , pParenExpr ]

pParenExpr :: Parser Expr
pParenExpr = do
    p1 <- getPosition
    token "("
    exprs <- pExpr `sepBy` token ","
    token ")"
    p2 <- getPosition
    case exprs of
        [expr] -> return expr
        _ -> return (Tup (SourceRange p1 p2) exprs)

pInt :: Parser (Int, SourceRange)
pInt = try (whitespace >> pInt0)
  where
    pInt0 = do
        p1 <- getPosition
        num <- read <$> many1 digit
        p2 <- getPosition
        return (num, SourceRange p1 p2)

data Case = LowerCase | UpperCase

pName0 :: Case -> Parser (Name, SourceRange)
pName0 vcase = do
    p1 <- getPosition
    s <- try $ do
        c <- pWordFirstChar vcase
        cs <- many pWordMidChar
        let s = c : cs
        guard (s `notElem` ["let", "in"])
        return s
    p2 <- getPosition
    notFollowedBy pWordMidChar
    return (s, SourceRange p1 p2)

pWordFirstChar :: Case -> Parser Char
pWordFirstChar LowerCase = lower <|> oneOf wordSymbols
pWordFirstChar UpperCase = upper <|> oneOf wordSymbols

pWordMidChar :: Parser Char
pWordMidChar = alphaNum <|> oneOf wordSymbols

wordSymbols :: [Char]
wordSymbols = "_$#!"

pName :: Case -> Parser (Name, SourceRange)
pName vcase = try (whitespace >> pName0 vcase)

symbol :: String -> Parser ()
symbol s = token s >> (eof <|> void space <|> void (oneOf "(){}[]"))

wordToken :: String -> Parser ()
wordToken s = token s >> notFollowedBy pWordMidChar

token :: String -> Parser ()
token s = try (whitespace >> void (string s))

emptyLines :: Parser ()
emptyLines = (try (whitespace >> newline) >> emptyLines) <|> try (whitespace >> eof) <|> return ()

whitespace, whitespace1 :: Parser ()
whitespace  = void (many  (void (char ' ') <|> void (try (string "\n "))))
whitespace1 = void (many1 (void (char ' ') <|> void (try (string "\n "))))

getPosition :: Parser SourcePos
getPosition = do
    pos <- Text.Parsec.getPosition
    return (SourcePos (sourceLine pos - 1) (sourceColumn pos - 1))