@@ -16,7 +16,8 @@ module Parse.Space
18import Data.Word (Word8, Word16)
18import Data.Word (Word8, Word16)
19−import Foreign.Ptr (Ptr, plusPtr)
19+import Foreign.Ptr (Ptr, plusPtr, minusPtr)
20+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
21import qualified AST.Source as Src
22import qualified AST.Source as Src
22import Parse.Primitives (Row, Col)
23import Parse.Primitives (Row, Col)
@@ -39,14 +40,14 @@ type Parser x a =
40chomp :: (E.Space -> Row -> Col -> x) -> P.Parser x ()
41chomp :: (E.Space -> Row -> Col -> x) -> P.Parser x ()
41chomp toError =
42chomp toError =
42− P.Parser $ \(P.State pos end indent row col) cok _ cerr _ ->
43+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr _ ->
44 (# status, newPos, newRow, newCol #) = eatSpaces pos end row col
45 (# status, newPos, newRow, newCol #) = eatSpaces pos end row col
46 case status of
47 case status of
49− !newState = P.State newPos end indent newRow newCol
50+ !newState = P.State src newPos end indent newRow newCol
51 cok () newState
52 cok () newState
@@ -60,7 +61,7 @@ chomp toError =
61checkIndent :: A.Position -> (Row -> Col -> x) -> P.Parser x ()
62checkIndent :: A.Position -> (Row -> Col -> x) -> P.Parser x ()
62checkIndent (A.Position endRow endCol) toError =
63checkIndent (A.Position endRow endCol) toError =
63− P.Parser $ \state@(P.State _ _ indent _ col) _ eok _ eerr ->
64+ P.Parser $ \state@(P.State _ _ _ indent _ col) _ eok _ eerr ->
64 if col > indent && col > 1
65 if col > indent && col > 1
65 then eok () state
66 then eok () state
66 else eerr endRow endCol toError
67 else eerr endRow endCol toError
@@ -68,7 +69,7 @@ checkIndent (A.Position endRow endCol) toError =
69checkAligned :: (Word16 -> Row -> Col -> x) -> P.Parser x ()
70checkAligned :: (Word16 -> Row -> Col -> x) -> P.Parser x ()
70checkAligned toError =
71checkAligned toError =
71− P.Parser $ \state@(P.State _ _ indent row col) _ eok _ eerr ->
72+ P.Parser $ \state@(P.State _ _ _ indent row col) _ eok _ eerr ->
72 if col == indent
73 if col == indent
73 then eok () state
74 then eok () state
74 else eerr row col (toError indent)
75 else eerr row col (toError indent)
@@ -76,7 +77,7 @@ checkAligned toError =
77checkFreshLine :: (Row -> Col -> x) -> P.Parser x ()
78checkFreshLine :: (Row -> Col -> x) -> P.Parser x ()
78checkFreshLine toError =
79checkFreshLine toError =
79− P.Parser $ \state@(P.State _ _ _ row col) _ eok _ eerr ->
80+ P.Parser $ \state@(P.State _ _ _ _ row col) _ eok _ eerr ->
80 if col == 1
81 if col == 1
81 then eok () state
82 then eok () state
82 else eerr row col toError
83 else eerr row col toError
@@ -88,7 +89,7 @@ checkFreshLine toError =
89chompAndCheckIndent :: (E.Space -> Row -> Col -> x) -> (Row -> Col -> x) -> P.Parser x ()
90chompAndCheckIndent :: (E.Space -> Row -> Col -> x) -> (Row -> Col -> x) -> P.Parser x ()
90chompAndCheckIndent toSpaceError toIndentError =
91chompAndCheckIndent toSpaceError toIndentError =
91− P.Parser $ \(P.State pos end indent row col) cok _ cerr _ ->
92+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr _ ->
93 (# status, newPos, newRow, newCol #) = eatSpaces pos end row col
94 (# status, newPos, newRow, newCol #) = eatSpaces pos end row col
@@ -98,7 +99,7 @@ chompAndCheckIndent toSpaceError toIndentError =
101− !newState = P.State newPos end indent newRow newCol
102+ !newState = P.State src newPos end indent newRow newCol
103 cok () newState
104 cok () newState
@@ -240,7 +241,7 @@ eatMultiCommentHelp pos end row col openComments =
241docComment :: (Row -> Col -> x) -> (E.Space -> Row -> Col -> x) -> P.Parser x Src.Comment
242docComment :: (Row -> Col -> x) -> (E.Space -> Row -> Col -> x) -> P.Parser x Src.Comment
242docComment toExpectation toSpaceError =
243docComment toExpectation toSpaceError =
243− P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
244+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
245 !pos3 = plusPtr pos 3
246 !pos3 = plusPtr pos 3
@@ -250,14 +251,19 @@ docComment toExpectation toSpaceError =
250 && P.unsafeIndex (plusPtr pos 2) == 0x7C {- | -}
251 && P.unsafeIndex (plusPtr pos 2) == 0x7C {- | -}
253 (# status, newPos, newRow, newCol #) =
256 (# status, newPos, newRow, newCol #) =
254− eatMultiCommentHelp pos3 end row (col + 3) 1
257+ eatMultiCommentHelp pos3 end row col3 1
256 case status of
259 case status of
257 MultiGood ->
260 MultiGood ->
262+ !off = minusPtr pos3 (unsafeForeignPtrToPtr src)
263+ !len = minusPtr newPos pos3 - 2
264+ !snippet = P.Snippet src off len row col3
259− !comment = Src.Comment pos3 (plusPtr newPos (-2))
265+ !comment = Src.Comment snippet
260− !newState = P.State newPos end indent newRow newCol
266+ !newState = P.State src newPos end indent newRow newCol
262 cok comment newState
268 cok comment newState