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 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 }
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 `alexAndPred` notFollowedBySymbol }
207 { special IToparenbar }
208 "|)" / { ifExtension arrowsEnabled } { special ITcparenbar }
212 \? @varid / { ifExtension ipEnabled } { skip_one_varid ITdupipvarid }
213 \% @varid / { ifExtension ipEnabled } { skip_one_varid ITsplitipvarid }
217 "(#" / { notFollowedBySymbol } { token IToubxparen }
218 "#)" { token ITcubxparen }
219 "{|" { token ITocurlybar }
220 "|}" { token ITccurlybar }
224 \( { special IToparen }
225 \) { special ITcparen }
226 \[ { special ITobrack }
227 \] { special ITcbrack }
228 \, { special ITcomma }
229 \; { special ITsemi }
230 \` { special ITbackquote }
237 @qual @varid { check_qvarid }
238 @qual @conid { idtoken qconid }
240 @conid { idtoken conid }
243 -- after an illegal qvarid, such as 'M.let',
244 -- we back up and try again in the bad_qvarid state:
246 @conid { pop_and (idtoken conid) }
247 @qual @conid { pop_and (idtoken qconid) }
251 @qual @varid "#"+ { idtoken qvarid }
252 @qual @conid "#"+ { idtoken qconid }
253 @varid "#"+ { varid }
254 @conid "#"+ { idtoken conid }
260 @qual @varsym { idtoken qvarsym }
261 @qual @consym { idtoken qconsym }
267 @decimal { tok_decimal }
268 0[oO] @octal { tok_octal }
269 0[xX] @hexadecimal { tok_hexadecimal }
273 @decimal \# { prim_decimal }
274 0[oO] @octal \# { prim_octal }
275 0[xX] @hexadecimal \# { prim_hexadecimal }
278 <0,glaexts> @floating_point { strtoken tok_float }
279 <glaexts> @floating_point \# { init_strtoken 1 prim_float }
280 <glaexts> @floating_point \# \# { init_strtoken 2 prim_double }
282 -- Strings and chars are lexed by hand-written code. The reason is
283 -- that even if we recognise the string or char here in the regex
284 -- lexer, we would still have to parse the string afterward in order
285 -- to convert it to a String.
288 \" { lex_string_tok }
292 -- work around bug in Alex 2.0
293 #if __GLASGOW_HASKELL__ < 503
294 unsafeAt arr i = arr ! i
297 -- -----------------------------------------------------------------------------
300 data Token = T SrcSpan Token__
303 = ITas -- Haskell keywords
327 | ITscc -- ToDo: remove (we use {-# SCC "..." #-} now)
329 | ITforall -- GHC extension keywords
342 | ITspecialise_prag -- Pragmas
350 | ITcore_prag -- hdaume: core annotations
353 | ITdotdot -- reserved symbols
369 | ITbiglam -- GHC-extension symbols
371 | ITocurly -- special symbols
373 | ITocurlybar -- {|, for type applications
374 | ITccurlybar -- |}, for type applications
378 | ITopabrack -- [:, for parallel arrays with -fparr
379 | ITcpabrack -- :], for parallel arrays with -fparr
390 | ITvarid FastString -- identifiers
392 | ITvarsym FastString
393 | ITconsym FastString
394 | ITqvarid (FastString,FastString)
395 | ITqconid (FastString,FastString)
396 | ITqvarsym (FastString,FastString)
397 | ITqconsym (FastString,FastString)
399 | ITdupipvarid FastString -- GHC extension: implicit param: ?x
400 | ITsplitipvarid FastString -- GHC extension: implicit param: %x
402 | ITpragma StringBuffer
405 | ITstring FastString
407 | ITrational Rational
410 | ITprimstring FastString
412 | ITprimfloat Rational
413 | ITprimdouble Rational
415 -- MetaHaskell extension tokens
416 | ITopenExpQuote -- [| or [e|
417 | ITopenPatQuote -- [p|
418 | ITopenDecQuote -- [d|
419 | ITopenTypQuote -- [t|
421 | ITidEscape FastString -- $x
422 | ITparenEscape -- $(
426 -- Arrow notation extension
433 | ITLarrowtail -- -<<
434 | ITRarrowtail -- >>-
436 | ITunknown String -- Used when the lexer can't make sense of it
437 | ITeof -- end of file token
439 deriving Show -- debugging
442 isSpecial :: Token__ -> Bool
443 -- If we see M.x, where x is a keyword, but
444 -- is special, we treat is as just plain M.x,
446 isSpecial ITas = True
447 isSpecial IThiding = True
448 isSpecial ITqualified = True
449 isSpecial ITforall = True
450 isSpecial ITexport = True
451 isSpecial ITlabel = True
452 isSpecial ITdynamic = True
453 isSpecial ITsafe = True
454 isSpecial ITthreadsafe = True
455 isSpecial ITunsafe = True
456 isSpecial ITccallconv = True
457 isSpecial ITstdcallconv = True
458 isSpecial ITmdo = True
461 -- the bitmap provided as the third component indicates whether the
462 -- corresponding extension keyword is valid under the extension options
463 -- provided to the compiler; if the extension corresponding to *any* of the
464 -- bits set in the bitmap is enabled, the keyword is valid (this setup
465 -- facilitates using a keyword in two different extensions that can be
466 -- activated independently)
468 reservedWordsFM = listToUFM $
469 map (\(x, y, z) -> (mkFastString x, (y, z)))
470 [( "_", ITunderscore, 0 ),
472 ( "case", ITcase, 0 ),
473 ( "class", ITclass, 0 ),
474 ( "data", ITdata, 0 ),
475 ( "default", ITdefault, 0 ),
476 ( "deriving", ITderiving, 0 ),
478 ( "else", ITelse, 0 ),
479 ( "hiding", IThiding, 0 ),
481 ( "import", ITimport, 0 ),
483 ( "infix", ITinfix, 0 ),
484 ( "infixl", ITinfixl, 0 ),
485 ( "infixr", ITinfixr, 0 ),
486 ( "instance", ITinstance, 0 ),
488 ( "module", ITmodule, 0 ),
489 ( "newtype", ITnewtype, 0 ),
491 ( "qualified", ITqualified, 0 ),
492 ( "then", ITthen, 0 ),
493 ( "type", ITtype, 0 ),
494 ( "where", ITwhere, 0 ),
495 ( "_scc_", ITscc, 0 ), -- ToDo: remove
497 ( "forall", ITforall, bit glaExtsBit),
498 ( "mdo", ITmdo, bit glaExtsBit),
500 ( "foreign", ITforeign, bit ffiBit),
501 ( "export", ITexport, bit ffiBit),
502 ( "label", ITlabel, bit ffiBit),
503 ( "dynamic", ITdynamic, bit ffiBit),
504 ( "safe", ITsafe, bit ffiBit),
505 ( "threadsafe", ITthreadsafe, bit ffiBit),
506 ( "unsafe", ITunsafe, bit ffiBit),
507 ( "stdcall", ITstdcallconv, bit ffiBit),
508 ( "ccall", ITccallconv, bit ffiBit),
509 ( "dotnet", ITdotnet, bit ffiBit),
511 ( "rec", ITrec, bit arrowsBit),
512 ( "proc", ITproc, bit arrowsBit)
515 reservedSymsFM = listToUFM $
516 map (\ (x,y,z) -> (mkFastString x,(y,z)))
517 [ ("..", ITdotdot, 0)
518 ,(":", ITcolon, 0) -- (:) is a reserved op,
519 -- meaning only list cons
532 ,("*", ITstar, bit glaExtsBit) -- For data T (a::*) = MkT
533 ,(".", ITdot, bit glaExtsBit) -- For 'forall a . t'
535 ,("-<", ITlarrowtail, bit arrowsBit)
536 ,(">-", ITrarrowtail, bit arrowsBit)
537 ,("-<<", ITLarrowtail, bit arrowsBit)
538 ,(">>-", ITRarrowtail, bit arrowsBit)
541 -- -----------------------------------------------------------------------------
544 type Action = SrcSpan -> StringBuffer -> Int -> P Token
546 special :: Token__ -> Action
547 special tok span _buf len = return (T span tok)
549 token, layout_token :: Token__ -> Action
550 token t span buf len = return (T span t)
551 layout_token t span buf len = pushLexState layout >> return (T span t)
553 idtoken :: (StringBuffer -> Int -> Token__) -> Action
554 idtoken f span buf len = return (T span $! (f buf len))
556 skip_one_varid :: (FastString -> Token__) -> Action
557 skip_one_varid f span buf len
558 = return (T span $! f (lexemeToFastString (stepOn buf) (len-1)))
560 strtoken :: (String -> Token__) -> Action
561 strtoken f span buf len =
562 return (T span $! (f $! lexemeToString buf len))
564 init_strtoken :: Int -> (String -> Token__) -> Action
565 -- like strtoken, but drops the last N character(s)
566 init_strtoken drop f span buf len =
567 return (T span $! (f $! lexemeToString buf (len-drop)))
569 begin :: Int -> Action
570 begin code _span _str _len = do pushLexState code; lexToken
573 pop _span _buf _len = do popLexState; lexToken
575 pop_and :: Action -> Action
576 pop_and act span buf len = do popLexState; act span buf len
578 notFollowedBy char _ _ _ (_,buf) = atEnd buf || currentChar buf /= char
580 notFollowedBySymbol _ _ _ (_,buf)
581 = atEnd buf || currentChar buf `notElem` "!#$%&*+./<=>?@\\^|-~"
583 ifExtension pred bits _ _ _ = pred bits
586 nested comments require traversing by hand, they can't be parsed
587 using regular expressions.
589 nested_comment :: Action
590 nested_comment span _str _len = do
593 where go 0 input = do setInput input; lexToken
595 case alexGetChar input of
600 case alexGetChar input of
602 Just ('\125',input) -> go (n-1) input
603 Just (c,_) -> go n input
605 case alexGetChar input of
607 Just ('-',input') -> go (n+1) input'
608 Just (c,input) -> go n input
611 err input = do failLocMsgP (srcSpanStart span) (fst input) "unterminated `{-'"
613 open_brace, close_brace :: Action
614 open_brace span _str _len = do
616 setContext (NoLayout:ctx)
617 return (T span ITocurly)
618 close_brace span _str _len = do
620 return (T span ITccurly)
622 -- We have to be careful not to count M.<varid> as a qualified name
623 -- when <varid> is a keyword. We hack around this by catching
624 -- the offending tokens afterward, and re-lexing in a different state.
625 check_qvarid span buf len = do
626 case lookupUFM reservedWordsFM var of
628 | not (isSpecial keyword) ->
632 b <- extension (\i -> exts .&. i /= 0)
635 _other -> return token
637 (mod,var) = splitQualName buf len
638 token = T span (ITqvarid (mod,var))
641 setInput (srcSpanStart span,buf)
642 pushLexState bad_qvarid
645 qvarid buf len = ITqvarid $! splitQualName buf len
646 qconid buf len = ITqconid $! splitQualName buf len
648 splitQualName :: StringBuffer -> Int -> (FastString,FastString)
649 -- takes a StringBuffer and a length, and returns the module name
650 -- and identifier parts of a qualified name. Splits at the *last* dot,
651 -- because of hierarchical module names.
652 splitQualName orig_buf len = split orig_buf 0 0
655 | n == len = done dot_off
656 | lookAhead buf n == '.' = split2 buf n (n+1)
657 | otherwise = split buf dot_off (n+1)
659 -- careful, we might get names like M....
660 -- so, if the character after the dot is not upper-case, this is
661 -- the end of the qualifier part.
663 | isUpper (lookAhead buf n) = split buf dot_off (n+1)
664 | otherwise = done dot_off
667 (lexemeToFastString orig_buf dot_off,
668 lexemeToFastString (stepOnBy (dot_off+1) orig_buf) (len - dot_off -1))
671 case lookupUFM reservedWordsFM fs of
672 Just (keyword,0) -> do
674 return (T span keyword)
675 Just (keyword,exts) -> do
676 b <- extension (\i -> exts .&. i /= 0)
677 if b then do maybe_layout keyword
678 return (T span keyword)
679 else return (T span (ITvarid fs))
680 _other -> return (T span (ITvarid fs))
682 fs = lexemeToFastString buf len
684 conid buf len = ITconid fs
685 where fs = lexemeToFastString buf len
687 qvarsym buf len = ITqvarsym $! splitQualName buf len
688 qconsym buf len = ITqconsym $! splitQualName buf len
690 varsym = sym ITvarsym
691 consym = sym ITconsym
693 sym con span buf len =
694 case lookupUFM reservedSymsFM fs of
695 Just (keyword,0) -> return (T span keyword)
696 Just (keyword,exts) -> do
697 b <- extension (\i -> exts .&. i /= 0)
698 if b then return (T span keyword)
699 else return (T span $! con fs)
700 _other -> return (T span $! con fs)
702 fs = lexemeToFastString buf len
704 tok_decimal span buf len
705 = return (T span (ITinteger $! parseInteger buf len 10 oct_or_dec))
707 tok_octal span buf len
708 = return (T span (ITinteger $! parseInteger (stepOnBy 2 buf) (len-2) 8 oct_or_dec))
710 tok_hexadecimal span buf len
711 = return (T span (ITinteger $! parseInteger (stepOnBy 2 buf) (len-2) 16 hex))
713 prim_decimal span buf len
714 = return (T span (ITprimint $! parseInteger buf (len-1) 10 oct_or_dec))
716 prim_octal span buf len
717 = return (T span (ITprimint $! parseInteger (stepOnBy 2 buf) (len-3) 8 oct_or_dec))
719 prim_hexadecimal span buf len
720 = return (T span (ITprimint $! parseInteger (stepOnBy 2 buf) (len-3) 16 hex))
722 tok_float str = ITrational $! readRational__ str
723 prim_float str = ITprimfloat $! readRational__ str
724 prim_double str = ITprimdouble $! readRational__ str
726 parseInteger :: StringBuffer -> Int -> Integer -> (Char->Int) -> Integer
727 parseInteger buf len radix to_int
729 where go i x | i == len = x
730 | otherwise = go (i+1) (x * radix + toInteger (to_int (lookAhead buf i)))
732 -- -----------------------------------------------------------------------------
735 -- we're at the first token on a line, insert layout tokens if necessary
737 do_bol span _str _len = do
738 pos <- getOffside (srcSpanEndCol span)
741 --trace "layout: inserting '}'" $ do
743 -- do NOT pop the lex state, we might have a ';' to insert
744 return (T span ITvccurly)
746 --trace "layout: inserting ';'" $ do
748 return (T span ITsemi)
753 -- certain keywords put us in the "layout" state, where we might
754 -- add an opening curly brace.
755 maybe_layout ITdo = pushLexState layout_do
756 maybe_layout ITmdo = pushLexState layout_do
757 maybe_layout ITof = pushLexState layout
758 maybe_layout ITlet = pushLexState layout
759 maybe_layout ITwhere = pushLexState layout
760 maybe_layout ITrec = pushLexState layout
761 maybe_layout _ = return ()
763 -- Pushing a new implicit layout context. If the indentation of the
764 -- next token is not greater than the previous layout context, then
765 -- Haskell 98 says that the new layout context should be empty; that is
766 -- the lexer must generate {}.
768 -- We are slightly more lenient than this: when the new context is started
769 -- by a 'do', then we allow the new context to be at the same indentation as
770 -- the previous context. This is what the 'strict' argument is for.
772 new_layout_context strict span _buf _len = do
774 let offset = srcSpanStartCol span
777 Layout prev_off : _ |
778 (strict && prev_off >= offset ||
779 not strict && prev_off > offset) -> do
780 -- token is indented to the left of the previous context.
781 -- we must generate a {} sequence now.
782 pushLexState layout_left
783 return (T span ITvocurly)
785 setContext (Layout offset : ctx)
786 return (T span ITvocurly)
788 do_layout_left span _buf _len = do
790 pushLexState bol -- we must be at the start of a line
791 return (T span ITvccurly)
793 -- -----------------------------------------------------------------------------
796 set_line :: Int -> Action
797 set_line code span buf len = do
798 let line = parseInteger buf len 10 oct_or_dec
799 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
800 -- subtract one: the line number refers to the *following* line
805 set_file :: Int -> Action
806 set_file code span buf len = do
807 let file = lexemeToFastString (stepOn buf) (len-2)
808 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
813 -- -----------------------------------------------------------------------------
816 -- This stuff is horrible. I hates it.
818 lex_string_tok :: Action
819 lex_string_tok span buf len = do
822 return (T (mkSrcSpan (srcSpanStart span) end) tok)
824 lex_string :: String -> P Token__
827 case alexGetChar i of
832 glaexts <- extension glaExtsEnabled
836 case alexGetChar i of
840 then failMsgP "primitive string literal must contain only characters <= \'\\xFF\'"
841 else let s' = mkFastStringNarrow (reverse s) in
842 -- always a narrow string/byte array
843 return (ITprimstring s')
845 return (ITstring (mkFastString (reverse s)))
847 return (ITstring (mkFastString (reverse s)))
850 | Just ('&',i) <- next -> do
851 setInput i; lex_string s
852 | Just (c,i) <- next, is_space c -> do
853 setInput i; lex_stringgap s
854 where next = alexGetChar i
864 c | is_space c -> lex_stringgap s
868 lex_char_tok :: Action
869 -- Here we are basically parsing character literals, such as 'x' or '\n'
870 -- but, when Template Haskell is on, we additionally spot
871 -- 'x and ''T, returning ITvarQuote and ITtyQuote respectively,
872 -- but WIHTOUT CONSUMING the x or T part (the parser does that).
873 -- So we have to do two characters of lookahead: when we see 'x we need to
874 -- see if there's a trailing quote
875 lex_char_tok span buf len = do -- We've seen '
876 i1 <- getInput -- Look ahead to first character
877 let loc = srcSpanStart span
878 case alexGetChar i1 of
881 Just ('\'', i2@(end2,_)) -> do -- We've seen ''
882 th_exts <- extension thEnabled
885 return (T (mkSrcSpan loc end2) ITtyQuote)
888 Just ('\\', i2@(end2,_)) -> do -- We've seen 'backslash
891 mc <- getCharOrFail -- Trailing quote
892 if mc == '\'' then finish_char_tok loc lit_ch
895 Just (c, i2@(end2,_)) | not (is_any c) -> lit_error
898 -- We've seen 'x, where x is a valid character
899 -- (i.e. not newline etc) but not a quote or backslash
900 case alexGetChar i2 of -- Look ahead one more character
902 Just ('\'', i3) -> do -- We've seen 'x'
904 finish_char_tok loc c
905 _other -> do -- We've seen 'x not followed by quote
906 -- If TH is on, just parse the quote only
907 th_exts <- extension thEnabled
908 if th_exts then return (T (mkSrcSpan loc (fst i1)) ITvarQuote)
911 finish_char_tok :: SrcLoc -> Char -> P Token
912 finish_char_tok loc ch -- We've already seen the closing quote
913 -- Just need to check for trailing #
914 = do glaexts <- extension glaExtsEnabled
916 i@(end,_) <- getInput
917 case alexGetChar i of
918 Just ('#',i@(end,_)) -> do
920 return (T (mkSrcSpan loc end) (ITprimchar ch))
922 return (T (mkSrcSpan loc end) (ITchar ch))
925 return (T (mkSrcSpan loc end) (ITchar ch))
932 c | is_any c -> return c
949 '^' -> do c <- getCharOrFail
950 if c >= '@' && c <= '_'
951 then return (chr (ord c - ord '@'))
954 'x' -> readNum is_hexdigit 16 hex
955 'o' -> readNum is_octdigit 8 oct_or_dec
956 x | is_digit x -> readNum2 is_digit 10 oct_or_dec (oct_or_dec x)
960 case alexGetChar i of
963 case alexGetChar i2 of
966 let str = [c1,c2,c3] in
967 case [ (c,rest) | (p,c) <- silly_escape_chars,
968 Just rest <- [maybePrefixMatch p str] ] of
969 (escape_char,[]):_ -> do
972 (escape_char,_:_):_ -> do
977 readNum :: (Char -> Bool) -> Int -> (Char -> Int) -> P Char
978 readNum is_digit base conv = do
981 then readNum2 is_digit base conv (conv c)
984 readNum2 is_digit base conv i = do
987 where read i input = do
988 case alexGetChar input of
989 Just (c,input') | is_digit c -> do
990 read (i*base + conv c) input'
993 if i >= 0 && i <= 0x10FFFF
999 || (c >= 'a' && c <= 'f')
1000 || (c >= 'A' && c <= 'F')
1002 hex c | is_digit c = ord c - ord '0'
1003 | otherwise = ord (to_lower c) - ord 'a' + 10
1005 oct_or_dec c = ord c - ord '0'
1007 is_octdigit c = c >= '0' && c <= '7'
1010 | c >= 'A' && c <= 'Z' = chr (ord c - (ord 'A' - ord 'a'))
1013 silly_escape_chars = [
1050 lit_error = lexError "lexical error in string/character literal"
1052 getCharOrFail :: P Char
1055 case alexGetChar i of
1056 Nothing -> lexError "unexpected end-of-file in string/character literal"
1057 Just (c,i) -> do setInput i; return c
1059 -- -----------------------------------------------------------------------------
1062 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
1064 (n,d,s) <- readFix r
1066 return ((n%1)*10^^(k-d), t)
1069 (ds,s) <- lexDecDigits r
1070 (ds',t) <- lexDotDigits s
1071 return (read (ds++ds'), length ds', t)
1073 readExp (e:s) | e `elem` "eE" = readExp' s
1074 readExp s = return (0,s)
1076 readExp' ('+':s) = readDec s
1077 readExp' ('-':s) = do
1080 readExp' s = readDec s
1083 (ds,r) <- nonnull isDigit s
1084 return (foldl1 (\n d -> n * 10 + d) [ ord d - ord '0' | d <- ds ],
1087 lexDecDigits = nonnull isDigit
1089 lexDotDigits ('.':s) = return (span isDigit s)
1090 lexDotDigits s = return ("",s)
1092 nonnull p s = do (cs@(_:_),t) <- return (span p s)
1095 readRational__ :: String -> Rational -- NB: *does* handle a leading "-"
1096 readRational__ top_s
1098 '-' : xs -> - (read_me xs)
1102 = case (do { (x,"") <- readRational s ; return x }) of
1104 [] -> error ("readRational__: no parse:" ++ top_s)
1105 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
1107 -- -----------------------------------------------------------------------------
1117 SrcSpan -- The spam the error. Might be used in environments
1118 -- which can show this span, e.g. by highlighting it.
1119 Message -- The error message
1121 showPFailed span err = hcat [ppr span, text ": ", err]
1123 data PState = PState {
1124 buffer :: StringBuffer,
1125 last_span :: SrcSpan, -- span of previous token
1127 loc :: SrcLoc, -- current loc (end of prev token + 1)
1128 extsBitmap :: !Int, -- bitmap that determines permitted extensions
1129 context :: [LayoutContext],
1132 -- last_span is used when generating error messages,
1133 -- and in pushCurrentContext only.
1134 -- last_len is used when generating error messages, and is
1135 -- needed because we need to back up the buffer pointer by that
1136 -- number of characters for outputing the token in the error message.
1138 newtype P a = P { unP :: PState -> ParseResult a }
1140 instance Monad P where
1146 returnP a = P $ \s -> POk s a
1148 thenP :: P a -> (a -> P b) -> P b
1149 (P m) `thenP` k = P $ \ s ->
1151 POk s1 a -> (unP (k a)) s1
1152 PFailed span err -> PFailed span err
1154 failP :: String -> P a
1155 failP msg = P $ \s -> PFailed (last_span s) (text msg)
1157 failMsgP :: String -> P a
1158 failMsgP msg = P $ \s -> PFailed (last_span s) (text msg)
1160 failLocMsgP :: SrcLoc -> SrcLoc -> String -> P a
1161 failLocMsgP loc1 loc2 str = P $ \s -> PFailed (mkSrcSpan loc1 loc2) (text str)
1163 extension :: (Int -> Bool) -> P Bool
1164 extension p = P $ \s -> POk s (p $! extsBitmap s)
1167 getExts = P $ \s -> POk s (extsBitmap s)
1169 setSrcLoc :: SrcLoc -> P ()
1170 setSrcLoc new_loc = P $ \s -> POk s{loc=new_loc} ()
1172 -- tmp, for supporting stuff in RdrHsSyn. The scope better not include
1173 -- any calls to the lexer, because it assumes things about the SrcLoc.
1174 setSrcLocFor :: SrcLoc -> P a -> P a
1175 setSrcLocFor new_loc scope = P $ \s@PState{ loc = old_loc } ->
1176 case unP scope s{loc=new_loc} of
1177 PFailed span msg -> PFailed span msg
1180 getSrcLoc :: P SrcLoc
1181 getSrcLoc = P $ \s@(PState{ loc=loc }) -> POk s loc
1183 setLastToken :: SrcSpan -> Int -> P ()
1184 setLastToken span len = P $ \s -> POk s{ last_span=span, last_len=len } ()
1186 type AlexInput = (SrcLoc,StringBuffer)
1188 alexInputPrevChar :: AlexInput -> Char
1189 alexInputPrevChar (_,s) = prevChar s '\n'
1191 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
1194 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
1195 where c = currentChar s
1196 loc' = advanceSrcLoc loc c
1199 getInput :: P AlexInput
1200 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
1202 setInput :: AlexInput -> P ()
1203 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()
1205 pushLexState :: Int -> P ()
1206 pushLexState ls = P $ \s@PState{ lex_state=l } -> POk s{lex_state=ls:l} ()
1208 popLexState :: P Int
1209 popLexState = P $ \s@PState{ lex_state=ls:l } -> POk s{ lex_state=l } ls
1211 getLexState :: P Int
1212 getLexState = P $ \s@PState{ lex_state=ls:l } -> POk s ls
1214 -- for reasons of efficiency, flags indicating language extensions (eg,
1215 -- -fglasgow-exts or -fparr) are represented by a bitmap stored in an unboxed
1218 glaExtsBit, ffiBit, parrBit :: Int
1226 glaExtsEnabled, ffiEnabled, parrEnabled :: Int -> Bool
1227 glaExtsEnabled flags = testBit flags glaExtsBit
1228 ffiEnabled flags = testBit flags ffiBit
1229 parrEnabled flags = testBit flags parrBit
1230 arrowsEnabled flags = testBit flags arrowsBit
1231 thEnabled flags = testBit flags thBit
1232 ipEnabled flags = testBit flags ipBit
1234 -- create a parse state
1236 mkPState :: StringBuffer -> SrcLoc -> DynFlags -> PState
1237 mkPState buf loc flags =
1240 last_span = mkSrcSpan loc loc,
1243 extsBitmap = fromIntegral bitmap,
1245 lex_state = [bol, if glaExtsEnabled bitmap then glaexts else 0]
1246 -- we begin in the layout state if toplev_layout is set
1249 bitmap = glaExtsBit `setBitIf` dopt Opt_GlasgowExts flags
1250 .|. ffiBit `setBitIf` dopt Opt_FFI flags
1251 .|. parrBit `setBitIf` dopt Opt_PArr flags
1252 .|. arrowsBit `setBitIf` dopt Opt_Arrows flags
1253 .|. thBit `setBitIf` dopt Opt_TH flags
1254 .|. ipBit `setBitIf` dopt Opt_ImplicitParams flags
1256 setBitIf :: Int -> Bool -> Int
1257 b `setBitIf` cond | cond = bit b
1260 getContext :: P [LayoutContext]
1261 getContext = P $ \s@PState{context=ctx} -> POk s ctx
1263 setContext :: [LayoutContext] -> P ()
1264 setContext ctx = P $ \s -> POk s{context=ctx} ()
1267 popContext = P $ \ s@(PState{ buffer = buf, context = ctx,
1268 loc = loc, last_len = len, last_span = last_span }) ->
1270 (_:tl) -> POk s{ context = tl } ()
1271 [] -> PFailed last_span (srcParseErr buf len)
1273 -- Push a new layout context at the indentation of the last token read.
1274 -- This is only used at the outer level of a module when the 'module'
1275 -- keyword is missing.
1276 pushCurrentContext :: P ()
1277 pushCurrentContext = P $ \ s@PState{ last_span=span, context=ctx } ->
1278 POk s{ context = Layout (srcSpanStartCol span) : ctx} ()
1280 getOffside :: Int -> P Ordering
1281 getOffside col = P $ \s@PState{context=stk} ->
1282 let ord = case stk of
1283 (Layout n:_) -> compare col n
1287 -- ---------------------------------------------------------------------------
1288 -- Construct a parse error
1291 :: StringBuffer -- current buffer (placed just after the last token)
1292 -> Int -- length of the previous token
1295 = hcat [ if null token
1296 then ptext SLIT("parse error (possibly incorrect indentation)")
1297 else hcat [ptext SLIT("parse error on input "),
1298 char '`', text token, char '\'']
1300 where token = lexemeToString (stepOnBy (-len) buf) len
1302 -- Report a parse failure, giving the span of the previous token as
1303 -- the location of the error. This is the entry point for errors
1304 -- detected during parsing.
1306 srcParseFail = P $ \PState{ buffer = buf, last_len = len,
1307 last_span = last_span, loc = loc } ->
1308 PFailed last_span (srcParseErr buf len)
1310 -- A lexical error is reported at a particular position in the source file,
1311 -- not over a token range. TODO: this is slightly wrong, because we record
1312 -- the error at the character position following the one which caused the
1313 -- error. We should somehow back up by one character.
1314 lexError :: String -> P a
1317 failLocMsgP loc loc str
1319 -- -----------------------------------------------------------------------------
1320 -- This is the top-level function: called from the parser each time a
1321 -- new token is to be read from the input.
1323 lexer :: (Token -> P a) -> P a
1325 tok@(T _ tok__) <- lexToken
1326 --trace ("token: " ++ show tok__) $ do
1331 inp@(loc1,buf) <- getInput
1334 case alexScanUser exts inp sc of
1335 AlexEOF -> do let span = mkSrcSpan loc1 loc1
1337 return (T span ITeof)
1338 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
1339 AlexSkip inp2 _ -> do
1342 AlexToken inp2@(end,buf2) len t -> do
1344 let span = mkSrcSpan loc1 end
1345 (setLastToken $! span) $! len