From 2700f64b858584167fa5292c810fa67f11640e35 Mon Sep 17 00:00:00 2001
From: Jed Barber <jjbarber@y7mail.com>
Date: Wed, 17 Dec 2014 11:34:35 +1100
Subject: Grasp parser now more forgiving about whitespace

---
 src/Grasp/Parser.hs | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

(limited to 'src')

diff --git a/src/Grasp/Parser.hs b/src/Grasp/Parser.hs
index 7274919..244bbc6 100644
--- a/src/Grasp/Parser.hs
+++ b/src/Grasp/Parser.hs
@@ -34,7 +34,7 @@ parseGrasp input =
 
 -- removes comments but otherwise leaves input unchanged
 
-removeComments = gline `sepEndBy` eol >>= return . concat
+removeComments = gline `sepEndBy` eol >>= return . concat . (map (++ "\n"))
 
 
 eol  =  try (string "\r\n")
@@ -48,12 +48,19 @@ gline = many thing >>= return . concat
 
 
 thing = try (some (noneOf "\r\n\"/#"))
-    <|> try (quotedString >>= return . ("\"" ++) . (++ "\""))
+    <|> try quotedNonComment
     <|> try singleLineComment
     <|> try multiLineComment
-    <|> (anyChar >>= return . (:[]) )
+    <|> ((noneOf "\r\n") >>= return . (:[]))
 
 
+quotedNonComment = do
+    char '\"'
+    x <- many (noneOf "\r\n")
+    char '\"'
+    return ("\"" ++ x ++ "\"")
+    
+
 singleLineComment =
         (string "//" >> many (noneOf "\r\n") >> return "")
     <|> (string "#" >> many (noneOf "\r\n") >> return "")
@@ -70,13 +77,14 @@ multiLineComment = do
 -- parses a DOT graph language file into the data for a grasp program
 
 graspDOT = do
+    whiteSpace
     optional strict
     graphType
     optional ident
     openBrace
     (n,e) <- stmtList ([],[])
     closeBrace
-    many blankLine
+    whiteSpace
     eof
     return (n,e)
 
@@ -98,13 +106,9 @@ stmtList (n,e) =
     <|> try (whiteSpace >> edge >>= (\x -> stmtList (n,x:e)) )
     <|> try (whiteSpace >> attr >> stmtList (n,e))
     <|> try (whiteSpace >> subgraph >>= (\(x,y) -> stmtList ((reverse x) ++ n, (reverse y) ++ e)) )
-    <|> try (blankLine >> stmtList (n,e))
     <|> return (reverse n, reverse e)
 
 
-blankLine = whiteSpace >> eol
-
-
 alphaNumString = do
     a <- nonDigitChar
     b <- many alphaNumChar
@@ -212,5 +216,5 @@ caseInsensitiveChar c = char (toLower c) <|> char (toUpper c)
 caseInsensitiveString s = mapM caseInsensitiveChar s
 
 
-whiteSpace = many (oneOf " \t")
+whiteSpace = many (oneOf " \t\r\n")
 
-- 
cgit