From ad324af3fa36961bf04361056fee6c2aff81a0a5 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Fri, 7 Feb 2014 03:53:53 +1100 Subject: Completed Thue parser --- thue.hs | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/thue.hs b/thue.hs index 8c56a2e..b76056e 100644 --- a/thue.hs +++ b/thue.hs @@ -3,8 +3,8 @@ import Text.Combinators.Parsec -data ThueProgram = ThueProgram { substitutionRules :: [Rule] - , initialState :: State } +data ThueProgram = ThueProgram { thueRules :: [Rule] + , thueInitialState :: State } deriving (Show) @@ -25,35 +25,48 @@ parseThue = parse thue "error" thue = do - rs <- many ruleLine + rs <- many rule separatorLine - i <- stateLine + i <- initialState + eof return (ThueProgram rs i) -ruleLine = do - r <- rule - eol - return r - - rule = do - o <- state + o <- ruleState separator r <- state + eol return (Rule o r) -separatorLine = separator >> eol +separatorLine = whiteSpace >> separator >> whiteSpace >> eol separator = string "::=" -stateLine = do - s <- state - eol - return s +initialState = do + s <- state `sepEndBy` eol + return (concat s) -state = - +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