on master help sign in
Move string parsing to Parse.String and use the new Elm.String type
commit8906d99757916037f2b496a1772b2993a985b0a8 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 3 Jun 2019, 10:57 (7 years ago) parent42972a3
1 file changed+34−33
compiler/src/Parse/Utf8.hs → compiler/src/Parse/String.hs+34−33
compiler/src/Parse/Utf8.hs → compiler/src/Parse/String.hsrenamed+34−33
@@ -1,6 +1,6 @@
11{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
22{-# LANGUAGE BangPatterns, MagicHash, OverloadedStrings, UnboxedTuples #-}
3−module Parse.Utf8
3+module Parse.String
44 ( string
55 , character
66 )
@@ -11,6 +11,7 @@ import qualified Data.Utf8 as Utf8
1111import Data.Word (Word8, Word16)
1212import Foreign.Ptr (Ptr, plusPtr, minusPtr)
1313
14+import qualified Elm.String as ES
1415import Parse.Primitives (Parser, Row, Col)
1516import qualified Parse.Number as Number
1617import qualified Parse.Primitives as P
@@ -21,9 +22,9 @@ import qualified Reporting.Error.Syntax as E
2122-- CHARACTER
2223
2324
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
2526character 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 ->
2728 if pos >= end || P.unsafeIndex pos /= 0x27 {- ' -} then
2829 eerr row col toExpectation
2930
@@ -34,8 +35,8 @@ character toExpectation toError =
3435 cerr row col (toError (E.CharNotString (fromIntegral (newCol - col))))
3536 else
3637 let
37− !newState = P.State newPos end indent row newCol
38− !char = Utf8.fromChunks [mostRecent]
38+ !newState = P.State src newPos end indent row newCol
39+ !char = ES.fromChunks [mostRecent]
3940 in
4041 cok char newState
4142
@@ -47,12 +48,12 @@ character toExpectation toError =
4748
4849
4950data CharResult
50− = Good (Ptr Word8) Col Word16 Utf8.Chunk
51+ = Good (Ptr Word8) Col Word16 ES.Chunk
5152 | CharEndless Col
5253 | CharEscape Row Col E.Escape
5354
5455
55−chompChar :: Ptr Word8 -> Ptr Word8 -> Row -> Col -> Word16 -> Utf8.Chunk -> CharResult
56+chompChar :: Ptr Word8 -> Ptr Word8 -> Row -> Col -> Word16 -> ES.Chunk -> CharResult
5657chompChar pos end row col numChars mostRecent =
5758 if pos >= end then
5859 CharEndless col
@@ -73,10 +74,10 @@ chompChar pos end row col numChars mostRecent =
7374 else if word == 0x5C {- \ -} then
7475 case eatEscape (plusPtr pos 1) end row col of
7576 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)
7778
7879 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)
8081
8182 EscapeProblem r c badEscape ->
8283 CharEscape r c badEscape
@@ -89,16 +90,16 @@ chompChar pos end row col numChars mostRecent =
8990 !width = P.getCharWidth pos end word
9091 !newPos = plusPtr pos width
9192 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)
9394
9495
9596
9697-- STRINGS
9798
9899
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
100101string 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 ->
102103 if isDoubleQuote pos end then
103104
104105 let
@@ -121,7 +122,7 @@ string toExpectation toError =
121122 Ok newPos newRow newCol utf8 ->
122123 let
123124 !newState =
124− P.State newPos end indent newRow newCol
125+ P.State src newPos end indent newRow newCol
125126 in
126127 cok utf8 newState
127128
@@ -139,32 +140,32 @@ isDoubleQuote pos end =
139140
140141
141142data StringResult
142− = Ok (Ptr Word8) Row Col !Utf8.String
143+ = Ok (Ptr Word8) Row Col !ES.String
143144 | Err Row Col E.String
144145
145146
146−finalize :: Ptr Word8 -> Ptr Word8 -> [Utf8.Chunk] -> Utf8.String
147+finalize :: Ptr Word8 -> Ptr Word8 -> [ES.Chunk] -> ES.String
147148finalize start end revChunks =
148− Utf8.fromChunks $ reverse $
149+ ES.fromChunks $ reverse $
149150 if start == end then
150151 revChunks
151152 else
152− Utf8.Slice start (minusPtr end start) : revChunks
153+ ES.Slice start (minusPtr end start) : revChunks
153154
154155
155−addEscape :: Utf8.Chunk -> Ptr Word8 -> Ptr Word8 -> [Utf8.Chunk] -> [Utf8.Chunk]
156+addEscape :: ES.Chunk -> Ptr Word8 -> Ptr Word8 -> [ES.Chunk] -> [ES.Chunk]
156157addEscape chunk start end revChunks =
157158 if start == end then
158159 chunk : revChunks
159160 else
160− chunk : Utf8.Slice start (minusPtr end start) : revChunks
161+ chunk : ES.Slice start (minusPtr end start) : revChunks
161162
162163
163164
164165-- SINGLE STRINGS
165166
166167
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
168169singleString pos end row col initialPos revChunks =
169170 if pos >= end then
170171 Err row col E.StringEndless_Single
@@ -193,7 +194,7 @@ singleString pos end row col initialPos revChunks =
193194 EscapeUnicode delta code ->
194195 let !newPos = plusPtr pos delta in
195196 singleString newPos end row (col + fromIntegral delta) newPos $
196− addEscape (Utf8.CodePoint code) initialPos pos revChunks
197+ addEscape (ES.CodePoint code) initialPos pos revChunks
197198
198199 EscapeProblem r c x ->
199200 Err r c (E.StringEscape x)
@@ -210,7 +211,7 @@ singleString pos end row col initialPos revChunks =
210211-- MULTI STRINGS
211212
212213
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
214215multiString pos end row col initialPos sr sc revChunks =
215216 if pos >= end then
216217 Err sr sc E.StringEndless_Multi
@@ -244,7 +245,7 @@ multiString pos end row col initialPos sr sc revChunks =
244245 EscapeUnicode delta code ->
245246 let !newPos = plusPtr pos delta in
246247 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
248249
249250 EscapeProblem r c x ->
250251 Err r c (E.StringEscape x)
@@ -313,30 +314,30 @@ eatUnicode pos end row col =
313314
314315
315316{-# NOINLINE singleQuote #-}
316−singleQuote :: Utf8.Chunk
317+singleQuote :: ES.Chunk
317318singleQuote =
318− Utf8.Escape 0x27 {- ' -}
319+ ES.Escape 0x27 {-'-}
319320
320321
321322{-# NOINLINE doubleQuote #-}
322−doubleQuote :: Utf8.Chunk
323+doubleQuote :: ES.Chunk
323324doubleQuote =
324− Utf8.Escape 0x22 {- " -}
325+ ES.Escape 0x22 {-"-}
325326
326327
327328{-# NOINLINE newline #-}
328−newline :: Utf8.Chunk
329+newline :: ES.Chunk
329330newline =
330− Utf8.Escape 0x6E {- n -}
331+ ES.Escape 0x6E {-n-}
331332
332333
333334{-# NOINLINE carriageReturn #-}
334−carriageReturn :: Utf8.Chunk
335+carriageReturn :: ES.Chunk
335336carriageReturn =
336− Utf8.Escape 0x72 {- \r -}
337+ ES.Escape 0x72 {-r-}
337338
338339
339340{-# NOINLINE placeholder #-}
340−placeholder :: Utf8.Chunk
341+placeholder :: ES.Chunk
341342placeholder =
342− Utf8.CodePoint 0xFFFD {- replacement character -}
343+ ES.CodePoint 0xFFFD {-replacement character-}