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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
{-# LANGUAGE OverloadedStrings #-}
module Clex.Lex
( lexC
) where
import Prelude hiding (takeWhile)
import Data.Foldable (asum)
import Data.Word8 ( Word8, isHexDigit, isOctDigit, isAlpha
, _e, _E, _x, _X,_f, _F, _u, _U, _l, _L, _0
, _plus, _hyphen, _underscore, _quotesingle
, _quotedbl, _backslash, _period)
import Data.Attoparsec.ByteString
import Data.Attoparsec.ByteString.Char8 (isDigit_w8, isSpace_w8)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Control.Applicative ((<|>))
import Clex.Types
lexC :: ByteString -> Either String [Token]
lexC = parseOnly (cParser <* endOfInput)
cParser :: Parser [Token]
cParser = many' tokenParser
-- Order is important! For example we need to first check for a floating point number
-- with a leading digit, instead of one without, because the second one will always match the first one.
tokenParser :: Parser Token
tokenParser = asum $ concat
[ [whitespaceParser]
, [ scientificParser, leadingDotParser, dotFloatParser
, hexNumParser, octNumParser, numParser, charParser, stringParser
]
, map keywordParser allKeyWords
, map operParser allOpers
, [identifierParser]]
whitespaceParser :: Parser Token
whitespaceParser = Whitespace <$> takeWhile1 isSpace_w8
keywordParser :: ByteString -> Parser Token
keywordParser kw = do
parsed <- recognize <$> string kw
-- we need to check if we still have more letters after the keyword
next <- peekWord8
let parsed' = pure parsed
case next of
Nothing -> parsed'
(Just x) -> if (not . isAlpha) x then parsed'
else fail "more input after keyword"
operParser :: ByteString -> Parser Token
operParser = (recognize <$>) . string
isDigit :: Word8 -> Bool
isDigit = isDigit_w8
isLetter :: Word8 -> Bool
isLetter b = isAlpha b
|| b == _underscore
exponentParser :: Parser ByteString
exponentParser = do
e <- satisfy (\c -> c == _E || c == _e)
sign <- option "" plusMinus
digits <- takeWhile1 isDigit_w8
pure $ BS.singleton e <> sign <> digits
where
plusMinus = BS.singleton <$> satisfy (\c -> c == _plus || c == _hyphen) -- plus or minus
float :: Parser Word8
float = satisfy $
\c -> c == _F
|| c == _f
|| c == _L
|| c == _l
unsigned :: Parser ByteString
unsigned = BS.pack <$> many' (satisfy $
\c -> c == _U
|| c == _u
|| c == _L
|| c == _l)
isxX :: Word8 -> Bool
isxX c = c == _X
|| c == _x
singleQuote :: Parser Word8
singleQuote = word8 _quotesingle
doubleQuote :: Parser Word8
doubleQuote = word8 _quotedbl
charParser :: Parser Token
charParser = do
l <- option "" (BS.singleton <$> word8 _L)
_ <- singleQuote
str <- BS.concat <$> many1' ((BS.snoc <$> string "\\" <*> anyWord8) <|> notInWeirdClass)
sq <- singleQuote
pure $ CharLit ((l <> (sq `BS.cons` str)) `BS.snoc` sq)
where
notInWeirdClass = BS.singleton <$>
satisfy (\c -> c /= _backslash
&& c /= _quotesingle)
stringParser :: Parser Token
stringParser = do
l <- option "" (BS.singleton <$> word8 _L)
_ <- doubleQuote
str <- BS.concat <$> many' ((BS.snoc <$> string "\\" <*> anyWord8) <|> notInWeirdClass)
dq <- doubleQuote
pure $ StringLit ((l <> (dq `BS.cons` str)) `BS.snoc` dq)
where
notInWeirdClass = BS.singleton <$>
satisfy (\c -> c /= _backslash
&& c /= _quotedbl)
zero :: Parser Word8
zero = word8 _0
hexNumParser :: Parser Token
hexNumParser = do
z <- zero
x <- satisfy isxX
hex <- takeWhile1 isHexDigit
f <- unsigned
pure $ HexNumber (z `BS.cons` x `BS.cons` hex <> f)
octNumParser :: Parser Token
octNumParser = do
z <- zero
oct <- takeWhile1 isOctDigit
f <- unsigned
pure $ OctNumber (z `BS.cons` oct <> f)
numParser :: Parser Token
numParser = do
num <- takeWhile1 isDigit
f <- unsigned
pure $ Number (num <> f)
scientificParser :: Parser Token
scientificParser = do
digits <- takeWhile1 isDigit
expon <- exponentParser
f <- option "" (BS.singleton <$> float)
pure $ Scientific $ digits <> expon <> f
dotFloatParser :: Parser Token
dotFloatParser = do
leadingDigits <- takeWhile isDigit
d <- BS.singleton <$> word8 _period
afterDigits <- takeWhile1 isDigit
expon <- option "" exponentParser
f <- option "" (BS.singleton <$> float)
pure $ DotFloat $ leadingDigits <> d <> afterDigits <> expon <> f
leadingDotParser :: Parser Token
leadingDotParser = do
leadingDigits <- takeWhile1 isDigit
d <- BS.singleton <$> word8 _period
afterDigits <- takeWhile isDigit
expon <- option "" exponentParser
f <- option "" (BS.singleton <$> float)
pure $ LeadingDot $ leadingDigits <> d <> afterDigits <> expon <> f
identifierParser :: Parser Token
identifierParser = Identifier <$> takeWhile1 isLetter
|