Skip to content

Commit 14a4a1a

Browse files
authored
Merge pull request #811 from tsoding/801
(#801) Finish off the fuzzer fiesta
2 parents bb7bd2c + 2c5d273 commit 14a4a1a

File tree

5 files changed

+59
-252
lines changed

5 files changed

+59
-252
lines changed

HyperNerd.cabal

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,6 @@ executable HyperNerd
152152
-- Base language which the package is written in.
153153
default-language: Haskell2010
154154

155-
executable Fuzzy
156-
ghc-options: -threaded
157-
-Wall
158-
-fwarn-incomplete-patterns
159-
-fwarn-incomplete-uni-patterns
160-
161-
main-is: FuzzyMain.hs
162-
163-
other-modules: Bot.Expr
164-
, HyperNerd.Parser
165-
166-
build-depends: base
167-
, text
168-
, random
169-
, aeson
170-
, bytestring
171-
172-
hs-source-dirs: src
173-
174-
default-language: Haskell2010
175-
176155
executable Markov
177156
ghc-options: -threaded
178157
-Wall
@@ -243,6 +222,7 @@ test-suite HyperNerdTest
243222
, Bot.PollTest
244223
, Bot.Friday
245224
, Bot.FridayTest
225+
, Bot.ExprTest
246226
, Bot.GitHub
247227
, Data.Maybe.Extra
248228
, Data.Time.Extra

src/Bot/Expr.hs

Lines changed: 22 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,46 @@ 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
6370
-- TODO(#600): interpretExprs is not implemented
6471
-- interpretExprs :: NameTable -> [Expr] -> Effect T.Text
6572
-- interpretExprs = undefined

src/FuzzyMain.hs

Lines changed: 0 additions & 216 deletions
This file was deleted.

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+
3+
module Bot.ExprTest where
4+
5+
import Bot.Expr
6+
import qualified Data.Text as T
7+
import HyperNerd.Parser
8+
import Test.HUnit
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Main where
22

3+
import qualified Bot.ExprTest
4+
35
{- Test Suite Conventions
46
~~~~~~~~~~~~~~~~~~~~~~~~~
57
1. If the module we are testing is called `Foo.Bar.Baz`,
@@ -30,6 +32,7 @@ main = do
3032
, Bot.PollTest.spec
3133
, Bot.TwitchTest.spec
3234
, Bot.FridayTest.spec
35+
, Bot.ExprTest.spec
3336
, CommandTest.spec
3437
, Sqlite.EntityPersistenceTest.spec
3538
, Data.Time.ExtraTest.spec

0 commit comments

Comments
 (0)