2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Lexical analysis]{Lexical analysis}
6 --------------------------------------------------------
8 There's a known bug in here:
10 If an interface file ends prematurely, Lex tries to
11 do headFS of an empty FastString.
13 An example that provokes the error is
15 f _:_ _forall_ [a] <<<END OF FILE>>>
16 --------------------------------------------------------
19 {-# OPTIONS -#include "ctypes.h" #-}
26 Token(..), lexer, ParseResult(..), PState(..),
30 P, thenP, thenP_, returnP, mapP, failP, failMsgP,
31 getSrcLocP, getSrcFile,
32 layoutOn, layoutOff, pushContext, popContext
35 #include "HsVersions.h"
37 import Char ( ord, isSpace, toUpper )
38 import List ( isSuffixOf )
40 import IdInfo ( InlinePragInfo(..), CprInfo(..) )
41 import Name ( isLowerISO, isUpperISO )
42 import PrelMods ( mkTupNameStr, mkUbxTupNameStr )
43 import CmdLineOpts ( opt_IgnoreIfacePragmas, opt_HiVersion, opt_NoHiCheck )
44 import Demand ( Demand(..) {- instance Read -} )
45 import UniqFM ( UniqFM, listToUFM, lookupUFM)
46 import BasicTypes ( NewOrData(..) )
47 import SrcLoc ( SrcLoc, incSrcLine, srcLocFile, srcLocLine,
48 replaceSrcLine, mkSrcLoc )
50 import Maybes ( MaybeErr(..) )
51 import ErrUtils ( Message )
59 #if __GLASGOW_HASKELL__ >= 303
66 import PrelRead ( readRational__ ) -- Glasgow non-std
69 %************************************************************************
71 \subsection{Data types}
73 %************************************************************************
75 The token data type, fairly un-interesting except from one
76 constructor, @ITidinfo@, which is used to lazily lex id info (arity,
77 strictness, unfolding etc).
79 The Idea/Observation here is that the renamer needs to scan through
80 all of an interface file before it can continue. But only a fraction
81 of the information contained in the file turns out to be useful, so
82 delaying as much as possible of the scanning and parsing of an
83 interface file Makes Sense (Heap profiles of the compiler
84 show a reduction in heap usage by at least a factor of two,
87 Hence, the interface file lexer spots when value declarations are
88 being scanned and return the @ITidinfo@ and @ITtype@ constructors
89 for the type and any other id info for that binding (unfolding, strictness
90 etc). These constructors are applied to the result of lexing these sub-chunks.
92 The lexing of the type and id info is all done lazily, of course, so
93 the scanning (and subsequent parsing) will be done *only* on the ids the
94 renamer finds out that it is interested in. The rest will just be junked.
95 Laziness, you know it makes sense :-)
99 = ITas -- Haskell keywords
125 | ITforall -- GHC extension keywords
132 | ITinterface -- interface keywords
140 | ITccall (Bool,Bool,Bool) -- (is_dyn, is_casm, may_gc)
155 | ITunfold InlinePragInfo
156 | ITstrict ([Demand], Bool)
158 | ITcprinfo (CprInfo)
162 | ITspecialise_prag -- Pragmas
170 | ITdotdot -- reserved symbols
184 | ITbiglam -- GHC-extension symbols
186 | ITocurly -- special symbols
200 | ITvarid FAST_STRING -- identifiers
201 | ITconid FAST_STRING
202 | ITvarsym FAST_STRING
203 | ITconsym FAST_STRING
204 | ITqvarid (FAST_STRING,FAST_STRING)
205 | ITqconid (FAST_STRING,FAST_STRING)
206 | ITqvarsym (FAST_STRING,FAST_STRING)
207 | ITqconsym (FAST_STRING,FAST_STRING)
209 | ITpragma StringBuffer
212 | ITstring FAST_STRING
214 | ITrational Rational
217 | ITprimstring FAST_STRING
219 | ITprimfloat Rational
220 | ITprimdouble Rational
221 | ITlitlit FAST_STRING
223 | ITunknown String -- Used when the lexer can't make sense of it
224 | ITeof -- end of file token
225 deriving Text -- debugging
228 -----------------------------------------------------------------------------
232 pragmaKeywordsFM = listToUFM $
233 map (\ (x,y) -> (_PK_ x,y))
234 [( "SPECIALISE", ITspecialise_prag ),
235 ( "SPECIALIZE", ITspecialise_prag ),
236 ( "SOURCE", ITsource_prag ),
237 ( "INLINE", ITinline_prag ),
238 ( "NOINLINE", ITnoinline_prag ),
239 ( "NOTINLINE", ITnoinline_prag ),
240 ( "LINE", ITline_prag ),
241 ( "RULES", ITrules_prag ),
242 ( "RULEZ", ITrules_prag ) -- american spelling :-)
245 haskellKeywordsFM = listToUFM $
246 map (\ (x,y) -> (_PK_ x,y))
247 [( "_", ITunderscore ),
250 ( "class", ITclass ),
252 ( "default", ITdefault ),
253 ( "deriving", ITderiving ),
256 ( "hiding", IThiding ),
258 ( "import", ITimport ),
260 ( "infix", ITinfix ),
261 ( "infixl", ITinfixl ),
262 ( "infixr", ITinfixr ),
263 ( "instance", ITinstance ),
265 ( "module", ITmodule ),
266 ( "newtype", ITnewtype ),
268 ( "qualified", ITqualified ),
271 ( "where", ITwhere ),
275 ghcExtensionKeywordsFM = listToUFM $
276 map (\ (x,y) -> (_PK_ x,y))
277 [ ( "forall", ITforall ),
278 ( "foreign", ITforeign ),
279 ( "export", ITexport ),
280 ( "label", ITlabel ),
281 ( "dynamic", ITdynamic ),
282 ( "unsafe", ITunsafe ),
283 ("_ccall_", ITccall (False, False, False)),
284 ("_ccall_GC_", ITccall (False, False, True)),
285 ("_casm_", ITccall (False, True, False)),
286 ("_casm_GC_", ITccall (False, True, True)),
288 -- interface keywords
289 ("__interface", ITinterface),
290 ("__export", IT__export),
291 ("__depends", ITdepends),
292 ("__forall", IT__forall),
293 ("__letrec", ITletrec),
294 ("__coerce", ITcoerce),
295 ("__inline_me", ITinlineMe),
296 ("__inline_call", ITinlineCall),
297 ("__depends", ITdepends),
298 ("__DEFAULT", ITdefaultbranch),
300 ("__integer", ITinteger_lit),
301 ("__float", ITfloat_lit),
302 ("__rational", ITrational_lit),
303 ("__addr", ITaddr_lit),
304 ("__litlit", ITlit_lit),
305 ("__string", ITstring_lit),
310 ("__P", ITspecialise),
313 ("__u", ITunfold NoInlinePragInfo),
315 ("__ccall", ITccall (False, False, False)),
316 ("__ccall_GC", ITccall (False, False, True)),
317 ("__dyn_ccall", ITccall (True, False, False)),
318 ("__dyn_ccall_GC", ITccall (True, False, True)),
319 ("__casm", ITccall (False, True, False)),
320 ("__dyn_casm", ITccall (True, True, False)),
321 ("__casm_GC", ITccall (False, True, True)),
322 ("__dyn_casm_GC", ITccall (True, True, True)),
328 haskellKeySymsFM = listToUFM $
329 map (\ (x,y) -> (_PK_ x,y))
342 ,(".", ITdot) -- sadly, for 'forall a . t'
346 -----------------------------------------------------------------------------
351 - (glaexts) lexing an interface file or -fglasgow-exts
352 - (bol) pointer to beginning of line (for column calculations)
353 - (buf) pointer to beginning of token
354 - (buf) pointer to current char
355 - (atbol) flag indicating whether we're at the beginning of a line
358 lexer :: (Token -> P a) -> P a
359 lexer cont buf s@(PState{
361 glasgow_exts = glaexts,
367 -- first, start a new lexeme and lose all the whitespace
368 = tab line bol atbol (stepOverLexeme buf)
370 line = srcLocLine loc
372 tab y bol atbol buf = --trace ("tab: " ++ show (I# y) ++ " : " ++ show (currentChar buf)) $
373 case currentChar# buf of
376 if bufferExhausted (stepOn buf)
377 then cont ITeof buf s'
378 else trace "lexer: misplaced NUL?" $
379 tab y bol atbol (stepOn buf)
381 '\n'# -> let buf' = stepOn buf
382 in tab (y +# 1#) (currentIndex# buf') 1# buf'
384 -- find comments. This got harder in Haskell 98.
385 '-'# -> let trundle n =
386 let next = lookAhead# buf n in
387 if next `eqChar#` '-'# then trundle (n +# 1#)
388 else if is_symbol next || n <# 2#
390 else case untilChar# (stepOnBy# buf n) '\n'# of
391 { buf' -> tab y bol atbol (stepOverLexeme buf')
395 -- comments and pragmas. We deal with LINE pragmas here,
396 -- and throw out any unrecognised pragmas as comments. Any
397 -- pragmas we know about are dealt with later (after any layout
398 -- processing if necessary).
400 '{'# | lookAhead# buf 1# `eqChar#` '-'# ->
401 if lookAhead# buf 2# `eqChar#` '#'# then
402 if lookAhead# buf 3# `eqChar#` '#'# then is_a_token else
403 case expandWhile# is_space (setCurrentPos# buf 3#) of { buf1->
404 case expandWhile# is_ident (stepOverLexeme buf1) of { buf2->
405 let lexeme = mkFastString -- ToDo: too slow
406 (map toUpper (lexemeToString buf2)) in
407 case lookupUFM pragmaKeywordsFM lexeme of
408 Just ITline_prag -> line_prag (lexer cont) buf2 s'
409 Just other -> is_a_token
410 Nothing -> skip_to_end (stepOnBy# buf 2#)
413 else skip_to_end (stepOnBy# buf 2#)
415 skip_to_end buf = nested_comment (lexer cont) buf s'
417 -- tabs have been expanded beforehand
418 c | is_space c -> tab y bol atbol (stepOn buf)
419 | otherwise -> is_a_token
421 where s' = s{loc = replaceSrcLine loc y,
425 is_a_token | atbol /=# 0# = lexBOL cont buf s'
426 | otherwise = lexToken cont glaexts buf s'
428 -- {-# LINE .. #-} pragmas. yeuch.
430 case expandWhile# is_space buf of { buf1 ->
431 case scanNumLit 0 (stepOverLexeme buf1) of { (line,buf2) ->
432 -- subtract one: the line number refers to the *following* line.
433 let real_line = line - 1 in
434 case fromInteger real_line of { i@(I# l) ->
435 case expandWhile# is_space buf2 of { buf3 ->
436 case currentChar# buf3 of
438 case untilEndOfString# (stepOn (stepOverLexeme buf3)) of { buf4 ->
439 let file = lexemeToFastString buf4 in
440 \s@PState{loc=loc} -> skipToEnd buf4 s{loc = mkSrcLoc file i}
442 other -> \s@PState{loc=loc} -> skipToEnd buf3 s{loc = replaceSrcLine loc l}
445 skipToEnd buf = nested_comment cont buf
447 nested_comment :: P a -> P a
448 nested_comment cont buf = loop buf
451 case currentChar# buf of
452 '\NUL'# | bufferExhausted (stepOn buf) ->
453 lexError "unterminated `{-'" buf
455 '-'# | lookAhead# buf 1# `eqChar#` '}'# ->
456 cont (stepOnBy# buf 2#)
458 '{'# | lookAhead# buf 1# `eqChar#` '-'# ->
459 nested_comment (nested_comment cont) (stepOnBy# buf 2#)
461 '\n'# -> \ s@PState{loc=loc} ->
462 let buf' = stepOn buf in
463 nested_comment cont buf'
464 s{loc = incSrcLine loc, bol = currentIndex# buf',
467 _ -> nested_comment cont (stepOn buf)
469 -- When we are lexing the first token of a line, check whether we need to
470 -- insert virtual semicolons or close braces due to layout.
472 lexBOL :: (Token -> P a) -> P a
473 lexBOL cont buf s@(PState{
475 glasgow_exts = glaexts,
480 if need_close_curly then
481 --trace ("col = " ++ show (I# col) ++ ", layout: inserting '}'") $
482 cont ITvccurly buf s{atbol = 1#, context = tail ctx}
483 else if need_semi_colon then
484 --trace ("col = " ++ show (I# col) ++ ", layout: inserting ';'") $
485 cont ITsemi buf s{atbol = 0#}
487 lexToken cont glaexts buf s{atbol = 0#}
489 col = currentIndex# buf -# bol
502 Layout n -> col ==# n
505 lexToken :: (Token -> P a) -> Int# -> P a
506 lexToken cont glaexts buf =
509 case currentChar# buf of
511 -- special symbols ----------------------------------------------------
512 '('# | flag glaexts && lookAhead# buf 1# `eqChar#` '#'#
513 -> cont IToubxparen (setCurrentPos# buf 2#)
515 -> cont IToparen (incLexeme buf)
517 ')'# -> cont ITcparen (incLexeme buf)
518 '['# -> cont ITobrack (incLexeme buf)
519 ']'# -> cont ITcbrack (incLexeme buf)
520 ','# -> cont ITcomma (incLexeme buf)
521 ';'# -> cont ITsemi (incLexeme buf)
523 '}'# -> \ s@PState{context = ctx} ->
525 (_:ctx') -> cont ITccurly (incLexeme buf) s{context=ctx'}
526 _ -> lexError "too many '}'s" buf s
528 '#'# -> case lookAhead# buf 1# of
529 ')'# | flag glaexts -> cont ITcubxparen (setCurrentPos# buf 2#)
530 '-'# -> case lookAhead# buf 2# of
531 '}'# -> cont ITclose_prag (setCurrentPos# buf 3#)
532 _ -> lex_sym cont (incLexeme buf)
533 _ -> lex_sym cont (incLexeme buf)
535 '`'# | flag glaexts && lookAhead# buf 1# `eqChar#` '`'#
536 -> lex_cstring cont (setCurrentPos# buf 2#)
538 -> cont ITbackquote (incLexeme buf)
540 '{'# -> -- look for "{-##" special iface pragma
541 case lookAhead# buf 1# of
542 '-'# -> case lookAhead# buf 2# of
543 '#'# -> case lookAhead# buf 3# of
546 = doDiscard False (stepOnBy# (stepOverLexeme buf) 4#) in
547 cont (ITpragma lexeme) buf'
548 _ -> lex_prag cont (setCurrentPos# buf 3#)
549 _ -> cont ITocurly (incLexeme buf)
550 _ -> (layoutOff `thenP_` cont ITocurly) (incLexeme buf)
552 -- strings/characters -------------------------------------------------
553 '\"'#{-"-} -> lex_string cont glaexts "" (incLexeme buf)
554 '\''# -> lex_char (char_end cont) glaexts (incLexeme buf)
556 -- strictness and cpr pragmas and __scc treated specially.
557 '_'# | flag glaexts ->
558 case lookAhead# buf 1# of
559 '_'# -> case lookAhead# buf 2# of
561 lex_demand cont (stepOnUntil (not . isSpace)
562 (stepOnBy# buf 3#)) -- past __S
564 lex_cpr cont (stepOnUntil (not . isSpace)
565 (stepOnBy# buf 3#)) -- past __M
567 case prefixMatch (stepOnBy# buf 3#) "cc" of
568 Just buf' -> lex_scc cont (stepOverLexeme buf')
569 Nothing -> lex_id cont glaexts buf
570 _ -> lex_id cont glaexts buf
571 _ -> lex_id cont glaexts buf
573 -- Hexadecimal and octal constants
574 '0'# | (ch `eqChar#` 'x'# || ch `eqChar#` 'X'#) && is_hexdigit ch2
575 -> readNum (after_lexnum cont glaexts) buf' is_hexdigit 16 hex
576 | (ch `eqChar#` 'o'# || ch `eqChar#` 'O'#) && is_octdigit ch2
577 -> readNum (after_lexnum cont glaexts) buf' is_octdigit 8 oct_or_dec
578 where ch = lookAhead# buf 1#
579 ch2 = lookAhead# buf 2#
580 buf' = setCurrentPos# buf 2#
583 if bufferExhausted (stepOn buf) then
586 trace "lexIface: misplaced NUL?" $
587 cont (ITunknown "\NUL") (stepOn buf)
589 c | is_digit c -> lex_num cont glaexts 0 buf
590 | is_symbol c -> lex_sym cont buf
591 | is_upper c -> lex_con cont glaexts buf
592 | is_ident c -> lex_id cont glaexts buf
593 | otherwise -> lexError "illegal character" buf
595 -- Int# is unlifted, and therefore faster than Bool for flags.
601 -------------------------------------------------------------------------------
605 = case expandWhile# is_space buf of { buf1 ->
606 case expandWhile# is_ident (stepOverLexeme buf1) of { buf2 ->
607 let lexeme = mkFastString (map toUpper (lexemeToString buf2)) in
608 case lookupUFM pragmaKeywordsFM lexeme of
609 Just kw -> cont kw (mergeLexemes buf buf2)
610 Nothing -> panic "lex_prag"
613 -------------------------------------------------------------------------------
616 lex_string cont glaexts s buf
617 = case currentChar# buf of
619 let buf' = incLexeme buf; s' = mkFastString (reverse s) in
620 case currentChar# buf' of
621 '#'# | flag glaexts -> cont (ITprimstring s') (incLexeme buf')
622 _ -> cont (ITstring s') buf'
624 -- ignore \& in a string, deal with string gaps
625 '\\'# | next_ch `eqChar#` '&'#
626 -> lex_string cont glaexts s (setCurrentPos# buf 2#)
628 -> lex_stringgap cont glaexts s buf'
630 where next_ch = lookAhead# buf 1#
631 buf' = setCurrentPos# buf 2#
633 _ -> lex_char (lex_next_string cont s) glaexts buf
635 lex_stringgap cont glaexts s buf
636 = let buf' = incLexeme buf in
637 case currentChar# buf of
638 '\n'# -> \st@PState{loc = loc} -> lex_stringgap cont glaexts s buf'
639 st{loc = incSrcLine loc}
640 '\\'# -> lex_string cont glaexts s buf'
641 c | is_space c -> lex_stringgap cont glaexts s buf'
642 other -> charError buf'
644 lex_next_string cont s glaexts c buf = lex_string cont glaexts (c:s) buf
646 lex_char :: (Int# -> Char -> P a) -> Int# -> P a
647 lex_char cont glaexts buf
648 = case currentChar# buf of
649 '\\'# -> lex_escape (cont glaexts) (incLexeme buf)
650 c | is_any c -> cont glaexts (C# c) (incLexeme buf)
651 other -> charError buf
653 char_end cont glaexts c buf
654 = case currentChar# buf of
655 '\''# -> let buf' = incLexeme buf in
656 case currentChar# buf' of
658 -> cont (ITprimchar c) (incLexeme buf')
659 _ -> cont (ITchar c) buf'
663 = let buf' = incLexeme buf in
664 case currentChar# buf of
665 'a'# -> cont '\a' buf'
666 'b'# -> cont '\b' buf'
667 'f'# -> cont '\f' buf'
668 'n'# -> cont '\n' buf'
669 'r'# -> cont '\r' buf'
670 't'# -> cont '\t' buf'
671 'v'# -> cont '\v' buf'
672 '\\'# -> cont '\\' buf'
673 '"'# -> cont '\"' buf'
674 '\''# -> cont '\'' buf'
675 '^'# -> let c = currentChar# buf' in
676 if c `geChar#` '@'# && c `leChar#` '_'#
677 then cont (C# (chr# (ord# c -# ord# '@'#))) (incLexeme buf')
680 'x'# -> readNum (after_charnum cont) buf' is_hexdigit 16 hex
681 'o'# -> readNum (after_charnum cont) buf' is_octdigit 8 oct_or_dec
683 -> readNum (after_charnum cont) buf is_digit 10 oct_or_dec
685 _ -> case [ (c,buf2) | (p,c) <- silly_escape_chars,
686 Just buf2 <- [prefixMatch buf p] ] of
687 (c,buf2):_ -> cont c buf2
690 after_charnum cont i buf
691 = let int = fromInteger i in
692 if i >= 0 && i <= 255
693 then cont (chr int) buf
696 readNum cont buf is_digit base conv = read buf 0
698 = case currentChar# buf of { c ->
700 then read (incLexeme buf) (i*base + (toInteger (I# (conv c))))
706 || (c `geChar#` 'a'# && c `leChar#` 'h'#)
707 || (c `geChar#` 'A'# && c `leChar#` 'H'#)
709 hex c | is_digit c = ord# c -# ord# '0'#
710 | otherwise = ord# (to_lower c) -# ord# 'a'# +# 10#
711 oct_or_dec c = ord# c -# ord# '0'#
713 is_octdigit c = c `geChar#` '0'# && c `leChar#` '7'#
716 | c `geChar#` 'A'# && c `leChar#` 'Z'#
717 = chr# (ord# c -# (ord# 'A'# -# ord# 'a'#))
720 charError buf = lexError "error in character literal" buf
722 silly_escape_chars = [
759 -------------------------------------------------------------------------------
761 lex_demand cont buf =
762 case read_em [] buf of { (ls,buf') ->
763 case currentChar# buf' of
764 'B'# -> cont (ITstrict (ls, True )) (incLexeme buf')
765 _ -> cont (ITstrict (ls, False)) buf'
768 -- code snatched from Demand.lhs
770 case currentChar# buf of
771 'L'# -> read_em (WwLazy False : acc) (stepOn buf)
772 'A'# -> read_em (WwLazy True : acc) (stepOn buf)
773 'S'# -> read_em (WwStrict : acc) (stepOn buf)
774 'P'# -> read_em (WwPrim : acc) (stepOn buf)
775 'E'# -> read_em (WwEnum : acc) (stepOn buf)
776 ')'# -> (reverse acc, stepOn buf)
777 'U'# -> do_unpack DataType True acc (stepOnBy# buf 2#)
778 'u'# -> do_unpack DataType False acc (stepOnBy# buf 2#)
779 'N'# -> do_unpack NewType True acc (stepOnBy# buf 2#)
780 'n'# -> do_unpack NewType False acc (stepOnBy# buf 2#)
781 _ -> (reverse acc, buf)
783 do_unpack new_or_data wrapper_unpacks acc buf
784 = case read_em [] buf of
785 (stuff, rest) -> read_em (WwUnpack new_or_data wrapper_unpacks stuff : acc) rest
788 case read_em [] buf of { (cpr_inf,buf') ->
789 ASSERT ( null (tail cpr_inf) )
790 cont (ITcprinfo $ head cpr_inf) buf'
793 -- code snatched from lex_demand above
795 case currentChar# buf of
796 '-'# -> read_em (NoCPRInfo : acc) (stepOn buf)
797 '('# -> do_unpack acc (stepOn buf)
798 ')'# -> (reverse acc, stepOn buf)
799 _ -> (reverse acc, buf)
802 = case read_em [] buf of
803 (stuff, rest) -> read_em ((CPRInfo stuff) : acc) rest
807 case currentChar# buf of
808 'C'# -> cont ITsccAllCafs (incLexeme buf)
809 other -> cont ITscc buf
811 -----------------------------------------------------------------------------
814 lex_num :: (Token -> P a) -> Int# -> Integer -> P a
815 lex_num cont glaexts acc buf =
816 case scanNumLit acc buf of
818 case currentChar# buf' of
819 '.'# | is_digit (lookAhead# buf' 1#) ->
820 -- this case is not optimised at all, as the
821 -- presence of floating point numbers in interface
822 -- files is not that common. (ToDo)
823 case expandWhile# is_digit (incLexeme buf') of
824 buf2 -> -- points to first non digit char
826 let l = case currentChar# buf2 of
832 = let buf3 = incLexeme buf2 in
833 case currentChar# buf3 of
834 '-'# -> expandWhile# is_digit (incLexeme buf3)
835 '+'# -> expandWhile# is_digit (incLexeme buf3)
836 x | is_digit x -> expandWhile# is_digit buf3
839 v = readRational__ (lexemeToString l)
841 in case currentChar# l of -- glasgow exts only
842 '#'# | flag glaexts -> let l' = incLexeme l in
843 case currentChar# l' of
844 '#'# -> cont (ITprimdouble v) (incLexeme l')
845 _ -> cont (ITprimfloat v) l'
846 _ -> cont (ITrational v) l
848 _ -> after_lexnum cont glaexts acc' buf'
850 after_lexnum cont glaexts i buf
851 = case currentChar# buf of
852 '#'# | flag glaexts -> cont (ITprimint i) (incLexeme buf)
853 _ -> cont (ITinteger i) buf
855 -----------------------------------------------------------------------------
856 -- C "literal literal"s (i.e. things like ``NULL'', ``stdout'' etc.)
858 -- we lexemeToFastString on the bit between the ``''s, but include the
859 -- quotes in the full lexeme.
861 lex_cstring cont buf =
862 case expandUntilMatch (stepOverLexeme buf) "\'\'" of
863 buf' -> cont (ITlitlit (lexemeToFastString
864 (setCurrentPos# buf' (negateInt# 2#))))
865 (mergeLexemes buf buf')
867 ------------------------------------------------------------------------------
870 is_ident, is_symbol, is_any, is_upper, is_digit :: Char# -> Bool
872 {-# INLINE is_ctype #-}
873 #if __GLASGOW_HASKELL__ >= 303
874 is_ctype :: Word8 -> Char# -> Bool
875 is_ctype mask = \c ->
876 (indexWord8OffAddr (``char_types'' :: Addr) (ord (C# c)) .&. mask) /= 0
878 is_ctype :: Int -> Char# -> Bool
879 is_ctype (I# mask) = \c ->
880 let (A# ctype) = ``char_types'' :: Addr
881 flag_word = int2Word# (ord# (indexCharOffAddr# ctype (ord# c)))
883 (flag_word `and#` (int2Word# mask)) `neWord#` (int2Word# 0#)
886 is_ident = is_ctype 1
887 is_symbol = is_ctype 2
889 is_space = is_ctype 8
890 is_upper = is_ctype 16
891 is_digit = is_ctype 32
893 -----------------------------------------------------------------------------
894 -- identifiers, symbols etc.
896 lex_id cont glaexts buf =
897 case expandWhile# is_ident buf of { buf1 ->
899 case (if flag glaexts
900 then expandWhile# (eqChar# '#'#) buf1 -- slurp trailing hashes
901 else buf1) of { buf' ->
903 let lexeme = lexemeToFastString buf' in
905 case _scc_ "Lex.haskellKeyword" lookupUFM haskellKeywordsFM lexeme of {
906 Just kwd_token -> --trace ("hkeywd: "++_UNPK_(lexeme)) $
910 let var_token = cont (mk_var_token lexeme) buf' in
912 if not (flag glaexts)
916 case lookupUFM ghcExtensionKeywordsFM lexeme of {
917 Just kwd_token -> cont kwd_token buf';
923 case expandWhile# is_symbol buf of
924 buf' -> case lookupUFM haskellKeySymsFM lexeme of {
925 Just kwd_token -> --trace ("keysym: "++unpackFS lexeme) $
926 cont kwd_token buf' ;
927 Nothing -> --trace ("sym: "++unpackFS lexeme) $
928 cont (mk_var_token lexeme) buf'
930 where lexeme = lexemeToFastString buf'
933 lex_con cont glaexts buf =
934 case expandWhile# is_ident buf of { buf1 ->
935 case slurp_trailing_hashes buf1 glaexts of { buf' ->
937 case currentChar# buf' of
942 just_a_conid = --trace ("con: "++unpackFS lexeme) $
943 cont (ITconid lexeme) buf'
944 lexeme = lexemeToFastString buf'
945 munch = lex_qid cont glaexts lexeme (incLexeme buf') just_a_conid
948 lex_qid cont glaexts mod buf just_a_conid =
949 case currentChar# buf of
950 '['# -> -- Special case for []
951 case lookAhead# buf 1# of
952 ']'# -> cont (ITqconid (mod,SLIT("[]"))) (setCurrentPos# buf 2#)
955 '('# -> -- Special case for (,,,)
956 -- This *is* necessary to deal with e.g. "instance C PrelBase.(,,)"
957 case lookAhead# buf 1# of
958 '#'# | flag glaexts -> case lookAhead# buf 2# of
959 ','# -> lex_ubx_tuple cont mod (setCurrentPos# buf 3#)
962 ')'# -> cont (ITqconid (mod,SLIT("()"))) (setCurrentPos# buf 2#)
963 ','# -> lex_tuple cont mod (setCurrentPos# buf 2#) just_a_conid
966 '-'# -> case lookAhead# buf 1# of
967 '>'# -> cont (ITqconid (mod,SLIT("->"))) (setCurrentPos# buf 2#)
968 _ -> lex_id3 cont glaexts mod buf just_a_conid
969 _ -> lex_id3 cont glaexts mod buf just_a_conid
971 lex_id3 cont glaexts mod buf just_a_conid
972 | is_symbol (currentChar# buf) =
974 start_new_lexeme = stepOverLexeme buf
976 case expandWhile# is_symbol start_new_lexeme of { buf' ->
978 lexeme = lexemeToFastString buf'
979 -- real lexeme is M.<sym>
980 new_buf = mergeLexemes buf buf'
982 cont (mk_qvar_token mod lexeme) new_buf
983 -- wrong, but arguably morally right: M... is now a qvarsym
988 start_new_lexeme = stepOverLexeme buf
990 case expandWhile# is_ident start_new_lexeme of { buf1 ->
995 case slurp_trailing_hashes buf1 glaexts of { buf' ->
998 lexeme = lexemeToFastString buf'
999 new_buf = mergeLexemes buf buf'
1000 is_a_qvarid = cont (mk_qvar_token mod lexeme) new_buf
1002 case _scc_ "Lex.haskellKeyword" lookupUFM haskellKeywordsFM lexeme of {
1003 Just kwd_token -> just_a_conid; -- avoid M.where etc.
1004 Nothing -> is_a_qvarid
1005 -- TODO: special ids (as, qualified, hiding) shouldn't be
1006 -- recognised as keywords here. ie. M.as is a qualified varid.
1010 slurp_trailing_hashes buf glaexts
1011 | flag glaexts = expandWhile# (`eqChar#` '#'#) buf
1016 | is_upper f = ITconid pk_str
1017 -- _[A-Z] is treated as a constructor in interface files.
1018 | f `eqChar#` '_'# && not (_NULL_ tl)
1019 && (case _HEAD_ tl of { C# g -> is_upper g }) = ITconid pk_str
1020 | is_ident f = ITvarid pk_str
1021 | f `eqChar#` ':'# = ITconsym pk_str
1022 | otherwise = ITvarsym pk_str
1024 (C# f) = _HEAD_ pk_str
1027 mk_qvar_token m token =
1028 case mk_var_token token of
1029 ITconid n -> ITqconid (m,n)
1030 ITvarid n -> ITqvarid (m,n)
1031 ITconsym n -> ITqconsym (m,n)
1032 ITvarsym n -> ITqvarsym (m,n)
1033 _ -> ITunknown (show token)
1036 ----------------------------------------------------------------------------
1037 Horrible stuff for dealing with M.(,,,)
1040 lex_tuple cont mod buf back_off =
1044 case currentChar# buf of
1045 ','# -> go (n+1) (stepOn buf)
1046 ')'# -> cont (ITqconid (mod, snd (mkTupNameStr n))) (stepOn buf)
1049 lex_ubx_tuple cont mod buf back_off =
1053 case currentChar# buf of
1054 ','# -> go (n+1) (stepOn buf)
1055 '#'# -> case lookAhead# buf 1# of
1056 ')'# -> cont (ITqconid (mod, snd (mkUbxTupNameStr n)))
1062 -----------------------------------------------------------------------------
1063 doDiscard rips along really fast, looking for a '#-}',
1064 indicating the end of the pragma we're skipping
1067 doDiscard inStr buf =
1068 case currentChar# buf of
1070 case lookAhead# buf 1# of { '#'# ->
1071 case lookAhead# buf 2# of { '-'# ->
1072 case lookAhead# buf 3# of { '}'# ->
1073 (lexemeToBuffer buf, stepOverLexeme (setCurrentPos# buf 4#));
1074 _ -> doDiscard inStr (incLexeme buf) };
1075 _ -> doDiscard inStr (incLexeme buf) };
1076 _ -> doDiscard inStr (incLexeme buf) }
1079 odd_slashes buf flg i# =
1080 case lookAhead# buf i# of
1081 '\\'# -> odd_slashes buf (not flg) (i# -# 1#)
1084 case lookAhead# buf (negateInt# 1#) of --backwards, actually
1085 '\\'# -> -- escaping something..
1086 if odd_slashes buf True (negateInt# 2#) then
1087 -- odd number of slashes, " is escaped.
1088 doDiscard inStr (incLexeme buf)
1090 -- even number of slashes, \ is escaped.
1091 doDiscard (not inStr) (incLexeme buf)
1092 _ -> case inStr of -- forced to avoid build-up
1093 True -> doDiscard False (incLexeme buf)
1094 False -> doDiscard True (incLexeme buf)
1095 _ -> doDiscard inStr (incLexeme buf)
1099 -----------------------------------------------------------------------------
1110 data PState = PState {
1112 glasgow_exts :: Int#,
1115 context :: [LayoutContext]
1118 type P a = StringBuffer -- Input string
1123 returnP a buf s = POk s a
1125 thenP :: P a -> (a -> P b) -> P b
1126 m `thenP` k = \ buf s ->
1128 POk s1 a -> k a buf s1
1129 PFailed err -> PFailed err
1131 thenP_ :: P a -> P b -> P b
1132 m `thenP_` k = m `thenP` \_ -> k
1134 mapP :: (a -> P b) -> [a] -> P [b]
1135 mapP f [] = returnP []
1138 mapP f as `thenP` \bs ->
1141 failP :: String -> P a
1142 failP msg buf s = PFailed (text msg)
1144 failMsgP :: Message -> P a
1145 failMsgP msg buf s = PFailed msg
1147 lexError :: String -> P a
1148 lexError str buf s@PState{ loc = loc }
1149 = failMsgP (hcat [ppr loc, text ": ", text str]) buf s
1151 getSrcLocP :: P SrcLoc
1152 getSrcLocP buf s@(PState{ loc = loc }) = POk s loc
1154 getSrcFile :: P FAST_STRING
1155 getSrcFile buf s@(PState{ loc = loc }) = POk s (srcLocFile loc)
1157 getContext :: P [LayoutContext]
1158 getContext buf s@(PState{ context = ctx }) = POk s ctx
1160 pushContext :: LayoutContext -> P ()
1161 pushContext ctxt buf s@(PState{ context = ctx }) = POk s{context = ctxt:ctx} ()
1165 This special case in layoutOn is to handle layout contexts with are
1166 indented the same or less than the current context. This is illegal
1167 according to the Haskell spec, so we have to arrange to close the
1168 current context. eg.
1173 after the first 'where', the sequence of events is:
1175 - layout system inserts a ';' (column 0)
1176 - parser begins a new context at column 0
1177 - parser shifts ';' (legal empty declaration)
1178 - parser sees 'class': parse error (we're still in the inner context)
1180 trouble is, by the time we know we need a new context, the lexer has
1181 already generated the ';'. Hacky solution is as follows: since we
1182 know the column of the next token (it's the column number of the new
1183 context), we set the ACTUAL column number of the new context to this
1184 numer plus one. Hence the next time the lexer is called, a '}' will
1185 be generated to close the new context straight away. Furthermore, we
1186 have to set the atbol flag so that the ';' that the parser shifted as
1187 part of the new context is re-generated.
1189 when the new context is *less* indented than the current one:
1191 f = f where g = g where
1194 - current context: column 12.
1195 - on seeing 'h' (column 0), the layout system inserts '}'
1196 - parser starts a new context, column 0
1197 - parser sees '}', uses it to close new context
1198 - we still need to insert another '}' followed by a ';',
1199 hence the atbol trick.
1204 layoutOn buf s@(PState{ bol = bol, context = ctx }) =
1205 let offset = lexemeIndex buf -# bol in
1207 Layout prev_off : _ | prev_off >=# offset ->
1208 --trace ("layout on, column: " ++ show (I# offset)) $
1209 POk s{ context = Layout (offset +# 1#) : ctx, atbol = 1# } ()
1211 --trace ("layout on, column: " ++ show (I# offset)) $
1212 POk s{ context = Layout offset : ctx } ()
1215 layoutOff buf s@(PState{ context = ctx }) =
1216 POk s{ context = NoLayout:ctx } ()
1219 popContext = \ buf s@(PState{ context = ctx }) ->
1221 (_:tl) -> POk s{ context = tl } ()
1222 [] -> panic "Lex.popContext: empty context"
1225 Note that if the name of the file we're processing ends
1226 with `hi-boot', we accept it on faith as having the right
1227 version. This is done so that .hi-boot files that comes
1228 with hsc don't have to be updated before every release,
1229 *and* it allows us to share .hi-boot files with versions
1230 of hsc that don't have .hi version checking (e.g., ghc-2.10's)
1232 If the version number is 0, the checking is also turned off.
1233 (needed to deal with GHC.hi only!)
1235 Once we can assume we're compiling with a version of ghc that
1236 supports interface file checking, we can drop the special
1239 checkVersion :: Maybe Integer -> P ()
1240 checkVersion mb@(Just v) buf s@(PState{loc = loc})
1241 | (v==0) || (v == fromInt opt_HiVersion) || opt_NoHiCheck = POk s ()
1242 | otherwise = PFailed (ifaceVersionErr mb loc ([]::[Token]){-Todo-})
1243 checkVersion mb@Nothing buf s@(PState{loc = loc})
1244 | "hi-boot" `isSuffixOf` (_UNPK_ (srcLocFile loc)) = POk s ()
1245 | otherwise = PFailed (ifaceVersionErr mb loc ([]::[Token]){-Todo-})
1247 -----------------------------------------------------------------
1249 ifaceParseErr :: StringBuffer -> SrcLoc -> Message
1251 = hsep [ppr l, ptext SLIT("Interface file parse error; on input `"),
1252 text (lexemeToString s), char '\'']
1254 ifaceVersionErr hi_vers l toks
1255 = hsep [ppr l, ptext SLIT("Interface file version error;"),
1256 ptext SLIT("Expected"), int opt_HiVersion,
1257 ptext SLIT("found "), pp_version]
1261 Nothing -> ptext SLIT("pre ghc-3.02 version")
1262 Just v -> ptext SLIT("version") <+> integer v