[project @ 2001-09-26 09:16:00 by simonmar]
authorsimonmar <unknown>
Wed, 26 Sep 2001 09:16:00 +0000 (09:16 +0000)
committersimonmar <unknown>
Wed, 26 Sep 2001 09:16:00 +0000 (09:16 +0000)
When skipping a nested comment, keep track of the source location of
the comment opener, so that we can report it in an error message
rather than reporting the line at the end of the file.

Also noticed a couple of ineficiencies in the nested comment code, so
fixed those too.

ghc/compiler/parser/Lex.lhs

index b4c9bc4..2a193d8 100644 (file)
@@ -498,25 +498,28 @@ line_prag cont buf s@PState{loc=loc} =
   }}}}
 
 nested_comment :: P a -> P a
-nested_comment cont buf = loop buf
+nested_comment cont buf orig_state@PState{loc=loc} = loop buf orig_state
  where
    loop buf = 
      case currentChar# buf of
-       '\NUL'# | bufferExhausted (stepOn buf) -> 
-               lexError "unterminated `{-'" buf -- -}
-       '-'# | lookAhead# buf 1# `eqChar#` '}'# ->
-               cont (stepOnBy# buf 2#)
+       '-'# | lookAhead# buf 1# `eqChar#` '}'# -> cont (stepOnBy# buf 2#)
 
        '{'# | lookAhead# buf 1# `eqChar#` '-'# ->
              nested_comment (nested_comment cont) (stepOnBy# buf 2#)
 
        '\n'# -> \ s@PState{loc=loc} ->
                 let buf' = stepOn buf in
-                nested_comment cont buf'
-                       s{loc = incSrcLine loc, bol = currentIndex# buf',
-                         atbol = 1#}
+                loop buf' s{loc = incSrcLine loc, 
+                            bol = currentIndex# buf',
+                            atbol = 1#}
+
+       -- pass the original state to lexError so that the error is
+       -- reported at the line it was originally on, not the line at
+       -- the end of the file.
+       '\NUL'# | bufferExhausted (stepOn buf) -> 
+               \_ -> lexError "unterminated `{-'" buf orig_state -- -}
 
-       _   -> nested_comment cont (stepOn buf)
+       _   -> loop (stepOn buf)
 
 -- When we are lexing the first token of a line, check whether we need to
 -- insert virtual semicolons or close braces due to layout.