packages feed

wyvern-diagrams-0.3.0.7 / src / lib / Parser.y

{module Parser whereimport Data.Charimport Data.String.Utils (lstrip)import IDimport Lexerimport Blocks}%name                                                     diagram%tokentype                                                { Token }%monad                                                    { P } { thenP } { returnP }%token  action                                                  { TokenAction $$ }  soloId                                                  { TokenSoloIdentifier $$ }  '{'                                                     { TokenOCB }  '}'                                                     { TokenCCB }%%skewers :   headline                                      { [$1] }            | skewers headline                            { $2 : $1 }            | skewer                                      { [$1] }skewer :    {- empty -}                                   { [] }            | block                                       { [$1] }            | skewer block                                { $2 : $1 }headline :  action '{' skewer '}'                         { $3 <> [toHeadline $1] }            | action '{' skewer soloId '}'                { toAddress $4 : $3 <> [toHeadline $1] }block :     action                                        { toAction $1 }            | action '{' skewer '}' '{' skewer '}'        { toFork $1 $3 $6 Nothing }            | action '{' skewer '}' '{' skewer soloId '}' { toFork $1 $3 $6 (Just (ID $7)) }            | action '{' skewer '}' '{' skewer soloId '}' { toFork $1 $3 $6 (Just (ID $7)) }            | action '{' skewer '}' '{' skewer '}'        { toFork $1 $3 $6 Nothing }{toId :: String -> Maybe IDtoId t =  case head $ words t of    "#" -> Nothing    x -> Just (ID x)toContent :: String -> StringtoContent t =  let id = head $ words t      idFreeContent = lstrip (drop (length id) t)      idFreeContent' = drop 1 idFreeContent  in  take (length idFreeContent' - 1) idFreeContent'toAction :: String -> BlocktoAction t = Action (toId t) (toContent t)toFork :: String -> [Block] -> [Block] -> Maybe ID -> BlocktoFork t l r rId = Fork (toId t) (toContent t) l r rIdtoHeadline :: String -> BlocktoHeadline t = Headline (toId t) (toContent t)toAddress :: String -> BlocktoAddress t = Address (toId t) (head $ words t)happyError = \tks i -> error ("Parse error in line " ++ show (i::Int) ++ ".\n")data ParseResult a  = ParseOk a  | ParseFail Stringtype P a = Int -> ParseResult athenP :: P a -> (a -> P b) -> P bm `thenP` k = \l ->  case m l of    ParseFail s -> ParseFail s    ParseOk a -> k a lreturnP :: a -> P areturnP a = \l -> ParseOk a}