Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ShaderLanguageData.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 using System.Linq;
6 
7 using Irony.Parsing;
8 
9 namespace SiliconStudio.Shaders.Grammar
10 {
12  {
13  private Dictionary<string, Terminal> keywordToTerminal = new Dictionary<string, Terminal>();
14  private Dictionary<string, Terminal> caseInsensitiveKeywordToTerminal = new Dictionary<string, Terminal>(StringComparer.InvariantCultureIgnoreCase);
15  private Terminal[] SymbolToToken;
16 
17  /// <summary>
18  /// Initializes a new instance of the <see cref="ShaderLanguageData"/> class.
19  /// </summary>
20  /// <param name="grammar">The grammar.</param>
21  public ShaderLanguageData(Irony.Parsing.Grammar grammar) : base(grammar)
22  {
23  SymbolToToken = new Terminal[256];
24 
25  // Add TokenType to terminals
26  foreach (var typeTerm in ((ShaderGrammar)grammar).TokenTypeToTerminals)
27  {
28  AddTerminal(typeTerm.Key, typeTerm.Value);
29  }
30 
31 
32  // Add Keywords
33  foreach (var term in Grammar.KeyTerms.Values.OfType<Terminal>())
34  {
35  if (char.IsLetter(term.Name[0]))
36  AddTerminal(TokenType.Identifier, term);
37  }
38  }
39 
40  private void AddTerminal(TokenType type, Terminal term)
41  {
42  if (term == Grammar.Empty || term == Grammar.Eof || term == Grammar.SyntaxError)
43  return;
44 
45  var tokenInfo = (TokenInfo)term.AstNodeConfig;
46 
47  if (tokenInfo == null)
48  {
49  Errors.Add(GrammarErrorLevel.Error, null, "Terminal {0} is doesn't have associated TokenInfo", term.Name);
50  }
51  else if(tokenInfo.TokenCategory == TokenCategory.Typename || tokenInfo.TokenCategory == TokenCategory.Keyword)
52  {
53  var keyMap = (tokenInfo.IsCaseInsensitive) ? caseInsensitiveKeywordToTerminal : keywordToTerminal;
54  if (!keyMap.ContainsKey(term.Name))
55  keyMap.Add(term.Name, term);
56  }
57  else
58  {
59  SymbolToToken[(int)type] = term;
60  }
61  }
62 
63 
64  public override Scanner CreateScanner()
65  {
66  return new CustomScanner(new Tokenizer(this));
67  }
68 
70  {
71  return SymbolToToken[(int)tokenType];
72  }
73 
74  public Terminal FindTerminalByName(string name)
75  {
76  Terminal terminal;
77  // First try case insensitive keywords, else try case sensitive
78  if (!caseInsensitiveKeywordToTerminal.TryGetValue(name, out terminal))
79  keywordToTerminal.TryGetValue(name, out terminal);
80  return terminal;
81  }
82  }
83 }
TokenCategory
Token category.
Definition: Token.cs:29
Methods used to create the Abstract Syntax Tree..
readonly Terminal Empty
Definition: Grammar.cs:412
Scanner base class. The Scanner's function is to transform a stream of characters into aggregates/wor...
Definition: Scanner.cs:22
ShaderLanguageData(Irony.Parsing.Grammar grammar)
Initializes a new instance of the ShaderLanguageData class.
KeyTermTable KeyTerms
Definition: Grammar.cs:458
Describes a language.
Definition: LanguageData.cs:23
readonly Terminal Eof
Definition: Grammar.cs:425
readonly Terminal SyntaxError
Definition: Grammar.cs:431