on master help sign in
Move string parsing to Parse.String and use the new Elm.String type
8906d99Evan Czaplicki
compiler/src/Parse/Utf8.hs → compiler/src/Parse/String.hsrenamed+34−33
@@ -1,6 +1,6 @@
1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2{-# LANGUAGE BangPatterns, MagicHash, OverloadedStrings, UnboxedTuples #-}
2{-# LANGUAGE BangPatterns, MagicHash, OverloadedStrings, UnboxedTuples #-}
3−module Parse.Utf8
3+module Parse.String
4 ( string
4 ( string
5 , character
5 , character
6 )
6 )
@@ -11,6 +11,7 @@ import qualified Data.Utf8 as Utf8
11import Data.Word (Word8, Word16)
11import Data.Word (Word8, Word16)
12import Foreign.Ptr (Ptr, plusPtr, minusPtr)
12import Foreign.Ptr (Ptr, plusPtr, minusPtr)
13
13
14+import qualified Elm.String as ES
14import Parse.Primitives (Parser, Row, Col)
15import Parse.Primitives (Parser, Row, Col)
15import qualified Parse.Number as Number
16import qualified Parse.Number as Number
16import qualified Parse.Primitives as P
17import qualified Parse.Primitives as P
@@ -21,9 +22,9 @@ import qualified Reporting.Error.Syntax as E
21-- CHARACTER
22-- CHARACTER
22
23
23
24
24−character :: (Row -> Col -> x) -> (E.Char -> Row -> Col -> x) -> Parser x Utf8.String
25+character :: (Row -> Col -> x) -> (E.Char -> Row -> Col -> x) -> Parser x ES.String
25character toExpectation toError =
26character toExpectation toError =
26− P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
27+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
27 if pos >= end || P.unsafeIndex pos /= 0x27 {- ' -} then
28 if pos >= end || P.unsafeIndex pos /= 0x27 {- ' -} then
28 eerr row col toExpectation
29 eerr row col toExpectation
29
30
@@ -34,8 +35,8 @@ character toExpectation toError =
34 cerr row col (toError (E.CharNotString (fromIntegral (newCol - col))))
35 cerr row col (toError (E.CharNotString (fromIntegral (newCol - col))))
35 else
36 else
36 let
37 let
37− !newState = P.State newPos end indent row newCol
38+ !newState = P.State src newPos end indent row newCol
38− !char = Utf8.fromChunks [mostRecent]
39+ !char = ES.fromChunks [mostRecent]
39 in
40 in
40 cok char newState
41 cok char newState
41
42
@@ -47,12 +48,12 @@ character toExpectation toError =
47
48
48
49
49data CharResult
50data CharResult
50− = Good (Ptr Word8) Col Word16 Utf8.Chunk
51+ = Good (Ptr Word8) Col Word16 ES.Chunk
51 | CharEndless Col
52 | CharEndless Col
52 | CharEscape Row Col E.Escape
53 | CharEscape Row Col E.Escape
53
54
54
55
55−chompChar :: Ptr Word8 -> Ptr Word8 -> Row -> Col -> Word16 -> Utf8.Chunk -> CharResult
56+chompChar :: Ptr Word8 -> Ptr Word8 -> Row -> Col -> Word16 -> ES.Chunk -> CharResult
56chompChar pos end row col numChars mostRecent =
57chompChar pos end row col numChars mostRecent =
57 if pos >= end then
58 if pos >= end then
58 CharEndless col
59 CharEndless col
@@ -73,10 +74,10 @@ chompChar pos end row col numChars mostRecent =
73 else if word == 0x5C {- \ -} then
74 else if word == 0x5C {- \ -} then
74 case eatEscape (plusPtr pos 1) end row col of
75 case eatEscape (plusPtr pos 1) end row col of
75 EscapeNormal ->
76 EscapeNormal ->
76− chompChar (plusPtr pos 2) end row (col + 2) (numChars + 1) (Utf8.Slice pos 2)
77+ chompChar (plusPtr pos 2) end row (col + 2) (numChars + 1) (ES.Slice pos 2)
77
78
78 EscapeUnicode delta code ->
79 EscapeUnicode delta code ->
79− chompChar (plusPtr pos delta) end row (col + fromIntegral delta) (numChars + 1) (Utf8.CodePoint code)
80+ chompChar (plusPtr pos delta) end row (col + fromIntegral delta) (numChars + 1) (ES.CodePoint code)
80
81
81 EscapeProblem r c badEscape ->
82 EscapeProblem r c badEscape ->
82 CharEscape r c badEscape
83 CharEscape r c badEscape
@@ -89,16 +90,16 @@ chompChar pos end row col numChars mostRecent =
89 !width = P.getCharWidth pos end word
90 !width = P.getCharWidth pos end word
90 !newPos = plusPtr pos width
91 !newPos = plusPtr pos width
91 in
92 in
92− chompChar newPos end row (col + 1) (numChars + 1) (Utf8.Slice pos width)
93+ chompChar newPos end row (col + 1) (numChars + 1) (ES.Slice pos width)
93
94
94
95
95
96
96-- STRINGS
97-- STRINGS
97
98
98
99
99−string :: (Row -> Col -> x) -> (E.String -> Row -> Col -> x) -> Parser x Utf8.String
100+string :: (Row -> Col -> x) -> (E.String -> Row -> Col -> x) -> Parser x ES.String
100string toExpectation toError =
101string toExpectation toError =
101− P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
102+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
102 if isDoubleQuote pos end then
103 if isDoubleQuote pos end then
103
104
104 let
105 let
@@ -121,7 +122,7 @@ string toExpectation toError =
121 Ok newPos newRow newCol utf8 ->
122 Ok newPos newRow newCol utf8 ->
122 let
123 let
123 !newState =
124 !newState =
124− P.State newPos end indent newRow newCol
125+ P.State src newPos end indent newRow newCol
125 in
126 in
126 cok utf8 newState
127 cok utf8 newState
127
128
@@ -139,32 +140,32 @@ isDoubleQuote pos end =
139
140
140
141
141data StringResult
142data StringResult
142− = Ok (Ptr Word8) Row Col !Utf8.String
143+ = Ok (Ptr Word8) Row Col !ES.String
143 | Err Row Col E.String
144 | Err Row Col E.String
144
145
145
146
146−finalize :: Ptr Word8 -> Ptr Word8 -> [Utf8.Chunk] -> Utf8.String
147+finalize :: Ptr Word8 -> Ptr Word8 -> [ES.Chunk] -> ES.String
147finalize start end revChunks =
148finalize start end revChunks =
148− Utf8.fromChunks $ reverse $
149+ ES.fromChunks $ reverse $
149 if start == end then
150 if start == end then
150 revChunks
151 revChunks
151 else
152 else
152− Utf8.Slice start (minusPtr end start) : revChunks
153+ ES.Slice start (minusPtr end start) : revChunks
153
154
154
155
155−addEscape :: Utf8.Chunk -> Ptr Word8 -> Ptr Word8 -> [Utf8.Chunk] -> [Utf8.Chunk]
156+addEscape :: ES.Chunk -> Ptr Word8 -> Ptr Word8 -> [ES.Chunk] -> [ES.Chunk]
156addEscape chunk start end revChunks =
157addEscape chunk start end revChunks =
157 if start == end then
158 if start == end then
158 chunk : revChunks
159 chunk : revChunks
159 else
160 else
160− chunk : Utf8.Slice start (minusPtr end start) : revChunks
161+ chunk : ES.Slice start (minusPtr end start) : revChunks
161
162
162
163
163
164
164-- SINGLE STRINGS
165-- SINGLE STRINGS
165
166
166
167
167−singleString :: Ptr Word8 -> Ptr Word8 -> Word16 -> Word16 -> Ptr Word8 -> [Utf8.Chunk] -> StringResult
168+singleString :: Ptr Word8 -> Ptr Word8 -> Word16 -> Word16 -> Ptr Word8 -> [ES.Chunk] -> StringResult
168singleString pos end row col initialPos revChunks =
169singleString pos end row col initialPos revChunks =
169 if pos >= end then
170 if pos >= end then
170 Err row col E.StringEndless_Single
171 Err row col E.StringEndless_Single
@@ -193,7 +194,7 @@ singleString pos end row col initialPos revChunks =
193 EscapeUnicode delta code ->
194 EscapeUnicode delta code ->
194 let !newPos = plusPtr pos delta in
195 let !newPos = plusPtr pos delta in
195 singleString newPos end row (col + fromIntegral delta) newPos $
196 singleString newPos end row (col + fromIntegral delta) newPos $
196− addEscape (Utf8.CodePoint code) initialPos pos revChunks
197+ addEscape (ES.CodePoint code) initialPos pos revChunks
197
198
198 EscapeProblem r c x ->
199 EscapeProblem r c x ->
199 Err r c (E.StringEscape x)
200 Err r c (E.StringEscape x)
@@ -210,7 +211,7 @@ singleString pos end row col initialPos revChunks =
210-- MULTI STRINGS
211-- MULTI STRINGS
211
212
212
213
213−multiString :: Ptr Word8 -> Ptr Word8 -> Word16 -> Word16 -> Ptr Word8 -> Word16 -> Word16 -> [Utf8.Chunk] -> StringResult
214+multiString :: Ptr Word8 -> Ptr Word8 -> Word16 -> Word16 -> Ptr Word8 -> Word16 -> Word16 -> [ES.Chunk] -> StringResult
214multiString pos end row col initialPos sr sc revChunks =
215multiString pos end row col initialPos sr sc revChunks =
215 if pos >= end then
216 if pos >= end then
216 Err sr sc E.StringEndless_Multi
217 Err sr sc E.StringEndless_Multi
@@ -244,7 +245,7 @@ multiString pos end row col initialPos sr sc revChunks =
244 EscapeUnicode delta code ->
245 EscapeUnicode delta code ->
245 let !newPos = plusPtr pos delta in
246 let !newPos = plusPtr pos delta in
246 multiString newPos end row (col + fromIntegral delta) newPos sr sc $
247 multiString newPos end row (col + fromIntegral delta) newPos sr sc $
247− addEscape (Utf8.CodePoint code) initialPos pos revChunks
248+ addEscape (ES.CodePoint code) initialPos pos revChunks
248
249
249 EscapeProblem r c x ->
250 EscapeProblem r c x ->
250 Err r c (E.StringEscape x)
251 Err r c (E.StringEscape x)
@@ -313,30 +314,30 @@ eatUnicode pos end row col =
313
314
314
315
315{-# NOINLINE singleQuote #-}
316{-# NOINLINE singleQuote #-}
316−singleQuote :: Utf8.Chunk
317+singleQuote :: ES.Chunk
317singleQuote =
318singleQuote =
318− Utf8.Escape 0x27 {- ' -}
319+ ES.Escape 0x27 {-'-}
319
320
320
321
321{-# NOINLINE doubleQuote #-}
322{-# NOINLINE doubleQuote #-}
322−doubleQuote :: Utf8.Chunk
323+doubleQuote :: ES.Chunk
323doubleQuote =
324doubleQuote =
324− Utf8.Escape 0x22 {- " -}
325+ ES.Escape 0x22 {-"-}
325
326
326
327
327{-# NOINLINE newline #-}
328{-# NOINLINE newline #-}
328−newline :: Utf8.Chunk
329+newline :: ES.Chunk
329newline =
330newline =
330− Utf8.Escape 0x6E {- n -}
331+ ES.Escape 0x6E {-n-}
331
332
332
333
333{-# NOINLINE carriageReturn #-}
334{-# NOINLINE carriageReturn #-}
334−carriageReturn :: Utf8.Chunk
335+carriageReturn :: ES.Chunk
335carriageReturn =
336carriageReturn =
336− Utf8.Escape 0x72 {- \r -}
337+ ES.Escape 0x72 {-r-}
337
338
338
339
339{-# NOINLINE placeholder #-}
340{-# NOINLINE placeholder #-}
340−placeholder :: Utf8.Chunk
341+placeholder :: ES.Chunk
341placeholder =
342placeholder =
342− Utf8.CodePoint 0xFFFD {- replacement character -}
343+ ES.CodePoint 0xFFFD {-replacement character-}