4 using System.Collections.Generic;
5 using System.Diagnostics;
10 using SiliconStudio.Shaders.Ast;
11 using SiliconStudio.Shaders.Grammar;
12 using SiliconStudio.Shaders.Utility;
15 namespace SiliconStudio.Shaders.
Parser
22 private static readonly Dictionary<Type, ShaderLanguageData> LanguageDatas =
new Dictionary<Type, ShaderLanguageData>();
76 if (includeDirectories != null)
77 defaultHandler.AddDirectories(includeDirectories);
79 defaultHandler.AddDirectory(Environment.CurrentDirectory);
81 var directoryName = Path.GetDirectoryName(sourceFileName);
82 if (!
string.IsNullOrEmpty(directoryName))
83 defaultHandler.AddDirectory(directoryName);
86 var preprocessedSource = PreProcessor.Run(source, sourceFileName, macros, defaultHandler);
89 return Parse(preprocessedSource, sourceFileName);
103 var result = TryPreProcessAndParse(source, sourceFileName, macros, includeDirectories);
104 return Check(result, sourceFileName);
117 if (!LanguageDatas.TryGetValue(typeof(T), out languageData))
120 LanguageDatas.Add(typeof(T), languageData);
132 public static T GetGrammar<T>() where T : ShaderGrammar,
new()
137 if (!LanguageDatas.TryGetValue(typeof(T), out languageData))
140 LanguageDatas.Add(typeof(T), languageData);
143 return (T)languageData.Grammar;
156 var clock =
new Stopwatch();
158 var parseTree = Parser.Parse(sourceCode, file);
163 TimeToParse = clock.ElapsedMilliseconds,
164 TokenCount = parseTree.Tokens.Count,
168 if (parseTree.Root != null && parseTree.Root.AstNode != null)
170 result.Shader = (
Shader)((IronyBrowsableNode)parseTree.Root.AstNode).
Node;
174 result.HasErrors =
true;
179 HandleMessages(parseTree, file, result);
192 var result = Parse(sourceCode, file);
193 return Check(result, file);
202 var errorText =
new StringBuilder();
203 errorText.AppendFormat(
"Unable to parse file [{0}]", sourceFileName).AppendLine();
204 foreach (var reportMessage
in result.
Messages)
206 errorText.AppendLine(reportMessage.ToString());
208 throw new InvalidOperationException(errorText.ToString());
210 return result.Shader;
218 switch (parserMessage.Level)
220 case ParserErrorLevel.Info:
221 level = ReportMessageLevel.Info;
223 case ParserErrorLevel.Error:
224 level = ReportMessageLevel.Error;
226 case ParserErrorLevel.Warning:
227 level = ReportMessageLevel.Warning;
Methods used to create the Abstract Syntax Tree..
Default IncludeHandler implementation loading files from a set of predefined directories.
Macro to be used with PreProcessor.
static Shader Check(ParsingResult result, string sourceFileName)
static SourceLocation Convert(Irony.Parsing.SourceLocation sourceLocation)
ParsingResult TryPreProcessAndParse(string source, string sourceFileName, ShaderMacro[] macros=null, params string[] includeDirectories)
Preprocesses and parses the specified source.
readonly ParserMessageList ParserMessages
Shader PreProcessAndParse(string source, string sourceFileName, ShaderMacro[] macros=null, params string[] includeDirectories)
Preprocesses and parses the specified source.
bool HasErrors
Gets or sets a value indicating whether this instance has errors.
IList< ReportMessage > Messages
Gets or sets the messages.
ReportMessageLevel
Level of a ReportMessage.
ParsingResult Parse(string sourceCode, string file)
Parses the specified source code.
Toplevel container of a shader parsing result.
SiliconStudio.Shaders.Ast.SourceSpan SourceSpan
Shader ParseAndCheck(string sourceCode, string file)
Parse a source code file and check that the result is valid.
Grammar Grammar
The linked Grammar