X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fparser%2FLexer.x;h=b2f08f205281035399108dab9ff861c02dabbed1;hb=e97c8fa852931d189ef5ae295aca09fb80a479e0;hp=6a25ae57b2aca19d77d06c70510b302134318159;hpb=046ee54f048ddd721dcee41916d6a6f68db3b15b;p=ghc-hetmet.git diff --git a/compiler/parser/Lexer.x b/compiler/parser/Lexer.x index 6a25ae5..b2f08f2 100644 --- a/compiler/parser/Lexer.x +++ b/compiler/parser/Lexer.x @@ -25,6 +25,7 @@ module Lexer ( Token(..), lexer, pragState, mkPState, PState(..), P(..), ParseResult(..), getSrcLoc, failLocMsgP, failSpanMsgP, srcParseFail, + getMessages, popContext, pushCurrentContext, setLastToken, setSrcLoc, getLexState, popLexState, pushLexState, extension, glaExtsEnabled, bangPatEnabled @@ -32,7 +33,8 @@ module Lexer ( #include "HsVersions.h" -import ErrUtils ( Message ) +import Bag +import ErrUtils import Outputable import StringBuffer import FastString @@ -43,6 +45,7 @@ import DynFlags import Ctype import Util ( maybePrefixMatch, readRational ) +import Control.Monad import Data.Bits import Data.Char ( chr, isSpace ) import Data.Ratio @@ -56,8 +59,9 @@ import Compat.Unicode ( GeneralCategory(..), generalCategory, isPrint, isUpper ) } $unispace = \x05 -$whitechar = [\ \t\n\r\f\v\xa0 $unispace] +$whitechar = [\ \n\r\f\v\xa0 $unispace] $white_no_nl = $whitechar # \n +$tab = \t $ascdigit = 0-9 $unidigit = \x03 @@ -108,6 +112,7 @@ haskell :- -- everywhere: skip whitespace and comments $white_no_nl+ ; +$tab+ { warn Opt_WarnTabs (text "Tab character") } -- Everywhere: deal with nested comments. We explicitly rule out -- pragmas, "{-#", so that we don't accidentally treat them as comments. @@ -233,6 +238,8 @@ $white_no_nl+ ; "{-#" $whitechar* (DEPRECATED|deprecated) { token ITdeprecated_prag } "{-#" $whitechar* (SCC|scc) { token ITscc_prag } + "{-#" $whitechar* (GENERATED|generated) + { token ITgenerated_prag } "{-#" $whitechar* (CORE|core) { token ITcore_prag } "{-#" $whitechar* (UNPACK|unpack) { token ITunpack_prag } @@ -264,8 +271,8 @@ $white_no_nl+ ; -- Haddock comments <0,glaexts> { - "-- " / $docsym { multiline_doc_comment } - "{-" \ ? / $docsym { nested_doc_comment } + "-- " $docsym / { ifExtension haddockEnabled } { multiline_doc_comment } + "{-" \ ? $docsym / { ifExtension haddockEnabled } { nested_doc_comment } } -- "special" symbols @@ -372,11 +379,6 @@ $white_no_nl+ ; } { --- work around bug in Alex 2.0 -#if __GLASGOW_HASKELL__ < 503 -unsafeAt arr i = arr ! i -#endif - -- ----------------------------------------------------------------------------- -- The token type @@ -387,9 +389,9 @@ data Token | ITdata | ITdefault | ITderiving + | ITderive | ITdo | ITelse - | ITfor | IThiding | ITif | ITimport @@ -420,7 +422,6 @@ data Token | ITccallconv | ITdotnet | ITmdo - | ITiso | ITfamily -- Pragmas @@ -432,6 +433,7 @@ data Token | ITdeprecated_prag | ITline_prag | ITscc_prag + | ITgenerated_prag | ITcore_prag -- hdaume: core annotations | ITunpack_prag | ITclose_prag @@ -541,7 +543,7 @@ isSpecial :: Token -> Bool -- not as a keyword. isSpecial ITas = True isSpecial IThiding = True -isSpecial ITfor = True +isSpecial ITderive = True isSpecial ITqualified = True isSpecial ITforall = True isSpecial ITexport = True @@ -553,7 +555,6 @@ isSpecial ITunsafe = True isSpecial ITccallconv = True isSpecial ITstdcallconv = True isSpecial ITmdo = True -isSpecial ITiso = True isSpecial ITfamily = True isSpecial _ = False @@ -573,9 +574,9 @@ reservedWordsFM = listToUFM $ ( "data", ITdata, 0 ), ( "default", ITdefault, 0 ), ( "deriving", ITderiving, 0 ), + ( "derive", ITderive, 0 ), ( "do", ITdo, 0 ), ( "else", ITelse, 0 ), - ( "for", ITfor, 0 ), ( "hiding", IThiding, 0 ), ( "if", ITif, 0 ), ( "import", ITimport, 0 ), @@ -640,7 +641,6 @@ reservedSymsFM = listToUFM $ ,(">>-", ITRarrowtail, bit arrowsBit) #if __GLASGOW_HASKELL__ >= 605 - ,("λ", ITlam, bit glaExtsBit) ,("∷", ITdcolon, bit glaExtsBit) ,("⇒", ITdarrow, bit glaExtsBit) ,("∀", ITforall, bit glaExtsBit) @@ -690,24 +690,28 @@ pop _span _buf _len = do popLexState; lexToken pop_and :: Action -> Action pop_and act span buf len = do popLexState; act span buf len -notFollowedBy char _ _ _ (AI _ _ buf) = atEnd buf || currentChar buf /= char +{-# INLINE nextCharIs #-} +nextCharIs buf p = not (atEnd buf) && p (currentChar buf) + +notFollowedBy char _ _ _ (AI _ _ buf) + = nextCharIs buf (/=char) notFollowedBySymbol _ _ _ (AI _ _ buf) - = atEnd buf || currentChar buf `notElem` "!#$%&*+./<=>?@\\^|-~" + = nextCharIs buf (`notElem` "!#$%&*+./<=>?@\\^|-~") +-- We must reject doc comments as being ordinary comments everywhere. +-- In some cases the doc comment will be selected as the lexeme due to +-- maximal munch, but not always, because the nested comment rule is +-- valid in all states, but the doc-comment rules are only valid in +-- the non-layout states. isNormalComment bits _ _ (AI _ _ buf) - = (if haddockEnabled bits then False else (followedBySpaceDoc buf)) - || notFollowedByDocOrPragma - where - notFollowedByDocOrPragma = not $ spaceAndP buf - (\buf' -> currentChar buf' `elem` "|^*$#") - -spaceAndP buf p = p buf || currentChar buf == ' ' && p buf' - where buf' = snd (nextChar buf) - -followedBySpaceDoc buf = spaceAndP buf followedByDoc + | haddockEnabled bits = notFollowedByDocOrPragma + | otherwise = nextCharIs buf (/='#') + where + notFollowedByDocOrPragma + = not $ spaceAndP buf (`nextCharIs` (`elem` "|^*$#")) -followedByDoc buf = currentChar buf `elem` "|^*$" +spaceAndP buf p = p buf || nextCharIs buf (==' ') && p (snd (nextChar buf)) haddockDisabledAnd p bits _ _ (AI _ _ buf) = if haddockEnabled bits then False else (p buf) @@ -786,13 +790,12 @@ nested_doc_comment span buf _len = withLexedDocType (go "") Just (c,input) -> go (c:commentAcc) input docType False withLexedDocType lexDocComment = do - input <- getInput - case alexGetChar input of - Nothing -> error "Can't happen" - Just ('|', input) -> lexDocComment input ITdocCommentNext False - Just ('^', input) -> lexDocComment input ITdocCommentPrev False - Just ('$', input) -> lexDocComment input ITdocCommentNamed False - Just ('*', input) -> lexDocSection 1 input + input@(AI _ _ buf) <- getInput + case prevChar buf ' ' of + '|' -> lexDocComment input ITdocCommentNext False + '^' -> lexDocComment input ITdocCommentPrev False + '$' -> lexDocComment input ITdocCommentNamed False + '*' -> lexDocSection 1 input where lexDocSection n input = case alexGetChar input of Just ('*', input) -> lexDocSection (n+1) input @@ -1299,6 +1302,14 @@ getCharOrFail = do Just (c,i) -> do setInput i; return c -- ----------------------------------------------------------------------------- +-- Warnings + +warn :: DynFlag -> SDoc -> Action +warn option warning span _buf _len = do + addWarning option (mkWarnMsg span alwaysQualify warning) + lexToken + +-- ----------------------------------------------------------------------------- -- The Parse Monad data LayoutContext @@ -1316,6 +1327,8 @@ data ParseResult a data PState = PState { buffer :: StringBuffer, + dflags :: DynFlags, + messages :: Messages, last_loc :: SrcSpan, -- pos of previous token last_offs :: !Int, -- offset of the previous token from the -- beginning of the current line. @@ -1500,6 +1513,10 @@ pragState :: StringBuffer -> SrcLoc -> PState pragState buf loc = PState { buffer = buf, + messages = emptyMessages, + -- XXX defaultDynFlags is not right, but we don't have a real + -- dflags handy + dflags = defaultDynFlags, last_loc = mkSrcSpan loc loc, last_offs = 0, last_len = 0, @@ -1517,6 +1534,8 @@ mkPState :: StringBuffer -> SrcLoc -> DynFlags -> PState mkPState buf loc flags = PState { buffer = buf, + dflags = flags, + messages = emptyMessages, last_loc = mkSrcSpan loc loc, last_offs = 0, last_len = 0, @@ -1543,6 +1562,15 @@ mkPState buf loc flags = b `setBitIf` cond | cond = bit b | otherwise = 0 +addWarning :: DynFlag -> WarnMsg -> P () +addWarning option w + = P $ \s@PState{messages=(ws,es), dflags=d} -> + let ws' = if dopt option d then ws `snocBag` w else ws + in POk s{messages=(ws', es)} () + +getMessages :: PState -> Messages +getMessages PState{messages=ms} = ms + getContext :: P [LayoutContext] getContext = P $ \s@PState{context=ctx} -> POk s ctx