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(..), Token__(..), lexer, mkPState, showPFailed,
26 P(..), ParseResult(..), setSrcLocFor, getSrcLoc,
27 failMsgP, failLocMsgP, srcParseFail,
28 popContext, pushCurrentContext,
31 #include "HsVersions.h"
33 import ForeignCall ( Safety(..) )
34 import ErrUtils ( Message )
43 import Util ( maybePrefixMatch )
51 $whitechar = [\ \t\n\r\f\v]
52 $white_no_nl = $whitechar # \n
56 $digit = [$ascdigit $unidigit]
58 $special = [\(\)\,\;\[\]\`\{\}]
59 $ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~]
61 $symbol = [$ascsymbol $unisymbol] # [$special \_\:\"\']
64 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
65 $large = [$asclarge $unilarge]
68 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
69 $small = [$ascsmall $unismall \_]
71 $graphic = [$small $large $symbol $digit $special \:\"\']
74 $hexit = [$digit A-F a-f]
75 $symchar = [$symbol \:]
77 $idchar = [$small $large $digit \']
79 @varid = $small $idchar*
80 @conid = $large $idchar*
82 @varsym = $symbol $symchar*
83 @consym = \: $symchar*
87 @hexadecimal = $hexit+
88 @exponent = [eE] [\-\+]? @decimal
90 -- we support the hierarchical module name extension:
93 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
97 -- everywhere: skip whitespace and comments
100 -- Everywhere: deal with nested comments. We explicitly rule out
101 -- pragmas, "{-#", so that we don't accidentally treat them as comments.
102 -- (this can happen even though pragmas will normally take precedence due to
103 -- longest-match, because pragmas aren't valid in every state, but comments
105 "{-" / { notFollowedBy '#' } { nested_comment }
107 -- Single-line comments are a bit tricky. Haskell 98 says that two or
108 -- more dashes followed by a symbol should be parsed as a varsym, so we
109 -- have to exclude those.
110 -- The regex says: "munch all the characters after the dashes, as long as
111 -- the first one is not a symbol".
112 "--"\-* ([^$symbol] .*)? ;
114 -- 'bol' state: beginning of a line. Slurp up all the whitespace (including
115 -- blank lines) until we find a non-whitespace character, then do layout
118 -- One slight wibble here: what if the line begins with {-#? In
119 -- theory, we have to lex the pragma to see if it's one we recognise,
120 -- and if it is, then we backtrack and do_bol, otherwise we treat it
121 -- as a nested comment. We don't bother with this: if the line begins
122 -- with {-#, then we'll assume it's a pragma we know about and go for do_bol.
125 ^\# (line)? { begin line_prag1 }
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 }
165 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
166 { token ITspecialise_prag }
167 "{-#" $whitechar* (SOURCE|source) { token ITsource_prag }
168 "{-#" $whitechar* (INLINE|inline) { token ITinline_prag }
169 "{-#" $whitechar* (NO(T?)INLINE|no(t?)inline)
170 { token ITnoinline_prag }
171 "{-#" $whitechar* (RULES|rules) { token ITrules_prag }
172 "{-#" $whitechar* (DEPRECATED|deprecated)
173 { token ITdeprecated_prag }
174 "{-#" $whitechar* (SCC|scc) { token ITscc_prag }
175 "{-#" $whitechar* (CORE|core) { token ITcore_prag }
177 "{-#" { nested_comment }
179 -- ToDo: should only be valid inside a pragma:
180 "#-}" { token ITclose_prag}
184 -- '0' state: ordinary lexemes
185 -- 'glaexts' state: glasgow extensions (postfix '#', etc.)
190 "[:" / { ifExtension parrEnabled } { token ITopabrack }
191 ":]" / { ifExtension parrEnabled } { token ITcpabrack }
195 "[|" / { ifExtension thEnabled } { token ITopenExpQuote }
196 "[e|" / { ifExtension thEnabled } { token ITopenExpQuote }
197 "[p|" / { ifExtension thEnabled } { token ITopenPatQuote }
198 "[d|" / { ifExtension thEnabled } { layout_token ITopenDecQuote }
199 "[t|" / { ifExtension thEnabled } { token ITopenTypQuote }
200 "|]" / { ifExtension thEnabled } { token ITcloseQuote }
201 \$ @varid / { ifExtension thEnabled } { skip_one_varid ITidEscape }
202 "$(" / { ifExtension thEnabled } { token ITparenEscape }
206 "(|" / { ifExtension arrowsEnabled } { special IToparenbar }
207 "|)" / { ifExtension arrowsEnabled } { special ITcparenbar }
211 \? @varid / { ifExtension ipEnabled } { skip_one_varid ITdupipvarid }
212 \% @varid / { ifExtension ipEnabled } { skip_one_varid ITsplitipvarid }
216 "(#" { token IToubxparen }
217 "#)" { token ITcubxparen }
218 "{|" { token ITocurlybar }
219 "|}" { token ITccurlybar }
223 \( { special IToparen }
224 \) { special ITcparen }
225 \[ { special ITobrack }
226 \] { special ITcbrack }
227 \, { special ITcomma }
228 \; { special ITsemi }
229 \` { special ITbackquote }
236 @qual @varid { check_qvarid }
237 @qual @conid { idtoken qconid }
239 @conid { idtoken conid }
242 -- after an illegal qvarid, such as 'M.let',
243 -- we back up and try again in the bad_qvarid state:
245 @conid { pop_and (idtoken conid) }
246 @qual @conid { pop_and (idtoken qconid) }
250 @qual @varid "#"+ { idtoken qvarid }
251 @qual @conid "#"+ { idtoken qconid }
252 @varid "#"+ { varid }
253 @conid "#"+ { idtoken conid }
259 @qual @varsym { idtoken qvarsym }
260 @qual @consym { idtoken qconsym }
266 @decimal { tok_decimal }
267 0[oO] @octal { tok_octal }
268 0[xX] @hexadecimal { tok_hexadecimal }
272 @decimal \# { prim_decimal }
273 0[oO] @octal \# { prim_octal }
274 0[xX] @hexadecimal \# { prim_hexadecimal }
277 <0,glaexts> @floating_point { strtoken tok_float }
278 <glaexts> @floating_point \# { init_strtoken 1 prim_float }
279 <glaexts> @floating_point \# \# { init_strtoken 2 prim_double }
281 -- Strings and chars are lexed by hand-written code. The reason is
282 -- that even if we recognise the string or char here in the regex
283 -- lexer, we would still have to parse the string afterward in order
284 -- to convert it to a String.
287 \" { lex_string_tok }
290 <glaexts> "``" (([$graphic $whitechar] # \') | \' ([$graphic $whitechar] # \'))*
294 -- work around bug in Alex 2.0
295 #if __GLASGOW_HASKELL__ < 503
296 unsafeAt arr i = arr ! i
299 -- -----------------------------------------------------------------------------
302 data Token = T SrcLoc{-start-} SrcLoc{-end-} Token__
305 = ITas -- Haskell keywords
329 | ITscc -- ToDo: remove (we use {-# SCC "..." #-} now)
331 | ITforall -- GHC extension keywords
343 | ITccall (Bool,Bool,Safety) -- (is_dyn, is_casm, may_gc)
346 | ITspecialise_prag -- Pragmas
354 | ITcore_prag -- hdaume: core annotations
357 | ITdotdot -- reserved symbols
373 | ITbiglam -- GHC-extension symbols
375 | ITocurly -- special symbols
377 | ITocurlybar -- {|, for type applications
378 | ITccurlybar -- |}, for type applications
382 | ITopabrack -- [:, for parallel arrays with -fparr
383 | ITcpabrack -- :], for parallel arrays with -fparr
394 | ITvarid FastString -- identifiers
396 | ITvarsym FastString
397 | ITconsym FastString
398 | ITqvarid (FastString,FastString)
399 | ITqconid (FastString,FastString)
400 | ITqvarsym (FastString,FastString)
401 | ITqconsym (FastString,FastString)
403 | ITdupipvarid FastString -- GHC extension: implicit param: ?x
404 | ITsplitipvarid FastString -- GHC extension: implicit param: %x
406 | ITpragma StringBuffer
409 | ITstring FastString
411 | ITrational Rational
414 | ITprimstring FastString
416 | ITprimfloat Rational
417 | ITprimdouble Rational
418 | ITlitlit FastString
420 -- MetaHaskell extension tokens
421 | ITopenExpQuote -- [| or [e|
422 | ITopenPatQuote -- [p|
423 | ITopenDecQuote -- [d|
424 | ITopenTypQuote -- [t|
426 | ITidEscape FastString -- $x
427 | ITparenEscape -- $(
432 -- Arrow notation extension
439 | ITLarrowtail -- -<<
440 | ITRarrowtail -- >>-
442 | ITunknown String -- Used when the lexer can't make sense of it
443 | ITeof -- end of file token
445 deriving Show -- debugging
448 isSpecial :: Token__ -> Bool
449 -- If we see M.x, where x is a keyword, but
450 -- is special, we treat is as just plain M.x,
452 isSpecial ITas = True
453 isSpecial IThiding = True
454 isSpecial ITqualified = True
455 isSpecial ITforall = True
456 isSpecial ITexport = True
457 isSpecial ITlabel = True
458 isSpecial ITdynamic = True
459 isSpecial ITsafe = True
460 isSpecial ITthreadsafe = True
461 isSpecial ITunsafe = True
462 isSpecial ITwith = True
463 isSpecial ITccallconv = True
464 isSpecial ITstdcallconv = True
465 isSpecial ITmdo = True
468 -- the bitmap provided as the third component indicates whether the
469 -- corresponding extension keyword is valid under the extension options
470 -- provided to the compiler; if the extension corresponding to *any* of the
471 -- bits set in the bitmap is enabled, the keyword is valid (this setup
472 -- facilitates using a keyword in two different extensions that can be
473 -- activated independently)
475 reservedWordsFM = listToUFM $
476 map (\(x, y, z) -> (mkFastString x, (y, z)))
477 [( "_", ITunderscore, 0 ),
479 ( "case", ITcase, 0 ),
480 ( "class", ITclass, 0 ),
481 ( "data", ITdata, 0 ),
482 ( "default", ITdefault, 0 ),
483 ( "deriving", ITderiving, 0 ),
485 ( "else", ITelse, 0 ),
486 ( "hiding", IThiding, 0 ),
488 ( "import", ITimport, 0 ),
490 ( "infix", ITinfix, 0 ),
491 ( "infixl", ITinfixl, 0 ),
492 ( "infixr", ITinfixr, 0 ),
493 ( "instance", ITinstance, 0 ),
495 ( "module", ITmodule, 0 ),
496 ( "newtype", ITnewtype, 0 ),
498 ( "qualified", ITqualified, 0 ),
499 ( "then", ITthen, 0 ),
500 ( "type", ITtype, 0 ),
501 ( "where", ITwhere, 0 ),
502 ( "_scc_", ITscc, 0 ), -- ToDo: remove
504 ( "forall", ITforall, bit glaExtsBit),
505 ( "mdo", ITmdo, bit glaExtsBit),
506 ( "reifyDecl", ITreifyDecl, bit thBit),
507 ( "reifyType", ITreifyType, bit thBit),
508 ( "reifyFixity",ITreifyFixity, bit thBit),
510 ( "foreign", ITforeign, bit ffiBit),
511 ( "export", ITexport, bit ffiBit),
512 ( "label", ITlabel, bit ffiBit),
513 ( "dynamic", ITdynamic, bit ffiBit),
514 ( "safe", ITsafe, bit ffiBit),
515 ( "threadsafe", ITthreadsafe, bit ffiBit),
516 ( "unsafe", ITunsafe, bit ffiBit),
517 ( "stdcall", ITstdcallconv, bit ffiBit),
518 ( "ccall", ITccallconv, bit ffiBit),
519 ( "dotnet", ITdotnet, bit ffiBit),
521 ( "with", ITwith, bit withBit),
523 ( "rec", ITrec, bit arrowsBit),
524 ( "proc", ITproc, bit arrowsBit),
527 ("_ccall_", ITccall (False, False, PlayRisky),
529 ("_ccall_GC_", ITccall (False, False, PlaySafe False),
531 ("_casm_", ITccall (False, True, PlayRisky),
533 ("_casm_GC_", ITccall (False, True, PlaySafe False),
537 reservedSymsFM = listToUFM $
538 map (\ (x,y,z) -> (mkFastString x,(y,z)))
539 [ ("..", ITdotdot, 0)
540 ,(":", ITcolon, 0) -- (:) is a reserved op,
541 -- meaning only list cons
554 ,("*", ITstar, bit glaExtsBit) -- For data T (a::*) = MkT
555 ,(".", ITdot, bit glaExtsBit) -- For 'forall a . t'
557 ,("-<", ITlarrowtail, bit arrowsBit)
558 ,(">-", ITrarrowtail, bit arrowsBit)
559 ,("-<<", ITLarrowtail, bit arrowsBit)
560 ,(">>-", ITRarrowtail, bit arrowsBit)
563 -- -----------------------------------------------------------------------------
566 type Action = SrcLoc -> SrcLoc -> StringBuffer -> Int -> P Token
568 special :: Token__ -> Action
569 special tok loc end _buf len = return (T loc end tok)
571 token, layout_token :: Token__ -> Action
572 token t loc end buf len = return (T loc end t)
573 layout_token t loc end buf len = pushLexState layout >> return (T loc end t)
575 idtoken :: (StringBuffer -> Int -> Token__) -> Action
576 idtoken f loc end buf len = return (T loc end $! (f buf len))
578 skip_one_varid :: (FastString -> Token__) -> Action
579 skip_one_varid f loc end buf len
580 = return (T loc end $! f (lexemeToFastString (stepOn buf) (len-1)))
582 strtoken :: (String -> Token__) -> Action
583 strtoken f loc end buf len =
584 return (T loc end $! (f $! lexemeToString buf len))
586 init_strtoken :: Int -> (String -> Token__) -> Action
587 -- like strtoken, but drops the last N character(s)
588 init_strtoken drop f loc end buf len =
589 return (T loc end $! (f $! lexemeToString buf (len-drop)))
591 begin :: Int -> Action
592 begin code _loc _end _str _len = do pushLexState code; lexToken
595 pop _loc _end _buf _len = do popLexState; lexToken
597 pop_and :: Action -> Action
598 pop_and act loc end buf len = do popLexState; act loc end buf len
600 notFollowedBy char _ _ _ (_,buf) = atEnd buf || currentChar buf /= char
602 ifExtension pred bits _ _ _ = pred bits
605 nested comments require traversing by hand, they can't be parsed
606 using regular expressions.
608 nested_comment :: Action
609 nested_comment loc _end _str _len = do
612 where go 0 input = do setInput input; lexToken
614 case alexGetChar input of
619 case alexGetChar input of
621 Just ('\125',input) -> go (n-1) input
622 Just (c,_) -> go n input
624 case alexGetChar input of
626 Just ('-',input') -> go (n+1) input'
627 Just (c,input) -> go n input
630 err input = do failLocMsgP loc (fst input) "unterminated `{-'"
632 open_brace, close_brace :: Action
633 open_brace loc end _str _len = do
635 setContext (NoLayout:ctx)
636 return (T loc end ITocurly)
637 close_brace loc end _str _len = do
639 return (T loc end ITccurly)
641 -- We have to be careful not to count M.<varid> as a qualified name
642 -- when <varid> is a keyword. We hack around this by catching
643 -- the offending tokens afterward, and re-lexing in a different state.
644 check_qvarid loc end buf len = do
645 case lookupUFM reservedWordsFM var of
647 | not (isSpecial keyword) ->
651 b <- extension (\i -> exts .&. i /= 0)
654 _other -> return token
656 (mod,var) = splitQualName buf len
657 token = T loc end (ITqvarid (mod,var))
661 pushLexState bad_qvarid
664 qvarid buf len = ITqvarid $! splitQualName buf len
665 qconid buf len = ITqconid $! splitQualName buf len
667 splitQualName :: StringBuffer -> Int -> (FastString,FastString)
668 -- takes a StringBuffer and a length, and returns the module name
669 -- and identifier parts of a qualified name. Splits at the *last* dot,
670 -- because of hierarchical module names.
671 splitQualName orig_buf len = split orig_buf 0 0
674 | n == len = done dot_off
675 | lookAhead buf n == '.' = split2 buf n (n+1)
676 | otherwise = split buf dot_off (n+1)
678 -- careful, we might get names like M....
679 -- so, if the character after the dot is not upper-case, this is
680 -- the end of the qualifier part.
682 | isUpper (lookAhead buf n) = split buf dot_off (n+1)
683 | otherwise = done dot_off
686 (lexemeToFastString orig_buf dot_off,
687 lexemeToFastString (stepOnBy (dot_off+1) orig_buf) (len - dot_off -1))
689 varid loc end buf len =
690 case lookupUFM reservedWordsFM fs of
691 Just (keyword,0) -> do
693 return (T loc end keyword)
694 Just (keyword,exts) -> do
695 b <- extension (\i -> exts .&. i /= 0)
696 if b then do maybe_layout keyword
697 return (T loc end keyword)
698 else return (T loc end (ITvarid fs))
699 _other -> return (T loc end (ITvarid fs))
701 fs = lexemeToFastString buf len
703 conid buf len = ITconid fs
704 where fs = lexemeToFastString buf len
706 qvarsym buf len = ITqvarsym $! splitQualName buf len
707 qconsym buf len = ITqconsym $! splitQualName buf len
709 varsym = sym ITvarsym
710 consym = sym ITconsym
712 sym con loc end buf len =
713 case lookupUFM reservedSymsFM fs of
714 Just (keyword,0) -> return (T loc end keyword)
715 Just (keyword,exts) -> do
716 b <- extension (\i -> exts .&. i /= 0)
717 if b then return (T loc end keyword)
718 else return (T loc end $! con fs)
719 _other -> return (T loc end $! con fs)
721 fs = lexemeToFastString buf len
723 tok_decimal loc end buf len
724 = return (T loc end (ITinteger $! parseInteger buf len 10 oct_or_dec))
726 tok_octal loc end buf len
727 = return (T loc end (ITinteger $! parseInteger (stepOnBy 2 buf) (len-2) 8 oct_or_dec))
729 tok_hexadecimal loc end buf len
730 = return (T loc end (ITinteger $! parseInteger (stepOnBy 2 buf) (len-2) 16 hex))
732 prim_decimal loc end buf len
733 = return (T loc end (ITprimint $! parseInteger buf (len-1) 10 oct_or_dec))
735 prim_octal loc end buf len
736 = return (T loc end (ITprimint $! parseInteger (stepOnBy 2 buf) (len-3) 8 oct_or_dec))
738 prim_hexadecimal loc end buf len
739 = return (T loc end (ITprimint $! parseInteger (stepOnBy 2 buf) (len-3) 16 hex))
741 tok_float str = ITrational $! readRational__ str
742 prim_float str = ITprimfloat $! readRational__ str
743 prim_double str = ITprimdouble $! readRational__ str
745 parseInteger :: StringBuffer -> Int -> Integer -> (Char->Int) -> Integer
746 parseInteger buf len radix to_int
748 where go i x | i == len = x
749 | otherwise = go (i+1) (x * radix + toInteger (to_int (lookAhead buf i)))
752 clitlit loc end buf len =
753 return (T loc end (ITlitlit $! lexemeToFastString (stepOnBy 2 buf) (len-4)))
755 -- -----------------------------------------------------------------------------
758 -- we're at the first token on a line, insert layout tokens if necessary
760 do_bol loc end _str _len = do
761 pos <- getOffside end
764 --trace "layout: inserting '}'" $ do
766 -- do NOT pop the lex state, we might have a ';' to insert
767 return (T loc end ITvccurly)
769 --trace "layout: inserting ';'" $ do
771 return (T loc end ITsemi)
776 -- certain keywords put us in the "layout" state, where we might
777 -- add an opening curly brace.
778 maybe_layout ITdo = pushLexState layout_do
779 maybe_layout ITmdo = pushLexState layout_do
780 maybe_layout ITof = pushLexState layout
781 maybe_layout ITlet = pushLexState layout
782 maybe_layout ITwhere = pushLexState layout
783 maybe_layout ITrec = pushLexState layout
784 maybe_layout _ = return ()
786 -- Pushing a new implicit layout context. If the indentation of the
787 -- next token is not greater than the previous layout context, then
788 -- Haskell 98 says that the new layout context should be empty; that is
789 -- the lexer must generate {}.
791 -- We are slightly more lenient than this: when the new context is started
792 -- by a 'do', then we allow the new context to be at the same indentation as
793 -- the previous context. This is what the 'strict' argument is for.
795 new_layout_context strict loc end _buf _len = do
797 let offset = srcLocCol loc
800 Layout prev_off : _ |
801 (strict && prev_off >= offset ||
802 not strict && prev_off > offset) -> do
803 -- token is indented to the left of the previous context.
804 -- we must generate a {} sequence now.
805 pushLexState layout_left
806 return (T loc end ITvocurly)
808 setContext (Layout offset : ctx)
809 return (T loc end ITvocurly)
811 do_layout_left loc end _buf _len = do
813 pushLexState bol -- we must be at the start of a line
814 return (T loc end ITvccurly)
816 -- -----------------------------------------------------------------------------
819 set_line :: Int -> Action
820 set_line code loc end buf len = do
821 let line = parseInteger buf len 10 oct_or_dec
822 setSrcLoc (mkSrcLoc (srcLocFile end) (fromIntegral line - 1) 0)
823 -- subtract one: the line number refers to the *following* line
828 set_file :: Int -> Action
829 set_file code loc end buf len = do
830 let file = lexemeToFastString (stepOn buf) (len-2)
831 setSrcLoc (mkSrcLoc file (srcLocLine end) (srcLocCol end))
836 -- -----------------------------------------------------------------------------
839 -- This stuff is horrible. I hates it.
841 lex_string_tok :: Action
842 lex_string_tok loc end buf len = do
845 return (T loc end tok)
847 lex_string :: String -> P Token__
850 case alexGetChar i of
855 glaexts <- extension glaExtsEnabled
859 case alexGetChar i of
863 then failMsgP "primitive string literal must contain only characters <= \'\\xFF\'"
864 else let s' = mkFastStringNarrow (reverse s) in
865 -- always a narrow string/byte array
866 return (ITprimstring s')
868 return (ITstring (mkFastString (reverse s)))
870 return (ITstring (mkFastString (reverse s)))
873 | Just ('&',i) <- next -> do
874 setInput i; lex_string s
875 | Just (c,i) <- next, is_space c -> do
876 setInput i; lex_stringgap s
877 where next = alexGetChar i
888 c | is_space c -> lex_stringgap s
892 lex_char_tok :: Action
893 lex_char_tok loc _end buf len = do
898 glaexts <- extension glaExtsEnabled
901 i@(end,_) <- getInput
902 case alexGetChar i of
903 Just ('#',i@(end,_)) -> do
905 return (T loc end (ITprimchar c))
907 return (T loc end (ITchar c))
910 return (T loc end (ITchar c))
919 c | is_any c -> return c
936 '^' -> do c <- getCharOrFail
937 if c >= '@' && c <= '_'
938 then return (chr (ord c - ord '@'))
941 'x' -> readNum is_hexdigit 16 hex
942 'o' -> readNum is_octdigit 8 oct_or_dec
943 x | is_digit x -> readNum2 is_digit 10 oct_or_dec (oct_or_dec x)
947 case alexGetChar i of
950 case alexGetChar i2 of
953 let str = [c1,c2,c3] in
954 case [ (c,rest) | (p,c) <- silly_escape_chars,
955 Just rest <- [maybePrefixMatch p str] ] of
956 (escape_char,[]):_ -> do
959 (escape_char,_:_):_ -> do
964 readNum :: (Char -> Bool) -> Int -> (Char -> Int) -> P Char
965 readNum is_digit base conv = do
968 then readNum2 is_digit base conv (conv c)
971 readNum2 is_digit base conv i = do
974 where read i input = do
975 case alexGetChar input of
976 Just (c,input') | is_digit c -> do
977 read (i*base + conv c) input'
980 if i >= 0 && i <= 0x10FFFF
986 || (c >= 'a' && c <= 'f')
987 || (c >= 'A' && c <= 'F')
989 hex c | is_digit c = ord c - ord '0'
990 | otherwise = ord (to_lower c) - ord 'a' + 10
992 oct_or_dec c = ord c - ord '0'
994 is_octdigit c = c >= '0' && c <= '7'
997 | c >= 'A' && c <= 'Z' = chr (ord c - (ord 'A' - ord 'a'))
1000 silly_escape_chars = [
1037 lit_error = lexError "lexical error in string/character literal"
1039 getCharOrFail :: P Char
1042 case alexGetChar i of
1043 Nothing -> lexError "unexpected end-of-file in string/character literal"
1044 Just (c,i) -> do setInput i; return c
1046 -- -----------------------------------------------------------------------------
1049 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
1051 (n,d,s) <- readFix r
1053 return ((n%1)*10^^(k-d), t)
1056 (ds,s) <- lexDecDigits r
1057 (ds',t) <- lexDotDigits s
1058 return (read (ds++ds'), length ds', t)
1060 readExp (e:s) | e `elem` "eE" = readExp' s
1061 readExp s = return (0,s)
1063 readExp' ('+':s) = readDec s
1064 readExp' ('-':s) = do
1067 readExp' s = readDec s
1070 (ds,r) <- nonnull isDigit s
1071 return (foldl1 (\n d -> n * 10 + d) [ ord d - ord '0' | d <- ds ],
1074 lexDecDigits = nonnull isDigit
1076 lexDotDigits ('.':s) = return (span isDigit s)
1077 lexDotDigits s = return ("",s)
1079 nonnull p s = do (cs@(_:_),t) <- return (span p s)
1082 readRational__ :: String -> Rational -- NB: *does* handle a leading "-"
1083 readRational__ top_s
1085 '-' : xs -> - (read_me xs)
1089 = case (do { (x,"") <- readRational s ; return x }) of
1091 [] -> error ("readRational__: no parse:" ++ top_s)
1092 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
1094 -- -----------------------------------------------------------------------------
1104 SrcLoc SrcLoc -- The start and end of the text span related to
1105 -- the error. Might be used in environments which can
1106 -- show this span, e.g. by highlighting it.
1107 Message -- The error message
1109 showPFailed loc1 loc2 err
1110 = showSDoc (hcat [ppr loc1, text ": ", err])
1112 data PState = PState {
1113 buffer :: StringBuffer,
1114 last_loc :: SrcLoc, -- pos of previous token
1115 last_len :: !Int, -- len of previous token
1116 loc :: SrcLoc, -- current loc (end of prev token + 1)
1117 extsBitmap :: !Int, -- bitmap that determines permitted extensions
1118 context :: [LayoutContext],
1121 -- last_loc and last_len are used when generating error messages,
1122 -- and in pushCurrentContext only.
1124 newtype P a = P { unP :: PState -> ParseResult a }
1126 instance Monad P where
1132 returnP a = P $ \s -> POk s a
1134 thenP :: P a -> (a -> P b) -> P b
1135 (P m) `thenP` k = P $ \ s ->
1137 POk s1 a -> (unP (k a)) s1
1138 PFailed l1 l2 err -> PFailed l1 l2 err
1140 failP :: String -> P a
1141 failP msg = P $ \s -> PFailed (last_loc s) (loc s) (text msg)
1143 failMsgP :: String -> P a
1144 failMsgP msg = P $ \s -> PFailed (last_loc s) (loc s) (text msg)
1146 failLocMsgP :: SrcLoc -> SrcLoc -> String -> P a
1147 failLocMsgP loc1 loc2 str = P $ \s -> PFailed loc1 loc2 (text str)
1149 extension :: (Int -> Bool) -> P Bool
1150 extension p = P $ \s -> POk s (p $! extsBitmap s)
1153 getExts = P $ \s -> POk s (extsBitmap s)
1155 setSrcLoc :: SrcLoc -> P ()
1156 setSrcLoc new_loc = P $ \s -> POk s{loc=new_loc} ()
1158 -- tmp, for supporting stuff in RdrHsSyn. The scope better not include
1159 -- any calls to the lexer, because it assumes things about the SrcLoc.
1160 setSrcLocFor :: SrcLoc -> P a -> P a
1161 setSrcLocFor new_loc scope = P $ \s@PState{ loc = old_loc } ->
1162 case unP scope s{loc=new_loc} of
1163 PFailed l1 l2 msg -> PFailed l1 l2 msg
1166 getSrcLoc :: P SrcLoc
1167 getSrcLoc = P $ \s@(PState{ loc=loc }) -> POk s loc
1169 setLastToken :: SrcLoc -> Int -> P ()
1170 setLastToken loc len = P $ \s -> POk s{ last_loc=loc, last_len=len } ()
1172 type AlexInput = (SrcLoc,StringBuffer)
1174 alexInputPrevChar :: AlexInput -> Char
1175 alexInputPrevChar (_,s) = prevChar s '\n'
1177 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
1180 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
1181 where c = currentChar s
1182 loc' = advanceSrcLoc loc c
1185 getInput :: P AlexInput
1186 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
1188 setInput :: AlexInput -> P ()
1189 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()
1191 pushLexState :: Int -> P ()
1192 pushLexState ls = P $ \s@PState{ lex_state=l } -> POk s{lex_state=ls:l} ()
1194 popLexState :: P Int
1195 popLexState = P $ \s@PState{ lex_state=ls:l } -> POk s{ lex_state=l } ls
1197 getLexState :: P Int
1198 getLexState = P $ \s@PState{ lex_state=ls:l } -> POk s ls
1200 -- for reasons of efficiency, flags indicating language extensions (eg,
1201 -- -fglasgow-exts or -fparr) are represented by a bitmap stored in an unboxed
1204 glaExtsBit, ffiBit, parrBit :: Int
1213 glaExtsEnabled, ffiEnabled, parrEnabled :: Int -> Bool
1214 glaExtsEnabled flags = testBit flags glaExtsBit
1215 ffiEnabled flags = testBit flags ffiBit
1216 withEnabled flags = testBit flags withBit
1217 parrEnabled flags = testBit flags parrBit
1218 arrowsEnabled flags = testBit flags arrowsBit
1219 thEnabled flags = testBit flags thBit
1220 ipEnabled flags = testBit flags ipBit
1222 -- create a parse state
1224 mkPState :: StringBuffer -> SrcLoc -> DynFlags -> PState
1225 mkPState buf loc flags =
1231 extsBitmap = fromIntegral bitmap,
1233 lex_state = [bol, if glaExtsEnabled bitmap then glaexts else 0]
1234 -- we begin in the layout state if toplev_layout is set
1237 bitmap = glaExtsBit `setBitIf` dopt Opt_GlasgowExts flags
1238 .|. ffiBit `setBitIf` dopt Opt_FFI flags
1239 .|. withBit `setBitIf` dopt Opt_With flags
1240 .|. parrBit `setBitIf` dopt Opt_PArr flags
1241 .|. arrowsBit `setBitIf` dopt Opt_Arrows flags
1242 .|. thBit `setBitIf` dopt Opt_TH flags
1243 .|. ipBit `setBitIf` dopt Opt_ImplicitParams flags
1245 setBitIf :: Int -> Bool -> Int
1246 b `setBitIf` cond | cond = bit b
1249 getContext :: P [LayoutContext]
1250 getContext = P $ \s@PState{context=ctx} -> POk s ctx
1252 setContext :: [LayoutContext] -> P ()
1253 setContext ctx = P $ \s -> POk s{context=ctx} ()
1256 popContext = P $ \ s@(PState{ buffer = buf, context = ctx,
1257 loc = loc, last_len = len, last_loc = last_loc }) ->
1259 (_:tl) -> POk s{ context = tl } ()
1260 [] -> PFailed last_loc loc (srcParseErr buf len)
1262 -- Push a new layout context at the indentation of the last token read.
1263 -- This is only used at the outer level of a module when the 'module'
1264 -- keyword is missing.
1265 pushCurrentContext :: P ()
1266 pushCurrentContext = P $ \ s@PState{ last_loc=loc, context=ctx } ->
1267 POk s{ context = Layout (srcLocCol loc) : ctx} ()
1269 getOffside :: SrcLoc -> P Ordering
1270 getOffside loc = P $ \s@PState{context=stk} ->
1271 let ord = case stk of
1272 (Layout n:_) -> compare (srcLocCol loc) n
1276 -- ---------------------------------------------------------------------------
1277 -- Construct a parse error
1280 :: StringBuffer -- current buffer (placed just after the last token)
1281 -> Int -- length of the previous token
1284 = hcat [ if null token
1285 then ptext SLIT("parse error (possibly incorrect indentation)")
1286 else hcat [ptext SLIT("parse error on input "),
1287 char '`', text token, char '\'']
1289 where token = lexemeToString (stepOnBy (-len) buf) len
1291 -- Report a parse failure, giving the span of the previous token as
1292 -- the location of the error. This is the entry point for errors
1293 -- detected during parsing.
1295 srcParseFail = P $ \PState{ buffer = buf, last_len = len,
1296 last_loc = last_loc, loc = loc } ->
1297 PFailed last_loc loc (srcParseErr buf len)
1299 -- A lexical error is reported at a particular position in the source file,
1300 -- not over a token range. TODO: this is slightly wrong, because we record
1301 -- the error at the character position following the one which caused the
1302 -- error. We should somehow back up by one character.
1303 lexError :: String -> P a
1306 failLocMsgP loc loc str
1308 -- -----------------------------------------------------------------------------
1309 -- This is the top-level function: called from the parser each time a
1310 -- new token is to be read from the input.
1312 lexer :: (Token -> P a) -> P a
1314 tok@(T _ _ tok__) <- lexToken
1315 --trace ("token: " ++ show tok__) $ do
1320 inp@(loc1,buf) <- getInput
1323 case alexScanUser exts inp sc of
1324 AlexEOF -> do setLastToken loc1 0
1325 return (T loc1 loc1 ITeof)
1326 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
1327 AlexSkip inp2 _ -> do
1330 AlexToken inp2@(end,buf2) len t -> do
1332 setLastToken loc1 len