summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-02-06 01:56:56 +1100
committerJed Barber <jjbarber@y7mail.com>2014-02-06 01:56:56 +1100
commit7900ae557ebe8319e43daf9c4202e8acb886317a (patch)
treed90b7164c49df183069bcd069ad0b8cc9c842573
parentcac23eb0080ee1abcf0e21004290fd79039d71a8 (diff)
Completed Fractran parser
-rw-r--r--Fractran/Parser.hs49
1 files changed, 31 insertions, 18 deletions
diff --git a/Fractran/Parser.hs b/Fractran/Parser.hs
index 63564d1..19e619f 100644
--- a/Fractran/Parser.hs
+++ b/Fractran/Parser.hs
@@ -1,11 +1,12 @@
module Parser (
- FractranProgram(..),
+ FractranProgram(..),
- parseFractran
- ) where
+ parseFractran
+ ) where
-import Text.Combinators.Parsec
+import Control.Applicative( some )
+import Text.ParserCombinators.Parsec
@@ -15,6 +16,7 @@ data FractranProgram = FractranProgram { fractions :: [(Int,Int)]
+
parseFractran :: String -> Either ParseError FractranProgram
parseFractran = parse fractran "error"
@@ -22,31 +24,42 @@ parseFractran = parse fractran "error"
fractran = do
- f <- many intPair
- v <- initVal
- eof
- return (FractranProgram f v)
+ value <- initVal
+ fractionList <- many intPair
+ whiteSpace
+ eof
+ return (FractranProgram fractionList value)
intPair = do
- whiteSpace
- n <- wholeNumber
- slash
- d <- wholeNumber
- return (n,d)
+ whiteSpace
+ numerator <- wholeNumber
+ slash
+ denominator <- positiveNumber
+ return (numerator,denominator)
slash = char '/'
initVal = do
- whiteSpace
- v <- wholeNumber
- whiteSpace
- return v
+ whiteSpace
+ value <- wholeNumber
+ return value
+
+
+wholeNumber = do
+ value <- some digit
+ return (read value)
+
+
+positiveNumber = do
+ firstDigit <- nonZeroDigit
+ rest <- many digit
+ return (read (firstDigit:rest))
-wholeNumber =
+nonZeroDigit = oneOf "123456789"
whiteSpace = many (oneOf "\t\n\r ")