From b27bbb421aa9806d1f3d6a524968a2f2df092b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Mon, 20 Feb 2023 22:28:55 +0100 Subject: Range enumeration syntax for lists --- src/Parser.hs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'src/Parser.hs') diff --git a/src/Parser.hs b/src/Parser.hs index 9ba702b..0bc5995 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -75,7 +75,7 @@ osymbol str = void $ try $ (string (TL.pack str) <* notFollowedBy operatorChar) wsymbol str = void $ try $ (string (TL.pack str) <* notFollowedBy wordChar) <* sc operatorChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s) -operatorChar = satisfy $ (`elem` ['+', '-', '*', '/', '=']) +operatorChar = satisfy $ (`elem` ['.', '+', '-', '*', '/', '=']) {-# INLINE operatorChar #-} localState :: TestParser a -> TestParser a @@ -212,13 +212,36 @@ list :: TestParser SomeExpr list = label "list" $ do symbol "[" SomeExpr x <- someExpr + + let enumErr off = parseError $ FancyError off $ S.singleton $ ErrorFail $ T.unpack $ + "list range enumeration not defined for '" <> textExprType x <> "'" choice [do symbol "]" return $ SomeExpr $ UnOp (:[]) x - ,do symbol "," - xs <- listOf typedExpr + + ,do off <- stateOffset <$> getParserState + osymbol ".." + ExprEnumerator fromTo _ <- maybe (enumErr off) return $ exprEnumerator x + y <- typedExpr symbol "]" - return $ SomeExpr $ foldr (BinOp (:)) (Literal []) (x:xs) + return $ SomeExpr $ UnOp fromTo x `App` y + + ,do symbol "," + y <- typedExpr + + choice + [do off <- stateOffset <$> getParserState + osymbol ".." + ExprEnumerator _ fromThenTo <- maybe (enumErr off) return $ exprEnumerator x + z <- typedExpr + symbol "]" + return $ SomeExpr $ UnOp fromThenTo x `App` y `App` z + + ,do symbol "," + xs <- listOf typedExpr + symbol "]" + return $ SomeExpr $ foldr (BinOp (:)) (Literal []) (x:y:xs) + ] ] data SomeExpr = forall a. ExprType a => SomeExpr (Expr a) -- cgit v1.2.3