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, pragState, mkPState, PState(..),
26 P(..), ParseResult(..), getSrcLoc,
27 failLocMsgP, failSpanMsgP, srcParseFail,
28 popContext, pushCurrentContext, setLastToken, setSrcLoc,
29 getLexState, popLexState, pushLexState,
30 extension, glaExtsEnabled, bangPatEnabled
33 #include "HsVersions.h"
35 import ErrUtils ( Message )
44 import Util ( maybePrefixMatch, readRational )
47 import Data.Char ( chr )
51 #if __GLASGOW_HASKELL__ >= 605
52 import Data.Char ( GeneralCategory(..), generalCategory, isPrint, isUpper )
54 import Compat.Unicode ( GeneralCategory(..), generalCategory, isPrint, isUpper )
59 $whitechar = [\ \t\n\r\f\v\xa0 $unispace]
60 $white_no_nl = $whitechar # \n
64 $decdigit = $ascdigit -- for now, should really be $digit (ToDo)
65 $digit = [$ascdigit $unidigit]
67 $special = [\(\)\,\;\[\]\`\{\}]
68 $ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~ \xa1-\xbf \xd7 \xf7]
70 $symbol = [$ascsymbol $unisymbol] # [$special \_\:\"\']
73 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
74 $large = [$asclarge $unilarge]
77 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
78 $small = [$ascsmall $unismall \_]
81 $graphic = [$small $large $symbol $digit $special $unigraphic \:\"\']
84 $hexit = [$decdigit A-F a-f]
85 $symchar = [$symbol \:]
87 $idchar = [$small $large $digit \']
89 @varid = $small $idchar*
90 @conid = $large $idchar*
92 @varsym = $symbol $symchar*
93 @consym = \: $symchar*
97 @hexadecimal = $hexit+
98 @exponent = [eE] [\-\+]? @decimal
100 -- we support the hierarchical module name extension:
103 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
107 -- everywhere: skip whitespace and comments
110 -- Everywhere: deal with nested comments. We explicitly rule out
111 -- pragmas, "{-#", so that we don't accidentally treat them as comments.
112 -- (this can happen even though pragmas will normally take precedence due to
113 -- longest-match, because pragmas aren't valid in every state, but comments
115 "{-" / { notFollowedBy '#' } { nested_comment }
117 -- Single-line comments are a bit tricky. Haskell 98 says that two or
118 -- more dashes followed by a symbol should be parsed as a varsym, so we
119 -- have to exclude those.
120 -- The regex says: "munch all the characters after the dashes, as long as
121 -- the first one is not a symbol".
122 "--"\-* [^$symbol :] .* ;
123 "--"\-* / { atEOL } ;
125 -- 'bol' state: beginning of a line. Slurp up all the whitespace (including
126 -- blank lines) until we find a non-whitespace character, then do layout
129 -- One slight wibble here: what if the line begins with {-#? In
130 -- theory, we have to lex the pragma to see if it's one we recognise,
131 -- and if it is, then we backtrack and do_bol, otherwise we treat it
132 -- as a nested comment. We don't bother with this: if the line begins
133 -- with {-#, then we'll assume it's a pragma we know about and go for do_bol.
136 ^\# (line)? { begin line_prag1 }
137 ^\# pragma .* \n ; -- GCC 3.3 CPP generated, apparently
138 ^\# \! .* \n ; -- #!, for scripts
142 -- after a layout keyword (let, where, do, of), we begin a new layout
143 -- context if the curly brace is missing.
144 -- Careful! This stuff is quite delicate.
145 <layout, layout_do> {
146 \{ / { notFollowedBy '-' } { pop_and open_brace }
147 -- we might encounter {-# here, but {- has been handled already
149 ^\# (line)? { begin line_prag1 }
152 -- do is treated in a subtly different way, see new_layout_context
153 <layout> () { new_layout_context True }
154 <layout_do> () { new_layout_context False }
156 -- after a new layout context which was found to be to the left of the
157 -- previous context, we have generated a '{' token, and we now need to
158 -- generate a matching '}' token.
159 <layout_left> () { do_layout_left }
161 <0,option_prags,glaexts> \n { begin bol }
163 "{-#" $whitechar* (line|LINE) { begin line_prag2 }
165 -- single-line line pragmas, of the form
166 -- # <line> "<file>" <extra-stuff> \n
167 <line_prag1> $decdigit+ { setLine line_prag1a }
168 <line_prag1a> \" [$graphic \ ]* \" { setFile line_prag1b }
169 <line_prag1b> .* { pop }
171 -- Haskell-style line pragmas, of the form
172 -- {-# LINE <line> "<file>" #-}
173 <line_prag2> $decdigit+ { setLine line_prag2a }
174 <line_prag2a> \" [$graphic \ ]* \" { setFile line_prag2b }
175 <line_prag2b> "#-}"|"-}" { pop }
176 -- NOTE: accept -} at the end of a LINE pragma, for compatibility
177 -- with older versions of GHC which generated these.
179 -- We only want RULES pragmas to be picked up when -fglasgow-exts
180 -- is on, because the contents of the pragma is always written using
181 -- glasgow-exts syntax (using forall etc.), so if glasgow exts are not
182 -- enabled, we're sure to get a parse error.
183 -- (ToDo: we should really emit a warning when ignoring pragmas)
185 "{-#" $whitechar* (RULES|rules) { token ITrules_prag }
187 <0,option_prags,glaexts> {
188 "{-#" $whitechar* (INLINE|inline) { token (ITinline_prag True) }
189 "{-#" $whitechar* (NO(T?)INLINE|no(t?)inline)
190 { token (ITinline_prag False) }
191 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
192 { token ITspec_prag }
193 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
194 $whitechar* (INLINE|inline) { token (ITspec_inline_prag True) }
195 "{-#" $whitechar* (SPECIALI[SZ]E|speciali[sz]e)
196 $whitechar* (NO(T?)INLINE|no(t?)inline)
197 { token (ITspec_inline_prag False) }
198 "{-#" $whitechar* (SOURCE|source) { token ITsource_prag }
199 "{-#" $whitechar* (DEPRECATED|deprecated)
200 { token ITdeprecated_prag }
201 "{-#" $whitechar* (SCC|scc) { token ITscc_prag }
202 "{-#" $whitechar* (CORE|core) { token ITcore_prag }
203 "{-#" $whitechar* (UNPACK|unpack) { token ITunpack_prag }
205 "{-#" { nested_comment }
207 -- ToDo: should only be valid inside a pragma:
208 "#-}" { token ITclose_prag}
212 "{-#" $whitechar* (OPTIONS|options) { lex_string_prag IToptions_prag }
213 "{-#" $whitechar* (OPTIONS_GHC|options_ghc)
214 { lex_string_prag IToptions_prag }
215 "{-#" $whitechar* (LANGUAGE|language) { token ITlanguage_prag }
216 "{-#" $whitechar* (INCLUDE|include) { lex_string_prag ITinclude_prag }
219 <0,option_prags,glaexts> {
220 -- This is to catch things like {-# OPTIONS OPTIONS_HUGS ...
221 "{-#" $whitechar* $idchar+ { nested_comment }
224 -- '0' state: ordinary lexemes
225 -- 'glaexts' state: glasgow extensions (postfix '#', etc.)
230 "[:" / { ifExtension parrEnabled } { token ITopabrack }
231 ":]" / { ifExtension parrEnabled } { token ITcpabrack }
235 "[|" / { ifExtension thEnabled } { token ITopenExpQuote }
236 "[e|" / { ifExtension thEnabled } { token ITopenExpQuote }
237 "[p|" / { ifExtension thEnabled } { token ITopenPatQuote }
238 "[d|" / { ifExtension thEnabled } { layout_token ITopenDecQuote }
239 "[t|" / { ifExtension thEnabled } { token ITopenTypQuote }
240 "|]" / { ifExtension thEnabled } { token ITcloseQuote }
241 \$ @varid / { ifExtension thEnabled } { skip_one_varid ITidEscape }
242 "$(" / { ifExtension thEnabled } { token ITparenEscape }
246 "(|" / { ifExtension arrowsEnabled `alexAndPred` notFollowedBySymbol }
247 { special IToparenbar }
248 "|)" / { ifExtension arrowsEnabled } { special ITcparenbar }
252 \? @varid / { ifExtension ipEnabled } { skip_one_varid ITdupipvarid }
256 "(#" / { notFollowedBySymbol } { token IToubxparen }
257 "#)" { token ITcubxparen }
258 "{|" { token ITocurlybar }
259 "|}" { token ITccurlybar }
262 <0,option_prags,glaexts> {
263 \( { special IToparen }
264 \) { special ITcparen }
265 \[ { special ITobrack }
266 \] { special ITcbrack }
267 \, { special ITcomma }
268 \; { special ITsemi }
269 \` { special ITbackquote }
275 <0,option_prags,glaexts> {
276 @qual @varid { check_qvarid }
277 @qual @conid { idtoken qconid }
279 @conid { idtoken conid }
282 -- after an illegal qvarid, such as 'M.let',
283 -- we back up and try again in the bad_qvarid state:
285 @conid { pop_and (idtoken conid) }
286 @qual @conid { pop_and (idtoken qconid) }
290 @qual @varid "#"+ { idtoken qvarid }
291 @qual @conid "#"+ { idtoken qconid }
292 @varid "#"+ { varid }
293 @conid "#"+ { idtoken conid }
299 @qual @varsym { idtoken qvarsym }
300 @qual @consym { idtoken qconsym }
306 @decimal { tok_decimal }
307 0[oO] @octal { tok_octal }
308 0[xX] @hexadecimal { tok_hexadecimal }
312 @decimal \# { prim_decimal }
313 0[oO] @octal \# { prim_octal }
314 0[xX] @hexadecimal \# { prim_hexadecimal }
317 <0,glaexts> @floating_point { strtoken tok_float }
318 <glaexts> @floating_point \# { init_strtoken 1 prim_float }
319 <glaexts> @floating_point \# \# { init_strtoken 2 prim_double }
321 -- Strings and chars are lexed by hand-written code. The reason is
322 -- that even if we recognise the string or char here in the regex
323 -- lexer, we would still have to parse the string afterward in order
324 -- to convert it to a String.
327 \" { lex_string_tok }
331 -- work around bug in Alex 2.0
332 #if __GLASGOW_HASKELL__ < 503
333 unsafeAt arr i = arr ! i
336 -- -----------------------------------------------------------------------------
340 = ITas -- Haskell keywords
365 | ITscc -- ToDo: remove (we use {-# SCC "..." #-} now)
367 | ITforall -- GHC extension keywords
383 | ITinline_prag Bool -- True <=> INLINE, False <=> NOINLINE
384 | ITspec_prag -- SPECIALISE
385 | ITspec_inline_prag Bool -- SPECIALISE INLINE (or NOINLINE)
391 | ITcore_prag -- hdaume: core annotations
394 | IToptions_prag String
395 | ITinclude_prag String
398 | ITdotdot -- reserved symbols
414 | ITbiglam -- GHC-extension symbols
416 | ITocurly -- special symbols
418 | ITocurlybar -- {|, for type applications
419 | ITccurlybar -- |}, for type applications
423 | ITopabrack -- [:, for parallel arrays with -fparr
424 | ITcpabrack -- :], for parallel arrays with -fparr
435 | ITvarid FastString -- identifiers
437 | ITvarsym FastString
438 | ITconsym FastString
439 | ITqvarid (FastString,FastString)
440 | ITqconid (FastString,FastString)
441 | ITqvarsym (FastString,FastString)
442 | ITqconsym (FastString,FastString)
444 | ITdupipvarid FastString -- GHC extension: implicit param: ?x
446 | ITpragma StringBuffer
449 | ITstring FastString
451 | ITrational Rational
454 | ITprimstring FastString
456 | ITprimfloat Rational
457 | ITprimdouble Rational
459 -- MetaHaskell extension tokens
460 | ITopenExpQuote -- [| or [e|
461 | ITopenPatQuote -- [p|
462 | ITopenDecQuote -- [d|
463 | ITopenTypQuote -- [t|
465 | ITidEscape FastString -- $x
466 | ITparenEscape -- $(
470 -- Arrow notation extension
477 | ITLarrowtail -- -<<
478 | ITRarrowtail -- >>-
480 | ITunknown String -- Used when the lexer can't make sense of it
481 | ITeof -- end of file token
483 deriving Show -- debugging
486 isSpecial :: Token -> Bool
487 -- If we see M.x, where x is a keyword, but
488 -- is special, we treat is as just plain M.x,
490 isSpecial ITas = True
491 isSpecial IThiding = True
492 isSpecial ITfor = True
493 isSpecial ITqualified = True
494 isSpecial ITforall = True
495 isSpecial ITexport = True
496 isSpecial ITlabel = True
497 isSpecial ITdynamic = True
498 isSpecial ITsafe = True
499 isSpecial ITthreadsafe = True
500 isSpecial ITunsafe = True
501 isSpecial ITccallconv = True
502 isSpecial ITstdcallconv = True
503 isSpecial ITmdo = True
504 isSpecial ITiso = True
505 isSpecial ITfamily = True
508 -- the bitmap provided as the third component indicates whether the
509 -- corresponding extension keyword is valid under the extension options
510 -- provided to the compiler; if the extension corresponding to *any* of the
511 -- bits set in the bitmap is enabled, the keyword is valid (this setup
512 -- facilitates using a keyword in two different extensions that can be
513 -- activated independently)
515 reservedWordsFM = listToUFM $
516 map (\(x, y, z) -> (mkFastString x, (y, z)))
517 [( "_", ITunderscore, 0 ),
519 ( "case", ITcase, 0 ),
520 ( "class", ITclass, 0 ),
521 ( "data", ITdata, 0 ),
522 ( "default", ITdefault, 0 ),
523 ( "deriving", ITderiving, 0 ),
525 ( "else", ITelse, 0 ),
527 ( "hiding", IThiding, 0 ),
529 ( "import", ITimport, 0 ),
531 ( "infix", ITinfix, 0 ),
532 ( "infixl", ITinfixl, 0 ),
533 ( "infixr", ITinfixr, 0 ),
534 ( "instance", ITinstance, 0 ),
536 ( "module", ITmodule, 0 ),
537 ( "newtype", ITnewtype, 0 ),
539 ( "qualified", ITqualified, 0 ),
540 ( "then", ITthen, 0 ),
541 ( "type", ITtype, 0 ),
542 ( "where", ITwhere, 0 ),
543 ( "_scc_", ITscc, 0 ), -- ToDo: remove
545 ( "forall", ITforall, bit tvBit),
546 ( "mdo", ITmdo, bit glaExtsBit),
547 ( "family", ITfamily, bit idxTysBit),
549 ( "foreign", ITforeign, bit ffiBit),
550 ( "export", ITexport, bit ffiBit),
551 ( "label", ITlabel, bit ffiBit),
552 ( "dynamic", ITdynamic, bit ffiBit),
553 ( "safe", ITsafe, bit ffiBit),
554 ( "threadsafe", ITthreadsafe, bit ffiBit),
555 ( "unsafe", ITunsafe, bit ffiBit),
556 ( "stdcall", ITstdcallconv, bit ffiBit),
557 ( "ccall", ITccallconv, bit ffiBit),
558 ( "dotnet", ITdotnet, bit ffiBit),
560 ( "rec", ITrec, bit arrowsBit),
561 ( "proc", ITproc, bit arrowsBit)
564 reservedSymsFM = listToUFM $
565 map (\ (x,y,z) -> (mkFastString x,(y,z)))
566 [ ("..", ITdotdot, 0)
567 ,(":", ITcolon, 0) -- (:) is a reserved op,
568 -- meaning only list cons
581 ,("*", ITstar, bit glaExtsBit .|.
582 bit idxTysBit) -- For data T (a::*) = MkT
583 ,(".", ITdot, bit tvBit) -- For 'forall a . t'
585 ,("-<", ITlarrowtail, bit arrowsBit)
586 ,(">-", ITrarrowtail, bit arrowsBit)
587 ,("-<<", ITLarrowtail, bit arrowsBit)
588 ,(">>-", ITRarrowtail, bit arrowsBit)
590 #if __GLASGOW_HASKELL__ >= 605
591 ,("λ", ITlam, bit glaExtsBit)
592 ,("∷", ITdcolon, bit glaExtsBit)
593 ,("⇒", ITdarrow, bit glaExtsBit)
594 ,("∀", ITforall, bit glaExtsBit)
595 ,("→", ITrarrow, bit glaExtsBit)
596 ,("←", ITlarrow, bit glaExtsBit)
597 ,("⋯", ITdotdot, bit glaExtsBit)
598 -- ToDo: ideally, → and ∷ should be "specials", so that they cannot
599 -- form part of a large operator. This would let us have a better
600 -- syntax for kinds: ɑ∷*→* would be a legal kind signature. (maybe).
604 -- -----------------------------------------------------------------------------
607 type Action = SrcSpan -> StringBuffer -> Int -> P (Located Token)
609 special :: Token -> Action
610 special tok span _buf len = return (L span tok)
612 token, layout_token :: Token -> Action
613 token t span buf len = return (L span t)
614 layout_token t span buf len = pushLexState layout >> return (L span t)
616 idtoken :: (StringBuffer -> Int -> Token) -> Action
617 idtoken f span buf len = return (L span $! (f buf len))
619 skip_one_varid :: (FastString -> Token) -> Action
620 skip_one_varid f span buf len
621 = return (L span $! f (lexemeToFastString (stepOn buf) (len-1)))
623 strtoken :: (String -> Token) -> Action
624 strtoken f span buf len =
625 return (L span $! (f $! lexemeToString buf len))
627 init_strtoken :: Int -> (String -> Token) -> Action
628 -- like strtoken, but drops the last N character(s)
629 init_strtoken drop f span buf len =
630 return (L span $! (f $! lexemeToString buf (len-drop)))
632 begin :: Int -> Action
633 begin code _span _str _len = do pushLexState code; lexToken
636 pop _span _buf _len = do popLexState; lexToken
638 pop_and :: Action -> Action
639 pop_and act span buf len = do popLexState; act span buf len
641 notFollowedBy char _ _ _ (AI _ _ buf) = atEnd buf || currentChar buf /= char
643 notFollowedBySymbol _ _ _ (AI _ _ buf)
644 = atEnd buf || currentChar buf `notElem` "!#$%&*+./<=>?@\\^|-~"
646 atEOL _ _ _ (AI _ _ buf) = atEnd buf || currentChar buf == '\n'
648 ifExtension pred bits _ _ _ = pred bits
651 nested comments require traversing by hand, they can't be parsed
652 using regular expressions.
654 nested_comment :: Action
655 nested_comment span _str _len = do
658 where go 0 input = do setInput input; lexToken
660 case alexGetChar input of
665 case alexGetChar input of
667 Just ('\125',input) -> go (n-1) input
668 Just (c,_) -> go n input
670 case alexGetChar input of
672 Just ('-',input') -> go (n+1) input'
673 Just (c,input) -> go n input
676 err (AI end _ _) = failLocMsgP (srcSpanStart span) end "unterminated `{-'"
678 open_brace, close_brace :: Action
679 open_brace span _str _len = do
681 setContext (NoLayout:ctx)
682 return (L span ITocurly)
683 close_brace span _str _len = do
685 return (L span ITccurly)
687 -- We have to be careful not to count M.<varid> as a qualified name
688 -- when <varid> is a keyword. We hack around this by catching
689 -- the offending tokens afterward, and re-lexing in a different state.
690 check_qvarid span buf len = do
691 case lookupUFM reservedWordsFM var of
693 | not (isSpecial keyword) ->
697 b <- extension (\i -> exts .&. i /= 0)
700 _other -> return token
702 (mod,var) = splitQualName buf len
703 token = L span (ITqvarid (mod,var))
706 (AI _ offs _) <- getInput
707 setInput (AI (srcSpanStart span) (offs-len) buf)
708 pushLexState bad_qvarid
711 qvarid buf len = ITqvarid $! splitQualName buf len
712 qconid buf len = ITqconid $! splitQualName buf len
714 splitQualName :: StringBuffer -> Int -> (FastString,FastString)
715 -- takes a StringBuffer and a length, and returns the module name
716 -- and identifier parts of a qualified name. Splits at the *last* dot,
717 -- because of hierarchical module names.
718 splitQualName orig_buf len = split orig_buf orig_buf
721 | orig_buf `byteDiff` buf >= len = done dot_buf
722 | c == '.' = found_dot buf'
723 | otherwise = split buf' dot_buf
725 (c,buf') = nextChar buf
727 -- careful, we might get names like M....
728 -- so, if the character after the dot is not upper-case, this is
729 -- the end of the qualifier part.
730 found_dot buf -- buf points after the '.'
731 | isUpper c = split buf' buf
732 | otherwise = done buf
734 (c,buf') = nextChar buf
737 (lexemeToFastString orig_buf (qual_size - 1),
738 lexemeToFastString dot_buf (len - qual_size))
740 qual_size = orig_buf `byteDiff` dot_buf
743 case lookupUFM reservedWordsFM fs of
744 Just (keyword,0) -> do
746 return (L span keyword)
747 Just (keyword,exts) -> do
748 b <- extension (\i -> exts .&. i /= 0)
749 if b then do maybe_layout keyword
750 return (L span keyword)
751 else return (L span (ITvarid fs))
752 _other -> return (L span (ITvarid fs))
754 fs = lexemeToFastString buf len
756 conid buf len = ITconid fs
757 where fs = lexemeToFastString buf len
759 qvarsym buf len = ITqvarsym $! splitQualName buf len
760 qconsym buf len = ITqconsym $! splitQualName buf len
762 varsym = sym ITvarsym
763 consym = sym ITconsym
765 sym con span buf len =
766 case lookupUFM reservedSymsFM fs of
767 Just (keyword,0) -> return (L span keyword)
768 Just (keyword,exts) -> do
769 b <- extension (\i -> exts .&. i /= 0)
770 if b then return (L span keyword)
771 else return (L span $! con fs)
772 _other -> return (L span $! con fs)
774 fs = lexemeToFastString buf len
776 tok_decimal span buf len
777 = return (L span (ITinteger $! parseInteger buf len 10 octDecDigit))
779 tok_octal span buf len
780 = return (L span (ITinteger $! parseInteger (offsetBytes 2 buf) (len-2) 8 octDecDigit))
782 tok_hexadecimal span buf len
783 = return (L span (ITinteger $! parseInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
785 prim_decimal span buf len
786 = return (L span (ITprimint $! parseInteger buf (len-1) 10 octDecDigit))
788 prim_octal span buf len
789 = return (L span (ITprimint $! parseInteger (offsetBytes 2 buf) (len-3) 8 octDecDigit))
791 prim_hexadecimal span buf len
792 = return (L span (ITprimint $! parseInteger (offsetBytes 2 buf) (len-3) 16 hexDigit))
794 tok_float str = ITrational $! readRational str
795 prim_float str = ITprimfloat $! readRational str
796 prim_double str = ITprimdouble $! readRational str
798 -- -----------------------------------------------------------------------------
801 -- we're at the first token on a line, insert layout tokens if necessary
803 do_bol span _str _len = do
807 --trace "layout: inserting '}'" $ do
809 -- do NOT pop the lex state, we might have a ';' to insert
810 return (L span ITvccurly)
812 --trace "layout: inserting ';'" $ do
814 return (L span ITsemi)
819 -- certain keywords put us in the "layout" state, where we might
820 -- add an opening curly brace.
821 maybe_layout ITdo = pushLexState layout_do
822 maybe_layout ITmdo = pushLexState layout_do
823 maybe_layout ITof = pushLexState layout
824 maybe_layout ITlet = pushLexState layout
825 maybe_layout ITwhere = pushLexState layout
826 maybe_layout ITrec = pushLexState layout
827 maybe_layout _ = return ()
829 -- Pushing a new implicit layout context. If the indentation of the
830 -- next token is not greater than the previous layout context, then
831 -- Haskell 98 says that the new layout context should be empty; that is
832 -- the lexer must generate {}.
834 -- We are slightly more lenient than this: when the new context is started
835 -- by a 'do', then we allow the new context to be at the same indentation as
836 -- the previous context. This is what the 'strict' argument is for.
838 new_layout_context strict span _buf _len = do
840 (AI _ offset _) <- getInput
843 Layout prev_off : _ |
844 (strict && prev_off >= offset ||
845 not strict && prev_off > offset) -> do
846 -- token is indented to the left of the previous context.
847 -- we must generate a {} sequence now.
848 pushLexState layout_left
849 return (L span ITvocurly)
851 setContext (Layout offset : ctx)
852 return (L span ITvocurly)
854 do_layout_left span _buf _len = do
856 pushLexState bol -- we must be at the start of a line
857 return (L span ITvccurly)
859 -- -----------------------------------------------------------------------------
862 setLine :: Int -> Action
863 setLine code span buf len = do
864 let line = parseInteger buf len 10 octDecDigit
865 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
866 -- subtract one: the line number refers to the *following* line
871 setFile :: Int -> Action
872 setFile code span buf len = do
873 let file = lexemeToFastString (stepOn buf) (len-2)
874 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
880 -- -----------------------------------------------------------------------------
881 -- Options, includes and language pragmas.
883 lex_string_prag :: (String -> Token) -> Action
884 lex_string_prag mkTok span buf len
885 = do input <- getInput
889 return (L (mkSrcSpan start end) tok)
891 = if isString input "#-}"
892 then do setInput input
893 return (mkTok (reverse acc))
894 else case alexGetChar input of
895 Just (c,i) -> go (c:acc) i
899 = case alexGetChar i of
900 Just (c,i') | c == x -> isString i' xs
902 err (AI end _ _) = failLocMsgP (srcSpanStart span) end "unterminated options pragma"
905 -- -----------------------------------------------------------------------------
908 -- This stuff is horrible. I hates it.
910 lex_string_tok :: Action
911 lex_string_tok span buf len = do
914 return (L (mkSrcSpan (srcSpanStart span) end) tok)
916 lex_string :: String -> P Token
919 case alexGetChar' i of
924 glaexts <- extension glaExtsEnabled
928 case alexGetChar' i of
932 then failMsgP "primitive string literal must contain only characters <= \'\\xFF\'"
933 else let s' = mkZFastString (reverse s) in
934 return (ITprimstring s')
935 -- mkZFastString is a hack to avoid encoding the
936 -- string in UTF-8. We just want the exact bytes.
938 return (ITstring (mkFastString (reverse s)))
940 return (ITstring (mkFastString (reverse s)))
943 | Just ('&',i) <- next -> do
944 setInput i; lex_string s
945 | Just (c,i) <- next, is_space c -> do
946 setInput i; lex_stringgap s
947 where next = alexGetChar' i
957 c | is_space c -> lex_stringgap s
961 lex_char_tok :: Action
962 -- Here we are basically parsing character literals, such as 'x' or '\n'
963 -- but, when Template Haskell is on, we additionally spot
964 -- 'x and ''T, returning ITvarQuote and ITtyQuote respectively,
965 -- but WIHTOUT CONSUMING the x or T part (the parser does that).
966 -- So we have to do two characters of lookahead: when we see 'x we need to
967 -- see if there's a trailing quote
968 lex_char_tok span buf len = do -- We've seen '
969 i1 <- getInput -- Look ahead to first character
970 let loc = srcSpanStart span
971 case alexGetChar' i1 of
974 Just ('\'', i2@(AI end2 _ _)) -> do -- We've seen ''
975 th_exts <- extension thEnabled
978 return (L (mkSrcSpan loc end2) ITtyQuote)
981 Just ('\\', i2@(AI end2 _ _)) -> do -- We've seen 'backslash
984 mc <- getCharOrFail -- Trailing quote
985 if mc == '\'' then finish_char_tok loc lit_ch
986 else do setInput i2; lit_error
988 Just (c, i2@(AI end2 _ _))
989 | not (isAny c) -> lit_error
992 -- We've seen 'x, where x is a valid character
993 -- (i.e. not newline etc) but not a quote or backslash
994 case alexGetChar' i2 of -- Look ahead one more character
996 Just ('\'', i3) -> do -- We've seen 'x'
998 finish_char_tok loc c
999 _other -> do -- We've seen 'x not followed by quote
1000 -- If TH is on, just parse the quote only
1001 th_exts <- extension thEnabled
1002 let (AI end _ _) = i1
1003 if th_exts then return (L (mkSrcSpan loc end) ITvarQuote)
1004 else do setInput i2; lit_error
1006 finish_char_tok :: SrcLoc -> Char -> P (Located Token)
1007 finish_char_tok loc ch -- We've already seen the closing quote
1008 -- Just need to check for trailing #
1009 = do glaexts <- extension glaExtsEnabled
1010 i@(AI end _ _) <- getInput
1012 case alexGetChar' i of
1013 Just ('#',i@(AI end _ _)) -> do
1015 return (L (mkSrcSpan loc end) (ITprimchar ch))
1017 return (L (mkSrcSpan loc end) (ITchar ch))
1019 return (L (mkSrcSpan loc end) (ITchar ch))
1021 lex_char :: Char -> AlexInput -> P Char
1024 '\\' -> do setInput inp; lex_escape
1025 c | isAny c -> do setInput inp; return c
1028 isAny c | c > '\xff' = isPrint c
1029 | otherwise = is_any c
1031 lex_escape :: P Char
1045 '^' -> do c <- getCharOrFail
1046 if c >= '@' && c <= '_'
1047 then return (chr (ord c - ord '@'))
1050 'x' -> readNum is_hexdigit 16 hexDigit
1051 'o' -> readNum is_octdigit 8 octDecDigit
1052 x | is_digit x -> readNum2 is_digit 10 octDecDigit (octDecDigit x)
1056 case alexGetChar' i of
1057 Nothing -> lit_error
1059 case alexGetChar' i2 of
1060 Nothing -> do setInput i2; lit_error
1062 let str = [c1,c2,c3] in
1063 case [ (c,rest) | (p,c) <- silly_escape_chars,
1064 Just rest <- [maybePrefixMatch p str] ] of
1065 (escape_char,[]):_ -> do
1068 (escape_char,_:_):_ -> do
1073 readNum :: (Char -> Bool) -> Int -> (Char -> Int) -> P Char
1074 readNum is_digit base conv = do
1078 then readNum2 is_digit base conv (conv c)
1079 else do setInput i; lit_error
1081 readNum2 is_digit base conv i = do
1084 where read i input = do
1085 case alexGetChar' input of
1086 Just (c,input') | is_digit c -> do
1087 read (i*base + conv c) input'
1089 if i >= 0 && i <= 0x10FFFF
1090 then do setInput input; return (chr i)
1093 silly_escape_chars = [
1130 -- before calling lit_error, ensure that the current input is pointing to
1131 -- the position of the error in the buffer. This is so that we can report
1132 -- a correct location to the user, but also so we can detect UTF-8 decoding
1133 -- errors if they occur.
1134 lit_error = lexError "lexical error in string/character literal"
1136 getCharOrFail :: P Char
1139 case alexGetChar' i of
1140 Nothing -> lexError "unexpected end-of-file in string/character literal"
1141 Just (c,i) -> do setInput i; return c
1143 -- -----------------------------------------------------------------------------
1153 SrcSpan -- The start and end of the text span related to
1154 -- the error. Might be used in environments which can
1155 -- show this span, e.g. by highlighting it.
1156 Message -- The error message
1158 data PState = PState {
1159 buffer :: StringBuffer,
1160 last_loc :: SrcSpan, -- pos of previous token
1161 last_offs :: !Int, -- offset of the previous token from the
1162 -- beginning of the current line.
1163 -- \t is equal to 8 spaces.
1164 last_len :: !Int, -- len of previous token
1165 loc :: SrcLoc, -- current loc (end of prev token + 1)
1166 extsBitmap :: !Int, -- bitmap that determines permitted extensions
1167 context :: [LayoutContext],
1170 -- last_loc and last_len are used when generating error messages,
1171 -- and in pushCurrentContext only. Sigh, if only Happy passed the
1172 -- current token to happyError, we could at least get rid of last_len.
1173 -- Getting rid of last_loc would require finding another way to
1174 -- implement pushCurrentContext (which is only called from one place).
1176 newtype P a = P { unP :: PState -> ParseResult a }
1178 instance Monad P where
1184 returnP a = P $ \s -> POk s a
1186 thenP :: P a -> (a -> P b) -> P b
1187 (P m) `thenP` k = P $ \ s ->
1189 POk s1 a -> (unP (k a)) s1
1190 PFailed span err -> PFailed span err
1192 failP :: String -> P a
1193 failP msg = P $ \s -> PFailed (last_loc s) (text msg)
1195 failMsgP :: String -> P a
1196 failMsgP msg = P $ \s -> PFailed (last_loc s) (text msg)
1198 failLocMsgP :: SrcLoc -> SrcLoc -> String -> P a
1199 failLocMsgP loc1 loc2 str = P $ \s -> PFailed (mkSrcSpan loc1 loc2) (text str)
1201 failSpanMsgP :: SrcSpan -> String -> P a
1202 failSpanMsgP span msg = P $ \s -> PFailed span (text msg)
1204 extension :: (Int -> Bool) -> P Bool
1205 extension p = P $ \s -> POk s (p $! extsBitmap s)
1208 getExts = P $ \s -> POk s (extsBitmap s)
1210 setSrcLoc :: SrcLoc -> P ()
1211 setSrcLoc new_loc = P $ \s -> POk s{loc=new_loc} ()
1213 getSrcLoc :: P SrcLoc
1214 getSrcLoc = P $ \s@(PState{ loc=loc }) -> POk s loc
1216 setLastToken :: SrcSpan -> Int -> P ()
1217 setLastToken loc len = P $ \s -> POk s{ last_loc=loc, last_len=len } ()
1219 data AlexInput = AI SrcLoc {-#UNPACK#-}!Int StringBuffer
1221 alexInputPrevChar :: AlexInput -> Char
1222 alexInputPrevChar (AI _ _ buf) = prevChar buf '\n'
1224 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
1225 alexGetChar (AI loc ofs s)
1227 | otherwise = adj_c `seq` loc' `seq` ofs' `seq` s' `seq`
1228 --trace (show (ord c)) $
1229 Just (adj_c, (AI loc' ofs' s'))
1230 where (c,s') = nextChar s
1231 loc' = advanceSrcLoc loc c
1232 ofs' = advanceOffs c ofs
1240 other_graphic = '\x6'
1243 | c <= '\x06' = non_graphic
1246 case generalCategory c of
1247 UppercaseLetter -> upper
1248 LowercaseLetter -> lower
1249 TitlecaseLetter -> upper
1250 ModifierLetter -> other_graphic
1251 OtherLetter -> other_graphic
1252 NonSpacingMark -> other_graphic
1253 SpacingCombiningMark -> other_graphic
1254 EnclosingMark -> other_graphic
1255 DecimalNumber -> digit
1256 LetterNumber -> other_graphic
1257 OtherNumber -> other_graphic
1258 ConnectorPunctuation -> other_graphic
1259 DashPunctuation -> other_graphic
1260 OpenPunctuation -> other_graphic
1261 ClosePunctuation -> other_graphic
1262 InitialQuote -> other_graphic
1263 FinalQuote -> other_graphic
1264 OtherPunctuation -> other_graphic
1265 MathSymbol -> symbol
1266 CurrencySymbol -> symbol
1267 ModifierSymbol -> symbol
1268 OtherSymbol -> symbol
1270 _other -> non_graphic
1272 -- This version does not squash unicode characters, it is used when
1274 alexGetChar' :: AlexInput -> Maybe (Char,AlexInput)
1275 alexGetChar' (AI loc ofs s)
1277 | otherwise = c `seq` loc' `seq` ofs' `seq` s' `seq`
1278 --trace (show (ord c)) $
1279 Just (c, (AI loc' ofs' s'))
1280 where (c,s') = nextChar s
1281 loc' = advanceSrcLoc loc c
1282 ofs' = advanceOffs c ofs
1284 advanceOffs :: Char -> Int -> Int
1285 advanceOffs '\n' offs = 0
1286 advanceOffs '\t' offs = (offs `quot` 8 + 1) * 8
1287 advanceOffs _ offs = offs + 1
1289 getInput :: P AlexInput
1290 getInput = P $ \s@PState{ loc=l, last_offs=o, buffer=b } -> POk s (AI l o b)
1292 setInput :: AlexInput -> P ()
1293 setInput (AI l o b) = P $ \s -> POk s{ loc=l, last_offs=o, buffer=b } ()
1295 pushLexState :: Int -> P ()
1296 pushLexState ls = P $ \s@PState{ lex_state=l } -> POk s{lex_state=ls:l} ()
1298 popLexState :: P Int
1299 popLexState = P $ \s@PState{ lex_state=ls:l } -> POk s{ lex_state=l } ls
1301 getLexState :: P Int
1302 getLexState = P $ \s@PState{ lex_state=ls:l } -> POk s ls
1304 -- for reasons of efficiency, flags indicating language extensions (eg,
1305 -- -fglasgow-exts or -fparr) are represented by a bitmap stored in an unboxed
1308 glaExtsBit, ffiBit, parrBit :: Int
1315 tvBit = 7 -- Scoped type variables enables 'forall' keyword
1316 bangPatBit = 8 -- Tells the parser to understand bang-patterns
1317 -- (doesn't affect the lexer)
1318 idxTysBit = 9 -- indexed type families: 'family' keyword and kind sigs
1320 glaExtsEnabled, ffiEnabled, parrEnabled :: Int -> Bool
1321 glaExtsEnabled flags = testBit flags glaExtsBit
1322 ffiEnabled flags = testBit flags ffiBit
1323 parrEnabled flags = testBit flags parrBit
1324 arrowsEnabled flags = testBit flags arrowsBit
1325 thEnabled flags = testBit flags thBit
1326 ipEnabled flags = testBit flags ipBit
1327 tvEnabled flags = testBit flags tvBit
1328 bangPatEnabled flags = testBit flags bangPatBit
1329 idxTysEnabled flags = testBit flags idxTysBit
1331 -- PState for parsing options pragmas
1333 pragState :: StringBuffer -> SrcLoc -> PState
1337 last_loc = mkSrcSpan loc loc,
1343 lex_state = [bol, option_prags, 0]
1347 -- create a parse state
1349 mkPState :: StringBuffer -> SrcLoc -> DynFlags -> PState
1350 mkPState buf loc flags =
1353 last_loc = mkSrcSpan loc loc,
1357 extsBitmap = fromIntegral bitmap,
1359 lex_state = [bol, if glaExtsEnabled bitmap then glaexts else 0]
1360 -- we begin in the layout state if toplev_layout is set
1363 bitmap = glaExtsBit `setBitIf` dopt Opt_GlasgowExts flags
1364 .|. ffiBit `setBitIf` dopt Opt_FFI flags
1365 .|. parrBit `setBitIf` dopt Opt_PArr flags
1366 .|. arrowsBit `setBitIf` dopt Opt_Arrows flags
1367 .|. thBit `setBitIf` dopt Opt_TH flags
1368 .|. ipBit `setBitIf` dopt Opt_ImplicitParams flags
1369 .|. tvBit `setBitIf` dopt Opt_ScopedTypeVariables flags
1370 .|. bangPatBit `setBitIf` dopt Opt_BangPatterns flags
1371 .|. idxTysBit `setBitIf` dopt Opt_IndexedTypes flags
1373 setBitIf :: Int -> Bool -> Int
1374 b `setBitIf` cond | cond = bit b
1377 getContext :: P [LayoutContext]
1378 getContext = P $ \s@PState{context=ctx} -> POk s ctx
1380 setContext :: [LayoutContext] -> P ()
1381 setContext ctx = P $ \s -> POk s{context=ctx} ()
1384 popContext = P $ \ s@(PState{ buffer = buf, context = ctx,
1385 loc = loc, last_len = len, last_loc = last_loc }) ->
1387 (_:tl) -> POk s{ context = tl } ()
1388 [] -> PFailed last_loc (srcParseErr buf len)
1390 -- Push a new layout context at the indentation of the last token read.
1391 -- This is only used at the outer level of a module when the 'module'
1392 -- keyword is missing.
1393 pushCurrentContext :: P ()
1394 pushCurrentContext = P $ \ s@PState{ last_offs=offs, last_len=len, context=ctx } ->
1395 POk s{context = Layout (offs-len) : ctx} ()
1397 getOffside :: P Ordering
1398 getOffside = P $ \s@PState{last_offs=offs, context=stk} ->
1399 let ord = case stk of
1400 (Layout n:_) -> compare offs n
1404 -- ---------------------------------------------------------------------------
1405 -- Construct a parse error
1408 :: StringBuffer -- current buffer (placed just after the last token)
1409 -> Int -- length of the previous token
1412 = hcat [ if null token
1413 then ptext SLIT("parse error (possibly incorrect indentation)")
1414 else hcat [ptext SLIT("parse error on input "),
1415 char '`', text token, char '\'']
1417 where token = lexemeToString (offsetBytes (-len) buf) len
1419 -- Report a parse failure, giving the span of the previous token as
1420 -- the location of the error. This is the entry point for errors
1421 -- detected during parsing.
1423 srcParseFail = P $ \PState{ buffer = buf, last_len = len,
1424 last_loc = last_loc } ->
1425 PFailed last_loc (srcParseErr buf len)
1427 -- A lexical error is reported at a particular position in the source file,
1428 -- not over a token range.
1429 lexError :: String -> P a
1432 i@(AI end _ buf) <- getInput
1433 reportLexError loc end buf str
1435 -- -----------------------------------------------------------------------------
1436 -- This is the top-level function: called from the parser each time a
1437 -- new token is to be read from the input.
1439 lexer :: (Located Token -> P a) -> P a
1441 tok@(L _ tok__) <- lexToken
1442 --trace ("token: " ++ show tok__) $ do
1445 lexToken :: P (Located Token)
1447 inp@(AI loc1 _ buf) <- getInput
1450 case alexScanUser exts inp sc of
1451 AlexEOF -> do let span = mkSrcSpan loc1 loc1
1453 return (L span ITeof)
1454 AlexError (AI loc2 _ buf) -> do
1455 reportLexError loc1 loc2 buf "lexical error"
1456 AlexSkip inp2 _ -> do
1459 AlexToken inp2@(AI end _ buf2) len t -> do
1461 let span = mkSrcSpan loc1 end
1462 let bytes = byteDiff buf buf2
1463 span `seq` setLastToken span bytes
1466 reportLexError loc1 loc2 buf str
1467 | atEnd buf = failLocMsgP loc1 loc2 (str ++ " at end of input")
1470 c = fst (nextChar buf)
1472 if c == '\0' -- decoding errors are mapped to '\0', see utf8DecodeChar#
1473 then failLocMsgP loc2 loc2 (str ++ " (UTF-8 decoding error)")
1474 else failLocMsgP loc1 loc2 (str ++ " at character " ++ show c)