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
|
> module Lexer where
> import Char
> import Maybe
>
> data Terminal = DO
> | ELSE
> | END
> | FUNCTION
> | IF
> | IN
> | LET
> | THEN
> | VAR
> | WHILE
> | ASSIGN
> | COLON
> | COMMA
> | CPAREN
> | DIV
> | EQU
> | LST
> | MINUS
> | OPAREN
> | PLUS
> | SEMI
> | TIMES
> | ID String
> | INT String
> deriving (Show)
>
> lexer :: String -> [Terminal]
> lexer [] = []
> lexer (':' : '=' : cs) = ASSIGN : lexer cs
> lexer (':' : cs) = COLON : lexer cs
> lexer (',' : cs) = COMMA : lexer cs
> lexer (')' : cs) = CPAREN : lexer cs
> lexer ('/' : cs) = DIV : lexer cs
> lexer ('=' : cs) = EQU : lexer cs
> lexer ('<' : '=' : cs) = LST : lexer cs
> lexer ('-' : cs) = MINUS : lexer cs
> lexer ('(' : cs) = OPAREN : lexer cs
> lexer ('+' : cs) = PLUS : lexer cs
> lexer (';' : cs) = SEMI : lexer cs
> lexer ('*' : cs) = TIMES : lexer cs
> lexer (c : cs) | isAlpha c = t : lexer cs'
> where (n, cs') = span isAlphaNum cs
> t = fromMaybe (ID (c : n)) (lookup (c : n) keywords)
> lexer (c : cs) | isDigit c = INT (c : n) : lexer cs'
> where (n, cs') = span isDigit cs
> lexer (_ : cs) = lexer cs
> keywords = [ ("do", DO)
> , ("else", ELSE)
> , ("end", END)
> , ("function", FUNCTION)
> , ("if", IF)
> , ("in", IN)
> , ("let", LET)
> , ("then", THEN)
> , ("var", VAR)
> , ("while", WHILE) ]
|