1 -----------------------------------------------------------------------------
2 -- (c) The University of Glasgow, 2006
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, PState(..),
26 P(..), ParseResult(..), getSrcLoc,
27 failLocMsgP, failSpanMsgP, srcParseFail,
28 popContext, pushCurrentContext, setLastToken, setSrcLoc,
29 getLexState, popLexState, pushLexState,
30 extension, bangPatEnabled
33 #include "HsVersions.h"
35 import ErrUtils ( Message )
44 import Util ( maybePrefixMatch, readRational )
53 $whitechar = [\ \t\n\r\f\v\xa0 $unispace]
54 $white_no_nl = $whitechar # \n
58 $decdigit = $ascdigit -- for now, should really be $digit (ToDo)
59 $digit = [$ascdigit $unidigit]
61 $special = [\(\)\,\;\[\]\`\{\}]
62 $ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~]
64 $symbol = [$ascsymbol $unisymbol] # [$special \_\:\"\']
67 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
68 $large = [$asclarge $unilarge]
71 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
72 $small = [$ascsmall $unismall \_]
75 $graphic = [$small $large $symbol $digit $special $unigraphic \:\"\']
78 $hexit = [$decdigit A-F a-f]
79 $symchar = [$symbol \:]
81 $idchar = [$small $large $digit \']
83 @varid = $small $idchar*
84 @conid = $large $idchar*
86 @varsym = $symbol $symchar*
87 @consym = \: $symchar*
91 @hexadecimal = $hexit+
92 @exponent = [eE] [\-\+]? @decimal
94 -- we support the hierarchical module name extension:
97 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
101 -- everywhere: skip whitespace and comments
104 -- Everywhere: deal with nested comments. We explicitly rule out
105 -- pragmas, "{-#", so that we don't accidentally treat them as comments.
106 -- (this can happen even though pragmas will normally take precedence due to
107 -- longest-match, because pragmas aren't valid in every state, but comments
109 "{-" / { notFollowedBy '#' } { nested_comment }
111 -- Single-line comments are a bit tricky. Haskell 98 says that two or
112 -- more dashes followed by a symbol should be parsed as a varsym, so we
113 -- have to exclude those.
114 -- The regex says: "munch all the characters after the dashes, as long as
115 -- the first one is not a symbol".
116 "--"\-* [^$symbol :] .* ;
117 "--"\-* / { atEOL } ;
119 -- 'bol' state: beginning of a line. Slurp up all the whitespace (including
120 -- blank lines) until we find a non-whitespace character, then do layout
123 -- One slight wibble here: what if the line begins with {-#? In
124 -- theory, we have to lex the pragma to see if it's one we recognise,
125 -- and if it is, then we backtrack and do_bol, otherwise we treat it
126 -- as a nested comment. We don't bother with this: if the line begins
127 -- with {-#, then we'll assume it's a pragma we know about and go for do_bol.
130 ^\# (line)? { begin line_prag1 }
131 ^\# pragma .* \n ; -- GCC 3.3 CPP generated, apparently
132 ^\# \! .* \n ; -- #!, for scripts
136 -- after a layout keyword (let, where, do, of), we begin a new layout
137 -- context if the curly brace is missing.
138 -- Careful! This stuff is quite delicate.
139 <layout, layout_do> {
140 \{ / { notFollowedBy '-' } { pop_and open_brace }
141 -- we might encounter {-# here, but {- has been handled already
143 ^\# (line)? { begin line_prag1 }
146 -- do is treated in a subtly different way, see new_layout_context
147 <layout> () { new_layout_context True }
148 <layout_do> () { new_layout_context False }
150 -- after a new layout context which was found to be to the left of the
151 -- previous context, we have generated a '{' token, and we now need to
152 -- generate a matching '}' token.
153 <layout_left> () { do_layout_left }
155 <0,glaexts> \n { begin bol }
157 "{-#" $whitechar* (line|LINE) { begin line_prag2 }
159 -- single-line line pragmas, of the form
160 -- # <line> "<file>" <extra-stuff> \n
161 <line_prag1> $decdigit+ { setLine line_prag1a }
162 <line_prag1a> \" [$graphic \ ]* \" { setFile line_prag1b }
163 <line_prag1b> .* { pop }
165 -- Haskell-style line pragmas, of the form
166 -- {-# LINE <line> "<file>" #-}
167 <line_prag2> $decdigit+ { setLine line_prag2a }
168 <line_prag2a> \" [$graphic \ ]* \" { setFile line_prag2b }
169 <line_prag2b> "#-}"|"-}" { pop }
170 -- NOTE: accept -} at the end of a LINE pragma, for compatibility
171 -- with older versions of GHC which generated these.
173 -- We only want RULES pragmas to be picked up when -fglasgow-exts
174 -- is on, because the contents of the pragma is always written using
175 -- glasgow-exts syntax (using forall etc.), so if glasgow exts are not
176 -- enabled, we're sure to get a parse error.
177 -- (ToDo: we should really emit a warning when ignoring pragmas)
179 "{-#" $whitechar* (RULES|rules) { token ITrules_prag }
182 "{-#" $whitechar* (INLINE|inline) { token (ITinline_prag True) }
183 "{-#" $whitechar* (NO(T?)INLINE|no(t?)inline)
184 { token (ITinline_prag False) }
185 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
186 { token ITspec_prag }
187 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
188 $whitechar* (INLINE|inline) { token (ITspec_inline_prag True) }
189 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
190 $whitechar* (NO(T?)INLINE|no(t?)inline)
191 { token (ITspec_inline_prag False) }
192 "{-#" $whitechar* (SOURCE|source) { token ITsource_prag }
193 "{-#" $whitechar* (DEPRECATED|deprecated)
194 { token ITdeprecated_prag }
195 "{-#" $whitechar* (SCC|scc) { token ITscc_prag }
196 "{-#" $whitechar* (CORE|core) { token ITcore_prag }
197 "{-#" $whitechar* (UNPACK|unpack) { token ITunpack_prag }
199 "{-#" { nested_comment }
201 -- ToDo: should only be valid inside a pragma:
202 "#-}" { token ITclose_prag}
206 -- '0' state: ordinary lexemes
207 -- 'glaexts' state: glasgow extensions (postfix '#', etc.)
212 "[:" / { ifExtension parrEnabled } { token ITopabrack }
213 ":]" / { ifExtension parrEnabled } { token ITcpabrack }
217 "[|" / { ifExtension thEnabled } { token ITopenExpQuote }
218 "[e|" / { ifExtension thEnabled } { token ITopenExpQuote }
219 "[p|" / { ifExtension thEnabled } { token ITopenPatQuote }
220 "[d|" / { ifExtension thEnabled } { layout_token ITopenDecQuote }
221 "[t|" / { ifExtension thEnabled } { token ITopenTypQuote }
222 "|]" / { ifExtension thEnabled } { token ITcloseQuote }
223 \$ @varid / { ifExtension thEnabled } { skip_one_varid ITidEscape }
224 "$(" / { ifExtension thEnabled } { token ITparenEscape }
228 "(|" / { ifExtension arrowsEnabled `alexAndPred` notFollowedBySymbol }
229 { special IToparenbar }
230 "|)" / { ifExtension arrowsEnabled } { special ITcparenbar }
234 \? @varid / { ifExtension ipEnabled } { skip_one_varid ITdupipvarid }
235 \% @varid / { ifExtension ipEnabled } { skip_one_varid ITsplitipvarid }
239 "(#" / { notFollowedBySymbol } { token IToubxparen }
240 "#)" { token ITcubxparen }
241 "{|" { token ITocurlybar }
242 "|}" { token ITccurlybar }
246 \( { special IToparen }
247 \) { special ITcparen }
248 \[ { special ITobrack }
249 \] { special ITcbrack }
250 \, { special ITcomma }
251 \; { special ITsemi }
252 \` { special ITbackquote }
259 @qual @varid { check_qvarid }
260 @qual @conid { idtoken qconid }
262 @conid { idtoken conid }
265 -- after an illegal qvarid, such as 'M.let',
266 -- we back up and try again in the bad_qvarid state:
268 @conid { pop_and (idtoken conid) }
269 @qual @conid { pop_and (idtoken qconid) }
273 @qual @varid "#"+ { idtoken qvarid }
274 @qual @conid "#"+ { idtoken qconid }
275 @varid "#"+ { varid }
276 @conid "#"+ { idtoken conid }
282 @qual @varsym { idtoken qvarsym }
283 @qual @consym { idtoken qconsym }
289 @decimal { tok_decimal }
290 0[oO] @octal { tok_octal }
291 0[xX] @hexadecimal { tok_hexadecimal }
295 @decimal \# { prim_decimal }
296 0[oO] @octal \# { prim_octal }
297 0[xX] @hexadecimal \# { prim_hexadecimal }
300 <0,glaexts> @floating_point { strtoken tok_float }
301 <glaexts> @floating_point \# { init_strtoken 1 prim_float }
302 <glaexts> @floating_point \# \# { init_strtoken 2 prim_double }
304 -- Strings and chars are lexed by hand-written code. The reason is
305 -- that even if we recognise the string or char here in the regex
306 -- lexer, we would still have to parse the string afterward in order
307 -- to convert it to a String.
310 \" { lex_string_tok }
314 -- work around bug in Alex 2.0
315 #if __GLASGOW_HASKELL__ < 503
316 unsafeAt arr i = arr ! i
319 -- -----------------------------------------------------------------------------
323 = ITas -- Haskell keywords
347 | ITscc -- ToDo: remove (we use {-# SCC "..." #-} now)
349 | ITforall -- GHC extension keywords
363 | ITinline_prag Bool -- True <=> INLINE, False <=> NOINLINE
364 | ITspec_prag -- SPECIALISE
365 | ITspec_inline_prag Bool -- SPECIALISE INLINE (or NOINLINE)
371 | ITcore_prag -- hdaume: core annotations
375 | ITdotdot -- reserved symbols
391 | ITbiglam -- GHC-extension symbols
393 | ITocurly -- special symbols
395 | ITocurlybar -- {|, for type applications
396 | ITccurlybar -- |}, for type applications
400 | ITopabrack -- [:, for parallel arrays with -fparr
401 | ITcpabrack -- :], for parallel arrays with -fparr
412 | ITvarid FastString -- identifiers
414 | ITvarsym FastString
415 | ITconsym FastString
416 | ITqvarid (FastString,FastString)
417 | ITqconid (FastString,FastString)
418 | ITqvarsym (FastString,FastString)
419 | ITqconsym (FastString,FastString)
421 | ITdupipvarid FastString -- GHC extension: implicit param: ?x
422 | ITsplitipvarid FastString -- GHC extension: implicit param: %x
424 | ITpragma StringBuffer
427 | ITstring FastString
429 | ITrational Rational
432 | ITprimstring FastString
434 | ITprimfloat Rational
435 | ITprimdouble Rational
437 -- MetaHaskell extension tokens
438 | ITopenExpQuote -- [| or [e|
439 | ITopenPatQuote -- [p|
440 | ITopenDecQuote -- [d|
441 | ITopenTypQuote -- [t|
443 | ITidEscape FastString -- $x
444 | ITparenEscape -- $(
448 -- Arrow notation extension
455 | ITLarrowtail -- -<<
456 | ITRarrowtail -- >>-
458 | ITunknown String -- Used when the lexer can't make sense of it
459 | ITeof -- end of file token
461 deriving Show -- debugging
464 isSpecial :: Token -> Bool
465 -- If we see M.x, where x is a keyword, but
466 -- is special, we treat is as just plain M.x,
468 isSpecial ITas = True
469 isSpecial IThiding = True
470 isSpecial ITqualified = True
471 isSpecial ITforall = True
472 isSpecial ITexport = True
473 isSpecial ITlabel = True
474 isSpecial ITdynamic = True
475 isSpecial ITsafe = True
476 isSpecial ITthreadsafe = True
477 isSpecial ITunsafe = True
478 isSpecial ITccallconv = True
479 isSpecial ITstdcallconv = True
480 isSpecial ITmdo = True
483 -- the bitmap provided as the third component indicates whether the
484 -- corresponding extension keyword is valid under the extension options
485 -- provided to the compiler; if the extension corresponding to *any* of the
486 -- bits set in the bitmap is enabled, the keyword is valid (this setup
487 -- facilitates using a keyword in two different extensions that can be
488 -- activated independently)
490 reservedWordsFM = listToUFM $
491 map (\(x, y, z) -> (mkFastString x, (y, z)))
492 [( "_", ITunderscore, 0 ),
494 ( "case", ITcase, 0 ),
495 ( "class", ITclass, 0 ),
496 ( "data", ITdata, 0 ),
497 ( "default", ITdefault, 0 ),
498 ( "deriving", ITderiving, 0 ),
500 ( "else", ITelse, 0 ),
501 ( "hiding", IThiding, 0 ),
503 ( "import", ITimport, 0 ),
505 ( "infix", ITinfix, 0 ),
506 ( "infixl", ITinfixl, 0 ),
507 ( "infixr", ITinfixr, 0 ),
508 ( "instance", ITinstance, 0 ),
510 ( "module", ITmodule, 0 ),
511 ( "newtype", ITnewtype, 0 ),
513 ( "qualified", ITqualified, 0 ),
514 ( "then", ITthen, 0 ),
515 ( "type", ITtype, 0 ),
516 ( "where", ITwhere, 0 ),
517 ( "_scc_", ITscc, 0 ), -- ToDo: remove
519 ( "forall", ITforall, bit tvBit),
520 ( "mdo", ITmdo, bit glaExtsBit),
522 ( "foreign", ITforeign, bit ffiBit),
523 ( "export", ITexport, bit ffiBit),
524 ( "label", ITlabel, bit ffiBit),
525 ( "dynamic", ITdynamic, bit ffiBit),
526 ( "safe", ITsafe, bit ffiBit),
527 ( "threadsafe", ITthreadsafe, bit ffiBit),
528 ( "unsafe", ITunsafe, bit ffiBit),
529 ( "stdcall", ITstdcallconv, bit ffiBit),
530 ( "ccall", ITccallconv, bit ffiBit),
531 ( "dotnet", ITdotnet, bit ffiBit),
533 ( "rec", ITrec, bit arrowsBit),
534 ( "proc", ITproc, bit arrowsBit)
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 tvBit) -- For 'forall a . t'
557 ,("-<", ITlarrowtail, bit arrowsBit)
558 ,(">-", ITrarrowtail, bit arrowsBit)
559 ,("-<<", ITLarrowtail, bit arrowsBit)
560 ,(">>-", ITRarrowtail, bit arrowsBit)
562 #if __GLASGOW_HASKELL__ >= 605
563 ,("λ", ITlam, bit glaExtsBit)
564 ,("∷", ITdcolon, bit glaExtsBit)
565 ,("⇒", ITdarrow, bit glaExtsBit)
566 ,("∀", ITforall, bit glaExtsBit)
567 ,("→", ITrarrow, bit glaExtsBit)
568 ,("←", ITlarrow, bit glaExtsBit)
569 ,("⋯", ITdotdot, bit glaExtsBit)
573 -- -----------------------------------------------------------------------------
576 type Action = SrcSpan -> StringBuffer -> Int -> P (Located Token)
578 special :: Token -> Action
579 special tok span _buf len = return (L span tok)
581 token, layout_token :: Token -> Action
582 token t span buf len = return (L span t)
583 layout_token t span buf len = pushLexState layout >> return (L span t)
585 idtoken :: (StringBuffer -> Int -> Token) -> Action
586 idtoken f span buf len = return (L span $! (f buf len))
588 skip_one_varid :: (FastString -> Token) -> Action
589 skip_one_varid f span buf len
590 = return (L span $! f (lexemeToFastString (stepOn buf) (len-1)))
592 strtoken :: (String -> Token) -> Action
593 strtoken f span buf len =
594 return (L span $! (f $! lexemeToString buf len))
596 init_strtoken :: Int -> (String -> Token) -> Action
597 -- like strtoken, but drops the last N character(s)
598 init_strtoken drop f span buf len =
599 return (L span $! (f $! lexemeToString buf (len-drop)))
601 begin :: Int -> Action
602 begin code _span _str _len = do pushLexState code; lexToken
605 pop _span _buf _len = do popLexState; lexToken
607 pop_and :: Action -> Action
608 pop_and act span buf len = do popLexState; act span buf len
610 notFollowedBy char _ _ _ (AI _ _ buf) = atEnd buf || currentChar buf /= char
612 notFollowedBySymbol _ _ _ (AI _ _ buf)
613 = atEnd buf || currentChar buf `notElem` "!#$%&*+./<=>?@\\^|-~"
615 atEOL _ _ _ (AI _ _ buf) = atEnd buf || currentChar buf == '\n'
617 ifExtension pred bits _ _ _ = pred bits
620 nested comments require traversing by hand, they can't be parsed
621 using regular expressions.
623 nested_comment :: Action
624 nested_comment span _str _len = do
627 where go 0 input = do setInput input; lexToken
629 case alexGetChar input of
634 case alexGetChar input of
636 Just ('\125',input) -> go (n-1) input
637 Just (c,_) -> go n input
639 case alexGetChar input of
641 Just ('-',input') -> go (n+1) input'
642 Just (c,input) -> go n input
645 err (AI end _ _) = failLocMsgP (srcSpanStart span) end "unterminated `{-'"
647 open_brace, close_brace :: Action
648 open_brace span _str _len = do
650 setContext (NoLayout:ctx)
651 return (L span ITocurly)
652 close_brace span _str _len = do
654 return (L span ITccurly)
656 -- We have to be careful not to count M.<varid> as a qualified name
657 -- when <varid> is a keyword. We hack around this by catching
658 -- the offending tokens afterward, and re-lexing in a different state.
659 check_qvarid span buf len = do
660 case lookupUFM reservedWordsFM var of
662 | not (isSpecial keyword) ->
666 b <- extension (\i -> exts .&. i /= 0)
669 _other -> return token
671 (mod,var) = splitQualName buf len
672 token = L span (ITqvarid (mod,var))
675 (AI _ offs _) <- getInput
676 setInput (AI (srcSpanStart span) (offs-len) buf)
677 pushLexState bad_qvarid
680 qvarid buf len = ITqvarid $! splitQualName buf len
681 qconid buf len = ITqconid $! splitQualName buf len
683 splitQualName :: StringBuffer -> Int -> (FastString,FastString)
684 -- takes a StringBuffer and a length, and returns the module name
685 -- and identifier parts of a qualified name. Splits at the *last* dot,
686 -- because of hierarchical module names.
687 splitQualName orig_buf len = split orig_buf orig_buf
690 | orig_buf `byteDiff` buf >= len = done dot_buf
691 | c == '.' = found_dot buf'
692 | otherwise = split buf' dot_buf
694 (c,buf') = nextChar buf
696 -- careful, we might get names like M....
697 -- so, if the character after the dot is not upper-case, this is
698 -- the end of the qualifier part.
699 found_dot buf -- buf points after the '.'
700 | isUpper c = split buf' buf
701 | otherwise = done buf
703 (c,buf') = nextChar buf
706 (lexemeToFastString orig_buf (qual_size - 1),
707 lexemeToFastString dot_buf (len - qual_size))
709 qual_size = orig_buf `byteDiff` dot_buf
712 case lookupUFM reservedWordsFM fs of
713 Just (keyword,0) -> do
715 return (L span keyword)
716 Just (keyword,exts) -> do
717 b <- extension (\i -> exts .&. i /= 0)
718 if b then do maybe_layout keyword
719 return (L span keyword)
720 else return (L span (ITvarid fs))
721 _other -> return (L span (ITvarid fs))
723 fs = lexemeToFastString buf len
725 conid buf len = ITconid fs
726 where fs = lexemeToFastString buf len
728 qvarsym buf len = ITqvarsym $! splitQualName buf len
729 qconsym buf len = ITqconsym $! splitQualName buf len
731 varsym = sym ITvarsym
732 consym = sym ITconsym
734 sym con span buf len =
735 case lookupUFM reservedSymsFM fs of
736 Just (keyword,0) -> return (L span keyword)
737 Just (keyword,exts) -> do
738 b <- extension (\i -> exts .&. i /= 0)
739 if b then return (L span keyword)
740 else return (L span $! con fs)
741 _other -> return (L span $! con fs)
743 fs = lexemeToFastString buf len
745 tok_decimal span buf len
746 = return (L span (ITinteger $! parseInteger buf len 10 octDecDigit))
748 tok_octal span buf len
749 = return (L span (ITinteger $! parseInteger (offsetBytes 2 buf) (len-2) 8 octDecDigit))
751 tok_hexadecimal span buf len
752 = return (L span (ITinteger $! parseInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
754 prim_decimal span buf len
755 = return (L span (ITprimint $! parseInteger buf (len-1) 10 octDecDigit))
757 prim_octal span buf len
758 = return (L span (ITprimint $! parseInteger (offsetBytes 2 buf) (len-3) 8 octDecDigit))
760 prim_hexadecimal span buf len
761 = return (L span (ITprimint $! parseInteger (offsetBytes 2 buf) (len-3) 16 hexDigit))
763 tok_float str = ITrational $! readRational str
764 prim_float str = ITprimfloat $! readRational str
765 prim_double str = ITprimdouble $! readRational str
767 -- -----------------------------------------------------------------------------
770 -- we're at the first token on a line, insert layout tokens if necessary
772 do_bol span _str _len = do
776 --trace "layout: inserting '}'" $ do
778 -- do NOT pop the lex state, we might have a ';' to insert
779 return (L span ITvccurly)
781 --trace "layout: inserting ';'" $ do
783 return (L span ITsemi)
788 -- certain keywords put us in the "layout" state, where we might
789 -- add an opening curly brace.
790 maybe_layout ITdo = pushLexState layout_do
791 maybe_layout ITmdo = pushLexState layout_do
792 maybe_layout ITof = pushLexState layout
793 maybe_layout ITlet = pushLexState layout
794 maybe_layout ITwhere = pushLexState layout
795 maybe_layout ITrec = pushLexState layout
796 maybe_layout _ = return ()
798 -- Pushing a new implicit layout context. If the indentation of the
799 -- next token is not greater than the previous layout context, then
800 -- Haskell 98 says that the new layout context should be empty; that is
801 -- the lexer must generate {}.
803 -- We are slightly more lenient than this: when the new context is started
804 -- by a 'do', then we allow the new context to be at the same indentation as
805 -- the previous context. This is what the 'strict' argument is for.
807 new_layout_context strict span _buf _len = do
809 (AI _ offset _) <- getInput
812 Layout prev_off : _ |
813 (strict && prev_off >= offset ||
814 not strict && prev_off > offset) -> do
815 -- token is indented to the left of the previous context.
816 -- we must generate a {} sequence now.
817 pushLexState layout_left
818 return (L span ITvocurly)
820 setContext (Layout offset : ctx)
821 return (L span ITvocurly)
823 do_layout_left span _buf _len = do
825 pushLexState bol -- we must be at the start of a line
826 return (L span ITvccurly)
828 -- -----------------------------------------------------------------------------
831 setLine :: Int -> Action
832 setLine code span buf len = do
833 let line = parseInteger buf len 10 octDecDigit
834 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
835 -- subtract one: the line number refers to the *following* line
840 setFile :: Int -> Action
841 setFile code span buf len = do
842 let file = lexemeToFastString (stepOn buf) (len-2)
843 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
848 -- -----------------------------------------------------------------------------
851 -- This stuff is horrible. I hates it.
853 lex_string_tok :: Action
854 lex_string_tok span buf len = do
857 return (L (mkSrcSpan (srcSpanStart span) end) tok)
859 lex_string :: String -> P Token
862 case alexGetChar' i of
867 glaexts <- extension glaExtsEnabled
871 case alexGetChar' i of
875 then failMsgP "primitive string literal must contain only characters <= \'\\xFF\'"
876 else let s' = mkZFastString (reverse s) in
877 return (ITprimstring s')
878 -- mkZFastString is a hack to avoid encoding the
879 -- string in UTF-8. We just want the exact bytes.
881 return (ITstring (mkFastString (reverse s)))
883 return (ITstring (mkFastString (reverse s)))
886 | Just ('&',i) <- next -> do
887 setInput i; lex_string s
888 | Just (c,i) <- next, is_space c -> do
889 setInput i; lex_stringgap s
890 where next = alexGetChar' i
900 c | is_space c -> lex_stringgap s
904 lex_char_tok :: Action
905 -- Here we are basically parsing character literals, such as 'x' or '\n'
906 -- but, when Template Haskell is on, we additionally spot
907 -- 'x and ''T, returning ITvarQuote and ITtyQuote respectively,
908 -- but WIHTOUT CONSUMING the x or T part (the parser does that).
909 -- So we have to do two characters of lookahead: when we see 'x we need to
910 -- see if there's a trailing quote
911 lex_char_tok span buf len = do -- We've seen '
912 i1 <- getInput -- Look ahead to first character
913 let loc = srcSpanStart span
914 case alexGetChar' i1 of
917 Just ('\'', i2@(AI end2 _ _)) -> do -- We've seen ''
918 th_exts <- extension thEnabled
921 return (L (mkSrcSpan loc end2) ITtyQuote)
924 Just ('\\', i2@(AI end2 _ _)) -> do -- We've seen 'backslash
927 mc <- getCharOrFail -- Trailing quote
928 if mc == '\'' then finish_char_tok loc lit_ch
929 else do setInput i2; lit_error
931 Just (c, i2@(AI end2 _ _))
932 | not (isAny c) -> lit_error
935 -- We've seen 'x, where x is a valid character
936 -- (i.e. not newline etc) but not a quote or backslash
937 case alexGetChar' i2 of -- Look ahead one more character
939 Just ('\'', i3) -> do -- We've seen 'x'
941 finish_char_tok loc c
942 _other -> do -- We've seen 'x not followed by quote
943 -- If TH is on, just parse the quote only
944 th_exts <- extension thEnabled
945 let (AI end _ _) = i1
946 if th_exts then return (L (mkSrcSpan loc end) ITvarQuote)
947 else do setInput i2; lit_error
949 finish_char_tok :: SrcLoc -> Char -> P (Located Token)
950 finish_char_tok loc ch -- We've already seen the closing quote
951 -- Just need to check for trailing #
952 = do glaexts <- extension glaExtsEnabled
953 i@(AI end _ _) <- getInput
955 case alexGetChar' i of
956 Just ('#',i@(AI end _ _)) -> do
958 return (L (mkSrcSpan loc end) (ITprimchar ch))
960 return (L (mkSrcSpan loc end) (ITchar ch))
962 return (L (mkSrcSpan loc end) (ITchar ch))
964 lex_char :: Char -> AlexInput -> P Char
967 '\\' -> do setInput inp; lex_escape
968 c | isAny c -> do setInput inp; return c
971 isAny c | c > '\xff' = isPrint c
972 | otherwise = is_any c
988 '^' -> do c <- getCharOrFail
989 if c >= '@' && c <= '_'
990 then return (chr (ord c - ord '@'))
993 'x' -> readNum is_hexdigit 16 hexDigit
994 'o' -> readNum is_octdigit 8 octDecDigit
995 x | is_digit x -> readNum2 is_digit 10 octDecDigit (octDecDigit x)
999 case alexGetChar' i of
1000 Nothing -> lit_error
1002 case alexGetChar' i2 of
1003 Nothing -> do setInput i2; lit_error
1005 let str = [c1,c2,c3] in
1006 case [ (c,rest) | (p,c) <- silly_escape_chars,
1007 Just rest <- [maybePrefixMatch p str] ] of
1008 (escape_char,[]):_ -> do
1011 (escape_char,_:_):_ -> do
1016 readNum :: (Char -> Bool) -> Int -> (Char -> Int) -> P Char
1017 readNum is_digit base conv = do
1021 then readNum2 is_digit base conv (conv c)
1022 else do setInput i; lit_error
1024 readNum2 is_digit base conv i = do
1027 where read i input = do
1028 case alexGetChar' input of
1029 Just (c,input') | is_digit c -> do
1030 read (i*base + conv c) input'
1032 if i >= 0 && i <= 0x10FFFF
1033 then do setInput input; return (chr i)
1036 silly_escape_chars = [
1073 -- before calling lit_error, ensure that the current input is pointing to
1074 -- the position of the error in the buffer. This is so that we can report
1075 -- a correct location to the user, but also so we can detect UTF-8 decoding
1076 -- errors if they occur.
1077 lit_error = lexError "lexical error in string/character literal"
1079 getCharOrFail :: P Char
1082 case alexGetChar' i of
1083 Nothing -> lexError "unexpected end-of-file in string/character literal"
1084 Just (c,i) -> do setInput i; return c
1086 -- -----------------------------------------------------------------------------
1096 SrcSpan -- The start and end of the text span related to
1097 -- the error. Might be used in environments which can
1098 -- show this span, e.g. by highlighting it.
1099 Message -- The error message
1101 data PState = PState {
1102 buffer :: StringBuffer,
1103 last_loc :: SrcSpan, -- pos of previous token
1104 last_offs :: !Int, -- offset of the previous token from the
1105 -- beginning of the current line.
1106 -- \t is equal to 8 spaces.
1107 last_len :: !Int, -- len of previous token
1108 loc :: SrcLoc, -- current loc (end of prev token + 1)
1109 extsBitmap :: !Int, -- bitmap that determines permitted extensions
1110 context :: [LayoutContext],
1113 -- last_loc and last_len are used when generating error messages,
1114 -- and in pushCurrentContext only. Sigh, if only Happy passed the
1115 -- current token to happyError, we could at least get rid of last_len.
1116 -- Getting rid of last_loc would require finding another way to
1117 -- implement pushCurrentContext (which is only called from one place).
1119 newtype P a = P { unP :: PState -> ParseResult a }
1121 instance Monad P where
1127 returnP a = P $ \s -> POk s a
1129 thenP :: P a -> (a -> P b) -> P b
1130 (P m) `thenP` k = P $ \ s ->
1132 POk s1 a -> (unP (k a)) s1
1133 PFailed span err -> PFailed span err
1135 failP :: String -> P a
1136 failP msg = P $ \s -> PFailed (last_loc s) (text msg)
1138 failMsgP :: String -> P a
1139 failMsgP msg = P $ \s -> PFailed (last_loc s) (text msg)
1141 failLocMsgP :: SrcLoc -> SrcLoc -> String -> P a
1142 failLocMsgP loc1 loc2 str = P $ \s -> PFailed (mkSrcSpan loc1 loc2) (text str)
1144 failSpanMsgP :: SrcSpan -> String -> P a
1145 failSpanMsgP span msg = P $ \s -> PFailed span (text msg)
1147 extension :: (Int -> Bool) -> P Bool
1148 extension p = P $ \s -> POk s (p $! extsBitmap s)
1151 getExts = P $ \s -> POk s (extsBitmap s)
1153 setSrcLoc :: SrcLoc -> P ()
1154 setSrcLoc new_loc = P $ \s -> POk s{loc=new_loc} ()
1156 getSrcLoc :: P SrcLoc
1157 getSrcLoc = P $ \s@(PState{ loc=loc }) -> POk s loc
1159 setLastToken :: SrcSpan -> Int -> P ()
1160 setLastToken loc len = P $ \s -> POk s{ last_loc=loc, last_len=len } ()
1162 data AlexInput = AI SrcLoc {-#UNPACK#-}!Int StringBuffer
1164 alexInputPrevChar :: AlexInput -> Char
1165 alexInputPrevChar (AI _ _ buf) = prevChar buf '\n'
1167 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
1168 alexGetChar (AI loc ofs s)
1170 | otherwise = adj_c `seq` loc' `seq` ofs' `seq` s' `seq`
1171 Just (adj_c, (AI loc' ofs' s'))
1172 where (c,s') = nextChar s
1173 loc' = advanceSrcLoc loc c
1174 ofs' = advanceOffs c ofs
1182 other_graphic = '\x6'
1185 #if __GLASGOW_HASKELL__ < 605
1186 = c -- no Unicode support
1188 | c <= '\x06' = non_graphic
1191 case generalCategory c of
1192 UppercaseLetter -> upper
1193 LowercaseLetter -> lower
1194 TitlecaseLetter -> upper
1195 ModifierLetter -> other_graphic
1196 OtherLetter -> other_graphic
1197 NonSpacingMark -> other_graphic
1198 SpacingCombiningMark -> other_graphic
1199 EnclosingMark -> other_graphic
1200 DecimalNumber -> digit
1201 LetterNumber -> other_graphic
1202 OtherNumber -> other_graphic
1203 ConnectorPunctuation -> other_graphic
1204 DashPunctuation -> other_graphic
1205 OpenPunctuation -> other_graphic
1206 ClosePunctuation -> other_graphic
1207 InitialQuote -> other_graphic
1208 FinalQuote -> other_graphic
1209 OtherPunctuation -> other_graphic
1210 MathSymbol -> symbol
1211 CurrencySymbol -> symbol
1212 ModifierSymbol -> symbol
1213 OtherSymbol -> symbol
1215 _other -> non_graphic
1218 -- This version does not squash unicode characters, it is used when
1220 alexGetChar' :: AlexInput -> Maybe (Char,AlexInput)
1221 alexGetChar' (AI loc ofs s)
1223 | otherwise = c `seq` loc' `seq` ofs' `seq` s' `seq`
1224 Just (c, (AI loc' ofs' s'))
1225 where (c,s') = nextChar s
1226 loc' = advanceSrcLoc loc c
1227 ofs' = advanceOffs c ofs
1229 advanceOffs :: Char -> Int -> Int
1230 advanceOffs '\n' offs = 0
1231 advanceOffs '\t' offs = (offs `quot` 8 + 1) * 8
1232 advanceOffs _ offs = offs + 1
1234 getInput :: P AlexInput
1235 getInput = P $ \s@PState{ loc=l, last_offs=o, buffer=b } -> POk s (AI l o b)
1237 setInput :: AlexInput -> P ()
1238 setInput (AI l o b) = P $ \s -> POk s{ loc=l, last_offs=o, buffer=b } ()
1240 pushLexState :: Int -> P ()
1241 pushLexState ls = P $ \s@PState{ lex_state=l } -> POk s{lex_state=ls:l} ()
1243 popLexState :: P Int
1244 popLexState = P $ \s@PState{ lex_state=ls:l } -> POk s{ lex_state=l } ls
1246 getLexState :: P Int
1247 getLexState = P $ \s@PState{ lex_state=ls:l } -> POk s ls
1249 -- for reasons of efficiency, flags indicating language extensions (eg,
1250 -- -fglasgow-exts or -fparr) are represented by a bitmap stored in an unboxed
1253 glaExtsBit, ffiBit, parrBit :: Int
1260 tvBit = 7 -- Scoped type variables enables 'forall' keyword
1261 bangPatBit = 8 -- Tells the parser to understand bang-patterns
1262 -- (doesn't affect the lexer)
1264 glaExtsEnabled, ffiEnabled, parrEnabled :: Int -> Bool
1265 glaExtsEnabled flags = testBit flags glaExtsBit
1266 ffiEnabled flags = testBit flags ffiBit
1267 parrEnabled flags = testBit flags parrBit
1268 arrowsEnabled flags = testBit flags arrowsBit
1269 thEnabled flags = testBit flags thBit
1270 ipEnabled flags = testBit flags ipBit
1271 tvEnabled flags = testBit flags tvBit
1272 bangPatEnabled flags = testBit flags bangPatBit
1274 -- create a parse state
1276 mkPState :: StringBuffer -> SrcLoc -> DynFlags -> PState
1277 mkPState buf loc flags =
1280 last_loc = mkSrcSpan loc loc,
1284 extsBitmap = fromIntegral bitmap,
1286 lex_state = [bol, if glaExtsEnabled bitmap then glaexts else 0]
1287 -- we begin in the layout state if toplev_layout is set
1290 bitmap = glaExtsBit `setBitIf` dopt Opt_GlasgowExts flags
1291 .|. ffiBit `setBitIf` dopt Opt_FFI flags
1292 .|. parrBit `setBitIf` dopt Opt_PArr flags
1293 .|. arrowsBit `setBitIf` dopt Opt_Arrows flags
1294 .|. thBit `setBitIf` dopt Opt_TH flags
1295 .|. ipBit `setBitIf` dopt Opt_ImplicitParams flags
1296 .|. tvBit `setBitIf` dopt Opt_ScopedTypeVariables flags
1297 .|. bangPatBit `setBitIf` dopt Opt_BangPatterns flags
1299 setBitIf :: Int -> Bool -> Int
1300 b `setBitIf` cond | cond = bit b
1303 getContext :: P [LayoutContext]
1304 getContext = P $ \s@PState{context=ctx} -> POk s ctx
1306 setContext :: [LayoutContext] -> P ()
1307 setContext ctx = P $ \s -> POk s{context=ctx} ()
1310 popContext = P $ \ s@(PState{ buffer = buf, context = ctx,
1311 loc = loc, last_len = len, last_loc = last_loc }) ->
1313 (_:tl) -> POk s{ context = tl } ()
1314 [] -> PFailed last_loc (srcParseErr buf len)
1316 -- Push a new layout context at the indentation of the last token read.
1317 -- This is only used at the outer level of a module when the 'module'
1318 -- keyword is missing.
1319 pushCurrentContext :: P ()
1320 pushCurrentContext = P $ \ s@PState{ last_offs=offs, last_len=len, context=ctx } ->
1321 POk s{context = Layout (offs-len) : ctx} ()
1323 getOffside :: P Ordering
1324 getOffside = P $ \s@PState{last_offs=offs, context=stk} ->
1325 let ord = case stk of
1326 (Layout n:_) -> compare offs n
1330 -- ---------------------------------------------------------------------------
1331 -- Construct a parse error
1334 :: StringBuffer -- current buffer (placed just after the last token)
1335 -> Int -- length of the previous token
1338 = hcat [ if null token
1339 then ptext SLIT("parse error (possibly incorrect indentation)")
1340 else hcat [ptext SLIT("parse error on input "),
1341 char '`', text token, char '\'']
1343 where token = lexemeToString (offsetBytes (-len) buf) len
1345 -- Report a parse failure, giving the span of the previous token as
1346 -- the location of the error. This is the entry point for errors
1347 -- detected during parsing.
1349 srcParseFail = P $ \PState{ buffer = buf, last_len = len,
1350 last_loc = last_loc } ->
1351 PFailed last_loc (srcParseErr buf len)
1353 -- A lexical error is reported at a particular position in the source file,
1354 -- not over a token range.
1355 lexError :: String -> P a
1358 i@(AI end _ buf) <- getInput
1359 reportLexError loc end buf str
1361 -- -----------------------------------------------------------------------------
1362 -- This is the top-level function: called from the parser each time a
1363 -- new token is to be read from the input.
1365 lexer :: (Located Token -> P a) -> P a
1367 tok@(L _ tok__) <- lexToken
1368 --trace ("token: " ++ show tok__) $ do
1371 lexToken :: P (Located Token)
1373 inp@(AI loc1 _ buf) <- getInput
1376 case alexScanUser exts inp sc of
1377 AlexEOF -> do let span = mkSrcSpan loc1 loc1
1379 return (L span ITeof)
1380 AlexError (AI loc2 _ buf) -> do
1381 reportLexError loc1 loc2 buf "lexical error"
1382 AlexSkip inp2 _ -> do
1385 AlexToken inp2@(AI end _ buf2) len t -> do
1387 let span = mkSrcSpan loc1 end
1388 let bytes = byteDiff buf buf2
1389 span `seq` setLastToken span bytes
1392 -- ToDo: Alex reports the buffer at the start of the erroneous lexeme,
1393 -- but it would be more informative to report the location where the
1394 -- error was actually discovered, especially if this is a decoding
1396 reportLexError loc1 loc2 buf str =
1398 c = fst (nextChar buf)
1400 if c == '\0' -- decoding errors are mapped to '\0', see utf8DecodeChar#
1401 then failLocMsgP loc2 loc2 "UTF-8 decoding error"
1402 else failLocMsgP loc1 loc2 (str ++ " at character " ++ show c)