1> { 2> module PgnParser (pgnMoveParser,pgnParser) where 3 4> import GenUtils 5> import OrdFM 6> import Board 7> import PgnTypes 8> } 9 10> %name pgnMoveParser 11> %tokentype { Token } 12> %token 13> str { StringToken $$ } 14> result { ResultToken $$ } 15> nag { NAGToken $$ } 16> tag { TagToken $$ } 17> comment { CommentToken $$ } 18> ']' { RightSBToken } 19> '(' { LeftRBToken } 20> ')' { RightRBToken } 21> '<' { LeftABToken } 22> '>' { RightABToken } 23> num { IntToken $$ } 24> '.' { PeriodToken } 25> move { PlyToken $$ } 26> %newline { NewlineToken } 27 28> %% 29 30You either parse a set of PGN games, 31or just a set of moves. 32 33> moves :: { AbsMove } 34> moves : opt_mv_num line_no move nags opt_comment analmoves opt_comment 35> more_moves 36> { AbsMove $1 $2 $3 $4 ($5++$7) $6 Nothing $8 } 37> | opt_mv_num line_no move nags opt_comment more_moves 38> { AbsMove $1 $2 $3 $4 $5 [] Nothing $6 } 39> | opt_mv_num line_no move '<' raw_moves '>' more_moves 40> { AbsMove $1 $2 $3 [] [] [] (Just $5) $7 } 41 42> more_moves :: { AbsMove } 43> more_moves 44> : moves { $1 } 45> | result { AbsResult $1 } 46> | { AbsEnd } 47 48> nags :: { [Int] } 49> nags : nag nags { $1 : $2 } 50> | { [] } 51 52> opt_mv_num :: { Maybe MoveNumber } 53> opt_mv_num 54> : num '.' '.' '.' { Just (MoveNumber $1 Black) } 55> | num '.' { Just (MoveNumber $1 White) } 56> | { Nothing } 57 58> mv_num :: { MoveNumber } 59> mv_num 60> : num '.' '.' '.' { (MoveNumber $1 Black) } 61> | num '.' { (MoveNumber $1 White) } 62 63> opt_comment :: { [String] } 64> opt_comment 65> : comment { $1 } 66> | { [] } 67 68> analmoves :: { [AbsMove] } 69> analmoves 70> : '(' moves ')' analmoves { $2 : $4 } 71> | '(' moves ')' { [$2] } 72 73> line_no :: { LineNo } 74> line_no 75> : { $# } 76 77> raw_moves :: { [AbsPly] } 78> raw_moves 79> : move raw_moves { $1 : $2 } 80> | { [] } 81 82 83 84 85> { 86 87> pgnParser = pgnGameMap pgnMoveParser 88 89> happyError :: Int -> [Token] -> a 90> happyError i xs = 91> error ("Parse error in line " ++ show i ++ "\n" 92> ++ show (take 10 xs)) 93 94> } 95 96