summaryrefslogtreecommitdiff
path: root/src/SPL/Lex.hs
blob: b8f09e3047fc9b9436aeda116abc55565a414982 (plain) (blame)
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
-- vim: et ts=2 sw=2 ai:
{-# LANGUAGE LambdaCase #-}
module SPL.Lex
where

import Prelude hiding(lex)

import Control.Applicative
import Control.Monad
import Data.Char
import Data.List.Utils

data Token
  = TIdent String
  | TInt Int
  | TChar Char
  | TBool Bool

  | TParenOpen
  | TParenClose
  | TBrackOpen
  | TBrackClose
  | TBraceOpen
  | TBraceClose
  | TColonColon
  | TEquals
  | TSemicolon
  | TDot
  | TComma
  | TArrow

  | TIf
  | TElse
  | TWhile
  | TReturn
  | TVar
  | TVoidType
  | TCharType
  | TBoolType
  | TIntType

  | TPlus
  | TMinus
  | TAsterisk
  | TSlash
  | TPercent
  | TEqEq
  | TLt
  | TGt
  | TLtEq
  | TGtEq
  | TExclamEq
  | TAmpAmp
  | TPipePipe
  | TColon
  | TExclam

  | TSingleComment String
  | TBlockComment String
  deriving (Show, Eq)

isCommentToken :: Token -> Bool
isCommentToken (TSingleComment _) = True
isCommentToken (TBlockComment _)  = True
isCommentToken _                  = False

isIdentToken :: Token -> Bool
isIdentToken (TIdent _) = True
isIdentToken _          = False

lex :: (Monad m, Alternative m) => String -> m [Token]
lex [] = pure []
lex (c:s) | isSpace c = lex s
lex s = (comment s <|> item s <|> int s <|> char s <|> bool s <|> ident s) >>=
  \(t,s') -> lex s' >>= \ts -> pure (t:ts)
  where
    ident :: (Alternative m) => String -> m (Token, String)
    ident (c:s)
      | isAlpha c = pure (TIdent (c:cs), s')
      | otherwise = empty
      where (cs,s') = span isIdentChar s

    int :: (Alternative m) => String -> m (Token, String)
    int (c:s)
      | isDigit c = pure (TInt $ read (c:cs), s')
      | otherwise = empty
      where (cs,s') = span isDigit s

    char :: (Alternative m) => String -> m (Token, String)
    char ('\'':c:'\'':s) = pure (TChar c, s)
    char _ = empty

    bool :: (Alternative m) => String -> m (Token, String)
    bool ('F':'a':'l':'s':'e':s) = noIdentifier (TBool False) s
    bool ('T':'r':'u':'e':s) = noIdentifier (TBool True) s
    bool _ = empty

    comment :: (Alternative m) => String -> m (Token, String)
    comment ('/':'/':s) = pure (TSingleComment cs, s')
      where
        (cs, s') = span (/= '\n') s
    comment ('/':'*':s) = pure (TBlockComment cs, s')
      where
        (cs, _:_:s') = spanList (\case
          ('*':'/':s) -> False
          _           -> True) s
    comment _ = empty

    item :: (Alternative m) => String -> m (Token, String)
    item ('i':'f':s) = noIdentifier TIf s
    item ('e':'l':'s':'e':s) = noIdentifier TElse s
    item ('w':'h':'i':'l':'e':s) = noIdentifier TWhile s
    item ('r':'e':'t':'u':'r':'n':s) = noIdentifier TReturn s
    item ('v':'a':'r':s) = noIdentifier TVar s
    item ('V':'o':'i':'d':s) = noIdentifier TVoidType s
    item ('C':'h':'a':'r':s) = noIdentifier TCharType s
    item ('B':'o':'o':'l':s) = noIdentifier TBoolType s
    item ('I':'n':'t':s) = noIdentifier TIntType s
    item ('=':'=':s) = pure (TEqEq, s)
    item ('<':'=':s) = pure (TLtEq, s)
    item ('>':'=':s) = pure (TGtEq, s)
    item ('!':'=':s) = pure (TExclamEq, s)
    item ('&':'&':s) = pure (TAmpAmp, s)
    item ('|':'|':s) = pure (TPipePipe, s)
    item (':':':':s) = pure (TColonColon, s)
    item ('-':'>':s) = pure (TArrow, s)
    item ('(':s) = pure (TParenOpen, s)
    item (')':s) = pure (TParenClose, s)
    item ('[':s) = pure (TBrackOpen, s)
    item (']':s) = pure (TBrackClose, s)
    item ('{':s) = pure (TBraceOpen, s)
    item ('}':s) = pure (TBraceClose, s)
    item ('=':s) = pure (TEquals, s)
    item (';':s) = pure (TSemicolon, s)
    item ('.':s) = pure (TDot, s)
    item (',':s) = pure (TComma, s)
    item ('+':s) = pure (TPlus, s)
    item ('-':s) = pure (TMinus, s)
    item ('*':s) = pure (TAsterisk, s)
    item ('/':s) = pure (TSlash, s)
    item ('%':s) = pure (TPercent, s)
    item ('<':s) = pure (TLt, s)
    item ('>':s) = pure (TGt, s)
    item (':':s) = pure (TColon, s)
    item ('!':s) = pure (TExclam, s)
    item _       = empty

    noIdentifier :: (Alternative m) => Token -> String -> m (Token, String)
    noIdentifier t [] = pure (t, [])
    noIdentifier t s@(c:_)
      | isIdentChar c = empty
      | otherwise     = pure (t, s)

    isIdentChar :: Char -> Bool
    isIdentChar = liftM2 (||) isAlphaNum (== '_')