-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c209ca4
commit abefc84
Showing
5 changed files
with
156 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Expr where | ||
|
||
import Data.Maybe (fromMaybe) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import Text.Parsec (many, many1, optionMaybe, parse, sepBy, | ||
(<|>)) | ||
import Text.Parsec.Char (char, digit, letter, spaces) | ||
import Text.Parsec.Error (ParseError) | ||
import Text.Parsec.Text (Parser) | ||
|
||
-- $setup | ||
-- >>> :set -XOverloadedStrings | ||
|
||
data Expr = IntLiteral Integer | ||
| StringLiteral Text | ||
| FunctionCall { fcName :: Text, fcArgs :: [Expr] } | ||
deriving (Show, Eq) | ||
|
||
expr :: Parser Expr | ||
expr = literal <|> functionCall | ||
|
||
literal :: Parser Expr | ||
literal = number <|> stringLiteral | ||
|
||
number :: Parser Expr | ||
number = IntLiteral . read <$> many1 digit | ||
|
||
stringLiteral :: Parser Expr | ||
stringLiteral = do | ||
_ <- char '\'' | ||
content <- many1 letter | ||
_ <- char '\'' | ||
pure $ StringLiteral $ T.pack content | ||
|
||
|
||
functionCall :: Parser Expr | ||
functionCall = do | ||
name <- ident | ||
args <- fromMaybe [] <$> optionMaybe functionArgs | ||
pure $ FunctionCall name args | ||
where | ||
functionArgs = do | ||
_ <- char '(' | ||
args <- expr `sepBy` (char ',' >> spaces) | ||
_ <- char ')' | ||
pure args | ||
|
||
|
||
ident :: Parser Text | ||
ident = do | ||
firstChar <- letter | ||
next <- many (digit <|> letter) | ||
pure $ T.pack (firstChar : next) | ||
|
||
|
||
|
||
-- | Parse an expression | ||
-- | ||
-- >>> parseExpr "20" | ||
-- Right (IntLiteral 20) | ||
-- | ||
-- >>> parseExpr "uuid4" | ||
-- Right (FunctionCall {fcName = "uuid4", fcArgs = []}) | ||
-- | ||
-- >>> parseExpr "randomInt(0, 10)" | ||
-- Right (FunctionCall {fcName = "randomInt", fcArgs = [IntLiteral 0,IntLiteral 10]}) | ||
parseExpr :: Text -> Either ParseError Expr | ||
parseExpr = parse expr "(unknown)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
import Test.DocTest | ||
|
||
main :: IO () | ||
main = doctest | ||
[ "-isrc" | ||
, "src/Expr.hs" | ||
, "app/Main.hs" | ||
] |
File renamed without changes.