Changeset 13473 for src/Pugs/Lexer.hs

Show
Ignore:
Timestamp:
09/20/06 07:24:32 (2 years ago)
Author:
audreyt
Message:

* Pugs.Lexer: Massive source code cleanup and warning avoidance.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Lexer.hs

    r13183 r13473  
    128128balanced = do 
    129129    notFollowedBy alphaNum 
    130     opendelim <- anyChar  
     130    opendelim <- anyChar 
    131131    contents <- many $ satisfy (/= balancedDelim opendelim) 
    132132    char $ balancedDelim opendelim 
     
    219219 
    220220interpolatingStringLiteral :: RuleParser String -- ^ Opening delimiter 
    221                            -> RuleParser String -- ^ Closing delimiter  
     221                           -> RuleParser String -- ^ Closing delimiter 
    222222                           -> RuleParser Exp    -- ^ Interpolator 
    223223                           -> RuleParser Exp    -- ^ Entire string 
     
    234234    homogenConcat (x:xs) 
    235235        = App (_Var "&infix:~") Nothing [x, homogenConcat xs] 
    236      
     236 
    237237    stringList :: Int -> RuleParser [Exp] 
    238238    stringList i = choice 
     
    310310        ; let n = foldl (\x d -> base*x + toInteger (digitToInt d)) 0 digits 
    311311        ; seq n (return n) 
    312         }           
     312        } 
    313313 
    314314charEsc         :: RuleParser Char 
     
    316316                where 
    317317                  parseEsc (c,code)     = do{ char c; return code } 
    318                    
     318 
    319319charAscii       :: RuleParser Char 
    320320charAscii       = choice (map parseAscii asciiMap) 
     
    327327escMap          = zip ("abfnrtv\\\"\'") ("\a\b\f\n\r\t\v\\\"\'") 
    328328asciiMap        :: [(String, Char)] 
    329 asciiMap        = zip (ascii3codes ++ ascii2codes) (ascii3 ++ ascii2)  
     329asciiMap        = zip (ascii3codes ++ ascii2codes) (ascii3 ++ ascii2) 
    330330 
    331331ascii2codes     :: [String] 
     
    455455-- Numbers 
    456456----------------------------------------------------------- 
    457 -- naturalOrFloat :: CharParser st (Either Integer Double) 
    458 naturalOrFloat  = lexeme natFloat   <?> "number" 
    459  
    460 float           = lexeme floating   <?> "float" 
    461 integer         = lexeme int        <?> "integer" 
    462 natural         = lexeme nat        <?> "natural" 
    463  
    464  
    465 -- floats 
    466 floating        = do{ n <- decimal  
    467                     ; fractExponent n 
    468                     } 
    469  
    470  
    471 natFloat        = do{ char '0' 
    472                     ; zeroNumFloat 
    473                     } 
    474                     <|> decimalFloat 
    475                      
    476 zeroNumFloat    =  do{ n <- hexadecimal <|> octal 
    477                         ; return (Left n) 
    478                         } 
    479                 <|> decimalFloat 
    480                 <|> fractFloat 0 
    481                 <|> return (Left 0)                   
    482                      
    483 decimalFloat    = do{ n <- decimal 
    484                     ; option (Left n)  
    485                                 (fractFloat n) 
    486                     } 
    487  
    488 fractFloat n    = do{ f <- fractExponent n 
    489                     ; return (Right f) 
    490                     } 
    491                      
    492 fractExponent n = do{ fract <- fraction 
    493                     ; expo  <- option (1.0 :: Double) exponent' 
    494                     ; return ((fromInteger n + fract)*expo) 
    495                     } 
    496                 <|> 
    497                     do{ expo <- exponent' 
    498                     ; return ((fromInteger n)*expo) 
    499                     } 
    500  
    501 fraction        = do{ char '.' 
    502                     ; digits <- many1 digit <?> "fraction" 
    503                     ; return (foldr op (0.0 :: Double) digits) 
    504                     } 
    505                     <?> "fraction" 
    506                 where 
    507                     op d f    = (f + fromIntegral (digitToInt d)) / (10.0 :: Double) 
    508                      
    509 exponent'       = do{ oneOf "eE" 
    510                     ; f <- sign' 
    511                     ; e <- decimal <?> "exponent" 
    512                     ; return (power (f e)) 
    513                     } 
    514                     <?> "exponent" 
    515                 where 
    516                     power e  | e < 0      = 1.0/power(-e) 
    517                             | otherwise  = fromInteger (10^e) 
    518  
    519  
    520 -- integers and naturals 
    521 int             = nat  
    522 {-do{ f <- lexeme sign 
    523                     ; n <- nat 
    524                     ; return (f n) 
    525                     } 
    526                     -} 
    527                      
    528 -- sign            :: CharParser st (Integer -> Integer) 
    529 sign'           =   (char '-' >> return negate)  
    530                 <|> (char '+' >> return id)      
    531                 <|> return id 
    532  
    533 nat             = zeroNumber <|> decimal 
    534      
    535 zeroNumber      = do{ char '0' 
    536                     ; hexadecimal <|> octal <|> decimal <|> return 0 
    537                     } 
    538                     <?> ""        
    539  
    540 decimal         = number 10 digit         
    541 hexadecimal     = do{ oneOf "xX"; number 16 hexDigit } 
    542 octal           = do{ oneOf "oO"; number 8 octDigit  } 
     457 
     458decimal :: RuleParser Integer 
     459decimal         = number 10 digit 
    543460 
    544461----------------------------------------------------------- 
    545462-- Identifiers & Reserved words 
    546463----------------------------------------------------------- 
    547 identifier = lexeme $ try $ ident 
    548      
    549 ident            
     464identifier, ident :: RuleParser String 
     465identifier = lexeme . try $ ident 
     466 
     467ident 
    550468    = do{ c <- identStart 
    551469        ; cs <- many identLetter 
     
    557475-- White space & symbols 
    558476----------------------------------------------------------- 
    559 lexeme p        
     477lexeme :: RuleParser a -> RuleParser a 
     478lexeme p 
    560479    = do{ x <- p; whiteSpace; return x  } 
    561      
    562      
    563 --whiteSpace     
     480 
     481 
     482whiteSpace :: RuleParser () 
    564483whiteSpace = skipMany (simpleSpace <|> comment) 
    565484 
     485comment :: RuleParser () 
    566486comment = do 
    567487    char '#' <?> "comment" 
     
    578498        many1 $ satisfy (\x -> isSpace x && x /= '\n') 
    579499        fileNameQuoted <|> fileNameBare 
    580     if file == Just Nothing then skipToLineEnd else do 
    581     many $ satisfy (/= '\n') 
    582     setPosition $ pos 
    583         `setSourceLine`     (fromInteger line - 1) 
    584         `setSourceColumn`   1 
    585         `setSourceName`     maybe (sourceName pos) fromJust file 
    586     return () 
    587  
     500    case file of 
     501        Just Nothing    -> skipToLineEnd 
     502        _               -> do 
     503            many $ satisfy (/= '\n') 
     504            setPosition $ pos 
     505                `setSourceLine`     (fromInteger line - 1) 
     506                `setSourceColumn`   1 
     507                `setSourceName`     maybe (sourceName pos) fromJust file 
     508 
     509fileNameQuoted :: RuleParser (Maybe (Maybe String)) 
    588510fileNameQuoted = try $ do 
    589511    char '"' 
     
    592514    many $ satisfy (\x -> isSpace x && x /= '\n') 
    593515    lookAhead (satisfy (== '\n')) 
    594     return $ Just $ Just file 
    595  
     516    return . Just $ Just file 
     517 
     518fileNameBare :: RuleParser (Maybe (Maybe String)) 
    596519fileNameBare = try $ do 
    597520    file <- many1 $ satisfy (not . isSpace) 
     
    599522    (<|> return (Just Nothing)) $ try $ do 
    600523        lookAhead (satisfy (== '\n')) 
    601         return $ Just $ Just file 
    602  
     524        return . Just $ Just file 
     525 
     526skipToLineEnd :: RuleParser () 
    603527skipToLineEnd = do 
    604528    skipMany (satisfy (/= '\n')) 
    605     return () 
    606          
     529 
     530simpleSpace :: RuleParser () 
    607531simpleSpace = do 
    608532    skipMany1 (satisfy (isSpace)) 
    609533 
    610      
    611 -- XXX - nesting 
     534multiLineComment :: RuleParser () 
    612535multiLineComment = do 
    613536    openOne <- satisfy (\x -> balancedDelim x /= x)