1 -----------------------------------------------------------------------------
2 -- (c) The University of Glasgow, 2003
6 -- This is a combination of an Alex-generated lexer from a regex
7 -- definition, with some hand-coded bits.
9 -- Completely accurate information about token-spans within the source
10 -- file is maintained. Every token has a start and end SrcLoc attached to it.
12 -----------------------------------------------------------------------------
16 -- - parsing integers is a bit slow
17 -- - readRational is a bit slow
19 -- Known bugs, that were also in the previous version:
20 -- - M... should be 3 tokens, not 1.
21 -- - pragma-end should be only valid in a pragma
25 Token(..), lexer, mkPState,
26 P(..), ParseResult(..), getSrcLoc,
27 failMsgP, failLocMsgP, failSpanMsgP, srcParseFail,
28 popContext, pushCurrentContext,
31 #include "HsVersions.h"
33 import ErrUtils ( Message )
42 import Util ( maybePrefixMatch )
50 $whitechar = [\ \t\n\r\f\v\xa0]
51 $white_no_nl = $whitechar # \n
55 $digit = [$ascdigit $unidigit]
57 $special = [\(\)\,\;\[\]\`\{\}]
58 $ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~]
60 $symbol = [$ascsymbol $unisymbol] # [$special \_\:\"\']
63 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
64 $large = [$asclarge $unilarge]
67 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
68 $small = [$ascsmall $unismall \_]
70 $graphic = [$small $large $symbol $digit $special \:\"\']
73 $hexit = [$digit A-F a-f]
74 $symchar = [$symbol \:]
76 $idchar = [$small $large $digit \']
78 @varid = $small $idchar*
79 @conid = $large $idchar*
81 @varsym = $symbol $symchar*
82 @consym = \: $symchar*
86 @hexadecimal = $hexit+
87 @exponent = [eE] [\-\+]? @decimal
89 -- we support the hierarchical module name extension:
92 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
96 -- everywhere: skip whitespace and comments
99 -- Everywhere: deal with nested comments. We explicitly rule out
100 -- pragmas, "{-#", so that we don't accidentally treat them as comments.
101 -- (this can happen even though pragmas will normally take precedence due to
102 -- longest-match, because pragmas aren't valid in every state, but comments
104 "{-" / { notFollowedBy '#' } { nested_comment }
106 -- Single-line comments are a bit tricky. Haskell 98 says that two or
107 -- more dashes followed by a symbol should be parsed as a varsym, so we
108 -- have to exclude those.
109 -- The regex says: "munch all the characters after the dashes, as long as
110 -- the first one is not a symbol".
111 "--"\-* ([^$symbol] .*)? ;
113 -- 'bol' state: beginning of a line. Slurp up all the whitespace (including
114 -- blank lines) until we find a non-whitespace character, then do layout
117 -- One slight wibble here: what if the line begins with {-#? In
118 -- theory, we have to lex the pragma to see if it's one we recognise,
119 -- and if it is, then we backtrack and do_bol, otherwise we treat it
120 -- as a nested comment. We don't bother with this: if the line begins
121 -- with {-#, then we'll assume it's a pragma we know about and go for do_bol.
124 ^\# (line)? { begin line_prag1 }
125 ^\# pragma .* \n ; -- GCC 3.3 CPP generated, apparently
129 -- after a layout keyword (let, where, do, of), we begin a new layout
130 -- context if the curly brace is missing.
131 -- Careful! This stuff is quite delicate.
132 <layout, layout_do> {
133 \{ / { notFollowedBy '-' } { pop_and open_brace }
134 -- we might encounter {-# here, but {- has been handled already
136 ^\# (line)? { begin line_prag1 }
139 -- do is treated in a subtly different way, see new_layout_context
140 <layout> () { new_layout_context True }
141 <layout_do> () { new_layout_context False }
143 -- after a new layout context which was found to be to the left of the
144 -- previous context, we have generated a '{' token, and we now need to
145 -- generate a matching '}' token.
146 <layout_left> () { do_layout_left }
148 <0,glaexts> \n { begin bol }
150 "{-#" $whitechar* (line|LINE) { begin line_prag2 }
152 -- single-line line pragmas, of the form
153 -- # <line> "<file>" <extra-stuff> \n
154 <line_prag1> $digit+ { set_line line_prag1a }
155 <line_prag1a> \" [$graphic \ ]* \" { set_file line_prag1b }
156 <line_prag1b> .* { pop }
158 -- Haskell-style line pragmas, of the form
159 -- {-# LINE <line> "<file>" #-}
160 <line_prag2> $digit+ { set_line line_prag2a }
161 <line_prag2a> \" [$graphic \ ]* \" { set_file line_prag2b }
162 <line_prag2b> "#-}"|"-}" { pop }
163 -- NOTE: accept -} at the end of a LINE pragma, for compatibility
164 -- with older versions of GHC which generated these.
167 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
168 { token ITspecialise_prag }
169 "{-#" $whitechar* (SOURCE|source) { token ITsource_prag }
170 "{-#" $whitechar* (INLINE|inline) { token ITinline_prag }
171 "{-#" $whitechar* (NO(T?)INLINE|no(t?)inline)
172 { token ITnoinline_prag }
173 "{-#" $whitechar* (RULES|rules) { token ITrules_prag }
174 "{-#" $whitechar* (DEPRECATED|deprecated)
175 { token ITdeprecated_prag }
176 "{-#" $whitechar* (SCC|scc) { token ITscc_prag }
177 "{-#" $whitechar* (CORE|core) { token ITcore_prag }
178 "{-#" $whitechar* (UNPACK|unpack) { token ITunpack_prag }
180 "{-#" { nested_comment }
182 -- ToDo: should only be valid inside a pragma:
183 "#-}" { token ITclose_prag}
187 -- '0' state: ordinary lexemes
188 -- 'glaexts' state: glasgow extensions (postfix '#', etc.)
193 "[:" / { ifExtension parrEnabled } { token ITopabrack }
194 ":]" / { ifExtension parrEnabled } { token ITcpabrack }
198 "[|" / { ifExtension thEnabled } { token ITopenExpQuote }
199 "[e|" / { ifExtension thEnabled } { token ITopenExpQuote }
200 "[p|" / { ifExtension thEnabled } { token ITopenPatQuote }
201 "[d|" / { ifExtension thEnabled } { layout_token ITopenDecQuote }
202 "[t|" / { ifExtension thEnabled } { token ITopenTypQuote }
203 "|]" / { ifExtension thEnabled } { token ITcloseQuote }
204 \$ @varid / { ifExtension thEnabled } { skip_one_varid ITidEscape }
205 "$(" / { ifExtension thEnabled } { token ITparenEscape }
209 "(|" / { ifExtension arrowsEnabled `alexAndPred` notFollowedBySymbol }
210 { special IToparenbar }
211 "|)" / { ifExtension arrowsEnabled } { special ITcparenbar }
215 \? @varid / { ifExtension ipEnabled } { skip_one_varid ITdupipvarid }
216 \% @varid / { ifExtension ipEnabled } { skip_one_varid ITsplitipvarid }
220 "(#" / { notFollowedBySymbol } { token IToubxparen }
221 "#)" { token ITcubxparen }
222 "{|" { token ITocurlybar }
223 "|}" { token ITccurlybar }
227 \( { special IToparen }
228 \) { special ITcparen }
229 \[ { special ITobrack }
230 \] { special ITcbrack }
231 \, { special ITcomma }
232 \; { special ITsemi }
233 \` { special ITbackquote }
240 @qual @varid { check_qvarid }
241 @qual @conid { idtoken qconid }
243 @conid { idtoken conid }
246 -- after an illegal qvarid, such as 'M.let',
247 -- we back up and try again in the bad_qvarid state:
249 @conid { pop_and (idtoken conid) }
250 @qual @conid { pop_and (idtoken qconid) }
254 @qual @varid "#"+ { idtoken qvarid }
255 @qual @conid "#"+ { idtoken qconid }
256 @varid "#"+ { varid }
257 @conid "#"+ { idtoken conid }
263 @qual @varsym { idtoken qvarsym }
264 @qual @consym { idtoken qconsym }
270 @decimal { tok_decimal }
271 0[oO] @octal { tok_octal }
272 0[xX] @hexadecimal { tok_hexadecimal }
276 @decimal \# { prim_decimal }
277 0[oO] @octal \# { prim_octal }
278 0[xX] @hexadecimal \# { prim_hexadecimal }
281 <0,glaexts> @floating_point { strtoken tok_float }
282 <glaexts> @floating_point \# { init_strtoken 1 prim_float }
283 <glaexts> @floating_point \# \# { init_strtoken 2 prim_double }
285 -- Strings and chars are lexed by hand-written code. The reason is
286 -- that even if we recognise the string or char here in the regex
287 -- lexer, we would still have to parse the string afterward in order
288 -- to convert it to a String.
291 \" { lex_string_tok }
295 -- work around bug in Alex 2.0
296 #if __GLASGOW_HASKELL__ < 503
297 unsafeAt arr i = arr ! i
300 -- -----------------------------------------------------------------------------
304 = ITas -- Haskell keywords
328 | ITscc -- ToDo: remove (we use {-# SCC "..." #-} now)
330 | ITforall -- GHC extension keywords
343 | ITspecialise_prag -- Pragmas
351 | ITcore_prag -- hdaume: core annotations
355 | ITdotdot -- reserved symbols
371 | ITbiglam -- GHC-extension symbols
373 | ITocurly -- special symbols
375 | ITocurlybar -- {|, for type applications
376 | ITccurlybar -- |}, for type applications
380 | ITopabrack -- [:, for parallel arrays with -fparr
381 | ITcpabrack -- :], for parallel arrays with -fparr
392 | ITvarid FastString -- identifiers
394 | ITvarsym FastString
395 | ITconsym FastString
396 | ITqvarid (FastString,FastString)
397 | ITqconid (FastString,FastString)
398 | ITqvarsym (FastString,FastString)
399 | ITqconsym (FastString,FastString)
401 | ITdupipvarid FastString -- GHC extension: implicit param: ?x
402 | ITsplitipvarid FastString -- GHC extension: implicit param: %x
404 | ITpragma StringBuffer
407 | ITstring FastString
409 | ITrational Rational
412 | ITprimstring FastString
414 | ITprimfloat Rational
415 | ITprimdouble Rational
417 -- MetaHaskell extension tokens
418 | ITopenExpQuote -- [| or [e|
419 | ITopenPatQuote -- [p|
420 | ITopenDecQuote -- [d|
421 | ITopenTypQuote -- [t|
423 | ITidEscape FastString -- $x
424 | ITparenEscape -- $(
428 -- Arrow notation extension
435 | ITLarrowtail -- -<<
436 | ITRarrowtail -- >>-
438 | ITunknown String -- Used when the lexer can't make sense of it
439 | ITeof -- end of file token
441 deriving Show -- debugging
444 isSpecial :: Token -> Bool
445 -- If we see M.x, where x is a keyword, but
446 -- is special, we treat is as just plain M.x,
448 isSpecial ITas = True
449 isSpecial IThiding = True
450 isSpecial ITqualified = True
451 isSpecial ITforall = True
452 isSpecial ITexport = True
453 isSpecial ITlabel = True
454 isSpecial ITdynamic = True
455 isSpecial ITsafe = True
456 isSpecial ITthreadsafe = True
457 isSpecial ITunsafe = True
458 isSpecial ITccallconv = True
459 isSpecial ITstdcallconv = True
460 isSpecial ITmdo = True
463 -- the bitmap provided as the third component indicates whether the
464 -- corresponding extension keyword is valid under the extension options
465 -- provided to the compiler; if the extension corresponding to *any* of the
466 -- bits set in the bitmap is enabled, the keyword is valid (this setup
467 -- facilitates using a keyword in two different extensions that can be
468 -- activated independently)
470 reservedWordsFM = listToUFM $
471 map (\(x, y, z) -> (mkFastString x, (y, z)))
472 [( "_", ITunderscore, 0 ),
474 ( "case", ITcase, 0 ),
475 ( "class", ITclass, 0 ),
476 ( "data", ITdata, 0 ),
477 ( "default", ITdefault, 0 ),
478 ( "deriving", ITderiving, 0 ),
480 ( "else", ITelse, 0 ),
481 ( "hiding", IThiding, 0 ),
483 ( "import", ITimport, 0 ),
485 ( "infix", ITinfix, 0 ),
486 ( "infixl", ITinfixl, 0 ),
487 ( "infixr", ITinfixr, 0 ),
488 ( "instance", ITinstance, 0 ),
490 ( "module", ITmodule, 0 ),
491 ( "newtype", ITnewtype, 0 ),
493 ( "qualified", ITqualified, 0 ),
494 ( "then", ITthen, 0 ),
495 ( "type", ITtype, 0 ),
496 ( "where", ITwhere, 0 ),
497 ( "_scc_", ITscc, 0 ), -- ToDo: remove
499 ( "forall", ITforall, bit glaExtsBit),
500 ( "mdo", ITmdo, bit glaExtsBit),
502 ( "foreign", ITforeign, bit ffiBit),
503 ( "export", ITexport, bit ffiBit),
504 ( "label", ITlabel, bit ffiBit),
505 ( "dynamic", ITdynamic, bit ffiBit),
506 ( "safe", ITsafe, bit ffiBit),
507 ( "threadsafe", ITthreadsafe, bit ffiBit),
508 ( "unsafe", ITunsafe, bit ffiBit),
509 ( "stdcall", ITstdcallconv, bit ffiBit),
510 ( "ccall", ITccallconv, bit ffiBit),
511 ( "dotnet", ITdotnet, bit ffiBit),
513 ( "rec", ITrec, bit arrowsBit),
514 ( "proc", ITproc, bit arrowsBit)
517 reservedSymsFM = listToUFM $
518 map (\ (x,y,z) -> (mkFastString x,(y,z)))
519 [ ("..", ITdotdot, 0)
520 ,(":", ITcolon, 0) -- (:) is a reserved op,
521 -- meaning only list cons
534 ,("*", ITstar, bit glaExtsBit) -- For data T (a::*) = MkT
535 ,(".", ITdot, bit glaExtsBit) -- For 'forall a . t'
537 ,("-<", ITlarrowtail, bit arrowsBit)
538 ,(">-", ITrarrowtail, bit arrowsBit)
539 ,("-<<", ITLarrowtail, bit arrowsBit)
540 ,(">>-", ITRarrowtail, bit arrowsBit)
543 -- -----------------------------------------------------------------------------
546 type Action = SrcSpan -> StringBuffer -> Int -> P (Located Token)
548 special :: Token -> Action
549 special tok span _buf len = return (L span tok)
551 token, layout_token :: Token -> Action
552 token t span buf len = return (L span t)
553 layout_token t span buf len = pushLexState layout >> return (L span t)
555 idtoken :: (StringBuffer -> Int -> Token) -> Action
556 idtoken f span buf len = return (L span $! (f buf len))
558 skip_one_varid :: (FastString -> Token) -> Action
559 skip_one_varid f span buf len
560 = return (L span $! f (lexemeToFastString (stepOn buf) (len-1)))
562 strtoken :: (String -> Token) -> Action
563 strtoken f span buf len =
564 return (L span $! (f $! lexemeToString buf len))
566 init_strtoken :: Int -> (String -> Token) -> Action
567 -- like strtoken, but drops the last N character(s)
568 init_strtoken drop f span buf len =
569 return (L span $! (f $! lexemeToString buf (len-drop)))
571 begin :: Int -> Action
572 begin code _span _str _len = do pushLexState code; lexToken
575 pop _span _buf _len = do popLexState; lexToken
577 pop_and :: Action -> Action
578 pop_and act span buf len = do popLexState; act span buf len
580 notFollowedBy char _ _ _ (_,buf) = atEnd buf || currentChar buf /= char
582 notFollowedBySymbol _ _ _ (_,buf)
583 = atEnd buf || currentChar buf `notElem` "!#$%&*+./<=>?@\\^|-~"
585 ifExtension pred bits _ _ _ = pred bits
588 nested comments require traversing by hand, they can't be parsed
589 using regular expressions.
591 nested_comment :: Action
592 nested_comment span _str _len = do
595 where go 0 input = do setInput input; lexToken
597 case alexGetChar input of
602 case alexGetChar input of
604 Just ('\125',input) -> go (n-1) input
605 Just (c,_) -> go n input
607 case alexGetChar input of
609 Just ('-',input') -> go (n+1) input'
610 Just (c,input) -> go n input
613 err input = do failLocMsgP (srcSpanStart span) (fst input)
616 open_brace, close_brace :: Action
617 open_brace span _str _len = do
619 setContext (NoLayout:ctx)
620 return (L span ITocurly)
621 close_brace span _str _len = do
623 return (L span ITccurly)
625 -- We have to be careful not to count M.<varid> as a qualified name
626 -- when <varid> is a keyword. We hack around this by catching
627 -- the offending tokens afterward, and re-lexing in a different state.
628 check_qvarid span buf len = do
629 case lookupUFM reservedWordsFM var of
631 | not (isSpecial keyword) ->
635 b <- extension (\i -> exts .&. i /= 0)
638 _other -> return token
640 (mod,var) = splitQualName buf len
641 token = L span (ITqvarid (mod,var))
644 setInput (srcSpanStart span,buf)
645 pushLexState bad_qvarid
648 qvarid buf len = ITqvarid $! splitQualName buf len
649 qconid buf len = ITqconid $! splitQualName buf len
651 splitQualName :: StringBuffer -> Int -> (FastString,FastString)
652 -- takes a StringBuffer and a length, and returns the module name
653 -- and identifier parts of a qualified name. Splits at the *last* dot,
654 -- because of hierarchical module names.
655 splitQualName orig_buf len = split orig_buf 0 0
658 | n == len = done dot_off
659 | lookAhead buf n == '.' = split2 buf n (n+1)
660 | otherwise = split buf dot_off (n+1)
662 -- careful, we might get names like M....
663 -- so, if the character after the dot is not upper-case, this is
664 -- the end of the qualifier part.
666 | isUpper (lookAhead buf n) = split buf dot_off (n+1)
667 | otherwise = done dot_off
670 (lexemeToFastString orig_buf dot_off,
671 lexemeToFastString (stepOnBy (dot_off+1) orig_buf) (len - dot_off -1))
674 case lookupUFM reservedWordsFM fs of
675 Just (keyword,0) -> do
677 return (L span keyword)
678 Just (keyword,exts) -> do
679 b <- extension (\i -> exts .&. i /= 0)
680 if b then do maybe_layout keyword
681 return (L span keyword)
682 else return (L span (ITvarid fs))
683 _other -> return (L span (ITvarid fs))
685 fs = lexemeToFastString buf len
687 conid buf len = ITconid fs
688 where fs = lexemeToFastString buf len
690 qvarsym buf len = ITqvarsym $! splitQualName buf len
691 qconsym buf len = ITqconsym $! splitQualName buf len
693 varsym = sym ITvarsym
694 consym = sym ITconsym
696 sym con span buf len =
697 case lookupUFM reservedSymsFM fs of
698 Just (keyword,0) -> return (L span keyword)
699 Just (keyword,exts) -> do
700 b <- extension (\i -> exts .&. i /= 0)
701 if b then return (L span keyword)
702 else return (L span $! con fs)
703 _other -> return (L span $! con fs)
705 fs = lexemeToFastString buf len
707 tok_decimal span buf len
708 = return (L span (ITinteger $! parseInteger buf len 10 oct_or_dec))
710 tok_octal span buf len
711 = return (L span (ITinteger $! parseInteger (stepOnBy 2 buf) (len-2) 8 oct_or_dec))
713 tok_hexadecimal span buf len
714 = return (L span (ITinteger $! parseInteger (stepOnBy 2 buf) (len-2) 16 hex))
716 prim_decimal span buf len
717 = return (L span (ITprimint $! parseInteger buf (len-1) 10 oct_or_dec))
719 prim_octal span buf len
720 = return (L span (ITprimint $! parseInteger (stepOnBy 2 buf) (len-3) 8 oct_or_dec))
722 prim_hexadecimal span buf len
723 = return (L span (ITprimint $! parseInteger (stepOnBy 2 buf) (len-3) 16 hex))
725 tok_float str = ITrational $! readRational__ str
726 prim_float str = ITprimfloat $! readRational__ str
727 prim_double str = ITprimdouble $! readRational__ str
729 parseInteger :: StringBuffer -> Int -> Integer -> (Char->Int) -> Integer
730 parseInteger buf len radix to_int
732 where go i x | i == len = x
733 | otherwise = go (i+1) (x * radix + toInteger (to_int (lookAhead buf i)))
735 -- -----------------------------------------------------------------------------
738 -- we're at the first token on a line, insert layout tokens if necessary
740 do_bol span _str _len = do
741 pos <- getOffside (srcSpanEnd span)
744 --trace "layout: inserting '}'" $ do
746 -- do NOT pop the lex state, we might have a ';' to insert
747 return (L span ITvccurly)
749 --trace "layout: inserting ';'" $ do
751 return (L span ITsemi)
756 -- certain keywords put us in the "layout" state, where we might
757 -- add an opening curly brace.
758 maybe_layout ITdo = pushLexState layout_do
759 maybe_layout ITmdo = pushLexState layout_do
760 maybe_layout ITof = pushLexState layout
761 maybe_layout ITlet = pushLexState layout
762 maybe_layout ITwhere = pushLexState layout
763 maybe_layout ITrec = pushLexState layout
764 maybe_layout _ = return ()
766 -- Pushing a new implicit layout context. If the indentation of the
767 -- next token is not greater than the previous layout context, then
768 -- Haskell 98 says that the new layout context should be empty; that is
769 -- the lexer must generate {}.
771 -- We are slightly more lenient than this: when the new context is started
772 -- by a 'do', then we allow the new context to be at the same indentation as
773 -- the previous context. This is what the 'strict' argument is for.
775 new_layout_context strict span _buf _len = do
777 let offset = srcSpanStartCol span
780 Layout prev_off : _ |
781 (strict && prev_off >= offset ||
782 not strict && prev_off > offset) -> do
783 -- token is indented to the left of the previous context.
784 -- we must generate a {} sequence now.
785 pushLexState layout_left
786 return (L span ITvocurly)
788 setContext (Layout offset : ctx)
789 return (L span ITvocurly)
791 do_layout_left span _buf _len = do
793 pushLexState bol -- we must be at the start of a line
794 return (L span ITvccurly)
796 -- -----------------------------------------------------------------------------
799 set_line :: Int -> Action
800 set_line code span buf len = do
801 let line = parseInteger buf len 10 oct_or_dec
802 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
803 -- subtract one: the line number refers to the *following* line
808 set_file :: Int -> Action
809 set_file code span buf len = do
810 let file = lexemeToFastString (stepOn buf) (len-2)
811 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
816 -- -----------------------------------------------------------------------------
819 -- This stuff is horrible. I hates it.
821 lex_string_tok :: Action
822 lex_string_tok span buf len = do
825 return (L (mkSrcSpan (srcSpanStart span) end) tok)
827 lex_string :: String -> P Token
830 case alexGetChar i of
835 glaexts <- extension glaExtsEnabled
839 case alexGetChar i of
843 then failMsgP "primitive string literal must contain only characters <= \'\\xFF\'"
844 else let s' = mkFastStringNarrow (reverse s) in
845 -- always a narrow string/byte array
846 return (ITprimstring s')
848 return (ITstring (mkFastString (reverse s)))
850 return (ITstring (mkFastString (reverse s)))
853 | Just ('&',i) <- next -> do
854 setInput i; lex_string s
855 | Just (c,i) <- next, is_space c -> do
856 setInput i; lex_stringgap s
857 where next = alexGetChar i
867 c | is_space c -> lex_stringgap s
871 lex_char_tok :: Action
872 -- Here we are basically parsing character literals, such as 'x' or '\n'
873 -- but, when Template Haskell is on, we additionally spot
874 -- 'x and ''T, returning ITvarQuote and ITtyQuote respectively,
875 -- but WIHTOUT CONSUMING the x or T part (the parser does that).
876 -- So we have to do two characters of lookahead: when we see 'x we need to
877 -- see if there's a trailing quote
878 lex_char_tok span buf len = do -- We've seen '
879 i1 <- getInput -- Look ahead to first character
880 let loc = srcSpanStart span
881 case alexGetChar i1 of
884 Just ('\'', i2@(end2,_)) -> do -- We've seen ''
885 th_exts <- extension thEnabled
888 return (L (mkSrcSpan loc end2) ITtyQuote)
891 Just ('\\', i2@(end2,_)) -> do -- We've seen 'backslash
894 mc <- getCharOrFail -- Trailing quote
895 if mc == '\'' then finish_char_tok loc lit_ch
898 Just (c, i2@(end2,_)) | not (is_any c) -> lit_error
901 -- We've seen 'x, where x is a valid character
902 -- (i.e. not newline etc) but not a quote or backslash
903 case alexGetChar i2 of -- Look ahead one more character
905 Just ('\'', i3) -> do -- We've seen 'x'
907 finish_char_tok loc c
908 _other -> do -- We've seen 'x not followed by quote
909 -- If TH is on, just parse the quote only
910 th_exts <- extension thEnabled
911 if th_exts then return (L (mkSrcSpan loc (fst i1)) ITvarQuote)
914 finish_char_tok :: SrcLoc -> Char -> P (Located Token)
915 finish_char_tok loc ch -- We've already seen the closing quote
916 -- Just need to check for trailing #
917 = do glaexts <- extension glaExtsEnabled
918 i@(end,_) <- getInput
920 case alexGetChar i of
921 Just ('#',i@(end,_)) -> do
923 return (L (mkSrcSpan loc end) (ITprimchar ch))
925 return (L (mkSrcSpan loc end) (ITchar ch))
927 return (L (mkSrcSpan loc end) (ITchar ch))
934 c | is_any c -> return c
951 '^' -> do c <- getCharOrFail
952 if c >= '@' && c <= '_'
953 then return (chr (ord c - ord '@'))
956 'x' -> readNum is_hexdigit 16 hex
957 'o' -> readNum is_octdigit 8 oct_or_dec
958 x | is_digit x -> readNum2 is_digit 10 oct_or_dec (oct_or_dec x)
962 case alexGetChar i of
965 case alexGetChar i2 of
968 let str = [c1,c2,c3] in
969 case [ (c,rest) | (p,c) <- silly_escape_chars,
970 Just rest <- [maybePrefixMatch p str] ] of
971 (escape_char,[]):_ -> do
974 (escape_char,_:_):_ -> do
979 readNum :: (Char -> Bool) -> Int -> (Char -> Int) -> P Char
980 readNum is_digit base conv = do
983 then readNum2 is_digit base conv (conv c)
986 readNum2 is_digit base conv i = do
989 where read i input = do
990 case alexGetChar input of
991 Just (c,input') | is_digit c -> do
992 read (i*base + conv c) input'
995 if i >= 0 && i <= 0x10FFFF
1001 || (c >= 'a' && c <= 'f')
1002 || (c >= 'A' && c <= 'F')
1004 hex c | is_digit c = ord c - ord '0'
1005 | otherwise = ord (to_lower c) - ord 'a' + 10
1007 oct_or_dec c = ord c - ord '0'
1009 is_octdigit c = c >= '0' && c <= '7'
1012 | c >= 'A' && c <= 'Z' = chr (ord c - (ord 'A' - ord 'a'))
1015 silly_escape_chars = [
1052 lit_error = lexError "lexical error in string/character literal"
1054 getCharOrFail :: P Char
1057 case alexGetChar i of
1058 Nothing -> lexError "unexpected end-of-file in string/character literal"
1059 Just (c,i) -> do setInput i; return c
1061 -- -----------------------------------------------------------------------------
1064 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
1066 (n,d,s) <- readFix r
1068 return ((n%1)*10^^(k-d), t)
1071 (ds,s) <- lexDecDigits r
1072 (ds',t) <- lexDotDigits s
1073 return (read (ds++ds'), length ds', t)
1075 readExp (e:s) | e `elem` "eE" = readExp' s
1076 readExp s = return (0,s)
1078 readExp' ('+':s) = readDec s
1079 readExp' ('-':s) = do
1082 readExp' s = readDec s
1085 (ds,r) <- nonnull isDigit s
1086 return (foldl1 (\n d -> n * 10 + d) [ ord d - ord '0' | d <- ds ],
1089 lexDecDigits = nonnull isDigit
1091 lexDotDigits ('.':s) = return (span isDigit s)
1092 lexDotDigits s = return ("",s)
1094 nonnull p s = do (cs@(_:_),t) <- return (span p s)
1097 readRational__ :: String -> Rational -- NB: *does* handle a leading "-"
1098 readRational__ top_s
1100 '-' : xs -> - (read_me xs)
1104 = case (do { (x,"") <- readRational s ; return x }) of
1106 [] -> error ("readRational__: no parse:" ++ top_s)
1107 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
1109 -- -----------------------------------------------------------------------------
1119 SrcSpan -- The start and end of the text span related to
1120 -- the error. Might be used in environments which can
1121 -- show this span, e.g. by highlighting it.
1122 Message -- The error message
1124 data PState = PState {
1125 buffer :: StringBuffer,
1126 last_loc :: SrcSpan, -- pos of previous token
1127 last_len :: !Int, -- len of previous token
1128 loc :: SrcLoc, -- current loc (end of prev token + 1)
1129 extsBitmap :: !Int, -- bitmap that determines permitted extensions
1130 context :: [LayoutContext],
1133 -- last_loc and last_len are used when generating error messages,
1134 -- and in pushCurrentContext only.
1136 newtype P a = P { unP :: PState -> ParseResult a }
1138 instance Monad P where
1144 returnP a = P $ \s -> POk s a
1146 thenP :: P a -> (a -> P b) -> P b
1147 (P m) `thenP` k = P $ \ s ->
1149 POk s1 a -> (unP (k a)) s1
1150 PFailed span err -> PFailed span err
1152 failP :: String -> P a
1153 failP msg = P $ \s -> PFailed (last_loc s) (text msg)
1155 failMsgP :: String -> P a
1156 failMsgP msg = P $ \s -> PFailed (last_loc s) (text msg)
1158 failLocMsgP :: SrcLoc -> SrcLoc -> String -> P a
1159 failLocMsgP loc1 loc2 str = P $ \s -> PFailed (mkSrcSpan loc1 loc2) (text str)
1161 failSpanMsgP :: SrcSpan -> String -> P a
1162 failSpanMsgP span msg = P $ \s -> PFailed span (text msg)
1164 extension :: (Int -> Bool) -> P Bool
1165 extension p = P $ \s -> POk s (p $! extsBitmap s)
1168 getExts = P $ \s -> POk s (extsBitmap s)
1170 setSrcLoc :: SrcLoc -> P ()
1171 setSrcLoc new_loc = P $ \s -> POk s{loc=new_loc} ()
1173 getSrcLoc :: P SrcLoc
1174 getSrcLoc = P $ \s@(PState{ loc=loc }) -> POk s loc
1176 setLastToken :: SrcSpan -> Int -> P ()
1177 setLastToken loc len = P $ \s -> POk s{ last_loc=loc, last_len=len } ()
1179 type AlexInput = (SrcLoc,StringBuffer)
1181 alexInputPrevChar :: AlexInput -> Char
1182 alexInputPrevChar (_,s) = prevChar s '\n'
1184 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
1187 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
1188 where c = currentChar s
1189 loc' = advanceSrcLoc loc c
1192 getInput :: P AlexInput
1193 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
1195 setInput :: AlexInput -> P ()
1196 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()
1198 pushLexState :: Int -> P ()
1199 pushLexState ls = P $ \s@PState{ lex_state=l } -> POk s{lex_state=ls:l} ()
1201 popLexState :: P Int
1202 popLexState = P $ \s@PState{ lex_state=ls:l } -> POk s{ lex_state=l } ls
1204 getLexState :: P Int
1205 getLexState = P $ \s@PState{ lex_state=ls:l } -> POk s ls
1207 -- for reasons of efficiency, flags indicating language extensions (eg,
1208 -- -fglasgow-exts or -fparr) are represented by a bitmap stored in an unboxed
1211 glaExtsBit, ffiBit, parrBit :: Int
1219 glaExtsEnabled, ffiEnabled, parrEnabled :: Int -> Bool
1220 glaExtsEnabled flags = testBit flags glaExtsBit
1221 ffiEnabled flags = testBit flags ffiBit
1222 parrEnabled flags = testBit flags parrBit
1223 arrowsEnabled flags = testBit flags arrowsBit
1224 thEnabled flags = testBit flags thBit
1225 ipEnabled flags = testBit flags ipBit
1227 -- create a parse state
1229 mkPState :: StringBuffer -> SrcLoc -> DynFlags -> PState
1230 mkPState buf loc flags =
1233 last_loc = mkSrcSpan loc loc,
1236 extsBitmap = fromIntegral bitmap,
1238 lex_state = [bol, if glaExtsEnabled bitmap then glaexts else 0]
1239 -- we begin in the layout state if toplev_layout is set
1242 bitmap = glaExtsBit `setBitIf` dopt Opt_GlasgowExts flags
1243 .|. ffiBit `setBitIf` dopt Opt_FFI flags
1244 .|. parrBit `setBitIf` dopt Opt_PArr flags
1245 .|. arrowsBit `setBitIf` dopt Opt_Arrows flags
1246 .|. thBit `setBitIf` dopt Opt_TH flags
1247 .|. ipBit `setBitIf` dopt Opt_ImplicitParams flags
1249 setBitIf :: Int -> Bool -> Int
1250 b `setBitIf` cond | cond = bit b
1253 getContext :: P [LayoutContext]
1254 getContext = P $ \s@PState{context=ctx} -> POk s ctx
1256 setContext :: [LayoutContext] -> P ()
1257 setContext ctx = P $ \s -> POk s{context=ctx} ()
1260 popContext = P $ \ s@(PState{ buffer = buf, context = ctx,
1261 loc = loc, last_len = len, last_loc = last_loc }) ->
1263 (_:tl) -> POk s{ context = tl } ()
1264 [] -> PFailed last_loc (srcParseErr buf len)
1266 -- Push a new layout context at the indentation of the last token read.
1267 -- This is only used at the outer level of a module when the 'module'
1268 -- keyword is missing.
1269 pushCurrentContext :: P ()
1270 pushCurrentContext = P $ \ s@PState{ last_loc=loc, context=ctx } ->
1271 POk s{ context = Layout (srcSpanStartCol loc) : ctx} ()
1273 getOffside :: SrcLoc -> P Ordering
1274 getOffside loc = P $ \s@PState{context=stk} ->
1275 let ord = case stk of
1276 (Layout n:_) -> compare (srcLocCol loc) n
1280 -- ---------------------------------------------------------------------------
1281 -- Construct a parse error
1284 :: StringBuffer -- current buffer (placed just after the last token)
1285 -> Int -- length of the previous token
1288 = hcat [ if null token
1289 then ptext SLIT("parse error (possibly incorrect indentation)")
1290 else hcat [ptext SLIT("parse error on input "),
1291 char '`', text token, char '\'']
1293 where token = lexemeToString (stepOnBy (-len) buf) len
1295 -- Report a parse failure, giving the span of the previous token as
1296 -- the location of the error. This is the entry point for errors
1297 -- detected during parsing.
1299 srcParseFail = P $ \PState{ buffer = buf, last_len = len,
1300 last_loc = last_loc, loc = loc } ->
1301 PFailed last_loc (srcParseErr buf len)
1303 -- A lexical error is reported at a particular position in the source file,
1304 -- not over a token range. TODO: this is slightly wrong, because we record
1305 -- the error at the character position following the one which caused the
1306 -- error. We should somehow back up by one character.
1307 lexError :: String -> P a
1310 i@(end,_) <- getInput
1311 failLocMsgP loc end str
1313 -- -----------------------------------------------------------------------------
1314 -- This is the top-level function: called from the parser each time a
1315 -- new token is to be read from the input.
1317 lexer :: (Located Token -> P a) -> P a
1319 tok@(L _ tok__) <- lexToken
1320 --trace ("token: " ++ show tok__) $ do
1323 lexToken :: P (Located Token)
1325 inp@(loc1,buf) <- getInput
1328 case alexScanUser exts inp sc of
1329 AlexEOF -> do let span = mkSrcSpan loc1 loc1
1331 return (L span ITeof)
1332 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
1333 AlexSkip inp2 _ -> do
1336 AlexToken inp2@(end,buf2) len t -> do
1338 let span = mkSrcSpan loc1 end
1339 span `seq` setLastToken span len