Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
HlslGrammar.Helpers.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.Collections.Generic;
5 
6 using Irony.Parsing;
7 using SiliconStudio.Shaders.Ast;
8 using SiliconStudio.Shaders.Utility;
9 
10 namespace SiliconStudio.Shaders.Grammar.Hlsl
11 {
12  public partial class HlslGrammar
13  {
14  #region Public Methods
15 
16  #endregion
17 
18  #region Methods
19 
20  protected static void CreateListFromNode<T>(ParsingContext context, ParseTreeNode node)
21  {
22  var list = new List<T>();
23  FillListFromNodes(node.ChildNodes, list);
24  node.AstNode = list;
25  }
26 
27  private static void FillListFromNodes<TItem>(IEnumerable<ParseTreeNode> nodes, List<TItem> items)
28  {
29  foreach (var childNode in nodes)
30  {
31  items.Add((TItem)childNode.AstNode);
32  }
33  }
34 
35  private static T ParseEnumFromNode<T>(ParseTreeNode node) where T : struct, IConvertible
36  {
37  if (node.ChildNodes.Count != 1)
38  {
39  return (T)Enum.ToObject(typeof(T), 0);
40  }
41 
42  return (T)Enum.Parse(typeof(T), node.ChildNodes[0].Token.Text, true);
43  }
44 
45  private NonTerminal CreateScalarTerminal<T>(T scalarType) where T : ScalarType, new()
46  {
47  return new NonTerminal(
48  scalarType.Name,
49  (context, node) =>
50  {
51  var value = Ast<T>(node);
52  value.Name = new Identifier(scalarType.Name) { Span = SpanConverter.Convert(node.Span) };
53  value.Type = scalarType.Type;
54  }) { Rule = Keyword(scalarType.Name) };
55  }
56 
57  #endregion
58  }
59 }