@@ -6,12 +6,13 @@ module Parse.Shader
66 where
77
88
9−import qualified Data.List as List
9+import qualified Data.ByteString.Internal as B
10+import qualified Data.ByteString.UTF8 as BS_UTF8
1011import qualified Data.Map as Map
1112import qualified Data.Name as Name
12−import qualified Data.Utf8 as Utf8
1313import Data.Word (Word8)
14−import Foreign.Ptr (Ptr, plusPtr)
14+import Foreign.Ptr (Ptr, plusPtr, minusPtr)
15+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
1516import qualified Language.GLSL.Parser as GLP
1617import qualified Language.GLSL.Syntax as GLS
1718import qualified Text.Parsec as Parsec
@@ -32,16 +33,16 @@ import qualified Reporting.Error.Syntax as E
3233shader :: A.Position -> Parser E.Expr Src.Expr
3334shader start@(A.Position row col) =
3435 do block <- parseBlock
35− shdr <- parseGlsl row col src
36+ shdr <- parseGlsl row col block
3637 end <- P.getPosition
37− return (A.at start end (Src.Shader (Shader.fromChars src) shdr))
38+ return (A.at start end (Src.Shader (Shader.fromChars block) shdr))
3839
3940
4041
4142-- BLOCK
4243
4344
44−parseBlock :: Parser E.Expr P.Snippet
45+parseBlock :: Parser E.Expr [Char]
4546parseBlock =
4647 P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
4748 let
@@ -62,11 +63,12 @@ parseBlock =
6263 case status of
6364 Good ->
6465 let
65− !block = Utf8.fromPtr pos6 newPos
66− !source = Utf8.toChars block
66+ !off = minusPtr pos6 (unsafeForeignPtrToPtr src)
67+ !len = minusPtr newPos pos6
68+ !block = BS_UTF8.toString (B.PS src off len)
6769 !newState = P.State src (plusPtr newPos 2) end indent newRow (newCol + 2)
6870 in
69− cok source newState
71+ cok block newState
7072
7173 Unending ->
7274 cerr row col E.EndlessShader
@@ -102,7 +104,7 @@ eatShader pos end row col =
102104-- GLSL
103105
104106
105−parseGlsl :: Row -> Col -> String -> Parser E.Expr Shader.Types
107+parseGlsl :: Row -> Col -> [Char] -> Parser E.Expr Shader.Types
106108parseGlsl startRow startCol src =
107109 case GLP.parse src of
108110 Right (GLS.TranslationUnit decls) ->
@@ -127,7 +129,7 @@ parseGlsl startRow startCol src =
127129 else failure (startRow + row - 1) col msg
128130
129131
130−failure :: Row -> Col -> String -> Parser E.Expr a
132+failure :: Row -> Col -> [Char] -> Parser E.Expr a
131133failure row col msg =
132134 P.Parser $ \(P.State _ _ _ _ _ _) _ _ cerr _ ->
133135 cerr row col (E.ShaderProblem msg)
@@ -142,7 +144,7 @@ emptyTypes =
142144 Shader.Types Map.empty Map.empty Map.empty
143145
144146
145−addInput :: (GLS.StorageQualifier, Shader.Type, String) -> Shader.Types -> Shader.Types
147+addInput :: (GLS.StorageQualifier, Shader.Type, [Char]) -> Shader.Types -> Shader.Types
146148addInput (qual, tipe, name) glDecls =
147149 case qual of
148150 GLS.Attribute -> glDecls { Shader._attribute = Map.insert (Name.fromChars name) tipe (Shader._attribute glDecls) }
@@ -151,7 +153,7 @@ addInput (qual, tipe, name) glDecls =
151153 _ -> error "Should never happen due to `extractInputs` function"
152154
153155
154−extractInputs :: GLS.ExternalDeclaration -> [(GLS.StorageQualifier, Shader.Type, String)]
156+extractInputs :: GLS.ExternalDeclaration -> [(GLS.StorageQualifier, Shader.Type, [Char])]
155157extractInputs decl =
156158 case decl of
157159 GLS.Declaration