@@ -123,13 +123,13 @@ commitIf check p =
123 try (lookAhead check) >> p
123 try (lookAhead check) >> p
126−anyUntil :: IParser String -> IParser String
126+anyUntil :: IParser () -> IParser ()
127anyUntil end =
127anyUntil end =
131− end <|> (:) <$> anyChar <*> go
130+ end <|> (anyChar >> go)
135followedBy :: IParser a -> IParser b -> IParser a
135followedBy :: IParser a -> IParser b -> IParser a
@@ -194,7 +194,7 @@ spacePrefix p =
194 constrainedSpacePrefix p (\_ -> return ())
194 constrainedSpacePrefix p (\_ -> return ())
197−constrainedSpacePrefix :: IParser a -> (String -> IParser ()) -> IParser [a]
197+constrainedSpacePrefix :: IParser a -> (Bool -> IParser ()) -> IParser [a]
198constrainedSpacePrefix parser constraint =
198constrainedSpacePrefix parser constraint =
199 many $ choice
199 many $ choice
200 [ try (spacing >> lookAhead (oneOf "[({\"'")) >> parser
200 [ try (spacing >> lookAhead (oneOf "[({\"'")) >> parser
@@ -202,8 +202,8 @@ constrainedSpacePrefix parser constraint =
204 spacing = do
204 spacing = do
205− n <- whitespace
205+ bool <- whitespace
206− constraint n <?> Syntax.whitespace
206+ constraint bool
@@ -212,26 +212,28 @@ constrainedSpacePrefix parser constraint =
214surround :: Char -> Char -> String -> IParser a -> IParser a
214surround :: Char -> Char -> String -> IParser a -> IParser a
215−surround a z name p = do
215+surround start end name parser =
216− char a
216+ do char start
217− v <- padded p
217+ whitespace
218− char z <?> unwords ["a closing", name, show z]
220+ char end <?> unwords ["a closing", name, show end]
219− return v
221+ return value
222braces :: IParser a -> IParser a
224braces :: IParser a -> IParser a
223−braces =
225+braces parser =
224− surround '[' ']' "brace"
226+ surround '[' ']' "brace" parser
227parens :: IParser a -> IParser a
229parens :: IParser a -> IParser a
228−parens =
230+parens parser =
229− surround '(' ')' "paren"
231+ surround '(' ')' "paren" parser
232brackets :: IParser a -> IParser a
234brackets :: IParser a -> IParser a
233−brackets =
235+brackets parser =
234− surround '{' '}' "bracket"
236+ surround '{' '}' "bracket" parser
@@ -300,22 +302,37 @@ padded p =
300 return out
302 return out
305+plus :: IParser a -> IParser ()
307+ parser >> star parser
310+star :: IParser a -> IParser ()
313+ [ parser >> star parser
303−spaces :: IParser String
318+spaces :: IParser ()
305− let space = string " " <|> multiComment <?> Syntax.whitespace
320+ plus $ expecting Syntax.whitespace $
307− concat <$> many1 space
322+ [ string " " >> return ()
310−forcedWS :: IParser String
327+forcedWS :: IParser ()
311forcedWS =
328forcedWS =
312 do failIfTabFound
329 do failIfTabFound
313− ws <- many1 (spaces <|> newline)
330+ plus (spaces <|> newline)
314 failIfTabFound
331 failIfTabFound
315 column <- sourceColumn <$> getPosition
332 column <- sourceColumn <$> getPosition
316 if column == 1
333 if column == 1
317 then fail badWhiteSpaceMessage
334 then fail badWhiteSpaceMessage
318− else return (concat ws)
335+ else return ()
321badWhiteSpaceMessage :: String
338badWhiteSpaceMessage :: String
@@ -335,25 +352,27 @@ failIfTabFound =
337-- Just eats whitespace until the next meaningful character.
354-- Just eats whitespace until the next meaningful character.
338−dumbWhitespace :: IParser String
355+dumbWhitespace :: IParser ()
339dumbWhitespace =
356dumbWhitespace =
340− concat <$> many (spaces <|> newline)
357+ star (spaces <|> newline)
343−whitespace :: IParser String
360+whitespace :: IParser Bool
344whitespace =
361whitespace =
345− option "" forcedWS
363+ [ forcedWS >> return True
348−freshLine :: IParser String
368+freshLine :: IParser ()
349freshLine =
369freshLine =
350− try (
370+ expecting Syntax.freshLine $ try $
351− do ws <- many1 (spaces <|> newline)
371+ do plus (spaces <|> newline)
352− column <- sourceColumn <$> getPosition
372+ column <- sourceColumn <$> getPosition
353− if column == 1
373+ if column == 1
354− then return (concat ws)
374+ then return ()
355− else fail badFreshLineMessage
375+ else fail badFreshLineMessage
356− ) <?> Syntax.freshLine
359badFreshLineMessage :: String
378badFreshLineMessage :: String
@@ -362,39 +381,59 @@ badFreshLineMessage =
362 ++ "starts with stuff, not with spaces or comments."
381 ++ "starts with stuff, not with spaces or comments."
365−newline :: IParser String
384+newline :: IParser ()
367 simpleNewline <|> lineComment <?> Syntax.newline
386 simpleNewline <|> lineComment <?> Syntax.newline
370−simpleNewline :: IParser String
389+simpleNewline :: IParser ()
371simpleNewline =
390simpleNewline =
372− try (string "\r\n") <|> string "\n"
391+ do try (string "\r\n") <|> string "\n"
375−lineComment :: IParser String
395+lineComment :: IParser ()
376lineComment =
396lineComment =
377 do try (string "--")
397 do try (string "--")
378− comment <- anyUntil $ simpleNewline <|> (eof >> return "\n")
398+ anyUntil (simpleNewline <|> eof)
379− return ("--" ++ comment)
382−docComment :: IParser String
384− do try (string "{-|")
385− contents <- closeComment
386− return (init (init contents))
389−multiComment :: IParser String
401+multiComment :: IParser ()
390multiComment =
402multiComment =
391− (++) <$> try (string "{-" <* notFollowedBy (string "|")) <*> closeComment
403+ do try (string "{-" >> notFollowedBy (string "|"))
394−closeComment :: IParser String
407+closeComment :: IParser ()
395closeComment =
408closeComment =
396− anyUntil $
409+ choice
397− choice $
410+ [ expecting "the end of a comment -}" $
411+ do try (string "-}")
413+ , do try (string "{-")
421+docComment :: IParser String
423+ do try (string "{-|")
424+ builder <- closeDocComment id
428+closeDocComment :: (String -> String) -> IParser (String -> String)
429+closeDocComment builder =
398− [ try (string "-}") <?> "the end of a comment -}"
431+ [ expecting "the end of a comment -}" $
399− , concat <$> sequence [ try (string "{-"), closeComment, closeComment ]
432+ do try (string "-}")
434+ , do try (string "{-")
435+ addComment <- closeDocComment (\end -> "{-" ++ end)
436+ closeDocComment (\end -> builder (addComment ("-}" ++ end)))
438+ closeDocComment (\end -> builder (c:end))