aboutsummaryrefslogtreecommitdiff
path: root/haskell/Reader.hs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-12-23 22:37:43 -0700
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:52 -0600
commitfa9a9758e0d15abe670fbbfd8efa1fce013b1414 (patch)
tree7ee5dceceaa79cf0269ae74d10aa297504c69da3 /haskell/Reader.hs
parentb76aa73bc76a28d7c6bb3c5a43acc9afd9ec42c8 (diff)
downloadmal-fa9a9758e0d15abe670fbbfd8efa1fce013b1414.tar.gz
mal-fa9a9758e0d15abe670fbbfd8efa1fce013b1414.zip
Haskell: steps 4-6. Line editing. Simpler fn calls.
Diffstat (limited to 'haskell/Reader.hs')
-rw-r--r--haskell/Reader.hs26
1 files changed, 14 insertions, 12 deletions
diff --git a/haskell/Reader.hs b/haskell/Reader.hs
index 04381f4..8def91c 100644
--- a/haskell/Reader.hs
+++ b/haskell/Reader.hs
@@ -4,14 +4,22 @@ where
import Text.ParserCombinators.Parsec (
Parser, parse, space, char, digit, letter,
- (<|>), oneOf, noneOf, many, many1, skipMany1, sepEndBy)
+ (<|>), oneOf, noneOf, many, many1, skipMany, skipMany1, sepEndBy)
import qualified Data.Map as Map
import Control.Monad (liftM)
import Types
spaces :: Parser ()
-spaces = skipMany1 (oneOf ", ")
+spaces = skipMany1 (oneOf ", \n")
+
+comment :: Parser ()
+comment = do
+ char ';'
+ skipMany (noneOf "\r\n")
+
+ignored :: Parser ()
+ignored = skipMany (spaces <|> comment)
symbol :: Parser Char
symbol = oneOf "!#$%&|*+-/:<=>?@^_~"
@@ -61,34 +69,28 @@ read_atom = read_number
read_list :: Parser MalVal
read_list = do
char '('
- x <- sepEndBy read_form spaces
+ x <- sepEndBy read_form ignored
char ')'
return $ MalList x
read_vector :: Parser MalVal
read_vector = do
char '['
- x <- sepEndBy read_form spaces
+ x <- sepEndBy read_form ignored
char ']'
return $ MalVector x
--- TODO: propagate error properly
-_pairs [x] = error "Odd number of element for hashmap"
-_pairs [] = []
-_pairs (MalString x:y:xs) = (x,y):_pairs xs
-_pairs (MalKeyword x:y:xs) = ("\x029e" ++ x,y):_pairs xs
-
read_hash_map :: Parser MalVal
read_hash_map = do
char '{'
- x <- sepEndBy read_form spaces
+ x <- sepEndBy read_form ignored
char '}'
return $ MalHashMap $ Map.fromList $ _pairs x
read_form :: Parser MalVal
read_form = do
- many spaces
+ ignored
x <- read_atom <|> read_list <|> read_vector <|> read_hash_map
return $ x