Changeset 13473 for src/Pugs/Lexer.hs
- Timestamp:
- 09/20/06 07:24:32 (2 years ago)
- Files:
-
- 1 modified
-
src/Pugs/Lexer.hs (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Lexer.hs
r13183 r13473 128 128 balanced = do 129 129 notFollowedBy alphaNum 130 opendelim <- anyChar 130 opendelim <- anyChar 131 131 contents <- many $ satisfy (/= balancedDelim opendelim) 132 132 char $ balancedDelim opendelim … … 219 219 220 220 interpolatingStringLiteral :: RuleParser String -- ^ Opening delimiter 221 -> RuleParser String -- ^ Closing delimiter 221 -> RuleParser String -- ^ Closing delimiter 222 222 -> RuleParser Exp -- ^ Interpolator 223 223 -> RuleParser Exp -- ^ Entire string … … 234 234 homogenConcat (x:xs) 235 235 = App (_Var "&infix:~") Nothing [x, homogenConcat xs] 236 236 237 237 stringList :: Int -> RuleParser [Exp] 238 238 stringList i = choice … … 310 310 ; let n = foldl (\x d -> base*x + toInteger (digitToInt d)) 0 digits 311 311 ; seq n (return n) 312 } 312 } 313 313 314 314 charEsc :: RuleParser Char … … 316 316 where 317 317 parseEsc (c,code) = do{ char c; return code } 318 318 319 319 charAscii :: RuleParser Char 320 320 charAscii = choice (map parseAscii asciiMap) … … 327 327 escMap = zip ("abfnrtv\\\"\'") ("\a\b\f\n\r\t\v\\\"\'") 328 328 asciiMap :: [(String, Char)] 329 asciiMap = zip (ascii3codes ++ ascii2codes) (ascii3 ++ ascii2) 329 asciiMap = zip (ascii3codes ++ ascii2codes) (ascii3 ++ ascii2) 330 330 331 331 ascii2codes :: [String] … … 455 455 -- Numbers 456 456 ----------------------------------------------------------- 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 458 decimal :: RuleParser Integer 459 decimal = number 10 digit 543 460 544 461 ----------------------------------------------------------- 545 462 -- Identifiers & Reserved words 546 463 ----------------------------------------------------------- 547 identifier = lexeme $ try $ ident 548 549 ident 464 identifier, ident :: RuleParser String 465 identifier = lexeme . try $ ident 466 467 ident 550 468 = do{ c <- identStart 551 469 ; cs <- many identLetter … … 557 475 -- White space & symbols 558 476 ----------------------------------------------------------- 559 lexeme p 477 lexeme :: RuleParser a -> RuleParser a 478 lexeme p 560 479 = do{ x <- p; whiteSpace; return x } 561 562 563 --whiteSpace 480 481 482 whiteSpace :: RuleParser () 564 483 whiteSpace = skipMany (simpleSpace <|> comment) 565 484 485 comment :: RuleParser () 566 486 comment = do 567 487 char '#' <?> "comment" … … 578 498 many1 $ satisfy (\x -> isSpace x && x /= '\n') 579 499 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 509 fileNameQuoted :: RuleParser (Maybe (Maybe String)) 588 510 fileNameQuoted = try $ do 589 511 char '"' … … 592 514 many $ satisfy (\x -> isSpace x && x /= '\n') 593 515 lookAhead (satisfy (== '\n')) 594 return $ Just $ Just file 595 516 return . Just $ Just file 517 518 fileNameBare :: RuleParser (Maybe (Maybe String)) 596 519 fileNameBare = try $ do 597 520 file <- many1 $ satisfy (not . isSpace) … … 599 522 (<|> return (Just Nothing)) $ try $ do 600 523 lookAhead (satisfy (== '\n')) 601 return $ Just $ Just file 602 524 return . Just $ Just file 525 526 skipToLineEnd :: RuleParser () 603 527 skipToLineEnd = do 604 528 skipMany (satisfy (/= '\n')) 605 return () 606 529 530 simpleSpace :: RuleParser () 607 531 simpleSpace = do 608 532 skipMany1 (satisfy (isSpace)) 609 533 610 611 -- XXX - nesting 534 multiLineComment :: RuleParser () 612 535 multiLineComment = do 613 536 openOne <- satisfy (\x -> balancedDelim x /= x)
