This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
/
EffectParser.cs
864 lines (726 loc) · 31.8 KB
/
EffectParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Copyright (c) 2010-2012 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D;
namespace SharpDX.Toolkit.Graphics
{
internal class EffectParser
{
/// <summary>
/// End of file token.
/// </summary>
private static readonly Token EndOfFile = new Token(TokenType.EndOfFile, null, new SourceSpan());
private int bracketCount = 0;
private int curlyBraceCount = 0;
private string currentFile;
private int currentLine;
private int currentLineAbsolutePos;
private Ast.Pass currentPass;
private List<Token> currentPreviewTokens = new List<Token>();
private Ast.Technique currentTechnique;
private Token currentToken;
private bool isPreviewToken = false;
private int parentCount = 0;
private List<Token> previewTokens = new List<Token>();
private EffectParserResult result;
private Token savedPreviewToken;
private IEnumerator<Token> tokenEnumerator;
private List<string> includeDirectoryList;
/// <summary>
/// Initializes a new instance of the <see cref="EffectParser" /> class.
/// </summary>
public EffectParser()
{
includeDirectoryList = new List<string>();
Macros = new List<ShaderMacro>();
}
/// <summary>
/// Gets or sets the include file callback.
/// </summary>
/// <value>The include file callback.</value>
public IncludeFileDelegate IncludeFileCallback { get; set; }
/// <summary>
/// Gets the macros.
/// </summary>
/// <value>The macros.</value>
public List<ShaderMacro> Macros { get; private set; }
/// <summary>
/// Gets the include directory list.
/// </summary>
/// <value>The include directory list.</value>
public List<string> IncludeDirectoryList
{
get { return includeDirectoryList; }
}
/// <summary>
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
public EffectCompilerLogger Logger { get; set; }
/// <summary>
/// Parses the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="fileName">Name of the file.</param>
/// <returns>Result of parsing</returns>
public EffectParserResult PrepareParsing(string input, string fileName)
{
var filePath = Path.GetFullPath(fileName);
fileName = Path.GetFileName(fileName);
var localResult = new EffectParserResult
{
SourceFileName = Path.Combine(Environment.CurrentDirectory, filePath),
IncludeHandler = new FileIncludeHandler { Logger = Logger }
};
localResult.IncludeHandler.CurrentDirectory.Push(Path.GetDirectoryName(localResult.SourceFileName));
localResult.IncludeHandler.IncludeDirectories.AddRange(IncludeDirectoryList);
localResult.IncludeHandler.IncludeFileCallback = IncludeFileCallback;
localResult.IncludeHandler.FileResolved.Add(fileName, new FileIncludeHandler.FileItem(fileName, filePath, File.GetLastWriteTime(filePath)));
string compilationErrors = null;
var preprocessedInput = ShaderBytecode.Preprocess(input, Macros.ToArray(), localResult.IncludeHandler, out compilationErrors, fileName);
localResult.PreprocessedSource = preprocessedInput;
localResult.Hashcode = CalculateHashcode(localResult);
return localResult;
}
/// <summary>
/// Continues the parsing.
/// </summary>
/// <param name="previousParsing">The previous parsing.</param>
/// <returns>EffectParserResult.</returns>
public EffectParserResult ContinueParsing(EffectParserResult previousParsing)
{
// Reset count
parentCount = 0;
curlyBraceCount = 0;
bracketCount = 0;
result = previousParsing;
tokenEnumerator = Tokenizer.Run(previousParsing.PreprocessedSource).GetEnumerator();
do
{
var token = NextToken();
if (token.Type == TokenType.EndOfFile)
break;
switch (token.Type)
{
case TokenType.Identifier:
if ((token.EqualString("technique") || token.EqualString("technique10") || token.EqualString("technique11")) && curlyBraceCount == 0)
ParseTechnique();
break;
}
} while (true);
if (!CheckAllBracketsClosed())
{
if (parentCount != 0)
{
Logger.Error("Error matching closing parenthese for '('", lastParentToken.Span);
}
if (bracketCount != 0)
{
Logger.Error("Error matching closing bracket for '['", lastBracketToken.Span);
}
if (curlyBraceCount != 0)
{
Logger.Error("Error matching closing curly brace for '{'", lastCurlyBraceToken.Span);
}
}
return result;
}
/// <summary>
/// Parses the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="fileName">Name of the file.</param>
/// <returns>Result of parsing</returns>
public EffectParserResult Parse(string input, string fileName)
{
return ContinueParsing(PrepareParsing(input, fileName));
}
private bool CheckAllBracketsClosed()
{
return parentCount == 0 && bracketCount == 0 && curlyBraceCount == 0;
}
private Token lastParentToken;
private Token lastBracketToken;
private Token lastCurlyBraceToken;
private bool HandleBrackets(Token token)
{
bool isBracketOk = true;
switch (token.Type)
{
case TokenType.LeftParent:
lastParentToken = token;
parentCount++;
break;
case TokenType.RightParent:
lastParentToken = token;
parentCount--;
if (parentCount < 0)
{
parentCount = 0;
Logger.Error("No matching parenthesis '(' for this closing token.", token.Span);
isBracketOk = false;
}
break;
case TokenType.LeftBracket:
lastBracketToken = token;
bracketCount++;
break;
case TokenType.RightBracket:
lastBracketToken = token;
bracketCount--;
if (bracketCount < 0)
{
bracketCount = 0;
Logger.Error("No matching bracket '[' for this closing token.", token.Span);
isBracketOk = false;
}
break;
case TokenType.LeftCurlyBrace:
lastCurlyBraceToken = token;
curlyBraceCount++;
break;
case TokenType.RightCurlyBrace:
lastCurlyBraceToken = token;
curlyBraceCount--;
if (curlyBraceCount < 0)
{
curlyBraceCount = 0;
Logger.Error("No matching curly brace '{{' for this closing token.", token.Span);
isBracketOk = false;
}
break;
}
return isBracketOk;
}
private void BeginPreviewToken()
{
isPreviewToken = true;
savedPreviewToken = currentToken;
}
private void EndPreviewToken()
{
isPreviewToken = false;
previewTokens = currentPreviewTokens;
currentPreviewTokens = new List<Token>();
currentToken = savedPreviewToken;
}
private Token NextToken()
{
var token = InternalNextToken();
// Handle preprocessor "line"
while (token.Type == TokenType.Preprocessor)
{
InternalNextToken();
if (!Expect("line"))
{
Logger.Error("Unsupported preprocessor token [{0}]. Only #line is supported.", token.Span, currentToken.Value);
return currentToken;
}
if (!ExpectNext(TokenType.Number))
return currentToken;
// Set currentLine - 1 as the NL at the end of the preprocessor line will add +1
currentLine = int.Parse(currentToken.Value) - 1;
// Check next token
token = InternalNextToken();
// If this is a string, then this is the end of preprocessor line
if (token.Type == TokenType.String)
{
currentFile = token.Value.Substring(1, token.Value.Length - 2);
// Replace "file" from #line preprocessor with the actual fullpath.
var includeHandler = result.IncludeHandler;
if (includeHandler.FileResolved.ContainsKey(currentFile))
{
currentFile = includeHandler.FileResolved[currentFile].FilePath;
}
currentFile = currentFile.Replace(@"\\", @"\");
token = InternalNextToken();
}
}
// Set correct location for current token
currentToken.Span.Line = currentLine;
currentToken.Span.Column = token.Span.Index - currentLineAbsolutePos + 1;
currentToken.Span.FilePath = currentFile;
return currentToken;
}
private Token InternalNextToken()
{
// TODO: token preview is not safe with NewLine count
if (previewTokens.Count > 0)
{
currentToken = previewTokens[0];
previewTokens.RemoveAt(0);
if (isPreviewToken)
currentPreviewTokens.Add(currentToken);
return currentToken;
}
while (tokenEnumerator.MoveNext())
{
currentToken = tokenEnumerator.Current;
if (currentToken.Type == TokenType.Newline)
{
currentLine++;
currentLineAbsolutePos = currentToken.Span.Index + currentToken.Span.Length;
}
else
{
currentToken.Span.Line = currentLine;
currentToken.Span.Column = currentToken.Span.Index - currentLineAbsolutePos + 1;
currentToken.Span.FilePath = currentFile;
HandleBrackets(currentToken);
if (isPreviewToken)
currentPreviewTokens.Add(currentToken);
return currentToken;
}
}
currentToken = EndOfFile;
return currentToken;
}
private bool ExpectNext(TokenType tokenType)
{
NextToken();
return Expect(tokenType);
}
private bool Expect(TokenType tokenType)
{
bool isSameTokenType = currentToken.Type == tokenType;
if (!isSameTokenType)
Logger.Error("Error while parsing unexpected token [{0}]. Expecting token [{1}]", currentToken.Span, currentToken.Value, tokenType);
return isSameTokenType;
}
private bool Expect(string keyword, bool isCaseSensitive = false)
{
if (Expect(TokenType.Identifier))
{
if (!(currentToken.EqualString(keyword, isCaseSensitive)))
{
Logger.Error("Error while parsing unexpected keyword [{0}]. Expecting keyword [{1}]", currentToken.Span, currentToken.Value, keyword);
}
else
{
return true;
}
}
return false;
}
private void ParseTechnique()
{
var technique = new Ast.Technique() {Span = currentToken.Span};
// Get Technique name if any.
var ident = NextToken();
if (ident.Type == TokenType.Identifier)
{
technique.Name = ident.Value;
NextToken();
}
if (!Expect(TokenType.LeftCurlyBrace))
return;
// Add the technique being parsed
currentTechnique = technique;
bool continueParsingTecnhique = true;
bool isParseOk = false;
do
{
var token = NextToken();
switch (token.Type)
{
case TokenType.Identifier:
if (token.EqualString("pass"))
ParsePass();
break;
case TokenType.RightCurlyBrace:
isParseOk = true;
continueParsingTecnhique = false;
break;
default:
Logger.Error("Unexpected token [{0}]. Expecting tokens ['pass','}}']", currentToken.Span, currentToken);
continueParsingTecnhique = false;
break;
}
} while (continueParsingTecnhique);
if (isParseOk)
{
if (result.Shader == null)
result.Shader = new Ast.Shader();
result.Shader.Techniques.Add(technique);
}
}
private void ParsePass()
{
var pass = new Ast.Pass() {Span = currentToken.Span};
// Get Pass name if any.
var ident = NextToken();
if (ident.Type == TokenType.Identifier)
{
pass.Name = ident.Value;
NextToken();
}
if (!Expect(TokenType.LeftCurlyBrace))
return;
// Add the technique being parsed
currentPass = pass;
bool continueParsingTecnhique = true;
bool isParseOk = false;
do
{
var token = NextToken();
switch (token.Type)
{
case TokenType.Identifier:
if (!ParseStatement())
continueParsingTecnhique = false;
break;
case TokenType.RightCurlyBrace:
isParseOk = true;
continueParsingTecnhique = false;
break;
// fxc doesn't support empty statements, so we don't support them.
//case TokenType.SemiColon:
// // Skip empty statements
// break;
default:
Logger.Error("Unexpected token [{0}]. Expecting tokens ['identifier','}}']", currentToken.Span, currentToken);
continueParsingTecnhique = false;
break;
}
} while (continueParsingTecnhique);
if (isParseOk)
currentTechnique.Passes.Add(pass);
}
private bool ParseStatement()
{
var expression = ParseExpression();
if (expression == null)
return false;
if (!ExpectNext(TokenType.SemiColon))
return false;
currentPass.Statements.Add(new Ast.ExpressionStatement(expression) {Span = expression.Span});
return true;
}
private Ast.Expression ParseExpression(bool allowIndirectIdentifier = false)
{
Ast.Expression expression = null;
var token = currentToken;
switch (token.Type)
{
case TokenType.Identifier:
if (token.Value == "true")
expression = new Ast.LiteralExpression(new Ast.Literal(true) {Span = token.Span}) {Span = token.Span};
else if (token.Value == "false")
expression = new Ast.LiteralExpression(new Ast.Literal(false) {Span = token.Span}) {Span = token.Span};
else if (string.Compare(token.Value, "null", StringComparison.OrdinalIgnoreCase) == 0)
expression = new Ast.LiteralExpression(new Ast.Literal(null) {Span = token.Span}) {Span = token.Span};
else if (token.Value == "compile")
{
expression = ParseCompileExpression();
}
else
{
var nextIdentifier = ParseIdentifier(token, true);
// check next token
BeginPreviewToken();
var nextToken = NextToken();
EndPreviewToken();
switch (nextToken.Type)
{
case TokenType.LeftParent:
var methodExpression = ParseMethodExpression();
methodExpression.Name = nextIdentifier;
expression = methodExpression;
break;
case TokenType.Equal:
var assignExpression = ParseAssignExpression();
assignExpression.Name = nextIdentifier;
expression = assignExpression;
break;
default:
expression = new Ast.IdentifierExpression(nextIdentifier) {Span = token.Span};
break;
}
}
break;
case TokenType.LeftParent:
expression = ParseArrayInitializerExpression(TokenType.RightParent);
//expression = ParseExpression();
//ExpectNext(TokenType.RightParent);
break;
case TokenType.String:
// TODO doesn't support escaped strings
var str = token.Value.Substring(1, token.Value.Length - 2);
// Temporary escape
str = str.Replace("\\r\\n", "\r\n");
str = str.Replace("\\n", "\n");
str = str.Replace("\\t", "\t");
expression = new Ast.LiteralExpression(new Ast.Literal(str) {Span = token.Span}) {Span = token.Span};
break;
case TokenType.Number:
var numberStr = token.Value.Replace(" ", "");
object numberValue;
if (numberStr.EndsWith("f", false, CultureInfo.InvariantCulture) || numberStr.Contains("e") || numberStr.Contains("E") || numberStr.Contains("."))
{
var floatStr = numberStr.TrimEnd('f');
numberValue = float.Parse(floatStr, NumberStyles.Float, CultureInfo.InvariantCulture);
}
else
{
numberValue = int.Parse(numberStr, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
expression = new Ast.LiteralExpression(new Ast.Literal(numberValue) {Span = token.Span}) {Span = token.Span};
break;
case TokenType.Hexa:
var intValue = int.Parse(token.Value.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
expression = new Ast.LiteralExpression(new Ast.Literal(intValue) {Span = token.Span}) {Span = token.Span};
break;
case TokenType.LeftCurlyBrace:
expression = ParseArrayInitializerExpression(TokenType.RightCurlyBrace);
break;
default:
if (token.Type == TokenType.LessThan && allowIndirectIdentifier)
{
NextToken();
var identifierExpression = ParseExpression() as Ast.IdentifierExpression;
bool isClosedIndirect = ExpectNext(TokenType.GreaterThan);
if (identifierExpression == null)
{
Logger.Error("Unexpected expression inside indirect reference.", token.Span);
}
else
{
identifierExpression.Name.IsIndirect = isClosedIndirect;
expression = identifierExpression;
}
}
else
{
Logger.Error("Unexpected token [{0}]. Expecting tokens ['identifier', 'string', 'number', '{{']", currentToken.Span, currentToken);
}
break;
}
return expression;
}
private Ast.Identifier ParseIdentifier(Token token, bool allowIndexed = false)
{
var identifierName = new System.Text.StringBuilder(token.Value);
// ------------------------------------------------------------
// Parse complex identifier XXXX::AAAA::BBBBB
// ------------------------------------------------------------
BeginPreviewToken();
var nextToken = NextToken();
EndPreviewToken();
// Dot are not supported by compiler XXXX.AAAA.BBBB
// if (nextToken.Type == TokenType.DoubleColon || nextToken.Type == TokenType.Dot)
if (nextToken.Type == TokenType.DoubleColon)
{
var identifierSeparatorType = nextToken.Type;
var endChar = nextToken.Type == TokenType.DoubleColon ? "::" : ".";
nextToken = NextToken();
identifierName.Append(nextToken.Value);
bool continueParsing = true;
do
{
BeginPreviewToken();
nextToken = NextToken();
EndPreviewToken();
switch (nextToken.Type)
{
case TokenType.Identifier:
identifierName.Append(nextToken.Value);
break;
case TokenType.DoubleColon:
case TokenType.Dot:
if (identifierName[identifierName.Length - 1] == endChar[0])
{
Logger.Error("Unexpected token [{0}]. Expecting tokens [identifier]", nextToken.Span, endChar);
}
if (identifierSeparatorType != nextToken.Type)
{
Logger.Error("Unexpected token [{0}]. Expecting tokens [{1}]", nextToken.Span, nextToken.Value, endChar);
}
identifierName.Append(nextToken.Value);
break;
default:
continueParsing = false;
break;
}
// If can go to the next token
if (continueParsing)
{
nextToken = NextToken();
}
} while (continueParsing);
if (identifierName[identifierName.Length - 1] == endChar[0])
{
Logger.Error("Unexpected token [{0}]. Expecting tokens [identifier]", nextToken.Span, endChar);
}
}
// ------------------------------------------------------------
// Parse optional indexer a[xxx]
// ------------------------------------------------------------
if (allowIndexed)
{
BeginPreviewToken();
var arrayToken = NextToken();
EndPreviewToken();
if (arrayToken.Type == TokenType.LeftBracket)
{
// Skip left bracket [
NextToken();
// Get first token from inside bracket
NextToken();
// Parse value inside bracket
var expression = ParseExpression() as Ast.LiteralExpression;
if (expression == null || !(expression.Value.Value is int))
{
Logger.Error("Unsupported expression for indexed variable. Only support for integer Literal", currentToken.Span);
return null;
}
ExpectNext(TokenType.RightBracket);
return new Ast.IndexedIdentifier(identifierName.ToString(), (int)expression.Value.Value) { Span = token.Span };
}
}
return new Ast.Identifier(identifierName.ToString()) { Span = token.Span };
}
private Ast.Expression ParseCompileExpression()
{
var compileExp = new Ast.CompileExpression() {Span = currentToken.Span};
if (!ExpectNext(TokenType.Identifier))
return compileExp;
compileExp.Profile = ParseIdentifier(currentToken);
// Go to expression token
NextToken();
compileExp.Method = ParseExpression();
return compileExp;
}
private Ast.MethodExpression ParseMethodExpression()
{
if (!ExpectNext(TokenType.LeftParent))
return null;
var methodExp = new Ast.MethodExpression {Span = currentToken.Span};
int expectedArguments = 0;
bool continueParsing = true;
do
{
var token = NextToken();
switch (token.Type)
{
case TokenType.RightParent:
continueParsing = false;
break;
default:
var nextValue = ParseExpression();
if (nextValue == null)
{
continueParsing = false;
}
else
{
methodExp.Arguments.Add(nextValue);
NextToken();
if (currentToken.Type == TokenType.RightParent)
{
continueParsing = false;
}
else if (currentToken.Type == TokenType.Comma)
{
expectedArguments += (expectedArguments == 0) ? 2 : 1;
}
else
{
Logger.Error("Unexpected token [{0}]. Expecting tokens [',', ')']", currentToken.Span, currentToken);
continueParsing = false;
}
}
break;
}
} while (continueParsing);
int argCount = methodExp.Arguments.Count;
if (expectedArguments > 0 && expectedArguments != argCount)
Logger.Error("Unexpected number of commas.", currentToken.Span);
return methodExp;
}
private Ast.AssignExpression ParseAssignExpression()
{
var span = currentToken.Span;
if (!ExpectNext(TokenType.Equal))
return null;
NextToken();
var value = ParseExpression(true);
return new Ast.AssignExpression {Value = value, Span = span};
}
private Ast.ArrayInitializerExpression ParseArrayInitializerExpression(TokenType rightParent)
{
var expression = new Ast.ArrayInitializerExpression() {Span = currentToken.Span};
var values = expression.Values;
bool continueParsing = true;
do
{
var token = NextToken();
if (token.Type == rightParent)
break;
var nextValue = ParseExpression();
if (nextValue == null)
{
continueParsing = false;
}
else
{
values.Add(nextValue);
NextToken();
if (currentToken.Type == rightParent)
{
continueParsing = false;
}
else if (currentToken.Type != TokenType.Comma)
{
Logger.Error("Unexpected token [{0}]. Expecting tokens [',', '{1}']", currentToken.Span, currentToken.Value, rightParent);
continueParsing = false;
}
}
} while (continueParsing);
return expression;
}
private int CalculateHashcode(EffectParserResult parserResult)
{
var keys = new List<string>(parserResult.IncludeHandler.FileResolved.Keys);
keys.Sort(StringComparer.InvariantCultureIgnoreCase);
// Compute a HashCode using FNVModified
// based on filename and file write time
const uint p = 16777619;
uint hash = 2166136261;
foreach (var fileKey in keys)
{
var fileItem = parserResult.IncludeHandler.FileResolved[fileKey];
var modifiedTime = fileItem.ModifiedTime;
var fileName = fileKey.ToLower();
hash = (hash ^ (uint)fileName.GetHashCode()) * p;
hash = (hash ^ (uint)modifiedTime.GetHashCode()) * p;
}
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return unchecked((int)hash);
}
}
}