-
Notifications
You must be signed in to change notification settings - Fork 658
/
Copy pathVariable.hs
376 lines (283 loc) · 9.88 KB
/
Variable.hs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE BangPatterns, UnboxedTuples, OverloadedStrings #-}
module Parse.Primitives.Variable
( lower
, upper
, moduleName
, Upper(..)
, foreignUpper
, foreignAlpha
, chompInnerChars
, getInnerWidth
, getInnerWidthHelp
)
where
import Control.Exception (assert)
import Data.Bits ((.&.), (.|.), shiftL)
import qualified Data.Char as Char
import qualified Data.Set as Set
import Foreign.ForeignPtr (ForeignPtr)
import GHC.Word (Word8)
import qualified AST.Source as Src
import qualified Elm.Name as N
import Parse.Primitives.Internals (Parser(..), State(..), expect, noError, unsafeIndex)
import qualified Reporting.Error.Syntax as E
-- LOCAL UPPER
upper :: Parser N.Name
upper =
Parser $ \(State fp offset terminal indent row col ctx) cok _ _ eerr ->
let (# newOffset, newCol #) = chompUpper fp offset terminal col in
if offset == newOffset then
eerr (expect row col ctx E.CapVar)
else
let !name = N.fromForeignPtr fp offset (newOffset - offset) in
cok name (State fp newOffset terminal indent row newCol ctx) noError
-- LOCAL LOWER
lower :: Parser N.Name
lower =
Parser $ \(State fp offset terminal indent row col ctx) cok _ _ eerr ->
let (# newOffset, newCol #) = chompLower fp offset terminal col in
if offset == newOffset then
eerr (expect row col ctx E.LowVar)
else
let !name = N.fromForeignPtr fp offset (newOffset - offset) in
if Set.member name reservedWords then
eerr (expect row col ctx E.LowVar)
else
cok name (State fp newOffset terminal indent row newCol ctx) noError
{-# NOINLINE reservedWords #-}
reservedWords :: Set.Set N.Name
reservedWords =
Set.fromList
[ "if", "then", "else"
, "case", "of"
, "let", "in"
, "type"
, "module", "where"
, "import", "exposing"
, "as"
, "port"
]
-- MODULE NAME
moduleName :: Parser N.Name
moduleName =
Parser $ \(State fp offset terminal indent row col ctx) cok _ _ eerr ->
let (# newOffset, newCol #) = chompUpper fp offset terminal col in
case moduleNameHelp fp newOffset terminal newCol of
Bad badCol ->
eerr (expect row badCol ctx E.CapVar)
Good end endCol ->
let
!name = N.fromForeignPtr fp offset (end - offset)
!newState = State fp end terminal indent row endCol ctx
in
cok name newState noError
data ModuleName = Bad Int | Good Int Int
moduleNameHelp :: ForeignPtr Word8 -> Int -> Int -> Int -> ModuleName
moduleNameHelp fp offset terminal col =
if isDot fp offset terminal then
let
!offset1 = offset + 1
(# newOffset, newCol #) = chompUpper fp offset1 terminal (col + 1)
in
if offset1 == newOffset then
Bad newCol
else
moduleNameHelp fp newOffset terminal newCol
else
Good offset col
-- FOREIGN UPPER
data Upper
= Unqualified N.Name
| Qualified N.Name N.Name
foreignUpper :: Parser Upper
foreignUpper =
Parser $ \(State fp offset terminal indent row col ctx) cok _ _ eerr ->
let (# start, end, newCol #) = foreignUpperHelp fp offset terminal col in
if start == end then
eerr (expect row newCol ctx E.CapVar)
else
let
!newState = State fp end terminal indent row newCol ctx
!name = N.fromForeignPtr fp start (end - start)
!foreign =
if start == offset then
Unqualified name
else
let !home = N.fromForeignPtr fp offset ((start - 1) - offset) in
Qualified home name
in
cok foreign newState noError
foreignUpperHelp :: ForeignPtr Word8 -> Int -> Int -> Int -> (# Int, Int, Int #)
foreignUpperHelp fp offset terminal col =
let
(# newOffset, newCol #) = chompUpper fp offset terminal col
in
if offset == newOffset then
(# offset, offset, col #)
else if isDot fp newOffset terminal then
foreignUpperHelp fp (newOffset + 1) terminal (newCol + 1)
else
(# offset, newOffset, newCol #)
-- FOREIGN ALPHA
foreignAlpha :: Parser Src.Expr_
foreignAlpha =
Parser $ \(State fp offset terminal indent row col ctx) cok _ _ eerr ->
let (# start, end, newCol, varType #) = foreignAlphaHelp fp offset terminal col in
if start == end then
eerr (E.ParseError row newCol (E.Theories ctx [E.LowVar, E.CapVar]))
else
let
!newState = State fp end terminal indent row newCol ctx
!name = N.fromForeignPtr fp start (end - start)
in
if start == offset then
if Set.member name reservedWords then
eerr noError
else
cok (Src.Var varType name) newState noError
else
let !home = N.fromForeignPtr fp offset ((start - 1) - offset) in
cok (Src.VarQual varType home name) newState noError
foreignAlphaHelp :: ForeignPtr Word8 -> Int -> Int -> Int -> (# Int, Int, Int, Src.VarType #)
foreignAlphaHelp fp offset terminal col =
let
(# lowerOffset, lowerCol #) = chompLower fp offset terminal col
in
if offset < lowerOffset then
(# offset, lowerOffset, lowerCol, Src.Value #)
else
let
(# upperOffset, upperCol #) = chompUpper fp offset terminal col
in
if offset == upperOffset then
(# offset, offset, col, Src.Ctor #)
else if isDot fp upperOffset terminal then
foreignAlphaHelp fp (upperOffset + 1) terminal (upperCol + 1)
else
(# offset, upperOffset, upperCol, Src.Ctor #)
---- CHAR CHOMPERS ----
-- DOTS
{-# INLINE isDot #-}
isDot :: ForeignPtr Word8 -> Int -> Int -> Bool
isDot fp offset terminal =
offset < terminal && unsafeIndex fp offset == 0x2e {- . -}
-- UPPER CHARS
chompUpper :: ForeignPtr Word8 -> Int -> Int -> Int -> (# Int, Int #)
chompUpper fp offset terminal col =
let !width = getUpperWidth fp offset terminal in
if width == 0 then
(# offset, col #)
else
chompInnerChars fp (offset + width) terminal (col + 1)
{-# INLINE getUpperWidth #-}
getUpperWidth :: ForeignPtr Word8 -> Int -> Int -> Int
getUpperWidth fp offset terminal =
if offset < terminal then
getUpperWidthHelp fp offset terminal (unsafeIndex fp offset)
else
0
{-# INLINE getUpperWidthHelp #-}
getUpperWidthHelp :: ForeignPtr Word8 -> Int -> Int -> Word8 -> Int
getUpperWidthHelp fp offset terminal word
| 0x41 {- A -} <= word && word <= 0x5A {- Z -} = 1
| word < 0xc0 = 0
| word < 0xe0 = if Char.isUpper (getChar2 fp offset terminal word) then 2 else 0
| word < 0xf0 = if Char.isUpper (getChar3 fp offset terminal word) then 3 else 0
| word < 0xf8 = if Char.isUpper (getChar4 fp offset terminal word) then 4 else 0
| True = 0
-- LOWER CHARS
chompLower :: ForeignPtr Word8 -> Int -> Int -> Int -> (# Int, Int #)
chompLower fp offset terminal col =
let !width = getLowerWidth fp offset terminal in
if width == 0 then
(# offset, col #)
else
chompInnerChars fp (offset + width) terminal (col + 1)
{-# INLINE getLowerWidth #-}
getLowerWidth :: ForeignPtr Word8 -> Int -> Int -> Int
getLowerWidth fp offset terminal =
if offset < terminal then
getLowerWidthHelp fp offset terminal (unsafeIndex fp offset)
else
0
{-# INLINE getLowerWidthHelp #-}
getLowerWidthHelp :: ForeignPtr Word8 -> Int -> Int -> Word8 -> Int
getLowerWidthHelp fp offset terminal word
| 0x61 {- a -} <= word && word <= 0x7A {- z -} = 1
| word < 0xc0 = 0
| word < 0xe0 = if Char.isLower (getChar2 fp offset terminal word) then 2 else 0
| word < 0xf0 = if Char.isLower (getChar3 fp offset terminal word) then 3 else 0
| word < 0xf8 = if Char.isLower (getChar4 fp offset terminal word) then 4 else 0
| True = 0
-- INNER CHARS
chompInnerChars :: ForeignPtr Word8 -> Int -> Int -> Int -> (# Int, Int #)
chompInnerChars fp !offset terminal !col =
let !width = getInnerWidth fp offset terminal in
if width == 0 then
(# offset, col #)
else
chompInnerChars fp (offset + width) terminal (col + 1)
getInnerWidth :: ForeignPtr Word8 -> Int -> Int -> Int
getInnerWidth fp offset terminal =
if offset < terminal then
getInnerWidthHelp fp offset terminal (unsafeIndex fp offset)
else
0
{-# INLINE getInnerWidthHelp #-}
getInnerWidthHelp :: ForeignPtr Word8 -> Int -> Int -> Word8 -> Int
getInnerWidthHelp fp offset terminal word
| 0x61 {- a -} <= word && word <= 0x7A {- z -} = 1
| 0x41 {- A -} <= word && word <= 0x5A {- Z -} = 1
| 0x30 {- 0 -} <= word && word <= 0x39 {- 9 -} = 1
| word == 0x5F {- _ -} = 1
| word < 0xc0 = 0
| word < 0xe0 = if Char.isAlpha (getChar2 fp offset terminal word) then 2 else 0
| word < 0xf0 = if Char.isAlpha (getChar3 fp offset terminal word) then 3 else 0
| word < 0xf8 = if Char.isAlpha (getChar4 fp offset terminal word) then 4 else 0
| True = 0
-- EXTRACT CHARACTERS
push :: Word8 -> Int -> Int
push word code =
assert (word .&. 0xc0 == 0x80) (
shiftL code 6 .|. fromEnum (word .&. 0x3f)
)
getChar2 :: ForeignPtr Word8 -> Int -> Int -> Word8 -> Char
getChar2 fp offset terminal word =
assert (offset + 2 <= terminal) (
let
!word1 = word .&. 0x1f
!word2 = unsafeIndex fp (offset + 1)
!code = push word2 (fromEnum word1)
in
assert (0x80 <= code) (
toEnum code
)
)
getChar3 :: ForeignPtr Word8 -> Int -> Int -> Word8 -> Char
getChar3 fp offset terminal word =
assert (offset + 3 <= terminal) (
let
!word1 = word .&. 0x0f
!word2 = unsafeIndex fp (offset + 1)
!word3 = unsafeIndex fp (offset + 2)
!code = push word3 (push word2 (fromEnum word1))
in
assert ((0x800 <= code && code < 0xd800) || (0xdfff < code && code < 0xfffe)) (
toEnum code
)
)
getChar4 :: ForeignPtr Word8 -> Int -> Int -> Word8 -> Char
getChar4 fp offset terminal word =
assert (offset + 4 <= terminal) (
let
!word1 = word .&. 0x07
!word2 = unsafeIndex fp (offset + 1)
!word3 = unsafeIndex fp (offset + 2)
!word4 = unsafeIndex fp (offset + 3)
!code = push word4 (push word3 (push word2 (fromEnum word1)))
in
assert (0x10000 <= code && code < 0x110000) (
toEnum code
)
)