summaryrefslogtreecommitdiff
path: root/src/LambdaCube/Compiler/Parser.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/LambdaCube/Compiler/Parser.hs')
-rw-r--r--src/LambdaCube/Compiler/Parser.hs51
1 files changed, 22 insertions, 29 deletions
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 40739265..74f7d3d0 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -358,10 +358,8 @@ parseTerm prec = withRange setSI $ case prec of
358 t' <- dbf' fe <$> parseTTerm PrecLam 358 t' <- dbf' fe <$> parseTTerm PrecLam
359 return $ foldr (uncurry f) t' ts 359 return $ foldr (uncurry f) t' ts
360 <|> do expNS $ do 360 <|> do expNS $ do
361 reservedOp "\\" 361 (fe, ts) <- reservedOp "\\" *> telescopePat <* reservedOp "->"
362 (fe, ts) <- telescopePat
363 checkPattern fe 362 checkPattern fe
364 reservedOp "->"
365 t' <- dbf' fe <$> parseTerm PrecLam 363 t' <- dbf' fe <$> parseTerm PrecLam
366 ge <- dsInfo 364 ge <- dsInfo
367 return $ foldr (uncurry (patLam_ id ge)) t' ts 365 return $ foldr (uncurry (patLam_ id ge)) t' ts
@@ -372,30 +370,25 @@ parseTerm prec = withRange setSI $ case prec of
372 (fe, p) <- longPattern 370 (fe, p) <- longPattern
373 (,) p <$> parseRHS (dbf' fe) "->" 371 (,) p <$> parseRHS (dbf' fe) "->"
374 <|> compileGuardTree id id <$> dsInfo <*> (Alts <$> parseSomeGuards (const True)) 372 <|> compileGuardTree id id <$> dsInfo <*> (Alts <$> parseSomeGuards (const True))
375 <|> do t <- parseTerm PrecEq 373 <|> do level PrecEq $ \t -> mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm PrecLam
376 option t $ mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm PrecLam 374 PrecEq -> level PrecAnn $ \t -> SAppV (SBuiltin "'EqCT" `SAppV` SType `SAppV` t) <$ reservedOp "~" <*> parseTTerm PrecAnn
377 PrecEq -> parseTerm PrecAnn >>= \t -> option t $ SAppV2 (SBuiltin "'EqCT" `SAppV` SType) t <$ reservedOp "~" <*> parseTTerm PrecAnn 375 PrecAnn -> level PrecOp $ \t -> SAnn t <$> parseType Nothing
378 PrecAnn -> parseTerm PrecOp >>= \t -> option t $ SAnn t <$> parseType Nothing
379 PrecOp -> join $ calculatePrecs <$> namespace <*> dsInfo <*> (notExp <|> notOp False) where 376 PrecOp -> join $ calculatePrecs <$> namespace <*> dsInfo <*> (notExp <|> notOp False) where
380 notExp = (++) <$> ope <*> option [] (notOp True) 377 notExp = (++) <$> ope <*> notOp True
381 notOp x = (++) <$> try "expression" ((++) <$> ex PrecApp <*> option [] ope) <*> option [] (notOp True) 378 notOp x = (++) <$> try "expression" ((++) <$> ex PrecApp <*> option [] ope) <*> notOp True
382 <|> if x then try "lambda" (ex PrecLam) else mzero 379 <|> if x then option [] (try "lambda" $ ex PrecLam) else mzero
383 ope = pure . Left <$> parseSIName operatorT 380 ope = pure . Left <$> parseSIName operatorT
384 ex pr = pure . Right <$> parseTerm pr 381 ex pr = pure . Right <$> parseTerm pr
385 PrecApp -> 382 PrecApp ->
386 apps' <$> try "record" (sVar upperCase <* reservedOp "{") <*> (commaSep $ lowerCase *> reservedOp "=" *> ((,) Visible <$> parseTerm PrecLam)) <* reservedOp "}" 383 apps' <$> try "record" (sVar upperCase <* reservedOp "{") <*> (commaSep $ lowerCase *> reservedOp "=" *> ((,) Visible <$> parseTerm PrecLam)) <* reservedOp "}"
387 <|> apps' <$> parseTerm PrecSwiz <*> many (hiddenTerm (parseTTerm PrecSwiz) $ parseTerm PrecSwiz) 384 <|> apps' <$> parseTerm PrecSwiz <*> many (hiddenTerm (parseTTerm PrecSwiz) $ parseTerm PrecSwiz)
388 PrecSwiz -> do 385 PrecSwiz -> level PrecProj $ \t -> try "swizzling" $ mkSwizzling t <$> lexeme (char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: String))))
389 t <- parseTerm PrecProj 386 PrecProj -> level PrecAtom $ \t -> try "projection" $ mkProjection t <$ char '.' <*> sepBy1 (sLit . LString <$> lowerCase) (char '.')
390 option t $ mkSwizzling t <$> try "swizzling" (lexeme $ char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: String))))
391 PrecProj -> do
392 t <- parseTerm PrecAtom
393 option t $ try "projection" $ mkProjection t <$ char '.' <*> sepBy1 (sLit . LString <$> lowerCase) (char '.')
394 PrecAtom -> 387 PrecAtom ->
395 sLit . LChar <$> try "char literal" charLiteral 388 sLit . LChar <$> try "char literal" charLiteral
396 <|> sLit . LString <$> stringLiteral 389 <|> sLit . LString <$> stringLiteral
397 <|> sLit . LFloat <$> try "float literal" float 390 <|> sLit . LFloat <$> try "float literal" float
398 <|> sLit . LInt . fromIntegral <$ char '#' <*> natural 391 <|> sLit . LInt . fromIntegral <$ char '#' <*> natural -- todo: remove
399 <|> mkNat <$> namespace <*> natural 392 <|> mkNat <$> namespace <*> natural
400 <|> Wildcard (Wildcard SType) <$ reserved "_" 393 <|> Wildcard (Wildcard SType) <$ reserved "_"
401 <|> char '\'' *> switchNS (parseTerm PrecAtom) 394 <|> char '\'' *> switchNS (parseTerm PrecAtom)
@@ -411,6 +404,8 @@ parseTerm prec = withRange setSI $ case prec of
411 dcls <- localIndentation Ge $ localAbsoluteIndentation $ parseDefs xSLabelEnd 404 dcls <- localIndentation Ge $ localAbsoluteIndentation $ parseDefs xSLabelEnd
412 mkLets True <$> dsInfo <*> pure dcls <* reserved "in" <*> parseTerm PrecLam 405 mkLets True <$> dsInfo <*> pure dcls <* reserved "in" <*> parseTerm PrecLam
413 where 406 where
407 level pr f = parseTerm pr >>= \t -> option t $ f t
408
414 mkSwizzling term = swizzcall 409 mkSwizzling term = swizzcall
415 where 410 where
416 sc c = SBuiltin ['S',c] 411 sc c = SBuiltin ['S',c]
@@ -469,19 +464,17 @@ parseTerm prec = withRange setSI $ case prec of
469 464
470 calculatePrecs :: Namespace -> DesugarInfo -> [Either SIName SExp] -> P SExp 465 calculatePrecs :: Namespace -> DesugarInfo -> [Either SIName SExp] -> P SExp
471 calculatePrecs ns dcls = either fail return . f where 466 calculatePrecs ns dcls = either fail return . f where
472 f (Left op@(_, "-"): xs) = calcPrec' (mkNat ns 0) <$> h op xs 467 f [] = error "impossible"
473 f (Left op: xs) = h op xs <&> \((op, e): oe) -> LeftSection op $ calcPrec' e oe 468 f (Right t: xs) = either (\(op, xs) -> RightSection (calcPrec' t xs) op) (calcPrec' t) <$> cont xs
474 f (Right t: xs) = either (\(op, xs) -> RightSection (calcPrec' t xs) op) (calcPrec' t) <$> cont (Right []) g xs 469 f xs@(Left op@(_, "-"): _) = f $ Right (mkNat ns 0): xs
475 f [] = Left "TODO: better error message @461" 470 f (Left op: xs) = g op xs >>= either (const $ Left "TODO: better error message @476")
476 g op (Right t: xs) = (second ((op, t):) +++ ((op, t):)) <$> cont (Right []) g xs 471 (\((op, e): oe) -> return $ LeftSection op $ calcPrec' e oe)
472 g op (Right t: xs) = (second ((op, t):) +++ ((op, t):)) <$> cont xs
477 g op [] = return $ Left (op, []) 473 g op [] = return $ Left (op, [])
478 g op _ = Left "TODO: better error message @470" 474 g op _ = Left "two operator is not allowed next to each-other"
479 h op (Right t: xs) = ((op, t):) <$> cont [] h xs 475 cont (Left op: xs) = g op xs
480 h op _ = Left "TODO: better error message @472" 476 cont [] = return $ Right []
481 cont :: forall a . a -> (SIName -> [Either SIName SExp] -> Either String a) -> [Either SIName SExp] -> Either String a 477 cont _ = error "impossible"
482 cont _ f (Left op: xs) = f op xs
483 cont e _ [] = return e
484 cont _ _ _ = Left "TODO: better error message @477"
485 478
486 calcPrec' = calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (getFixity dcls . snd) 479 calcPrec' = calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (getFixity dcls . snd)
487 480