@@ -6,12 +6,13 @@ module Parse.Shader
9−import qualified Data.List as List
9+import qualified Data.ByteString.Internal as B
10+import qualified Data.ByteString.UTF8 as BS_UTF8
10import qualified Data.Map as Map
11import qualified Data.Map as Map
11import qualified Data.Name as Name
12import qualified Data.Name as Name
12−import qualified Data.Utf8 as Utf8
13import Data.Word (Word8)
13import Data.Word (Word8)
14−import Foreign.Ptr (Ptr, plusPtr)
14+import Foreign.Ptr (Ptr, plusPtr, minusPtr)
15+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
15import qualified Language.GLSL.Parser as GLP
16import qualified Language.GLSL.Parser as GLP
16import qualified Language.GLSL.Syntax as GLS
17import qualified Language.GLSL.Syntax as GLS
17import qualified Text.Parsec as Parsec
18import qualified Text.Parsec as Parsec
@@ -32,16 +33,16 @@ import qualified Reporting.Error.Syntax as E
32shader :: A.Position -> Parser E.Expr Src.Expr
33shader :: A.Position -> Parser E.Expr Src.Expr
33shader start@(A.Position row col) =
34shader start@(A.Position row col) =
34 do block <- parseBlock
35 do block <- parseBlock
35− shdr <- parseGlsl row col src
36+ shdr <- parseGlsl row col block
36 end <- P.getPosition
37 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))
44−parseBlock :: Parser E.Expr P.Snippet
45+parseBlock :: Parser E.Expr [Char]
45parseBlock =
46parseBlock =
46 P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
47 P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
@@ -62,11 +63,12 @@ parseBlock =
62 case status of
63 case status of
65− !block = Utf8.fromPtr pos6 newPos
66+ !off = minusPtr pos6 (unsafeForeignPtrToPtr src)
66− !source = Utf8.toChars block
67+ !len = minusPtr newPos pos6
68+ !block = BS_UTF8.toString (B.PS src off len)
67 !newState = P.State src (plusPtr newPos 2) end indent newRow (newCol + 2)
69 !newState = P.State src (plusPtr newPos 2) end indent newRow (newCol + 2)
69− cok source newState
71+ cok block newState
71 Unending ->
73 Unending ->
72 cerr row col E.EndlessShader
74 cerr row col E.EndlessShader
@@ -102,7 +104,7 @@ eatShader pos end row col =
105−parseGlsl :: Row -> Col -> String -> Parser E.Expr Shader.Types
107+parseGlsl :: Row -> Col -> [Char] -> Parser E.Expr Shader.Types
106parseGlsl startRow startCol src =
108parseGlsl startRow startCol src =
107 case GLP.parse src of
109 case GLP.parse src of
108 Right (GLS.TranslationUnit decls) ->
110 Right (GLS.TranslationUnit decls) ->
@@ -127,7 +129,7 @@ parseGlsl startRow startCol src =
127 else failure (startRow + row - 1) col msg
129 else failure (startRow + row - 1) col msg
130−failure :: Row -> Col -> String -> Parser E.Expr a
132+failure :: Row -> Col -> [Char] -> Parser E.Expr a
131failure row col msg =
133failure row col msg =
132 P.Parser $ \(P.State _ _ _ _ _ _) _ _ cerr _ ->
134 P.Parser $ \(P.State _ _ _ _ _ _) _ _ cerr _ ->
133 cerr row col (E.ShaderProblem msg)
135 cerr row col (E.ShaderProblem msg)
@@ -142,7 +144,7 @@ emptyTypes =
142 Shader.Types Map.empty Map.empty Map.empty
144 Shader.Types Map.empty Map.empty Map.empty
145−addInput :: (GLS.StorageQualifier, Shader.Type, String) -> Shader.Types -> Shader.Types
147+addInput :: (GLS.StorageQualifier, Shader.Type, [Char]) -> Shader.Types -> Shader.Types
146addInput (qual, tipe, name) glDecls =
148addInput (qual, tipe, name) glDecls =
147 case qual of
149 case qual of
148 GLS.Attribute -> glDecls { Shader._attribute = Map.insert (Name.fromChars name) tipe (Shader._attribute glDecls) }
150 GLS.Attribute -> glDecls { Shader._attribute = Map.insert (Name.fromChars name) tipe (Shader._attribute glDecls) }
@@ -151,7 +153,7 @@ addInput (qual, tipe, name) glDecls =
151 _ -> error "Should never happen due to `extractInputs` function"
153 _ -> error "Should never happen due to `extractInputs` function"
154−extractInputs :: GLS.ExternalDeclaration -> [(GLS.StorageQualifier, Shader.Type, String)]
156+extractInputs :: GLS.ExternalDeclaration -> [(GLS.StorageQualifier, Shader.Type, [Char])]
155extractInputs decl =
157extractInputs decl =
156 case decl of
158 case decl of
157 GLS.Declaration
159 GLS.Declaration