1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Frown --- An LALR(k) parser generator for Haskell 98 %
% Copyright (C) 2001-2005 Ralf Hinze %
% %
% This program is free software; you can redistribute it and/or modify %
% it under the terms of the GNU General Public License (version 2) as %
% published by the Free Software Foundation. %
% %
% This program is distributed in the hope that it will be useful, %
% but WITHOUT ANY WARRANTY; without even the implied warranty of %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the %
% GNU General Public License for more details. %
% %
% You should have received a copy of the GNU General Public License %
% along with this program; see the file COPYING. If not, write to %
% the Free Software Foundation, Inc., 59 Temple Place - Suite 330, %
% Boston, MA 02111-1307, USA. %
% %
% Contact information %
% Email: Ralf Hinze <ralf@cs.uni-bonn.de> %
% Homepage: http://www.informatik.uni-bonn.de/~ralf/ %
% Paper mail: Dr. Ralf Hinze %
% Institut für Informatik III %
% Universität Bonn %
% Römerstraße 164 %
% 53117 Bonn, Germany %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------------------------------= --------------------------------------------
\section{The type of tokens}
%-------------------------------= --------------------------------------------
> module Lexer2
> where
> import Prelude
> import Data.Char
> import System.IO
> import Control.Monad
> import Base
> import Options
% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
\subsection{The |Token| type}
% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
> data Token = White String
> | Comment String
> | Nested String
> | Conid String
> | Varid String
> | Consym String
> | Varsym String
> | Numeral String
> | Char String
> | String String
> | Comma
> | Semicolon
> | LeftParen
> | RightParen
> | LeftBracket
> | RightBracket
> | LeftCurly
> | RightCurly
> | Backquote
> | LeftSpecial
> | RightSpecial
> | EOF
> deriving (Eq, Ord, Show)
> isVarid, isConid, isWhite :: Token -> Bool
> isVarid (Varid _) = True
> isVarid _ = False
>
> isConid (Conid _) = True
> isConid _ = False
>
> isWhite (White _) = True
> isWhite (Comment _) = True
> isWhite (Nested _) = True
> isWhite _ = False
> toString :: Token -> String
> toString (White s) = s
> toString (Comment s) = "--" ++ s
> toString (Nested s) = "{-" ++ s ++ "-}"
> toString (Conid s) = s
> toString (Varid s) = s
> toString (Consym s) = s
> toString (Varsym s) = s
> toString (Numeral s) = s
> toString (Char s) = s
> toString (String s) = s
> toString Comma = ","
> toString Semicolon = ";"
> toString LeftParen = "("
> toString RightParen = ")"
> toString LeftBracket = "["
> toString RightBracket = "]"
> toString LeftCurly = "{"
> toString RightCurly = "}"
> toString Backquote = "`"
> toString LeftSpecial = "%{"
> toString RightSpecial = "}%"
> toString EOF = "<end of input>"
% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
\subsection{Some helper functions}
% - - - - - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - - - - -
> lexFracExp :: String -> Maybe (String, String)
> lexFracExp s = do t <- match "." s
> (ds, u) <- lexDigits' t
> (e, v) <- lexExp u
> return ('.' : ds ++ e, v)
> `mplus` return ("", s)
>
> lexExp :: String -> Maybe (String, String)
> lexExp (e : s)
> | e `elem` "eE" = do (c : t) <- return s
> if c `elem` "+-" then return () else mzero
> (ds, u) <- lexDigits' t
> return (e : c : ds, u)
> `mplus` do (ds, t) <- lexDigits' s
> return (e : ds, t)
> lexExp s = return ("", s)
>
> lexDigits' :: String -> Maybe (String, String)
> lexDigits' s = do (cs@(_ : _), t) <- return (span isDigit s)
> return (cs, t)
> match :: String -> String -> Maybe String
> match p s
> | p == t = Just u
> | otherwise = Nothing
> where (t, u) = splitAt (length p) s
> nested :: Int -> String -> (String, String)
> nested _ [] = ([], [])
> nested 0 ('-' : '}' : s) = ([], '-':'}':s)
> nested n ('-' : '}' : s) = '-' <| '}' <| nested (n - 1) s
> nested n ('{' : '-' : s) = '{' <| '-' <| nested (n + 1) s
> nested n (c : s) = c <| nested n s
> lexCharLit, lexStrLit :: String -> (String, String)
> lexCharLit [] = ([], [])
> lexCharLit ('\'' : s) = ([], '\'' : s)
> lexCharLit ('\\' : c : s) = '\\' <| c <| lexCharLit s
> lexCharLit (c : s) = c <| lexCharLit s
>
> lexStrLit [] = ([], [])
> lexStrLit ('"' : s) = ([], '"' : s)
> lexStrLit ('\\' : c : s) = '\\' <| c <| lexStrLit s
> lexStrLit (c : s) = c <| lexStrLit s
> isSymbol, isIdChar :: Char -> Bool
> isSymbol c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
> isIdChar c = isAlphaNum c || c `elem` "_'"
|