4 using System.Collections.Generic;
10 using SiliconStudio.Shaders.Properties;
12 namespace SiliconStudio.Shaders.
Grammar
16 internal GoldParser.Parser GoldParser;
17 private static GoldParser.Grammar grammar;
19 private string source;
23 private string sourceFileName = null;
27 var grammarStream =
new MemoryStream(Resources.Tokenizer);
28 grammar =
new GoldParser.Grammar(
new BinaryReader(grammarStream));
29 grammarStream.Close();
34 GoldParser =
new GoldParser.Parser(grammar);
35 this.languageData = languageData;
42 return new Irony.Parsing.SourceLocation(GoldParser.CharPosition, (GoldParser.LineNumber - previousLine) + newLine, GoldParser.LinePosition, sourceFileName);
46 int tempNewLine = (value.Line - newLine) + previousLine;
48 GoldParser.CharPosition = value.Position;
49 GoldParser.LineNumber = tempNewLine;
56 GoldParser.SetSourceCode(sourceText);
59 this.sourceFileName = sourceFileName;
64 var location = Location;
65 var symbol = GoldParser.ReadToken();
73 case SymbolType.WhiteSpace:
74 token =
new Token(languageData.FindTerminalByType(tokenType), location, GoldParser.TokenLength, source, null);
77 case SymbolType.CommentLine:
78 case SymbolType.CommentStart:
79 int length = GoldParser.CommentTextLength(location.Position) - location.Position;
80 token =
new Token(languageData.FindTerminalByType(tokenType), location, length, source, null);
82 case SymbolType.Error:
83 token =
new Token(languageData.Grammar.SyntaxError, location, GoldParser.TokenLength, source,
"Unexpected token");
86 token =
new Token(languageData.Grammar.Eof, location,
string.Empty, languageData.Grammar.Eof.Name);
92 if (symbol.Index == (
int)
TokenType.Preprocessor)
94 int tempPreviousLine = GoldParser.LineNumber;
95 bool isEndOfLine =
false;
97 bool preprocessorDecoded =
false;
101 symbol = GoldParser.ReadToken();
107 case TokenType.NewLine:
110 case TokenType.Identifier:
111 if (!preprocessorDecoded)
112 preprocessorDecoded = GoldParser.TokenText !=
"line";
115 case TokenType.StringLiteral:
116 if (preprocessorDecoded)
117 sourceFileName = GoldParser.TokenText.Trim(
'"').Replace(
@"\\",
@"\");
120 case TokenType.Whitespace:
122 case TokenType.StartWithNoZeroDecimalIntegerLiteral:
123 if (!preprocessorDecoded)
125 previousLine = tempPreviousLine;
126 newLine = int.Parse(GoldParser.TokenText) - 1;
127 preprocessorDecoded =
true;
131 preprocessorDecoded =
true;
142 terminal = languageData.FindTerminalByName(GoldParser.TokenText);
144 if (terminal == null)
145 terminal = languageData.FindTerminalByType((
TokenType)symbol.Index);
147 if (terminal == null)
149 token =
new Token(languageData.Grammar.SyntaxError, location, GoldParser.TokenText,
string.Format(
"Unable to find terminal for token text [{0}]", GoldParser.TokenText));
153 if (terminal is DynamicKeyTerm)
155 ((DynamicKeyTerm)terminal).Match(
this, out token);
159 token =
new Token(terminal, location, GoldParser.TokenLength, source, null);
Tokenizer(ShaderLanguageData languageData)
void SetSourceText(string sourceText, string sourceFileName)
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...