Skip to content

Commit db50c5c

Browse files
committed
(#801) Simplify the syntax of Custom Command DSL
There are no variable anymore. Only function calls.
1 parent bb7bd2c commit db50c5c

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

HyperNerd.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ executable Fuzzy
168168
, random
169169
, aeson
170170
, bytestring
171+
, process
171172

172173
hs-source-dirs: src
173174

@@ -243,6 +244,7 @@ test-suite HyperNerdTest
243244
, Bot.PollTest
244245
, Bot.Friday
245246
, Bot.FridayTest
247+
, Bot.ExprTest
246248
, Bot.GitHub
247249
, Data.Maybe.Extra
248250
, Data.Time.Extra

src/Bot/Expr.hs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ data Expr
1313
= TextExpr T.Text
1414
| FunCallExpr T.Text
1515
[Expr]
16-
| VarExpr T.Text
1716
deriving (Eq, Show)
1817

1918
type NameTable = ()
@@ -28,38 +27,47 @@ stringLiteral = do
2827
_ <- charP '"'
2928
return $ TextExpr value
3029

31-
funcallarg :: Parser Expr
32-
funcallarg = funcall <|> var <|> stringLiteral
30+
funCallArg :: Parser Expr
31+
funCallArg = funCall <|> stringLiteral
3332

34-
funcall :: Parser Expr
35-
funcall = do
36-
_ <- charP '%'
37-
name <- symbol
38-
_ <- whitespaces >> charP '(' >> whitespaces
33+
funCallArgList :: Parser [Expr]
34+
funCallArgList = do
35+
_ <- charP '(' <* whitespaces
3936
args <-
40-
sepBy funcallarg (whitespaces >> charP ',' >> whitespaces) <|> return []
37+
sepBy funCallArg (whitespaces >> charP ',' >> whitespaces) <|> return []
4138
_ <- whitespaces >> charP ')'
39+
return args
40+
41+
funCall :: Parser Expr
42+
funCall = do
43+
name <- charP '%' *> symbol
44+
args <- funCallArgList <|> return []
4245
return $ FunCallExpr name args
4346

4447
whitespaces :: Parser T.Text
4548
whitespaces = takeWhileP isSpace
4649

47-
var :: Parser Expr
48-
var = charP '%' *> (VarExpr <$> symbol) <* charP '%'
49-
5050
textBlock :: Parser Expr
5151
textBlock =
5252
Parser $ \input ->
5353
case T.uncons input of
5454
Nothing -> Left EOF
55-
Just ('%', _) -> Left (SyntaxError "Text block does not start with %")
55+
Just ('%', input') ->
56+
return $ fmap (TextExpr . T.cons '%') $ swap $ T.span (/= '%') input'
5657
_ -> return $ fmap TextExpr $ swap $ T.span (/= '%') input
5758

5859
expr :: Parser Expr
59-
expr = funcall <|> var <|> textBlock
60+
expr = funCall <|> textBlock
6061

6162
exprs :: Parser [Expr]
62-
exprs = many expr
63+
exprs = normalizeExprs <$> many expr
64+
where
65+
normalizeExprs :: [Expr] -> [Expr]
66+
normalizeExprs [] = []
67+
normalizeExprs (TextExpr t1:TextExpr t2:rest) =
68+
normalizeExprs (TextExpr (t1 <> t2) : rest)
69+
normalizeExprs (x:rest) = x : normalizeExprs rest
70+
6371
-- TODO(#600): interpretExprs is not implemented
6472
-- interpretExprs :: NameTable -> [Expr] -> Effect T.Text
6573
-- interpretExprs = undefined

test/Bot/ExprTest.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE QuasiQuotes #-}
3+
module Bot.ExprTest where
4+
5+
import Test.HUnit
6+
import Bot.Expr
7+
import HyperNerd.Parser
8+
import qualified Data.Text as T
9+
10+
spec :: Test
11+
spec =
12+
TestLabel "Parsing Custom Command DSL" $
13+
TestList $
14+
map
15+
(\(input, expected) ->
16+
TestCase $
17+
assertEqual
18+
("Cannot parse `" <> input <> "` as Custom Command DSL")
19+
expected
20+
(runParser exprs $ T.pack input))
21+
[ ("Hello world", Right ("", [TextExpr "Hello world"]))
22+
, ("%Hello world", Right ("", [FunCallExpr "Hello" [], TextExpr " world"]))
23+
, ("%Helloworld", Right ("", [FunCallExpr "Helloworld" []]))
24+
, ("%Hello()world", Right ("", [FunCallExpr "Hello" [], TextExpr "world"]))
25+
, ( "%Hello()%world"
26+
, Right ("", [FunCallExpr "Hello" [], FunCallExpr "world" []]))
27+
, ( "%Hello ()%world"
28+
, Right
29+
("", [FunCallExpr "Hello" [], TextExpr " ()", FunCallExpr "world" []]))
30+
, ( "% Hello ()%world"
31+
, Right ("", [TextExpr "% Hello ()", FunCallExpr "world" []]))
32+
, ("%%%%%%", Right ("", [TextExpr "%%%%%%"]))
33+
]

test/Test.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import qualified Bot.LinksTest
1414
import qualified Bot.LogTest
1515
import qualified Bot.PollTest
1616
import qualified Bot.TwitchTest
17+
import qualified Bot.ExprTest
1718
import qualified CommandTest
1819
import qualified Data.Time.ExtraTest
1920
import qualified Sqlite.EntityPersistenceTest
@@ -30,6 +31,7 @@ main = do
3031
, Bot.PollTest.spec
3132
, Bot.TwitchTest.spec
3233
, Bot.FridayTest.spec
34+
, Bot.ExprTest.spec
3335
, CommandTest.spec
3436
, Sqlite.EntityPersistenceTest.spec
3537
, Data.Time.ExtraTest.spec

0 commit comments

Comments
 (0)