-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
cs-tokenizer.cs
3859 lines (3291 loc) · 88.9 KB
/
cs-tokenizer.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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// cs-tokenizer.cs: The Tokenizer for the C# compiler
// This also implements the preprocessor
//
// Author: Miguel de Icaza (miguel@gnu.org)
// Marek Safar (marek.safar@gmail.com)
//
// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2001, 2002 Ximian, Inc (http://www.ximian.com)
// Copyright 2004-2008 Novell, Inc
// Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
//
using System;
using System.Text;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using System.Collections;
namespace Mono.CSharp
{
//
// This class has to be used by parser only, it reuses token
// details once a file is parsed
//
public class LocatedToken
{
public int row, column;
public string value;
public SourceFile file;
public LocatedToken ()
{
}
public LocatedToken (string value, Location loc)
{
this.value = value;
file = loc.SourceFile;
row = loc.Row;
column = loc.Column;
}
public override string ToString ()
{
return string.Format ("Token '{0}' at {1},{2}", Value, row, column);
}
public Location Location
{
get { return new Location (file, row, column); }
}
public string Value
{
get { return value; }
}
}
/// <summary>
/// Tokenizer for C# source code.
/// </summary>
public class Tokenizer : yyParser.yyInput
{
class KeywordEntry<T>
{
public readonly T Token;
public KeywordEntry<T> Next;
public readonly char[] Value;
public KeywordEntry (string value, T token)
{
this.Value = value.ToCharArray ();
this.Token = token;
}
}
sealed class IdentifiersComparer : IEqualityComparer<char[]>
{
readonly int length;
public IdentifiersComparer (int length)
{
this.length = length;
}
public bool Equals (char[] x, char[] y)
{
for (int i = 0; i < length; ++i)
if (x [i] != y [i])
return false;
return true;
}
public int GetHashCode (char[] obj)
{
int h = 0;
for (int i = 0; i < length; ++i)
h = (h << 5) - h + obj [i];
return h;
}
}
public class LocatedTokenBuffer
{
readonly LocatedToken[] buffer;
public int pos;
public LocatedTokenBuffer ()
{
buffer = new LocatedToken[0];
}
public LocatedTokenBuffer (LocatedToken[] buffer)
{
this.buffer = buffer ?? new LocatedToken[0];
}
public LocatedToken Create (SourceFile file, int row, int column)
{
return Create (null, file, row, column);
}
public LocatedToken Create (string value, SourceFile file, int row, int column)
{
//
// TODO: I am not very happy about the logic but it's the best
// what I could come up with for now.
// Ideally we should be using just tiny buffer (256 elements) which
// is enough to hold all details for currect stack and recycle elements
// poped from the stack but there is a trick needed to recycle
// them properly.
//
LocatedToken entry;
if (pos >= buffer.Length) {
entry = new LocatedToken ();
} else {
entry = buffer[pos];
if (entry == null) {
entry = new LocatedToken ();
buffer[pos] = entry;
}
++pos;
}
entry.value = value;
entry.file = file;
entry.row = row;
entry.column = column;
return entry;
}
//
// Used for token not required by expression evaluator
//
[Conditional ("FULL_AST")]
public void CreateOptional (SourceFile file, int row, int col, ref object token)
{
token = Create (file, row, col);
}
}
public enum PreprocessorDirective
{
Invalid = 0,
Region = 1,
Endregion = 2,
If = 3 | RequiresArgument,
Endif = 4,
Elif = 5 | RequiresArgument,
Else = 6,
Define = 7 | RequiresArgument,
Undef = 8 | RequiresArgument,
Error = 9,
Warning = 10,
Pragma = 11 | CustomArgumentsParsing,
Line = 12 | CustomArgumentsParsing,
CustomArgumentsParsing = 1 << 10,
RequiresArgument = 1 << 11
}
readonly SeekableStreamReader reader;
readonly CompilationSourceFile source_file;
readonly CompilerContext context;
readonly Report Report;
SourceFile current_source;
Location hidden_block_start;
int ref_line = 1;
int line = 1;
int col = 0;
int previous_col;
int current_token;
readonly int tab_size;
bool handle_get_set = false;
bool handle_remove_add = false;
bool handle_where;
bool lambda_arguments_parsing;
List<Location> escaped_identifiers;
int parsing_generic_less_than;
readonly bool doc_processing;
readonly LocatedTokenBuffer ltb;
//
// Used mainly for parser optimizations. Some expressions for instance
// can appear only in block (including initializer, base initializer)
// scope only
//
public int parsing_block;
internal bool query_parsing;
//
// When parsing type only, useful for ambiguous nullable types
//
public int parsing_type;
//
// Set when parsing generic declaration (type or method header)
//
public bool parsing_generic_declaration;
public bool parsing_generic_declaration_doc;
//
// The value indicates that we have not reach any declaration or
// namespace yet
//
public int parsing_declaration;
public bool parsing_attribute_section;
public bool parsing_modifiers;
//
// The special characters to inject on streams to run the unit parser
// in the special expression mode. Using private characters from
// Plane Sixteen (U+100000 to U+10FFFD)
//
// This character is only tested just before the tokenizer is about to report
// an error; So on the regular operation mode, this addition will have no
// impact on the tokenizer's performance.
//
public const int EvalStatementParserCharacter = 0x100000;
public const int EvalCompilationUnitParserCharacter = 0x100001;
public const int EvalUsingDeclarationsParserCharacter = 0x100002;
public const int DocumentationXref = 0x100003;
const int UnicodeLS = 0x2028;
const int UnicodePS = 0x2029;
//
// XML documentation buffer. The save point is used to divide
// comments on types and comments on members.
//
StringBuilder xml_comment_buffer;
//
// See comment on XmlCommentState enumeration.
//
XmlCommentState xml_doc_state = XmlCommentState.Allowed;
//
// Whether tokens have been seen on this line
//
bool tokens_seen = false;
//
// Set to true once the GENERATE_COMPLETION token has bee
// returned. This helps produce one GENERATE_COMPLETION,
// as many COMPLETE_COMPLETION as necessary to complete the
// AST tree and one final EOF.
//
bool generated;
//
// Whether a token has been seen on the file
// This is needed because `define' is not allowed to be used
// after a token has been seen.
//
bool any_token_seen;
//
// Class variables
//
static readonly KeywordEntry<int>[][] keywords;
static readonly KeywordEntry<PreprocessorDirective>[][] keywords_preprocessor;
static readonly HashSet<string> keyword_strings;
static readonly NumberStyles styles;
static readonly NumberFormatInfo csharp_format_info;
// Pragma arguments
static readonly char[] pragma_warning = "warning".ToCharArray ();
static readonly char[] pragma_warning_disable = "disable".ToCharArray ();
static readonly char[] pragma_warning_restore = "restore".ToCharArray ();
static readonly char[] pragma_checksum = "checksum".ToCharArray ();
static readonly char[] line_hidden = "hidden".ToCharArray ();
static readonly char[] line_default = "default".ToCharArray ();
static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
public bool PropertyParsing {
get { return handle_get_set; }
set { handle_get_set = value; }
}
public bool EventParsing {
get { return handle_remove_add; }
set { handle_remove_add = value; }
}
public bool ConstraintsParsing {
get { return handle_where; }
set { handle_where = value; }
}
public XmlCommentState doc_state {
get { return xml_doc_state; }
set {
if (value == XmlCommentState.Allowed) {
check_incorrect_doc_comment ();
reset_doc_comment ();
}
xml_doc_state = value;
}
}
//
// This is used to trigger completion generation on the parser
public bool CompleteOnEOF;
void AddEscapedIdentifier (Location loc)
{
if (escaped_identifiers == null)
escaped_identifiers = new List<Location> ();
escaped_identifiers.Add (loc);
}
public bool IsEscapedIdentifier (ATypeNameExpression name)
{
return escaped_identifiers != null && escaped_identifiers.Contains (name.Location);
}
//
// Values for the associated token returned
//
internal int putback_char; // Used by repl only
object val;
//
// Pre-processor
//
const int TAKING = 1;
const int ELSE_SEEN = 4;
const int PARENT_TAKING = 8;
const int REGION = 16;
//
// pre-processor if stack state:
//
Stack<int> ifstack;
public const int MaxIdentifierLength = 512;
public const int MaxNumberLength = 512;
readonly char[] id_builder;
readonly Dictionary<char[], string>[] identifiers;
readonly char[] number_builder;
int number_pos;
char[] value_builder = new char[64];
public int Line {
get {
return ref_line;
}
}
//
// This is used when the tokenizer needs to save
// the current position as it needs to do some parsing
// on its own to deamiguate a token in behalf of the
// parser.
//
Stack<Position> position_stack = new Stack<Position> (2);
class Position {
public int position;
public int line;
public int ref_line;
public int col;
public Location hidden;
public int putback_char;
public int previous_col;
public Stack<int> ifstack;
public int parsing_generic_less_than;
public int current_token;
public object val;
public Position (Tokenizer t)
{
position = t.reader.Position;
line = t.line;
ref_line = t.ref_line;
col = t.col;
hidden = t.hidden_block_start;
putback_char = t.putback_char;
previous_col = t.previous_col;
if (t.ifstack != null && t.ifstack.Count != 0) {
// There is no simple way to clone Stack<T> all
// methods reverse the order
var clone = t.ifstack.ToArray ();
Array.Reverse (clone);
ifstack = new Stack<int> (clone);
}
parsing_generic_less_than = t.parsing_generic_less_than;
current_token = t.current_token;
val = t.val;
}
}
public Tokenizer (SeekableStreamReader input, CompilationSourceFile file, ParserSession session, Report report)
{
this.source_file = file;
this.context = file.Compiler;
this.current_source = file.SourceFile;
this.identifiers = session.Identifiers;
this.id_builder = session.IDBuilder;
this.number_builder = session.NumberBuilder;
this.ltb = new LocatedTokenBuffer (session.LocatedTokens);
this.Report = report;
reader = input;
putback_char = -1;
xml_comment_buffer = new StringBuilder ();
doc_processing = context.Settings.DocumentationFile != null;
tab_size = context.Settings.TabSize;
}
public void PushPosition ()
{
position_stack.Push (new Position (this));
}
public void PopPosition ()
{
Position p = position_stack.Pop ();
reader.Position = p.position;
ref_line = p.ref_line;
line = p.line;
col = p.col;
hidden_block_start = p.hidden;
putback_char = p.putback_char;
previous_col = p.previous_col;
ifstack = p.ifstack;
parsing_generic_less_than = p.parsing_generic_less_than;
current_token = p.current_token;
val = p.val;
}
// Do not reset the position, ignore it.
public void DiscardPosition ()
{
position_stack.Pop ();
}
static void AddKeyword (string kw, int token)
{
keyword_strings.Add (kw);
AddKeyword (keywords, kw, token);
}
static void AddPreprocessorKeyword (string kw, PreprocessorDirective directive)
{
AddKeyword (keywords_preprocessor, kw, directive);
}
static void AddKeyword<T> (KeywordEntry<T>[][] keywords, string kw, T token)
{
int length = kw.Length;
if (keywords[length] == null) {
keywords[length] = new KeywordEntry<T>['z' - '_' + 1];
}
int char_index = kw[0] - '_';
var kwe = keywords[length][char_index];
if (kwe == null) {
keywords[length][char_index] = new KeywordEntry<T> (kw, token);
return;
}
while (kwe.Next != null) {
kwe = kwe.Next;
}
kwe.Next = new KeywordEntry<T> (kw, token);
}
//
// Class initializer
//
static Tokenizer ()
{
keyword_strings = new HashSet<string> ();
// 11 is the length of the longest keyword for now
keywords = new KeywordEntry<int>[11][];
AddKeyword ("__arglist", Token.ARGLIST);
AddKeyword ("__makeref", Token.MAKEREF);
AddKeyword ("__reftype", Token.REFTYPE);
AddKeyword ("__refvalue", Token.REFVALUE);
AddKeyword ("abstract", Token.ABSTRACT);
AddKeyword ("as", Token.AS);
AddKeyword ("add", Token.ADD);
AddKeyword ("base", Token.BASE);
AddKeyword ("bool", Token.BOOL);
AddKeyword ("break", Token.BREAK);
AddKeyword ("byte", Token.BYTE);
AddKeyword ("case", Token.CASE);
AddKeyword ("catch", Token.CATCH);
AddKeyword ("char", Token.CHAR);
AddKeyword ("checked", Token.CHECKED);
AddKeyword ("class", Token.CLASS);
AddKeyword ("const", Token.CONST);
AddKeyword ("continue", Token.CONTINUE);
AddKeyword ("decimal", Token.DECIMAL);
AddKeyword ("default", Token.DEFAULT);
AddKeyword ("delegate", Token.DELEGATE);
AddKeyword ("do", Token.DO);
AddKeyword ("double", Token.DOUBLE);
AddKeyword ("else", Token.ELSE);
AddKeyword ("enum", Token.ENUM);
AddKeyword ("event", Token.EVENT);
AddKeyword ("explicit", Token.EXPLICIT);
AddKeyword ("extern", Token.EXTERN);
AddKeyword ("false", Token.FALSE);
AddKeyword ("finally", Token.FINALLY);
AddKeyword ("fixed", Token.FIXED);
AddKeyword ("float", Token.FLOAT);
AddKeyword ("for", Token.FOR);
AddKeyword ("foreach", Token.FOREACH);
AddKeyword ("goto", Token.GOTO);
AddKeyword ("get", Token.GET);
AddKeyword ("if", Token.IF);
AddKeyword ("implicit", Token.IMPLICIT);
AddKeyword ("in", Token.IN);
AddKeyword ("int", Token.INT);
AddKeyword ("interface", Token.INTERFACE);
AddKeyword ("internal", Token.INTERNAL);
AddKeyword ("is", Token.IS);
AddKeyword ("lock", Token.LOCK);
AddKeyword ("long", Token.LONG);
AddKeyword ("namespace", Token.NAMESPACE);
AddKeyword ("new", Token.NEW);
AddKeyword ("null", Token.NULL);
AddKeyword ("object", Token.OBJECT);
AddKeyword ("operator", Token.OPERATOR);
AddKeyword ("out", Token.OUT);
AddKeyword ("override", Token.OVERRIDE);
AddKeyword ("params", Token.PARAMS);
AddKeyword ("private", Token.PRIVATE);
AddKeyword ("protected", Token.PROTECTED);
AddKeyword ("public", Token.PUBLIC);
AddKeyword ("readonly", Token.READONLY);
AddKeyword ("ref", Token.REF);
AddKeyword ("remove", Token.REMOVE);
AddKeyword ("return", Token.RETURN);
AddKeyword ("sbyte", Token.SBYTE);
AddKeyword ("sealed", Token.SEALED);
AddKeyword ("set", Token.SET);
AddKeyword ("short", Token.SHORT);
AddKeyword ("sizeof", Token.SIZEOF);
AddKeyword ("stackalloc", Token.STACKALLOC);
AddKeyword ("static", Token.STATIC);
AddKeyword ("string", Token.STRING);
AddKeyword ("struct", Token.STRUCT);
AddKeyword ("switch", Token.SWITCH);
AddKeyword ("this", Token.THIS);
AddKeyword ("throw", Token.THROW);
AddKeyword ("true", Token.TRUE);
AddKeyword ("try", Token.TRY);
AddKeyword ("typeof", Token.TYPEOF);
AddKeyword ("uint", Token.UINT);
AddKeyword ("ulong", Token.ULONG);
AddKeyword ("unchecked", Token.UNCHECKED);
AddKeyword ("unsafe", Token.UNSAFE);
AddKeyword ("ushort", Token.USHORT);
AddKeyword ("using", Token.USING);
AddKeyword ("virtual", Token.VIRTUAL);
AddKeyword ("void", Token.VOID);
AddKeyword ("volatile", Token.VOLATILE);
AddKeyword ("while", Token.WHILE);
AddKeyword ("partial", Token.PARTIAL);
AddKeyword ("where", Token.WHERE);
// LINQ keywords
AddKeyword ("from", Token.FROM);
AddKeyword ("join", Token.JOIN);
AddKeyword ("on", Token.ON);
AddKeyword ("equals", Token.EQUALS);
AddKeyword ("select", Token.SELECT);
AddKeyword ("group", Token.GROUP);
AddKeyword ("by", Token.BY);
AddKeyword ("let", Token.LET);
AddKeyword ("orderby", Token.ORDERBY);
AddKeyword ("ascending", Token.ASCENDING);
AddKeyword ("descending", Token.DESCENDING);
AddKeyword ("into", Token.INTO);
// Contextual async keywords
AddKeyword ("async", Token.ASYNC);
AddKeyword ("await", Token.AWAIT);
keywords_preprocessor = new KeywordEntry<PreprocessorDirective>[10][];
AddPreprocessorKeyword ("region", PreprocessorDirective.Region);
AddPreprocessorKeyword ("endregion", PreprocessorDirective.Endregion);
AddPreprocessorKeyword ("if", PreprocessorDirective.If);
AddPreprocessorKeyword ("endif", PreprocessorDirective.Endif);
AddPreprocessorKeyword ("elif", PreprocessorDirective.Elif);
AddPreprocessorKeyword ("else", PreprocessorDirective.Else);
AddPreprocessorKeyword ("define", PreprocessorDirective.Define);
AddPreprocessorKeyword ("undef", PreprocessorDirective.Undef);
AddPreprocessorKeyword ("error", PreprocessorDirective.Error);
AddPreprocessorKeyword ("warning", PreprocessorDirective.Warning);
AddPreprocessorKeyword ("pragma", PreprocessorDirective.Pragma);
AddPreprocessorKeyword ("line", PreprocessorDirective.Line);
csharp_format_info = NumberFormatInfo.InvariantInfo;
styles = NumberStyles.Float;
}
int GetKeyword (char[] id, int id_len)
{
//
// Keywords are stored in an array of arrays grouped by their
// length and then by the first character
//
if (id_len >= keywords.Length || keywords [id_len] == null)
return -1;
int first_index = id [0] - '_';
if (first_index > 'z' - '_')
return -1;
var kwe = keywords [id_len] [first_index];
if (kwe == null)
return -1;
int res;
do {
res = kwe.Token;
for (int i = 1; i < id_len; ++i) {
if (id [i] != kwe.Value [i]) {
res = 0;
kwe = kwe.Next;
break;
}
}
} while (res == 0 && kwe != null);
if (res == 0)
return -1;
int next_token;
switch (res) {
case Token.GET:
case Token.SET:
if (!handle_get_set)
res = -1;
break;
case Token.REMOVE:
case Token.ADD:
if (!handle_remove_add)
res = -1;
break;
case Token.EXTERN:
if (parsing_declaration == 0)
res = Token.EXTERN_ALIAS;
break;
case Token.DEFAULT:
if (peek_token () == Token.COLON) {
token ();
res = Token.DEFAULT_COLON;
}
break;
case Token.WHERE:
if (!(handle_where && current_token != Token.COLON) && !query_parsing)
res = -1;
break;
case Token.FROM:
//
// A query expression is any expression that starts with `from identifier'
// followed by any token except ; , =
//
if (!query_parsing) {
if (lambda_arguments_parsing || parsing_block == 0) {
res = -1;
break;
}
PushPosition ();
// HACK: to disable generics micro-parser, because PushPosition does not
// store identifiers array
parsing_generic_less_than = 1;
switch (xtoken ()) {
case Token.IDENTIFIER:
case Token.INT:
case Token.BOOL:
case Token.BYTE:
case Token.CHAR:
case Token.DECIMAL:
case Token.DOUBLE:
case Token.FLOAT:
case Token.LONG:
case Token.OBJECT:
case Token.STRING:
case Token.UINT:
case Token.ULONG:
next_token = xtoken ();
if (next_token == Token.SEMICOLON || next_token == Token.COMMA || next_token == Token.EQUALS || next_token == Token.ASSIGN)
goto default;
res = Token.FROM_FIRST;
query_parsing = true;
if (context.Settings.Version <= LanguageVersion.ISO_2)
Report.FeatureIsNotAvailable (context, Location, "query expressions");
break;
case Token.VOID:
Expression.Error_VoidInvalidInTheContext (Location, Report);
break;
default:
PopPosition ();
// HACK: A token is not a keyword so we need to restore identifiers buffer
// which has been overwritten before we grabbed the identifier
id_builder [0] = 'f'; id_builder [1] = 'r'; id_builder [2] = 'o'; id_builder [3] = 'm';
return -1;
}
PopPosition ();
}
break;
case Token.JOIN:
case Token.ON:
case Token.EQUALS:
case Token.SELECT:
case Token.GROUP:
case Token.BY:
case Token.LET:
case Token.ORDERBY:
case Token.ASCENDING:
case Token.DESCENDING:
case Token.INTO:
if (!query_parsing)
res = -1;
break;
case Token.USING:
case Token.NAMESPACE:
// TODO: some explanation needed
check_incorrect_doc_comment ();
parsing_modifiers = false;
break;
case Token.PARTIAL:
if (parsing_block > 0) {
res = -1;
break;
}
// Save current position and parse next token.
PushPosition ();
next_token = token ();
bool ok = (next_token == Token.CLASS) ||
(next_token == Token.STRUCT) ||
(next_token == Token.INTERFACE) ||
(next_token == Token.VOID);
PopPosition ();
if (ok) {
if (next_token == Token.VOID) {
if (context.Settings.Version <= LanguageVersion.ISO_2)
Report.FeatureIsNotAvailable (context, Location, "partial methods");
} else if (context.Settings.Version == LanguageVersion.ISO_1)
Report.FeatureIsNotAvailable (context, Location, "partial types");
return res;
}
if (next_token < Token.LAST_KEYWORD) {
Report.Error (267, Location,
"The `partial' modifier can be used only immediately before `class', `struct', `interface', or `void' keyword");
return token ();
}
// HACK: A token is not a keyword so we need to restore identifiers buffer
// which has been overwritten before we grabbed the identifier
id_builder[0] = 'p';
id_builder[1] = 'a';
id_builder[2] = 'r';
id_builder[3] = 't';
id_builder[4] = 'i';
id_builder[5] = 'a';
id_builder[6] = 'l';
res = -1;
break;
case Token.ASYNC:
if (parsing_modifiers) {
//
// Skip attributes section or constructor called async
//
if (parsing_attribute_section || peek_token () == Token.OPEN_PARENS) {
res = -1;
} else {
// async is keyword
}
} else if (parsing_block > 0) {
switch (peek_token ()) {
case Token.DELEGATE:
case Token.OPEN_PARENS_LAMBDA:
// async is keyword
break;
case Token.IDENTIFIER:
PushPosition ();
xtoken ();
if (xtoken () != Token.ARROW) {
PopPosition ();
goto default;
}
PopPosition ();
break;
default:
// peek_token could overwrite id_buffer
id_builder [0] = 'a'; id_builder [1] = 's'; id_builder [2] = 'y'; id_builder [3] = 'n'; id_builder [4] = 'c';
res = -1;
break;
}
} else {
res = -1;
}
if (res == Token.ASYNC && context.Settings.Version <= LanguageVersion.V_4) {
Report.FeatureIsNotAvailable (context, Location, "asynchronous functions");
}
break;
case Token.AWAIT:
if (parsing_block == 0)
res = -1;
break;
}
return res;
}
static PreprocessorDirective GetPreprocessorDirective (char[] id, int id_len)
{
//
// Keywords are stored in an array of arrays grouped by their
// length and then by the first character
//
if (id_len >= keywords_preprocessor.Length || keywords_preprocessor[id_len] == null)
return PreprocessorDirective.Invalid;
int first_index = id[0] - '_';
if (first_index > 'z' - '_')
return PreprocessorDirective.Invalid;
var kwe = keywords_preprocessor[id_len][first_index];
if (kwe == null)
return PreprocessorDirective.Invalid;
PreprocessorDirective res = PreprocessorDirective.Invalid;
do {
res = kwe.Token;
for (int i = 1; i < id_len; ++i) {
if (id[i] != kwe.Value[i]) {
res = 0;
kwe = kwe.Next;
break;
}
}
} while (res == PreprocessorDirective.Invalid && kwe != null);
return res;
}
public Location Location {
get {
return new Location (current_source, ref_line, col);
}
}
static bool is_identifier_start_character (int c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter ((char)c);
}
static bool is_identifier_part_character (char c)
{
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
return true;
if (c == '_' || (c >= '0' && c <= '9'))
return true;
if (c < 0x80)
return false;
return is_identifier_part_character_slow_part (c);
}
static bool is_identifier_part_character_slow_part (char c)
{
if (Char.IsLetter (c))
return true;
switch (Char.GetUnicodeCategory (c)) {
case UnicodeCategory.ConnectorPunctuation:
// combining-character: A Unicode character of classes Mn or Mc
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.SpacingCombiningMark:
// decimal-digit-character: A Unicode character of the class Nd
case UnicodeCategory.DecimalDigitNumber:
return true;
}
return false;
}
public static bool IsKeyword (string s)
{
return keyword_strings.Contains (s);
}
//
// Open parens micro parser. Detects both lambda and cast ambiguity.
//
int TokenizeOpenParens ()
{
int ptoken;
current_token = -1;
int bracket_level = 0;
bool is_type = false;
bool can_be_type = false;
while (true) {
ptoken = current_token;
token ();
switch (current_token) {
case Token.CLOSE_PARENS:
token ();
//
// Expression inside parens is lambda, (int i) =>
//
if (current_token == Token.ARROW)
return Token.OPEN_PARENS_LAMBDA;
//
// Expression inside parens is single type, (int[])
//
if (is_type) {
if (current_token == Token.SEMICOLON)
return Token.OPEN_PARENS;
return Token.OPEN_PARENS_CAST;
}
//
// Expression is possible cast, look at next token, (T)null
//
if (can_be_type) {
switch (current_token) {