private static void CompileUniforms(ShaderGroup output, ZScriptTokenizer t)
        {
            // so a type of a variable is normally identifier+array dimensions
            // array dimensions may also exist on the variable itself (oh god this shitty C syntax)
            t.SkipWhitespace();
            ZScriptToken token;

            token = t.ExpectToken(ZScriptTokenType.OpenCurly);
            if (!(token?.IsValid ?? true))
            {
                throw new ShaderCompileException("Expected uniforms block, got {0}", token?.ToString() ?? "<EOF>");
            }

            while (true)
            {
                t.SkipWhitespace();
                token = t.ExpectToken(ZScriptTokenType.Identifier, ZScriptTokenType.CloseCurly);
                if (!(token?.IsValid ?? true))
                {
                    throw new ShaderCompileException("Expected uniform or end of block, got {0}", token?.ToString() ?? "<EOF>");
                }

                if (token.Type == ZScriptTokenType.CloseCurly)
                {
                    break; // done reading uniforms
                }
                // first goes the name, then array dimensions, then the variable name, then array dimensions, then initializer
                ShaderField field = new ShaderField();
                field.Line     = t.PositionToLine(token.Position);
                field.TypeName = token.Value;

                CompileShaderField(field, t);

                // done reading field, add it
                output.Uniforms.Add(field);
            }
        }
        private static void CompileShader(ShaderGroup output, ZScriptTokenizer t)
        {
            t.SkipWhitespace();

            ZScriptToken token = t.ExpectToken(ZScriptTokenType.Identifier);

            if (!(token?.IsValid ?? true))
            {
                throw new ShaderCompileException("Expected shader identifier, got {0}", token?.ToString() ?? "<EOF>");
            }

            t.SkipWhitespace();

            Shader s = new Shader(output);

            s.Name = token.Value;
            output.Shaders.Add(s);

            token = t.ExpectToken(ZScriptTokenType.Identifier, ZScriptTokenType.OpenCurly);
            if (!(token?.IsValid ?? true))
            {
                throw new ShaderCompileException("Expected parent identifier or shader block, got {0}", token?.ToString() ?? "<EOF>");
            }

            // has parent shader id
            if (token.Type == ZScriptTokenType.Identifier)
            {
                if (token.Value != "extends")
                {
                    throw new ShaderCompileException("Expected 'extends', got {0}", token.ToString());
                }

                t.SkipWhitespace();
                token = t.ExpectToken(ZScriptTokenType.Identifier);
                if (!(token?.IsValid ?? true))
                {
                    throw new ShaderCompileException("Expected parent identifier, got {0}", token?.ToString() ?? "<EOF>");
                }

                s.ParentName = token.Value;

                t.SkipWhitespace();
                token = t.ExpectToken(ZScriptTokenType.OpenCurly);
                if (!(token?.IsValid ?? true))
                {
                    throw new ShaderCompileException("Expected shader block, got {0}", token?.ToString() ?? "<EOF>");
                }
            }


            s.CodeLine = t.PositionToLine(token.Position);

            while (true)
            {
                t.SkipWhitespace();
                token = t.ExpectToken(ZScriptTokenType.Identifier, ZScriptTokenType.CloseCurly);
                if (!(token?.IsValid ?? true))
                {
                    throw new ShaderCompileException("Expected shader sub-block or end of block, got {0}", token?.ToString() ?? "<EOF>");
                }

                if (token.Type == ZScriptTokenType.CloseCurly)
                {
                    break;
                }

                switch (token.Value)
                {
                case "in":
                    s.In = CompileShaderDataBlock(t);
                    break;

                case "out":
                    s.Out = CompileShaderDataBlock(t);
                    break;

                case "v2f":
                    s.V2F = CompileShaderDataBlock(t);
                    break;

                case "functions":
                    CompileShaderFunctions(s, t);
                    break;

                case "vertex":
                    s.SourceVertex = CompileShaderSource(t);
                    if (s.SourceVertex != null && s.SourceVertex.Count > 0)
                    {
                        s.SourceVertexLine = t.PositionToLine(s.SourceVertex[0].Position);
                    }
                    break;

                case "fragment":
                    s.SourceFragment = CompileShaderSource(t);
                    if (s.SourceFragment != null && s.SourceFragment.Count > 0)
                    {
                        s.SourceFragmentLine = t.PositionToLine(s.SourceFragment[0].Position);
                    }
                    break;

                default:
                    throw new ShaderCompileException("Expected shader sub-block, got {0}", token.ToString());
                }
            }
        }
        private static void CompileFunctions(ShaderGroup output, ZScriptTokenizer t)
        {
            t.SkipWhitespace();
            ZScriptToken token;

            token = t.ExpectToken(ZScriptTokenType.OpenCurly);
            if (!(token?.IsValid ?? true))
            {
                throw new ShaderCompileException("Expected functions block, got {0}", token?.ToString() ?? "<EOF>");
            }

            while (true)
            {
                t.SkipWhitespace();
                token = t.ExpectToken(ZScriptTokenType.Identifier, ZScriptTokenType.CloseCurly);
                if (!(token?.IsValid ?? true))
                {
                    throw new ShaderCompileException("Expected function or end of block, got {0}", token?.ToString() ?? "<EOF>");
                }

                if (token.Type == ZScriptTokenType.CloseCurly)
                {
                    break; // done reading functions
                }
                bool isoverride = false;
                if (token.Value == "override")
                {
                    isoverride = true;

                    t.SkipWhitespace();
                    token = t.ExpectToken(ZScriptTokenType.Identifier);
                    if (!(token?.IsValid ?? true))
                    {
                        throw new ShaderCompileException("Expected function return type, got {0}", token?.ToString() ?? "<EOF>");
                    }
                }

                // <return value> <name> (<arguments>) { <code> }
                ShaderFunction func = new ShaderFunction();
                func.Line        = t.PositionToLine(token.Position);
                func.ReturnValue = token.Value;
                func.Override    = isoverride;

                CompileShaderFunction(func, t);

                // check if function with such name already exists in the shader
                // delete it.
                // overloading is not supported for now
                for (int i = 0; i < output.Functions.Count; i++)
                {
                    if (output.Functions[i].Name == func.Name)
                    {
                        if (!isoverride)
                        {
                            throw new ShaderCompileException("Function {0} is double-defined without 'override' keyword! (previous declaration at line {1})", func.Name, output.Functions[i].Line);
                        }
                        output.Functions.RemoveAt(i);
                        i--;
                        continue;
                    }
                }

                output.Functions.Add(func);
            }
        }
 public Shader(ShaderGroup group)
 {
     Group = group;
 }
        public static ShaderGroup Compile(string src)
        {
            ShaderGroup output = new ShaderGroup();

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(src)))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    ZScriptTokenizer t = new ZScriptTokenizer(br);

                    // main cycle
                    // in the root scope, we allow three blocks:
                    //  - uniforms{}
                    //  - functions{}
                    //  - shader <name> {}
                    // everything else is a syntax error.
                    while (true)
                    {
                        t.SkipWhitespace();
                        ZScriptToken token = t.ExpectToken(ZScriptTokenType.Identifier);
                        if (token == null)
                        {
                            break;
                        }

                        if (!token.IsValid)
                        {
                            throw new ShaderCompileException("Expected 'uniforms', 'functions', or 'shader'; got {0}", token.ToString());
                        }

                        switch (token.Value)
                        {
                        case "uniforms":
                            CompileUniforms(output, t);
                            break;

                        case "functions":
                            CompileFunctions(output, t);
                            break;

                        case "shader":
                            CompileShader(output, t);
                            break;

                        default:
                            throw new ShaderCompileException("Expected 'uniforms', 'functions', or 'shader'; got {0}", token.ToString());
                        }
                    }

                    // done parsing, postprocess - apply parents
                    foreach (Shader s in output.Shaders)
                    {
                        List <string> parents = new List <string>();
                        parents.Add(s.Name);
                        Shader p = s;
                        while (p.ParentName != null && p.ParentName != "")
                        {
                            string parentName = p.ParentName;
                            if (parents.Contains(parentName))
                            {
                                throw new ShaderCompileException("Recursive parent shader {0} found", parentName);
                            }
                            parents.Add(parentName);
                            p = output.GetShader(parentName);
                            if (p == null)
                            {
                                throw new ShaderCompileException("Parent shader {0} not found", parentName);
                            }

                            if (s.In == null)
                            {
                                s.In = p.In;
                            }
                            if (s.Out == null)
                            {
                                s.Out = p.Out;
                            }
                            if (s.V2F == null)
                            {
                                s.V2F = p.V2F;
                            }
                            if (s.SourceFragment == null)
                            {
                                s.SourceFragment     = p.SourceFragment;
                                s.SourceFragmentLine = p.SourceFragmentLine;
                            }
                            if (s.SourceVertex == null)
                            {
                                s.SourceVertex     = p.SourceVertex;
                                s.SourceVertexLine = p.SourceVertexLine;
                            }

                            // add functions from parent
                            foreach (ShaderFunction func in p.Functions)
                            {
                                if (s.GetFunction(func.Name) == null)
                                {
                                    s.Functions.Add(func);
                                }
                            }
                        }
                    }

                    return(output);
                }
        }