From 29ec75e72f7b0c71442d9cd22d387ad0726bdd04 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Fri, 7 Feb 2014 03:57:38 +1100 Subject: Moved Thue parser into its own module --- Thue/Parser.hs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ thue.hs | 72 ---------------------------------------------------- 2 files changed, 79 insertions(+), 72 deletions(-) create mode 100644 Thue/Parser.hs delete mode 100644 thue.hs diff --git a/Thue/Parser.hs b/Thue/Parser.hs new file mode 100644 index 0000000..dd40e23 --- /dev/null +++ b/Thue/Parser.hs @@ -0,0 +1,79 @@ +module Parser ( + ThueProgram(..), + Rule(..), + State, + + parseThue + ) where + +import Text.ParserCombinators.Parsec + + + +data ThueProgram = ThueProgram { thueRules :: [Rule] + , thueInitialState :: State } + deriving (Show) + + +data Rule = Rule { original :: State + , replacement :: State } + deriving (Show) + + +type State = String + + + + +parseThue :: String -> Either ParseError ThueProgram +parseThue = parse thue "error" + + + + +thue = do + rs <- many rule + separatorLine + i <- initialState + eof + return (ThueProgram rs i) + + +rule = do + o <- ruleState + separator + r <- state + eol + return (Rule o r) + + +separatorLine = whiteSpace >> separator >> whiteSpace >> eol +separator = string "::=" + + +initialState = do + s <- state `sepEndBy` eol + return (concat s) + + +ruleState = many ruleStateChar + + +ruleStateChar = noneOf "\n\r:" + <|> try (do { char ':'; notFollowedBy (string ":="); return ':'}) + + +state = many stateChar + + +stateChar = noneOf "\n\r" + + +whiteSpace = many (oneOf "\t ") + + +eol = try (string "\r\n") + <|> try (string "\n\r") + <|> try (string "\r") + <|> try (string "\n") + diff --git a/thue.hs b/thue.hs deleted file mode 100644 index b76056e..0000000 --- a/thue.hs +++ /dev/null @@ -1,72 +0,0 @@ - -import Text.Combinators.Parsec - - - -data ThueProgram = ThueProgram { thueRules :: [Rule] - , thueInitialState :: State } - deriving (Show) - - -data Rule = Rule { original :: State - , replacement :: State } - deriving (Show) - - -type State = String - - - - -parseThue :: String -> Either ParseError ThueProgram -parseThue = parse thue "error" - - - - -thue = do - rs <- many rule - separatorLine - i <- initialState - eof - return (ThueProgram rs i) - - -rule = do - o <- ruleState - separator - r <- state - eol - return (Rule o r) - - -separatorLine = whiteSpace >> separator >> whiteSpace >> eol -separator = string "::=" - - -initialState = do - s <- state `sepEndBy` eol - return (concat s) - - -ruleState = many ruleStateChar - - -ruleStateChar = noneOf "\n\r:" - <|> try (do { char ':'; notFollowedBy (string ":="); return ':'}) - - -state = many stateChar - - -stateChar = noneOf "\n\r" - - -whiteSpace = many (oneOf "\t ") - - -eol = try (string "\r\n") - <|> try (string "\n\r") - <|> try (string "\r") - <|> try (string "\n") - -- cgit