Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
HlslGrammar.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Diagnostics.CodeAnalysis;
5 using System.Text.RegularExpressions;
6 
7 using Irony.Parsing;
8 using SiliconStudio.Shaders.Ast;
9 using SiliconStudio.Shaders.Ast.Hlsl;
10 using SiliconStudio.Shaders.Utility;
11 
12 namespace SiliconStudio.Shaders.Grammar.Hlsl
13 {
14  /// <summary>
15  /// Grammar for Hlsl.
16  /// </summary>
17  [Language("hlsl", "5.0", "Sample hlsl grammar")]
18  [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
19  [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore", Justification = "Reviewed. Suppression is OK here.")]
20  [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1304:NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter", Justification = "Reviewed. Suppression is OK here.")]
21  public partial class HlslGrammar : ShaderGrammar
22  {
23  // ReSharper disable InconsistentNaming
24  // ------------------------------------------------------------------------------------
25  // Literals
26  // ------------------------------------------------------------------------------------
27  protected readonly Terminal string_literal_raw = new Terminal("string") { AstNodeConfig = new TokenInfo(TokenCategory.String) };
28  protected readonly NonTerminal string_literal = T("string_literal", CreateStringLiteralAst);
29 
30  // ------------------------------------------------------------------------------------
31  // NonTerminals
32  // ------------------------------------------------------------------------------------
33  protected readonly NonTerminal annotations = T("annotations", CreateAnnotationsAst);
34  protected readonly NonTerminal annotations_opt = T("annotations_opt", CreateAnnotationsOptAst);
35  protected readonly NonTerminal asm_expression = T("asm_expression", CreateAsmAst);
36  private readonly NamedBlockKeyTerm asm_block = new NamedBlockKeyTerm("asm_block", "asm") { AstNodeConfig = new TokenInfo() { TokenCategory = TokenCategory.Keyword }};
37  protected readonly NonTerminal attribute_list_opt = T("attribute_list_opt", CreateListFromNode<AttributeBase>);
38  protected readonly NonTerminal attribute_modifier = T("attribute_modifier", CreateAttributeAst);
39  protected readonly NonTerminal buffer_type = T("buffer_type", CreateGenericTypeAst<ObjectType>);
40  protected readonly NonTerminal byte_address_buffer = T("byte_address_buffer", CreateTypeNameFromTokenAst);
41  protected readonly NonTerminal cast_expression_raw = T("cast_expression_raw", CreateCastExpressionAst);
42  protected readonly NonTerminal class_base_type = T("class_base_type", CreateClassBaseTypeAst);
43  protected readonly NonTerminal class_base_type_list = T("class_base_type_list", CreateListFromNode<TypeName>);
44  protected readonly NonTerminal class_specifier = T("class_specifier", CreateClassDeclarationAst);
45  protected readonly NonTerminal compile_expression = T("compile_expression", CreateCompileExpressionAst);
46  protected readonly NonTerminal constant_buffer_resource = T("constant_buffer_resource", CreateConstantBufferAst);
47  protected readonly NonTerminal constant_buffer_resource_type = T("constant_buffer_resource_type", CreateConstantBufferTypeAst);
48  protected readonly NonTerminal float_qualifier = T("float_qualifier", CreateFloatQualifier);
49  protected readonly NonTerminal geometry_stream = TT("geomery_stream");
50  protected readonly NonTerminal identifier_dot = T("identifier_dot", CreateIdentifierDotAst);
51  protected readonly NonTerminal identifier_or_dot = TT("identifier_or_dot");
52  protected readonly NonTerminal identifier_ns = T("identifier_ns", CreateIdentifierNsAst);
53  protected readonly NonTerminal identifier_ns_list = T("identifier_ns_list", CreateListFromNode<Identifier>);
54  protected readonly NonTerminal identifier_dot_list = T("identifier_dot_list", CreateListFromNode<Identifier>);
55  protected readonly NonTerminal identifier_generic_parameter_list = T("identifier_generic_parameter_list", CreateListFromNode<Identifier>);
56  protected readonly NonTerminal identifier_generic = T("identifier_generic", CreateIdentifierGenericAst);
57  protected readonly NonTerminal identifier_or_generic = TT("identifier_or_generic");
58  protected readonly NonTerminal type_generic = T("type_generic", CreateTypeGenericAst);
59  protected readonly NonTerminal identifier_special_reference_expression = T("identifier_special_reference_expression", CreateIdentifierSpecialReferenceAst);
60  protected readonly NonTerminal identifier_keyword = T("identifier_keyword", CreateIdentifierAst);
61  protected readonly NonTerminal indexable_identifier_declarator_list = T("indexable_identifier_declarator_list", CreateIdentifierListAst);
62  protected readonly NonTerminal identifier_sub_generic = T("identifier_sub_generic");
63  protected readonly NonTerminal interface_specifier = T("interface_specifier", CreateInterfaceAst);
64  protected readonly NonTerminal line_stream = T("line_strean", CreateGenericTypeAst<ObjectType>);
65  protected readonly NonTerminal method_operator_identifier = T("method_operator_identifier", CreateMethodOperatorIdentifierAst);
66  protected readonly NonTerminal method_special_identifier = T("method_special_identifier", CreateMethodSpecialIdentifierAst);
67  protected readonly NonTerminal packoffset = T("packoffset", CreatePackOffsetAst);
68  protected readonly NonTerminal parameter_qualifier = T("parameter_qualifier");
69  protected readonly NonTerminal parameter_qualifier_pre_list_opt = T("parameter_qualifier_pre_list_opt", CreateQualifiersAst);
70  protected readonly NonTerminal pass_definition = T("pass_definition", CreatePassAst);
71  protected readonly NonTerminal pass_keyword = TT("pass_keyword");
72  protected readonly NonTerminal pass_statement = T("pass_statement", CreatePassStatementAst);
73  protected readonly NonTerminal patch_generic_type = T("patch_generic_type", CreateGenericTypeAst<ObjectType, Literal>);
74  protected readonly NonTerminal patch_type = T("patch_type", CreateTypeNameFromTokenAst);
75  protected readonly NonTerminal point_stream = T("point_stream", CreateGenericTypeAst<ObjectType>);
76  protected readonly NonTerminal register = T("register", CreateRegisterLocationAst);
77  protected readonly NonTerminal state_type = T("state_type", CreateTypeFromTokenAst<StateType>);
78  protected readonly NonTerminal semantic = T("semantic", CreateSemanticAst);
79  protected readonly NonTerminal semantic_list_opt = T("semantic_list_opt", CreateQualifiersAst);
80  protected readonly NonTerminal shader_objects = T("shader_objects", CreateTypeNameFromTokenAst);
81  protected readonly NonTerminal state_expression = T("state_expression", CreateStateExpressionAst);
82  protected readonly NonTerminal state_initializer = T("state_initializer", CreateStateValuesAst);
83  protected readonly NonTerminal state_initializer_list = T("state_initializer", CreateListFromNode<StateInitializer>);
84  protected readonly NonTerminal state_array_initializer = T("state_array_initializer", CreateStateValuesAst);
85  protected readonly NonTerminal stream_output_object = T("stream_output_object", CreateGenericTypeAst<ObjectType>);
86  protected readonly NonTerminal string_type = T("string_type", (context, node) => Ast<TypeName>(node).Name = new Identifier("string") { Span = SpanConverter.Convert(node.Span) });
87  protected readonly NonTerminal structured_buffer = T("structured_buffer", CreateGenericTypeAst<ObjectType>);
88  protected readonly NonTerminal structured_buffer_type = T("structured_buffer_type", CreateTypeNameFromTokenAst);
89  protected readonly NonTerminal technique_definition = T("technique_definition", CreateTechniqueAst);
90  protected readonly NonTerminal technique_keyword = T("technique_keyword", CreateIdentifierAst);
91  protected readonly NonTerminal texture_dms_type_profile_5 = T("texture_dms_type_profile_5", CreateTypeFromTokenAst<TextureType>);
92  protected readonly NonTerminal texture_generic_dms_type = T("texture_generic_dms_type", CreateTextureDMSAst);
93  protected readonly NonTerminal texture_generic_simple_type = T("texture_generic_simple_type", CreateGenericTypeAst<ObjectType>);
94  protected readonly NonTerminal texture_generic_type = TT("texture_generic_type");
95  protected readonly NonTerminal texture_type = T("texture_type", CreateTypeFromTokenAst<TextureType>);
96  protected readonly NonTerminal texture_type_list = TT("texture_type_list");
97  protected readonly NonTerminal texture_type_profile_4 = T("texture_type_profile_4", CreateTypeFromTokenAst<TextureType>);
98  protected readonly NonTerminal texture_type_profile_5 = T("texture_type_profile_5", CreateTypeFromTokenAst<TextureType>);
99  protected readonly NonTerminal triangle_stream = T("triangle_stream", CreateGenericTypeAst<ObjectType>);
100  protected readonly NonTerminal typedef_declaration = T("typedef_modifier", CreateTypedefAst);
101 
102  protected readonly NonTerminal variable_declarator_qualifier_post_hlsl = T("variable_declarator_qualifier_post_hlsl", CreateVariableDeclaratorQualifierPostAst);
103  protected readonly TerminalSet _skipTokensInPreview = new TerminalSet();
104  // ReSharper restore InconsistentNaming
105 
106 
108  {
109  return new ShaderLanguageData(this);
110  }
111 
112  /// <summary>
113  /// Initializes a new instance of the <see cref="HlslGrammar"/> class.
114  /// </summary>
115  public HlslGrammar()
116  {
117  GrammarComments = "Hlsl version 5.0";
118 
119  Term(string_literal_raw, TokenCategory.String, TokenType.StringLiteral);
120  Punc("::", TokenType.IdentifierSeparator);
121 
122  // ------------------------------------------------------------------------------------
123  // Comments
124  // ------------------------------------------------------------------------------------
125 
126  identifier_ns_list.Rule = MakePlusRule(identifier_ns_list, ToTerm("::"), identifier_raw);
127  identifier_dot_list.Rule = MakePlusRule(identifier_dot_list, ToTerm("."), identifier_raw);
128  identifier_ns.Rule = identifier_raw + "::" + identifier_ns_list;
129  identifier_dot.Rule = identifier_or_generic + ToTerm(".") + identifier_dot_list;
130  identifier_or_dot.Rule = identifier | identifier_dot;
131 
132  identifier.Rule |= identifier_ns;
133 
134  semi_opt.Rule = Empty | PreferShiftHere() + ";";
135 
136  //Prepare term set for conflict resolution
137  _skipTokensInPreview.UnionWith(new[] { ToTerm("."), identifier_raw, ToTerm(","), ToTerm("::"), ToTerm("["), ToTerm("]"), float_literal, integer_literal });
138 
139 
140  var genericResolverHint = new GenericResolverHint(_skipTokensInPreview);
141  less_than.Rule = genericResolverHint + "<";
142 
143  // ------------------------------------------------------------------------------------
144  // Types
145  // ------------------------------------------------------------------------------------
146 
147  // String
148  string_literal.Rule = string_literal_raw.List();
149  string_literal_raw.AstNodeCreator = CreateStringRawLiteral;
150 
151  // Add string to literals
152  literal.Rule |= string_literal;
153 
154  float_qualifier.Rule = Keyword("unorm") | Keyword("snorm");
155 
156  // scalars
157  var scalarTypes = new[] { ScalarType.Bool, ScalarType.Int, ScalarType.UInt, ScalarType.Float, ScalarType.Half, ScalarType.Double };
158  foreach (var scalarType in scalarTypes)
159  {
160  NonTerminal scalarTerm;
161  var localScalarType = scalarType;
162 
163  if (scalarType == ScalarType.Float)
164  {
165  scalarTerm = new NonTerminal(
166  "float",
167  (context, node) =>
168  {
169  var dynamicFloatType = Ast<ScalarType>(node);
170  dynamicFloatType.Name = new Identifier(localScalarType.Name) { Span = SpanConverter.Convert(node.Span) };
171  dynamicFloatType.Type = localScalarType.Type;
172  dynamicFloatType.Qualifiers = Qualifier.None;
173  if (node.ChildNodes.Count == 2)
174  {
175  dynamicFloatType.Qualifiers = (Qualifier)node.ChildNodes[0].AstNode;
176  }
177  })
178  {
179  Rule = Keyword("float", true) | float_qualifier + Keyword("float", true)
180  };
181 
182  }
183  else
184  {
185  scalarTerm = CreateScalarTerminal(scalarType);
186  }
187 
188  if (scalars.Rule == null) scalars.Rule = scalarTerm;
189  else scalars.Rule |= scalarTerm;
190  }
191 
192  // Buffer Rules
193  buffer_type.Rule = TypeName("Buffer") + less_than + simple_type_or_type_name + ">";
194 
195  // Vectors Rules
196  vector_type.AstNodeCreator = CreateVectorAst;
197  vector_type.Rule = Keyword("vector") + less_than + scalars_or_typename + "," + number + ">";
198  vector_type_list.Rule = vector_type;
199 
200  // Add all vector int1 int2 int3 int4... float1 float2 float3 float4... etc.
201  foreach (var scalarTypeIt in scalarTypes)
202  {
203  var scalarType = scalarTypeIt;
204  for (var dim = 1; dim <= 4; dim++)
205  {
206  var vectorTypeInstance = new VectorType(scalarTypeIt, dim);
207  var name = string.Format("{0}{1}", scalarType.Name, dim);
208  vector_type_list.Rule |= new NonTerminal(name,
209  (ctx, node) =>
210  {
211  var typeName = Ast<TypeName>(node);
212  typeName.Name = new Identifier(name) { Span = SpanConverter.Convert(node.Span) };
213  typeName.TypeInference.TargetType = vectorTypeInstance;
214  }) { Rule = Keyword(name) };
215  }
216  }
217 
218  // Matrices
219  matrix_type_simple.Rule = Keyword("matrix");
220  matrix_type_simple.AstNodeCreator = (ctx, node) =>
221  {
222  var typeName = Ast<TypeName>(node);
223  typeName.Name = new Identifier("matrix") { Span = SpanConverter.Convert(node.Span) };
224  typeName.TypeInference.TargetType = new MatrixType(ScalarType.Float, 4, 4);
225  };
226 
227  matrix_type.Rule = Keyword("matrix") + less_than + scalars_or_typename + "," + number + "," + number + ">";
228  matrix_type.AstNodeCreator = CreateMatrixAst;
229  matrix_type_list.Rule = matrix_type | matrix_type_simple;
230 
231  // Add all matrix typedefs: int1x1 int1x2... float1x1 float1x2 float1x3 float1x4... etc.
232  foreach (var scalarTypeIt in scalarTypes)
233  {
234  var scalarType = scalarTypeIt;
235  for (var dimX = 1; dimX <= 4; dimX++)
236  for (var dimY = 1; dimY <= 4; dimY++)
237  {
238  var matrixTypeInstance = new MatrixType(scalarTypeIt, dimY, dimX);
239 
240  var name = string.Format("{0}{1}x{2}", scalarType.Name, dimY, dimX);
241 
242  // var typeName = new TypeName(name) { Alias = matrixTypeInstance };
243  matrix_type_list.Rule |= new NonTerminal(
244  name,
245  (ctx, node) =>
246  {
247  var typeName = Ast<TypeName>(node);
248  typeName.Name = new Identifier(name) { Span = SpanConverter.Convert(node.Span) };
249  typeName.TypeInference.TargetType = matrixTypeInstance;
250  }) { Rule = Keyword(name) };
251  }
252  }
253 
254  // Sampler types
255  state_type.Rule = CreateRuleFromObjectTypes(
262 
263  sampler_type.Rule = CreateRuleFromObjectTypes(
269 
270  sampler_type.AstNodeCreator = CreateTypeFromTokenAst<SamplerType>;
271 
272  // Texture types
273  texture_type_profile_4.Rule = CreateRuleFromObjectTypes(
280 
281  texture_type.Rule = Keyword("texture") | texture_type_profile_4;
282 
283  // ByteAddressBuffer
284  byte_address_buffer.Rule = TypeName("ByteAddressBuffer") | TypeName("RWByteAddressBuffer");
285 
286  // StructuredBuffer
287  structured_buffer_type.Rule = TypeName("AppendStructuredBuffer") | TypeName("ConsumeStructuredBuffer") | TypeName("RWStructuredBuffer") | TypeName("StructuredBuffer");
288  structured_buffer.Rule = structured_buffer_type + less_than + scalars_and_vectors + ">";
289 
290  // RWTexture.*
291  texture_type_profile_5.Rule = TypeName("RWBuffer") | TypeName("RWTexture1D") | TypeName("RWTexture1DArray") | TypeName("RWTexture2D") | TypeName("RWTexture2DArray") | TypeName("RWTexture3D");
292 
293  texture_generic_simple_type.Rule = texture_type_profile_4 + less_than + scalars_and_vectors + ">"
294  | texture_type_profile_5 + less_than + scalars_and_vectors + ">";
295 
296  texture_dms_type_profile_5.Rule = TypeName("Texture2DMS") | TypeName("Texture2DMSArray");
297 
298  texture_generic_dms_type.Rule = texture_dms_type_profile_5 + less_than + scalars_and_vectors + ">"
299  | texture_dms_type_profile_5 + less_than + scalars_and_vectors + "," + number + ">";
300 
301  texture_generic_type.Rule = texture_generic_simple_type | texture_generic_dms_type;
302 
303  // HullShader/DomainShader InputPatch/OutputPatch
304  patch_type.Rule = TypeName("InputPatch") | TypeName("OutputPatch");
305 
306  patch_generic_type.Rule = patch_type + less_than + type_name + "," + number + ">";
307 
308  texture_type_list.Rule = texture_type | texture_generic_type;
309 
310  // Types used by the geometry shader
311  geometry_stream.Rule = line_stream | point_stream | triangle_stream | stream_output_object;
312 
313  triangle_stream.Rule = TypeName("TriangleStream") + less_than + type_name + ">";
314 
315  point_stream.Rule = TypeName("PointStream") + less_than + type_name + ">";
316 
317  line_stream.Rule = TypeName("LineStream") + less_than + type_name + ">";
318 
319  stream_output_object.Rule = TypeName("StreamOutputObject") + less_than + type_name + ">";
320 
321  //// Shader object
322  //// shader_objects.Rule = ToTerm("VertexShader") | "PixelShader" | "GeometryShader";
323 
324  string_type.Rule = Keyword("string");
325 
326  // Add string to simple types
327  simple_type.Rule |= string_type;
328 
329  // Add Object types
330  object_type.Rule |= buffer_type
331  | state_type
332  | texture_type_list
333  | byte_address_buffer
334  | structured_buffer
335  | patch_generic_type
336  | interface_specifier
337  | class_specifier
338  | geometry_stream;
339  ////| shader_objects;
340 
341  // Type name
342  typename_for_cast.Rule = identifier + new IdentifierResolverHint(true);
343 
344  identifier_generic_parameter_list.Rule = MakePlusRule(identifier_generic_parameter_list, ToTerm(","), identifier_sub_generic);
345 
346  identifier_sub_generic.Rule = identifier_or_generic;
347  identifier_sub_generic.AstNodeCreator = CreateIdentifierSubGenericAst;
348 
349  //identifier_generic.Rule = identifier + new IdentifierResolverHint(true) + "<" + identifier_generic_parameter_list + ">";
350  identifier_generic.Rule = identifier + new GenericResolverHint(_skipTokensInPreview) + "<" + identifier_generic_parameter_list + ">";
351 
352  identifier_or_generic.Rule = identifier + new IdentifierResolverHint(true)
353  | identifier_generic + this.ReduceHere();
354 
355  type_generic.Rule = identifier_or_generic;
356 
357  // Type used for cast (use valuetype)
358  type_for_cast.Rule = typename_for_cast
359  | value_type;
360 
361  // ------------------------------------------------------------------------------------
362  // Expressions
363  // ------------------------------------------------------------------------------------
364 
365  // Add special variable allowed as variable name and keyword
366  identifier_extended.Rule |= Keyword("sample") | Keyword("point");
367 
368  // postfix_expression
369  postfix_expression.Rule |= compile_expression
370  | asm_expression
371  | state_expression;
372 
373  compile_expression.Rule = Keyword("compile") + identifier + method_invoke_expression_simple;
374 
375  // Match an asm block: asm { ... }
376  asm_expression.Rule = asm_block;
377  KeyTerms.Add(asm_block.Name, asm_block);
378 
379  state_expression.Rule = state_type + state_initializer;
380 
381  // Add cast_expression
382  cast_expression_raw.Rule = "(" + type_for_cast + rank_specifier.ListOpt() + ")" + cast_expression;
383 
384  cast_expression.Rule |= cast_expression_raw;
385 
386  // Syntax is for example: texture = <g_textureref>;
387  identifier_special_reference_expression.Rule = less_than + indexable_identifier + ">";
388 
389  identifier_keyword.Rule = Keyword("texture");
390 
391  simple_assignment_expression_statement.Rule |= indexable_identifier + assignment_operator + identifier_special_reference_expression + ";"
392  | identifier_keyword + assignment_operator + identifier_special_reference_expression + ";"
393  | identifier_keyword + assignment_operator + expression + ";";
394 
395  state_initializer.Rule = "{" + simple_assignment_expression_statement.ListOpt() + "}";
396 
397  // ------------------------------------------------------------------------------------
398  // Attribute modifiers
399  // ------------------------------------------------------------------------------------
400  attribute_qualifier_pre.Rule = attribute_list_opt;
401 
402  attribute_list_opt.Rule = MakeStarRule(attribute_list_opt, null, attribute_modifier);
403 
404  attribute_modifier.Rule = "[" + identifier + "]"
405  | "[" + identifier + "(" + literal_list.Opt() + ")" + "]";
406 
407  // ------------------------------------------------------------------------------------
408  // Variable modifiers
409  // ------------------------------------------------------------------------------------
410  // storageClass = Storage_Class + Type_Modifier
411  storage_qualifier.Rule |= Keyword("extern") | Keyword("nointerpolation") | Keyword("precise") | Keyword("shared") | Keyword("groupshared") | Keyword("static") | Keyword("volatile")
412  | Keyword("row_major") | Keyword("column_major") | Keyword("linear") | Keyword("centroid") | Keyword("noperspective") | Keyword("sample") | Keyword("unsigned")
413  | Keyword("inline");
414 
415  semantic.Rule = ToTerm(":") + identifier;
416 
417  packoffset.Rule = ToTerm(":") + Keyword("packoffset") + "(" + identifier_or_dot + ")";
418 
419  register.Rule = ToTerm(":") + Keyword("register") + "(" + indexable_identifier + ")"
420  | ToTerm(":") + Keyword("register") + "(" + identifier + "," + indexable_identifier + ")";
421 
422 
423  variable_declarator_qualifier_post_hlsl.Rule = Empty
424  | semantic
425  | semantic + packoffset + register.ListOpt()
426  | semantic + register.List()
427  | packoffset + register.ListOpt()
428  | register.List();
429 
430  variable_declarator_qualifier_post.Rule = variable_declarator_qualifier_post_hlsl;
431 
432  // ------------------------------------------------------------------------------------
433  // Declarations
434  // ------------------------------------------------------------------------------------
435 
436  // Add typedef and constant buffer resource
437  declaration.Rule |= typedef_declaration
438  | constant_buffer_resource;
439 
440  indexable_identifier_declarator_list.Rule = MakePlusRule(indexable_identifier_declarator_list, ToTerm(","), indexable_identifier_declarator);
441 
442  // typedef [const] Type Name[Index];
443  typedef_declaration.Rule = Keyword("typedef") + type + indexable_identifier_declarator_list + ";"
444  | Keyword("typedef") + storage_qualifier + type + indexable_identifier_declarator_list + ";";
445 
446  annotations.Rule = less_than + variable_declaration_raw.ListOpt() + ">";
447 
448  annotations_opt.Rule = Empty | annotations;
449 
450  // todo: add annotations_opt to variable_declarator qualifier post
451 
452  // Add annotations to variable declarator
453  variable_declarator_raw.Rule += annotations_opt;
454 
455  // Add special
456  variable_declarator.Rule |= variable_declarator_raw + state_initializer
457  | variable_declarator_raw + state_array_initializer;
458 
459  state_initializer_list.Rule = MakePlusRule(state_initializer_list, ToTerm(","), state_initializer);
460 
461  state_array_initializer.Rule = "{" + state_initializer_list + "}"
462  | "{" + state_initializer_list + "," + "}";
463 
464  // interface definition
465  interface_specifier.Rule = Keyword("interface") + identifier_or_generic + "{" + method_declaration.ListOpt() + "}";
466 
467  // class definition
468  class_specifier.Rule = Keyword("class") + identifier_or_generic + class_base_type + "{" + scope_declaration.ListOpt() + "}";
469  class_base_type_list.Rule = MakePlusRule(class_base_type_list, ToTerm(","), type_generic);
470  class_base_type.Rule = (ToTerm(":") + class_base_type_list).Opt();
471 
472  // buffer definition
473  constant_buffer_resource_type.Rule = Keyword("cbuffer") | Keyword("tbuffer");
474 
475  constant_buffer_resource.Rule = attribute_qualifier_pre + constant_buffer_resource_type + identifier.Opt() + register.Opt() + "{" + declaration.ListOpt() + "}" + semi_opt;
476 
477  semantic_list_opt.Rule = semantic.ListOpt();
478 
479  // Method
480  method_qualifier_post.Rule = semantic_list_opt;
481 
482  method_operator_identifier.Rule = Keyword("operator") + "[" + "]"
483  | Keyword("operator") + "[" + "]" + "[" + "]";
484  method_special_identifier.Rule = identifier_extended + "." + method_operator_identifier | method_operator_identifier;
485 
486  method_declarator.Rule |= method_special_identifier + "(" + parameter_list + ")";
487 
488  parameter_qualifier.Rule = storage_qualifier | Keyword("in") | Keyword("out") | Keyword("inout") | Keyword("point") | Keyword("line") | Keyword("lineadj") | Keyword("triangle") | Keyword("triangleadj");
489  parameter_qualifier.AstNodeCreator = CreateParameterQualifier;
490 
491  parameter_qualifier_pre_list_opt.Rule = parameter_qualifier.ListOpt();
492  parameter_qualifier_pre.Rule = parameter_qualifier_pre_list_opt;
493  // Make parameter_qualifier_pre transient as there is nothing else to parse then parameter_qualifier_pre_list_opt
494  parameter_qualifier_pre.Flags = TermFlags.IsTransient | TermFlags.NoAstNode;
495 
496  parameter_qualifier_post.Rule = semantic_list_opt
497  | "=" + initializer + semantic_list_opt;
498  parameter_qualifier_post.AstNodeCreator = CreateParameterQualifierPost;
499 
500  // ------------------------------------------------------------------------------------
501  // Technique/pass
502  // ------------------------------------------------------------------------------------
503 
504  // technique
505  technique_keyword.Rule = Keyword("technique") | Keyword("Technique") | Keyword("technique10") | Keyword("Technique10") | Keyword("technique11") | Keyword("Technique11");
506 
507  technique_definition.Rule = attribute_qualifier_pre + technique_keyword + identifier.Opt() + annotations_opt + "{" + pass_definition.List() + "}" + semi_opt;
508 
509  // pass
510  pass_keyword.Rule = Keyword("pass") | Keyword("Pass");
511 
512  pass_statement.Rule = method_invoke_expression_simple + ";"
513  | simple_assignment_expression_statement;
514 
515  pass_definition.Rule = attribute_qualifier_pre + pass_keyword + identifier.Opt() + annotations_opt + "{" + pass_statement.ListOpt() + "}" + semi_opt;
516 
517  // ------------------------------------------------------------------------------------
518  // Top Level
519  // ------------------------------------------------------------------------------------
520 
521  // Add the technique to the top level
522  toplevel_declaration.Rule |= technique_definition;
523 
524  /*
525  //// ------------------------------------------------------------------------------------
526  //// Paradox Grammar
527  //// ------------------------------------------------------------------------------------
528  //var identifier_csharp = new NonTerminal("identifier_csharp");
529  //var group = new NonTerminal("group");
530  //var using_statement = new NonTerminal("using_statement");
531  //group.Rule = "group" + identifier + "{" + scope_declaration.ListOpt() + "}";
532  //identifier_csharp.Rule = MakePlusRule(identifier_csharp, ToTerm("."), identifier);
533  //using_statement.Rule = "using" + identifier + "=" + identifier_csharp + ";"
534  // | "using" + identifier_csharp + ";";
535  //scope_declaration.Rule |= using_statement;
536  //toplevel_declaration.Rule |= group;
537  */
538 
539  // ------------------------------------------------------------------------------------
540  // Globals
541  // ------------------------------------------------------------------------------------
542  // LanguageFlags = LanguageFlags.NewLineBeforeEOF;
543  LanguageFlags |= LanguageFlags.CreateAst;
544  }
545 
546  //public override void OnResolvingConflict(ConflictResolutionArgs args)
547  //{
548  // switch (args.Context.CurrentParserInput.Term.Name)
549  // {
550  // case "<":
551  // args.Scanner.BeginPreview();
552  // int ltCount = 0;
553  // string previewSym;
554  // while (true)
555  // {
556  // //Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else
557  // Token preview;
558  // do
559  // {
560  // preview = args.Scanner.GetToken();
561  // } while (_skipTokensInPreview.Contains(preview.Terminal) && preview.Terminal != base.Eof);
562  // //See what did we find
563  // previewSym = preview.Terminal.Name;
564  // if (previewSym == "<")
565  // ltCount++;
566  // else if (previewSym == ">" && ltCount > 0)
567  // {
568  // ltCount--;
569  // continue;
570  // }
571  // else
572  // break;
573  // }
574  // //if we see ">", then it is type argument, not operator
575  // if (previewSym == ">")
576  // args.Result = ParserActionType.Shift;
577  // else
578  // args.Result = ParserActionType.Reduce;
579  // args.Scanner.EndPreview(true);
580  // return;
581  // }
582  //}
583  }
584 }
Base class for all vector types
Definition: VectorType.cs:10
HlslGrammar()
Initializes a new instance of the HlslGrammar class.
Definition: HlslGrammar.cs:115
static readonly SamplerType SamplerCube
A samplerCUBE.
Definition: SamplerType.cs:39
A typeless reference.
Definition: TypeName.cs:10
static readonly TextureType Texture2D
A Texture2D
Definition: TextureType.cs:36
static readonly SamplerType Sampler3D
A sampler3D.
Definition: SamplerType.cs:34
static readonly TextureType TextureCube
An TextureCube.
Definition: TextureType.cs:51
static readonly TextureType Texture3D
A Texture3D.
Definition: TextureType.cs:46
static readonly SamplerStateType SamplerStateOld
An old sampler_state declaration.
static readonly SamplerStateType SamplerState
A SamplerState.
static readonly TextureType Texture1DArray
A Texture1DArray.
Definition: TextureType.cs:31
static readonly StateType BlendState
A BlendState.
Definition: StateType.cs:18
static readonly TextureType Texture2DArray
A Texture2DArray.
Definition: TextureType.cs:41
static readonly StateType DepthStencilState
A DepthStencilState.
Definition: StateType.cs:23
static readonly ScalarType Float
Sclar float.
Definition: ScalarType.cs:27
static readonly TextureType Texture1D
A Texture1D.
Definition: TextureType.cs:26
Describes a language.
Definition: LanguageData.cs:23
static readonly SamplerType Sampler1D
A sampler1D.
Definition: SamplerType.cs:24
SiliconStudio.Paradox.Games.Mathematics.Half Half
static readonly SamplerType Sampler
A sampler.
Definition: SamplerType.cs:19
static readonly StateType RasterizerState
A RasterizerState
Definition: StateType.cs:28
static readonly SamplerStateType SamplerComparisonState
A SamplerComparisonState.
static readonly SamplerType Sampler2D
A sampler2D
Definition: SamplerType.cs:29