2015-08-03 10:19:23 +03:00
|
|
|
|
--
|
|
|
|
|
-- QuickCheck tests for Megaparsec's expression parsers.
|
|
|
|
|
--
|
2016-01-09 15:56:33 +03:00
|
|
|
|
-- Copyright © 2015–2016 Megaparsec contributors
|
2015-08-03 10:19:23 +03:00
|
|
|
|
--
|
|
|
|
|
-- Redistribution and use in source and binary forms, with or without
|
|
|
|
|
-- modification, are permitted provided that the following conditions are
|
|
|
|
|
-- met:
|
|
|
|
|
--
|
|
|
|
|
-- * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
-- this list of conditions and the following disclaimer.
|
|
|
|
|
--
|
|
|
|
|
-- * Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
-- notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
-- documentation and/or other materials provided with the distribution.
|
|
|
|
|
--
|
2015-10-30 16:41:21 +03:00
|
|
|
|
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS “AS IS” AND ANY
|
|
|
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
|
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
2016-04-10 12:28:04 +03:00
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
module Expr (tests) where
|
|
|
|
|
|
2015-08-29 13:54:45 +03:00
|
|
|
|
import Control.Applicative (some, (<|>))
|
2015-11-03 14:42:01 +03:00
|
|
|
|
import Data.Char (isDigit, digitToInt)
|
2015-08-29 13:54:45 +03:00
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
import Test.Framework
|
2015-08-29 13:54:45 +03:00
|
|
|
|
import Test.Framework.Providers.QuickCheck2 (testProperty)
|
|
|
|
|
import Test.QuickCheck
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
2015-08-29 13:54:45 +03:00
|
|
|
|
import Text.Megaparsec.Char
|
|
|
|
|
import Text.Megaparsec.Combinator
|
2015-08-03 10:19:23 +03:00
|
|
|
|
import Text.Megaparsec.Expr
|
2015-08-29 13:54:45 +03:00
|
|
|
|
import Text.Megaparsec.Prim
|
|
|
|
|
|
|
|
|
|
import Util
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
2015-09-30 20:30:50 +03:00
|
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
|
|
|
|
import Control.Applicative ((<$>), (<*), (<*>), (*>), pure)
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
tests :: Test
|
|
|
|
|
tests = testGroup "Expression parsers"
|
2016-02-08 10:30:57 +03:00
|
|
|
|
[ testProperty "correctness of expression parser" prop_correctness
|
|
|
|
|
, testProperty "error message on empty input" prop_empty_error
|
|
|
|
|
, testProperty "error message on missing term" prop_missing_term
|
|
|
|
|
, testProperty "error message on missing op" prop_missing_op ]
|
2015-08-29 13:54:45 +03:00
|
|
|
|
|
|
|
|
|
-- Algebraic structures to build abstract syntax tree of our expression.
|
|
|
|
|
|
|
|
|
|
data Node
|
|
|
|
|
= Val Integer -- ^ literal value
|
|
|
|
|
| Neg Node -- ^ negation (prefix unary)
|
|
|
|
|
| Fac Node -- ^ factorial (postfix unary)
|
|
|
|
|
| Mod Node Node -- ^ modulo
|
|
|
|
|
| Sum Node Node -- ^ summation (addition)
|
|
|
|
|
| Sub Node Node -- ^ subtraction
|
|
|
|
|
| Pro Node Node -- ^ product
|
|
|
|
|
| Div Node Node -- ^ division
|
|
|
|
|
| Exp Node Node -- ^ exponentiation
|
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
|
|
instance Enum Node where
|
|
|
|
|
fromEnum (Val _) = 0
|
|
|
|
|
fromEnum (Neg _) = 0
|
|
|
|
|
fromEnum (Fac _) = 0
|
|
|
|
|
fromEnum (Mod _ _) = 0
|
|
|
|
|
fromEnum (Exp _ _) = 1
|
|
|
|
|
fromEnum (Pro _ _) = 2
|
|
|
|
|
fromEnum (Div _ _) = 2
|
|
|
|
|
fromEnum (Sum _ _) = 3
|
|
|
|
|
fromEnum (Sub _ _) = 3
|
|
|
|
|
toEnum _ = error "Oops!"
|
|
|
|
|
|
|
|
|
|
instance Ord Node where
|
|
|
|
|
x `compare` y = fromEnum x `compare` fromEnum y
|
|
|
|
|
|
|
|
|
|
showNode :: Node -> String
|
|
|
|
|
showNode (Val x) = show x
|
|
|
|
|
showNode n@(Neg x) = "-" ++ showGT n x
|
|
|
|
|
showNode n@(Fac x) = showGT n x ++ "!"
|
|
|
|
|
showNode n@(Mod x y) = showGE n x ++ " % " ++ showGE n y
|
|
|
|
|
showNode n@(Sum x y) = showGT n x ++ " + " ++ showGE n y
|
|
|
|
|
showNode n@(Sub x y) = showGT n x ++ " - " ++ showGE n y
|
|
|
|
|
showNode n@(Pro x y) = showGT n x ++ " * " ++ showGE n y
|
|
|
|
|
showNode n@(Div x y) = showGT n x ++ " / " ++ showGE n y
|
|
|
|
|
showNode n@(Exp x y) = showGE n x ++ " ^ " ++ showGT n y
|
|
|
|
|
|
|
|
|
|
showGT :: Node -> Node -> String
|
2015-10-22 11:34:25 +03:00
|
|
|
|
showGT parent node = (if node > parent then showCmp else showNode) node
|
2015-08-29 13:54:45 +03:00
|
|
|
|
|
|
|
|
|
showGE :: Node -> Node -> String
|
2015-10-22 11:34:25 +03:00
|
|
|
|
showGE parent node = (if node >= parent then showCmp else showNode) node
|
2015-08-29 13:54:45 +03:00
|
|
|
|
|
|
|
|
|
showCmp :: Node -> String
|
2015-10-22 11:34:25 +03:00
|
|
|
|
showCmp node = (if fromEnum node == 0 then showNode else inParens) node
|
2015-08-29 13:54:45 +03:00
|
|
|
|
|
|
|
|
|
inParens :: Node -> String
|
|
|
|
|
inParens x = "(" ++ showNode x ++ ")"
|
|
|
|
|
|
|
|
|
|
instance Arbitrary Node where
|
|
|
|
|
arbitrary = sized arbitraryN0
|
|
|
|
|
|
|
|
|
|
arbitraryN0 :: Int -> Gen Node
|
|
|
|
|
arbitraryN0 n = frequency [ (1, Mod <$> leaf <*> leaf)
|
|
|
|
|
, (9, arbitraryN1 n) ]
|
|
|
|
|
where leaf = arbitraryN1 (n `div` 2)
|
|
|
|
|
|
|
|
|
|
arbitraryN1 :: Int -> Gen Node
|
2015-11-03 13:03:22 +03:00
|
|
|
|
arbitraryN1 n =
|
|
|
|
|
frequency [ (1, Neg <$> arbitraryN2 n)
|
|
|
|
|
, (1, Fac <$> arbitraryN2 n)
|
|
|
|
|
, (7, arbitraryN2 n)]
|
|
|
|
|
|
|
|
|
|
arbitraryN2 :: Int -> Gen Node
|
|
|
|
|
arbitraryN2 0 = Val . getNonNegative <$> arbitrary
|
|
|
|
|
arbitraryN2 n = elements [Sum,Sub,Pro,Div,Exp] <*> leaf <*> leaf
|
2015-08-29 13:54:45 +03:00
|
|
|
|
where leaf = arbitraryN0 (n `div` 2)
|
|
|
|
|
|
2015-10-04 21:27:34 +03:00
|
|
|
|
-- Some helpers are put here since we don't want to depend on
|
2015-08-30 13:00:07 +03:00
|
|
|
|
-- "Text.Megaparsec.Lexer".
|
2015-08-29 13:54:45 +03:00
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
lexeme :: MonadParsec s m Char => m a -> m a
|
2015-08-29 13:54:45 +03:00
|
|
|
|
lexeme p = p <* hidden space
|
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
symbol :: MonadParsec s m Char => String -> m String
|
2015-08-29 13:54:45 +03:00
|
|
|
|
symbol = lexeme . string
|
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
parens :: MonadParsec s m Char => m a -> m a
|
2015-08-29 13:54:45 +03:00
|
|
|
|
parens = between (symbol "(") (symbol ")")
|
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
integer :: MonadParsec s m Char => m Integer
|
2015-08-29 13:54:45 +03:00
|
|
|
|
integer = lexeme (read <$> some digitChar <?> "integer")
|
|
|
|
|
|
|
|
|
|
-- Here we use table of operators that makes use of all features of
|
|
|
|
|
-- 'makeExprParser'. Then we generate abstract syntax tree (AST) of complex
|
|
|
|
|
-- but valid expressions and render them to get their textual
|
|
|
|
|
-- representation.
|
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
expr :: MonadParsec s m Char => m Node
|
2015-10-15 21:01:15 +03:00
|
|
|
|
expr = makeExprParser term table
|
2015-08-29 13:54:45 +03:00
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
term :: MonadParsec s m Char => m Node
|
2015-08-29 13:54:45 +03:00
|
|
|
|
term = parens expr <|> (Val <$> integer) <?> "term"
|
|
|
|
|
|
2015-09-18 12:41:18 +03:00
|
|
|
|
table :: MonadParsec s m Char => [[Operator m Node]]
|
2015-08-29 13:54:45 +03:00
|
|
|
|
table = [ [ Prefix (symbol "-" *> pure Neg)
|
|
|
|
|
, Postfix (symbol "!" *> pure Fac)
|
|
|
|
|
, InfixN (symbol "%" *> pure Mod) ]
|
|
|
|
|
, [ InfixR (symbol "^" *> pure Exp) ]
|
|
|
|
|
, [ InfixL (symbol "*" *> pure Pro)
|
|
|
|
|
, InfixL (symbol "/" *> pure Div) ]
|
|
|
|
|
, [ InfixL (symbol "+" *> pure Sum)
|
|
|
|
|
, InfixL (symbol "-" *> pure Sub)] ]
|
|
|
|
|
|
|
|
|
|
prop_correctness :: Node -> Property
|
|
|
|
|
prop_correctness node = checkParser expr (Right node) (showNode node)
|
2015-11-03 14:42:01 +03:00
|
|
|
|
|
|
|
|
|
prop_empty_error :: Property
|
|
|
|
|
prop_empty_error = checkParser expr r s
|
|
|
|
|
where r = posErr 0 s [uneEof, exSpec "term"]
|
|
|
|
|
s = ""
|
|
|
|
|
|
|
|
|
|
prop_missing_term :: Char -> Property
|
|
|
|
|
prop_missing_term c = checkParser expr r s
|
|
|
|
|
where r | c `elem` "-(" = posErr 1 s [uneEof, exSpec "term"]
|
|
|
|
|
| isDigit c = Right . Val . fromIntegral . digitToInt $ c
|
|
|
|
|
| otherwise = posErr 0 s [uneCh c, exSpec "term"]
|
|
|
|
|
s = pure c
|
|
|
|
|
|
|
|
|
|
prop_missing_op :: Node -> Node -> Property
|
|
|
|
|
prop_missing_op a b = checkParser expr r s
|
|
|
|
|
where a' = inParens a
|
|
|
|
|
c = s !! n
|
|
|
|
|
n = succ $ length a'
|
|
|
|
|
r | c == '-' = Right $ Sub a b
|
2015-11-07 13:24:45 +03:00
|
|
|
|
| otherwise = posErr n s [uneCh c, exEof, exSpec "operator"]
|
2015-11-03 14:42:01 +03:00
|
|
|
|
s = a' ++ " " ++ inParens b
|