From 8c6e4f6aac48a194e65a904ed924397a77e60960 Mon Sep 17 00:00:00 2001 From: Adam Sampson Date: Wed, 25 Apr 2007 22:32:35 +0000 Subject: [PATCH] Show what line indentation errors occur on --- fco2/Indentation.hs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/fco2/Indentation.hs b/fco2/Indentation.hs index 0aae77b..e136dff 100644 --- a/fco2/Indentation.hs +++ b/fco2/Indentation.hs @@ -11,42 +11,42 @@ indentMarker = "__indent" outdentMarker = "__outdent" eolMarker = "__eol" -countIndent :: String -> Int +countIndent :: String -> Int -> Int -- Tabs are 8 spaces. -countIndent ('\t':cs) = 4 + (countIndent cs) -countIndent (' ':' ':cs) = 1 + (countIndent cs) -countIndent (' ':cs) = error "Bad indentation" -countIndent _ = 0 +countIndent ('\t':cs) lineNum = 4 + (countIndent cs lineNum) +countIndent (' ':' ':cs) lineNum = 1 + (countIndent cs lineNum) +countIndent (' ':cs) lineNum = error $ "Bad indentation at line " ++ show lineNum +countIndent _ _ = 0 stripIndent :: String -> String stripIndent (' ':cs) = stripIndent cs stripIndent cs = cs -stripComment :: String -> String -stripComment [] = [] -stripComment ('-':'-':s) = [] -stripComment ('"':s) = '"' : stripCommentInString s -stripComment (c:s) = c : stripComment s +stripComment :: String -> Int -> String +stripComment [] _ = [] +stripComment ('-':'-':s) _ = [] +stripComment ('"':s) lineNum = '"' : stripCommentInString s lineNum +stripComment (c:s) lineNum = c : stripComment s lineNum -stripCommentInString :: String -> String -stripCommentInString [] = error "In string at end of line" -stripCommentInString ('"':s) = '"' : stripComment s -stripCommentInString (c:s) = c : stripCommentInString s +stripCommentInString :: String -> Int -> String +stripCommentInString [] lineNum = error $ "In string at end of line " ++ show lineNum +stripCommentInString ('"':s) lineNum = '"' : stripComment s lineNum +stripCommentInString (c:s) lineNum = c : stripCommentInString s lineNum parseIndentation :: [String] -> String parseIndentation ls = concat $ intersperse "\n" $ lines where - (initSuffix, lines) = flatten' ls 0 + (initSuffix, lines) = flatten' ls 0 1 rep n i = concat $ take n (repeat i) - flatten' [] level = ("", [rep level (' ' : outdentMarker)]) - flatten' (s:ss) level - | isBlankLine = let (suffix, rest) = flatten' ss level in (suffix, "" : rest) + flatten' [] level lineNum = ("", [rep level (' ' : outdentMarker)]) + flatten' (s:ss) level lineNum + | isBlankLine = let (suffix, rest) = flatten' ss level (lineNum + 1) in (suffix, "" : rest) | newLevel > level = (rep (newLevel - level) (' ' : indentMarker), processed : rest) | newLevel < level = (rep (level - newLevel) (' ' : outdentMarker), processed : rest) | otherwise = ("", processed : rest) - where newLevel = countIndent s - stripped' = stripComment s + where newLevel = countIndent s lineNum + stripped' = stripComment s lineNum isBlankLine = stripIndent stripped' == "" processed = (if isBlankLine then "" else (stripped' ++ (' ' : eolMarker))) ++ suffix - (suffix, rest) = flatten' ss newLevel + (suffix, rest) = flatten' ss newLevel (lineNum + 1)