A token stream parser.
Inheritance: RecursiveDescentParser
示例#1
0
        private static SyntaxTree Parse(SourceText sourceText, ParserOptions options, IIncludeFileSystem fileSystem, Func <HlslParser, SyntaxNode> parseFunc)
        {
            var lexer  = new HlslLexer(sourceText, options, fileSystem);
            var parser = new HlslParser(lexer);

            var result = new SyntaxTree(sourceText,
                                        syntaxTree => new Tuple <SyntaxNode, List <FileSegment> >(
                                            parseFunc(parser),
                                            lexer.FileSegments));

            Debug.WriteLine(DateTime.Now + " - Finished parsing");

            return(result);
        }
示例#2
0
        /// <summary>
        /// Converts the specified hlsl source code to glsl.
        /// </summary>
        /// <param name="hlslSourcecode">The HLSL source code.</param>
        /// <param name="hlslEntryPoint">The HLSL entry point.</param>
        /// <param name="stage">The stage to convert.</param>
        /// <param name="shader">The shader.</param>
        /// <param name="inputHlslFilepath">The input HLSL filepath.</param>
        /// <returns>
        /// The resulting glsl AST tree.
        /// </returns>
        public global::SiliconStudio.Shaders.Ast.Shader Convert(string hlslSourcecode, string hlslEntryPoint, PipelineStage stage, string inputHlslFilepath = null)
        {
            try
            {
                // Convert from Framework.Graphics ShaderMacro to Framework.Shaders ShaderMacro
                var macros = new global::SiliconStudio.Shaders.Parser.ShaderMacro[Macros.Count];
                for (int index = 0; index < Macros.Count; index++)
                {
                    macros[index] = new global::SiliconStudio.Shaders.Parser.ShaderMacro(Macros[index].Name, Macros[index].Definition);
                }

                var result = HlslParser.TryPreProcessAndParse(hlslSourcecode, inputHlslFilepath, macros, IncludeDirectories);

                if (result.HasErrors)
                {
                    throw new NotImplementedException("Logging");
                    //DisplayError(log, result, "Error while parsing file:");
                    return(null);
                }

                // Prepare the shader before type inference analysis
                HlslToGlslConvertor.Prepare(result.Shader);

                HlslSemanticAnalysis.Run(result);

                // If there are any type inference analysis, just display all errors but ytu
                if (result.HasErrors)
                {
                    throw new NotImplementedException("Logging");
                    //DisplayError(log, result, "Error with type inferencing:");
                }

                return(Convert(result, hlslEntryPoint, stage, inputHlslFilepath));
            }
            catch (Exception ex)
            {
                throw new NotImplementedException("Logging");
                //log.WriteLine("Unexpected error while converting file [{0}] with entry point [{1}] : {2}", inputHlslFilepath, hlslEntryPoint, ex.Message);
                return(null);
            }
        }
示例#3
0
        private void CreatesAst(string source)
        {
            //ANTLRFileStream fStream = new ANTLRFileStream(source, Encoding.ASCII);
            ANTLRStringStream stream = new ANTLRStringStream(source);
            HlslLexer         lexer  = new HlslLexer(stream);
            HlslParser        parser = new HlslParser(new CommonTokenStream(lexer));

            parser.GlobalScope = _globalScope;
            var program = parser.Program();

            _programs.Add(program);

            if (parser.Errors.Count > 0)
            {
                foreach (var item in parser.Errors)
                {
                    _log.Error(item.Message, item.Line, item.CharPositionInLine);
                }
                return;
            }
        }
示例#4
0
        /// <summary>
        /// Converts the specified hlsl source code to glsl.
        /// </summary>
        /// <param name="hlslSourcecode">The HLSL source code.</param>
        /// <param name="hlslEntryPoint">The HLSL entry point.</param>
        /// <param name="stage">The stage to convert.</param>
        /// <param name="shader">The shader.</param>
        /// <param name="inputHlslFilepath">The input HLSL filepath.</param>
        /// <returns>
        /// The resulting glsl AST tree.
        /// </returns>
        public global::SiliconStudio.Shaders.Ast.Shader Convert(string hlslSourcecode, string hlslEntryPoint, PipelineStage stage, string inputHlslFilepath, IDictionary <int, string> inputAttributeNames, LoggerResult log)
        {
            try
            {
                // Convert from Framework.Graphics ShaderMacro to Framework.Shaders ShaderMacro
                var macros = new global::SiliconStudio.Shaders.Parser.ShaderMacro[Macros.Count];
                for (int index = 0; index < Macros.Count; index++)
                {
                    macros[index] = new global::SiliconStudio.Shaders.Parser.ShaderMacro(Macros[index].Name, Macros[index].Definition);
                }

                var result = HlslParser.TryPreProcessAndParse(hlslSourcecode, inputHlslFilepath, macros, IncludeDirectories);

                if (result.HasErrors)
                {
                    log.Error(result.ToString());
                    return(null);
                }

                // Prepare the shader before type inference analysis
                HlslToGlslConvertor.Prepare(result.Shader);

                HlslSemanticAnalysis.Run(result);

                // If there are any type inference analysis, just display all errors but ytu
                if (result.HasErrors)
                {
                    log.Error(result.ToString());
                    return(null);
                }

                return(Convert(result, hlslEntryPoint, stage, inputHlslFilepath, inputAttributeNames, log));
            }
            catch (Exception ex)
            {
                log.Error("Unexpected error while converting file [{0}] with entry point [{1}]", ex, inputHlslFilepath, hlslEntryPoint);
            }
            return(null);
        }
示例#5
0
 Node ParseFile(String file, HlslAnalyzer analyzer)
 {
     HlslParser hlslParser = new HlslParser(ReadFile(file), analyzer);
     return hlslParser.Parse();
 }
示例#6
0
        Node ParseFile(String file, HlslAnalyzer analyzer)
        {
            HlslParser hlslParser = new HlslParser(ReadFile(file), analyzer);

            return(hlslParser.Parse());
        }