1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
{
import Control.Monad(when)
import System.Exit
}
%monad { Maybe } { (>>=) } { return }
%tokentype { Char }
%token
'a' { 'a' }
'b' { 'b' }
%name test1 test1
%name test2 test2
%%
test1
: sepBy('a','b') { $1 }
test2
: endBy('a','b') { $1 }
many_rev1(p)
: p { [$1] }
| many_rev1(p) p { $2 : $1 }
many1(p)
: many_rev1(p) { reverse $1 }
many(p)
: many1(p) { $1 }
| { [] }
optional(p)
: p { Just $1 }
| { Nothing }
sepR(p,q)
: p q { $2 }
sepL(p,q)
: p q { $1 }
sepBy1(p,q)
: p many(sepR(q,p)) { $1 : $2 }
sepBy(p,q)
: sepBy1(p,q) { $1 }
| { [] }
endBy(p,q)
: many (sepL(p,q)) { $1 }
endBy1(p,q)
: many1 (sepL(p,q)) { $1 }
{
happyError _ = Nothing
tests = [ test1 "" == Just ""
, test1 "a" == Just "a"
, test1 "ab" == Nothing
, test1 "aba" == Just "aa"
, test1 "abab" == Nothing
, test2 "" == Just ""
, test2 "a" == Nothing
, test2 "ab" == Just "a"
, test2 "aba" == Nothing
, test2 "abab" == Just "aa"
]
main = do let failed = filter (not . snd) (zip [0..] tests)
when (not (null failed)) $
do putStrLn ("Failed tests: " ++ show (map fst failed))
exitFailure
putStrLn "Tests passed."
}
|