2 // cs-tokenizer.cs: The Tokenizer for the C# compiler
\r
3 // This also implements the preprocessor
\r
5 // Author: Miguel de Icaza (miguel@gnu.org)
\r
7 // Licensed under the terms of the GNU GPL
\r
9 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
\r
14 * Make sure we accept the proper Unicode ranges, per the spec.
\r
20 using System.Collections;
\r
22 using System.Globalization;
\r
23 using System.Reflection;
\r
25 namespace Mono.CSharp
\r
28 /// Tokenizer for C# source code.
\r
31 public class Tokenizer : yyParser.yyInput
\r
33 SeekableStreamReader reader;
\r
34 public SourceFile ref_name;
\r
35 public SourceFile file_name;
\r
36 public int ref_line = 1;
\r
37 public int line = 1;
\r
39 public int current_token;
\r
40 bool handle_get_set = false;
\r
41 bool handle_remove_add = false;
\r
42 bool handle_assembly = false;
\r
43 bool handle_constraints = false;
\r
46 // Whether tokens have been seen on this line
\r
48 bool tokens_seen = false;
\r
51 // Whether a token has been seen on the file
\r
52 // This is needed because `define' is not allowed to be used
\r
53 // after a token has been seen.
\r
55 bool any_token_seen = false;
\r
56 static Hashtable tokenValues;
\r
58 private static Hashtable TokenValueName
\r
61 if (tokenValues == null)
\r
62 tokenValues = GetTokenValueNameHash ();
\r
68 private static Hashtable GetTokenValueNameHash ()
\r
70 Type t = typeof (Token);
\r
71 FieldInfo [] fields = t.GetFields ();
\r
72 Hashtable hash = new Hashtable ();
\r
73 foreach (FieldInfo field in fields) {
\r
74 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
\r
75 hash.Add (field.GetValue (null), field.Name);
\r
81 // Returns a verbose representation of the current location
\r
83 public string location {
\r
87 if (current_token == Token.ERROR)
\r
88 det = "detail: " + error_details;
\r
92 // return "Line: "+line+" Col: "+col + "\n" +
\r
93 // "VirtLine: "+ref_line +
\r
94 // " Token: "+current_token + " " + det;
\r
95 string current_token_name = TokenValueName [current_token] as string;
\r
96 if (current_token_name == null)
\r
97 current_token_name = current_token.ToString ();
\r
99 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
\r
102 current_token_name,
\r
107 public bool PropertyParsing {
\r
109 return handle_get_set;
\r
113 handle_get_set = value;
\r
117 public bool AssemblyTargetParsing {
\r
119 return handle_assembly;
\r
123 handle_assembly = value;
\r
127 public bool EventParsing {
\r
129 return handle_remove_add;
\r
133 handle_remove_add = value;
\r
137 public bool ConstraintsParsing {
\r
139 return handle_constraints;
\r
143 handle_constraints = value;
\r
150 static CharArrayHashtable[] keywords;
\r
151 static NumberStyles styles;
\r
152 static NumberFormatInfo csharp_format_info;
\r
155 // Values for the associated token returned
\r
165 const int TAKING = 1;
\r
166 const int TAKEN_BEFORE = 2;
\r
167 const int ELSE_SEEN = 4;
\r
168 const int PARENT_TAKING = 8;
\r
169 const int REGION = 16;
\r
172 // pre-processor if stack state:
\r
176 static System.Text.StringBuilder string_builder;
\r
178 const int max_id_size = 512;
\r
179 static char [] id_builder = new char [max_id_size];
\r
181 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
\r
183 const int max_number_size = 128;
\r
184 static char [] number_builder = new char [max_number_size];
\r
185 static int number_pos;
\r
188 // Details about the error encoutered by the tokenizer
\r
190 string error_details;
\r
192 public string error {
\r
194 return error_details;
\r
210 static void AddKeyword (string kw, int token) {
\r
211 if (keywords [kw.Length] == null) {
\r
212 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
\r
214 keywords [kw.Length] [kw.ToCharArray ()] = token;
\r
217 static void InitTokens ()
\r
219 keywords = new CharArrayHashtable [64];
\r
221 AddKeyword ("abstract", Token.ABSTRACT);
\r
222 AddKeyword ("as", Token.AS);
\r
223 AddKeyword ("add", Token.ADD);
\r
224 AddKeyword ("assembly", Token.ASSEMBLY);
\r
225 AddKeyword ("base", Token.BASE);
\r
226 AddKeyword ("bool", Token.BOOL);
\r
227 AddKeyword ("break", Token.BREAK);
\r
228 AddKeyword ("byte", Token.BYTE);
\r
229 AddKeyword ("case", Token.CASE);
\r
230 AddKeyword ("catch", Token.CATCH);
\r
231 AddKeyword ("char", Token.CHAR);
\r
232 AddKeyword ("checked", Token.CHECKED);
\r
233 AddKeyword ("class", Token.CLASS);
\r
234 AddKeyword ("const", Token.CONST);
\r
235 AddKeyword ("continue", Token.CONTINUE);
\r
236 AddKeyword ("decimal", Token.DECIMAL);
\r
237 AddKeyword ("default", Token.DEFAULT);
\r
238 AddKeyword ("delegate", Token.DELEGATE);
\r
239 AddKeyword ("do", Token.DO);
\r
240 AddKeyword ("double", Token.DOUBLE);
\r
241 AddKeyword ("else", Token.ELSE);
\r
242 AddKeyword ("enum", Token.ENUM);
\r
243 AddKeyword ("event", Token.EVENT);
\r
244 AddKeyword ("explicit", Token.EXPLICIT);
\r
245 AddKeyword ("extern", Token.EXTERN);
\r
246 AddKeyword ("false", Token.FALSE);
\r
247 AddKeyword ("finally", Token.FINALLY);
\r
248 AddKeyword ("fixed", Token.FIXED);
\r
249 AddKeyword ("float", Token.FLOAT);
\r
250 AddKeyword ("for", Token.FOR);
\r
251 AddKeyword ("foreach", Token.FOREACH);
\r
252 AddKeyword ("goto", Token.GOTO);
\r
253 AddKeyword ("get", Token.GET);
\r
254 AddKeyword ("if", Token.IF);
\r
255 AddKeyword ("implicit", Token.IMPLICIT);
\r
256 AddKeyword ("in", Token.IN);
\r
257 AddKeyword ("int", Token.INT);
\r
258 AddKeyword ("interface", Token.INTERFACE);
\r
259 AddKeyword ("internal", Token.INTERNAL);
\r
260 AddKeyword ("is", Token.IS);
\r
261 AddKeyword ("lock", Token.LOCK);
\r
262 AddKeyword ("long", Token.LONG);
\r
263 AddKeyword ("namespace", Token.NAMESPACE);
\r
264 AddKeyword ("new", Token.NEW);
\r
265 AddKeyword ("null", Token.NULL);
\r
266 AddKeyword ("object", Token.OBJECT);
\r
267 AddKeyword ("operator", Token.OPERATOR);
\r
268 AddKeyword ("out", Token.OUT);
\r
269 AddKeyword ("override", Token.OVERRIDE);
\r
270 AddKeyword ("params", Token.PARAMS);
\r
271 AddKeyword ("private", Token.PRIVATE);
\r
272 AddKeyword ("protected", Token.PROTECTED);
\r
273 AddKeyword ("public", Token.PUBLIC);
\r
274 AddKeyword ("readonly", Token.READONLY);
\r
275 AddKeyword ("ref", Token.REF);
\r
276 AddKeyword ("remove", Token.REMOVE);
\r
277 AddKeyword ("return", Token.RETURN);
\r
278 AddKeyword ("sbyte", Token.SBYTE);
\r
279 AddKeyword ("sealed", Token.SEALED);
\r
280 AddKeyword ("set", Token.SET);
\r
281 AddKeyword ("short", Token.SHORT);
\r
282 AddKeyword ("sizeof", Token.SIZEOF);
\r
283 AddKeyword ("stackalloc", Token.STACKALLOC);
\r
284 AddKeyword ("static", Token.STATIC);
\r
285 AddKeyword ("string", Token.STRING);
\r
286 AddKeyword ("struct", Token.STRUCT);
\r
287 AddKeyword ("switch", Token.SWITCH);
\r
288 AddKeyword ("this", Token.THIS);
\r
289 AddKeyword ("throw", Token.THROW);
\r
290 AddKeyword ("true", Token.TRUE);
\r
291 AddKeyword ("try", Token.TRY);
\r
292 AddKeyword ("typeof", Token.TYPEOF);
\r
293 AddKeyword ("uint", Token.UINT);
\r
294 AddKeyword ("ulong", Token.ULONG);
\r
295 AddKeyword ("unchecked", Token.UNCHECKED);
\r
296 AddKeyword ("unsafe", Token.UNSAFE);
\r
297 AddKeyword ("ushort", Token.USHORT);
\r
298 AddKeyword ("using", Token.USING);
\r
299 AddKeyword ("virtual", Token.VIRTUAL);
\r
300 AddKeyword ("void", Token.VOID);
\r
301 AddKeyword ("volatile", Token.VOLATILE);
\r
302 AddKeyword ("where", Token.WHERE);
\r
303 AddKeyword ("while", Token.WHILE);
\r
307 // Class initializer
\r
309 static Tokenizer ()
\r
312 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
313 styles = NumberStyles.Float;
\r
315 string_builder = new System.Text.StringBuilder ();
\r
318 int GetKeyword (char[] id, int id_len)
\r
321 * Keywords are stored in an array of hashtables grouped by their
\r
325 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
\r
327 object o = keywords [id_len] [id];
\r
334 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
336 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
338 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
340 if (handle_constraints == false && res == Token.WHERE)
\r
346 public Location Location {
\r
348 return new Location (ref_line);
\r
352 void define (string def)
\r
354 if (!RootContext.AllDefines.Contains (def)){
\r
355 RootContext.AllDefines [def] = true;
\r
357 if (defines.Contains (def))
\r
359 defines [def] = true;
\r
362 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
364 this.ref_name = file;
\r
365 this.file_name = file;
\r
371 defines = new Hashtable ();
\r
372 foreach (string def in defs)
\r
377 // FIXME: This could be `Location.Push' but we have to
\r
378 // find out why the MS compiler allows this
\r
380 Mono.CSharp.Location.Push (file);
\r
383 public static void Cleanup () {
\r
384 identifiers = null;
\r
387 bool is_identifier_start_character (char c)
\r
389 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
392 bool is_identifier_part_character (char c)
\r
394 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
397 bool parse_less_than ()
\r
400 int the_token = token ();
\r
401 switch (the_token) {
\r
402 case Token.IDENTIFIER:
\r
406 case Token.DECIMAL:
\r
425 the_token = token ();
\r
427 if (the_token == Token.OP_GENERICS_GT)
\r
429 else if (the_token == Token.COMMA)
\r
431 else if (the_token == Token.OP_GENERICS_LT) {
\r
432 if (!parse_less_than ())
\r
440 bool parsing_less_than = false;
\r
441 int parsing_generic_less_than = 0;
\r
443 int is_punct (char c, ref bool doread)
\r
452 return Token.OPEN_BRACE;
\r
454 return Token.CLOSE_BRACE;
\r
456 return Token.OPEN_BRACKET;
\r
458 return Token.CLOSE_BRACKET;
\r
460 return Token.OPEN_PARENS;
\r
462 if (deambiguate_close_parens == 0)
\r
463 return Token.CLOSE_PARENS;
\r
465 --deambiguate_close_parens;
\r
467 // Save current position and parse next token.
\r
468 int old = reader.Position;
\r
469 int new_token = token ();
\r
470 reader.Position = old;
\r
473 if (new_token == Token.OPEN_PARENS)
\r
474 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
475 else if (new_token == Token.MINUS)
\r
476 return Token.CLOSE_PARENS_MINUS;
\r
477 else if (IsCastToken (new_token))
\r
478 return Token.CLOSE_PARENS_CAST;
\r
480 return Token.CLOSE_PARENS_NO_CAST;
\r
484 return Token.COMMA;
\r
486 return Token.COLON;
\r
488 return Token.SEMICOLON;
\r
490 return Token.TILDE;
\r
492 return Token.INTERR;
\r
496 if (parsing_generic_less_than++ > 0)
\r
497 return Token.OP_GENERICS_LT;
\r
499 // Save current position and parse next token.
\r
500 int old = reader.Position;
\r
501 bool is_generic_lt = parse_less_than ();
\r
502 reader.Position = old;
\r
505 if (is_generic_lt) {
\r
506 parsing_generic_less_than++;
\r
507 return Token.OP_GENERICS_LT;
\r
509 parsing_generic_less_than = 0;
\r
518 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
520 return Token.OP_SHIFT_LEFT;
\r
521 } else if (d == '='){
\r
523 return Token.OP_LE;
\r
525 return Token.OP_LT;
\r
526 } else if (c == '>') {
\r
527 if (parsing_generic_less_than > 0) {
\r
528 parsing_generic_less_than--;
\r
529 return Token.OP_GENERICS_GT;
\r
539 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
541 return Token.OP_SHIFT_RIGHT;
\r
542 } else if (d == '='){
\r
544 return Token.OP_GE;
\r
546 return Token.OP_GT;
\r
555 t = Token.OP_ADD_ASSIGN;
\r
565 t = Token.OP_SUB_ASSIGN;
\r
569 return Token.MINUS;
\r
577 return Token.OP_NE;
\r
585 return Token.OP_EQ;
\r
587 return Token.ASSIGN;
\r
593 return Token.OP_AND;
\r
594 } else if (d == '='){
\r
596 return Token.OP_AND_ASSIGN;
\r
598 return Token.BITWISE_AND;
\r
604 return Token.OP_OR;
\r
605 } else if (d == '='){
\r
607 return Token.OP_OR_ASSIGN;
\r
609 return Token.BITWISE_OR;
\r
615 return Token.OP_MULT_ASSIGN;
\r
623 return Token.OP_DIV_ASSIGN;
\r
631 return Token.OP_MOD_ASSIGN;
\r
633 return Token.PERCENT;
\r
639 return Token.OP_XOR_ASSIGN;
\r
641 return Token.CARRET;
\r
644 return Token.ERROR;
\r
647 int deambiguate_close_parens = 0;
\r
649 public void Deambiguate_CloseParens ()
\r
652 deambiguate_close_parens++;
\r
655 void Error_NumericConstantTooLong ()
\r
657 Report.Error (1021, Location, "Numeric constant too long");
\r
660 bool decimal_digits (int c)
\r
663 bool seen_digits = false;
\r
666 if (number_pos == max_number_size)
\r
667 Error_NumericConstantTooLong ();
\r
668 number_builder [number_pos++] = (char) c;
\r
672 // We use peekChar2, because decimal_digits needs to do a
\r
673 // 2-character look-ahead (5.ToString for example).
\r
675 while ((d = peekChar2 ()) != -1){
\r
676 if (d >= '0' && d <= '9'){
\r
677 if (number_pos == max_number_size)
\r
678 Error_NumericConstantTooLong ();
\r
679 number_builder [number_pos++] = (char) d;
\r
681 seen_digits = true;
\r
686 return seen_digits;
\r
689 bool is_hex (int e)
\r
691 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
694 void hex_digits (int c)
\r
697 number_builder [number_pos++] = (char) c;
\r
701 int real_type_suffix (int c)
\r
706 case 'F': case 'f':
\r
707 t = Token.LITERAL_FLOAT;
\r
709 case 'D': case 'd':
\r
710 t = Token.LITERAL_DOUBLE;
\r
712 case 'M': case 'm':
\r
713 t= Token.LITERAL_DECIMAL;
\r
721 int integer_type_suffix (ulong ul, int c)
\r
723 bool is_unsigned = false;
\r
724 bool is_long = false;
\r
727 bool scanning = true;
\r
730 case 'U': case 'u':
\r
733 is_unsigned = true;
\r
740 // if we have not seen anything in between
\r
741 // report this error
\r
745 "the 'l' suffix is easily confused with digit `1'," +
\r
746 " use 'L' for clarity");
\r
762 } while (scanning);
\r
765 if (is_long && is_unsigned){
\r
767 return Token.LITERAL_INTEGER;
\r
768 } else if (is_unsigned){
\r
769 // uint if possible, or ulong else.
\r
771 if ((ul & 0xffffffff00000000) == 0)
\r
775 } else if (is_long){
\r
776 // long if possible, ulong otherwise
\r
777 if ((ul & 0x8000000000000000) != 0)
\r
782 // int, uint, long or ulong in that order
\r
783 if ((ul & 0xffffffff00000000) == 0){
\r
784 uint ui = (uint) ul;
\r
786 if ((ui & 0x80000000) != 0)
\r
791 if ((ul & 0x8000000000000000) != 0)
\r
797 return Token.LITERAL_INTEGER;
\r
801 // given `c' as the next char in the input decide whether
\r
802 // we need to convert to a special type, and then choose
\r
803 // the best representation for the integer
\r
805 int adjust_int (int c)
\r
808 if (number_pos > 9){
\r
809 ulong ul = (uint) (number_builder [0] - '0');
\r
811 for (int i = 1; i < number_pos; i++){
\r
812 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
814 return integer_type_suffix (ul, c);
\r
816 uint ui = (uint) (number_builder [0] - '0');
\r
818 for (int i = 1; i < number_pos; i++){
\r
819 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
821 return integer_type_suffix (ui, c);
\r
823 } catch (OverflowException) {
\r
824 error_details = "Integral constant is too large";
\r
825 Report.Error (1021, Location, error_details);
\r
827 return Token.LITERAL_INTEGER;
\r
831 int adjust_real (int t)
\r
833 string s = new String (number_builder, 0, number_pos);
\r
836 case Token.LITERAL_DECIMAL:
\r
838 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
839 } catch (OverflowException) {
\r
841 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
842 Report.Error (594, Location, error_details);
\r
845 case Token.LITERAL_FLOAT:
\r
847 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
848 } catch (OverflowException) {
\r
850 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
851 Report.Error (594, Location, error_details);
\r
855 case Token.LITERAL_DOUBLE:
\r
857 t = Token.LITERAL_DOUBLE;
\r
859 val = System.Double.Parse (s, styles, csharp_format_info);
\r
860 } catch (OverflowException) {
\r
862 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
863 Report.Error (594, Location, error_details);
\r
876 while ((d = peekChar ()) != -1){
\r
878 number_builder [number_pos++] = (char) d;
\r
884 string s = new String (number_builder, 0, number_pos);
\r
886 if (number_pos <= 8)
\r
887 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
889 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
890 } catch (OverflowException){
\r
891 error_details = "Integral constant is too large";
\r
892 Report.Error (1021, Location, error_details);
\r
894 return Token.LITERAL_INTEGER;
\r
897 return integer_type_suffix (ul, peekChar ());
\r
901 // Invoked if we know we have .digits or digits
\r
903 int is_number (int c)
\r
905 bool is_real = false;
\r
910 if (c >= '0' && c <= '9'){
\r
912 int peek = peekChar ();
\r
914 if (peek == 'x' || peek == 'X')
\r
915 return handle_hex ();
\r
917 decimal_digits (c);
\r
922 // We need to handle the case of
\r
923 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
926 if (decimal_digits ('.')){
\r
932 return adjust_int (-1);
\r
936 if (c == 'e' || c == 'E'){
\r
938 if (number_pos == max_number_size)
\r
939 Error_NumericConstantTooLong ();
\r
940 number_builder [number_pos++] = 'e';
\r
944 if (number_pos == max_number_size)
\r
945 Error_NumericConstantTooLong ();
\r
946 number_builder [number_pos++] = '+';
\r
948 } else if (c == '-') {
\r
949 if (number_pos == max_number_size)
\r
950 Error_NumericConstantTooLong ();
\r
951 number_builder [number_pos++] = '-';
\r
954 if (number_pos == max_number_size)
\r
955 Error_NumericConstantTooLong ();
\r
956 number_builder [number_pos++] = '+';
\r
959 decimal_digits (c);
\r
963 type = real_type_suffix (c);
\r
964 if (type == Token.NONE && !is_real){
\r
966 return adjust_int (c);
\r
970 if (type == Token.NONE){
\r
975 return adjust_real (type);
\r
977 Console.WriteLine ("This should not be reached");
\r
978 throw new Exception ("Is Number should never reach this point");
\r
982 // Accepts exactly count (4 or 8) hex, no more no less
\r
984 int getHex (int count, out bool error)
\r
989 int top = count != -1 ? count : 4;
\r
993 for (i = 0; i < top; i++){
\r
996 if (c >= '0' && c <= '9')
\r
997 c = (int) c - (int) '0';
\r
998 else if (c >= 'A' && c <= 'F')
\r
999 c = (int) c - (int) 'A' + 10;
\r
1000 else if (c >= 'a' && c <= 'f')
\r
1001 c = (int) c - (int) 'a' + 10;
\r
1007 total = (total * 16) + c;
\r
1009 int p = peekChar ();
\r
1012 if (!is_hex ((char)p))
\r
1019 int escape (int c)
\r
1053 v = getHex (-1, out error);
\r
1058 v = getHex (4, out error);
\r
1063 v = getHex (8, out error);
\r
1068 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1077 if (putback_char != -1){
\r
1078 int x = putback_char;
\r
1079 putback_char = -1;
\r
1083 return reader.Read ();
\r
1088 if (putback_char != -1)
\r
1089 return putback_char;
\r
1090 putback_char = reader.Read ();
\r
1091 return putback_char;
\r
1096 if (putback_char != -1)
\r
1097 return putback_char;
\r
1098 return reader.Peek ();
\r
1101 void putback (int c)
\r
1103 if (putback_char != -1){
\r
1104 Console.WriteLine ("Col: " + col);
\r
1105 Console.WriteLine ("Row: " + line);
\r
1106 Console.WriteLine ("Name: " + ref_name.Name);
\r
1107 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1108 throw new Exception ("This should not happen putback on putback");
\r
1113 public bool advance ()
\r
1115 return peekChar () != -1;
\r
1118 public Object Value {
\r
1124 public Object value ()
\r
1129 bool IsCastToken (int token)
\r
1134 case Token.IDENTIFIER:
\r
1135 case Token.LITERAL_INTEGER:
\r
1136 case Token.LITERAL_FLOAT:
\r
1137 case Token.LITERAL_DOUBLE:
\r
1138 case Token.LITERAL_DECIMAL:
\r
1139 case Token.LITERAL_CHARACTER:
\r
1140 case Token.LITERAL_STRING:
\r
1142 case Token.CHECKED:
\r
1147 case Token.SIZEOF:
\r
1151 case Token.TYPEOF:
\r
1152 case Token.UNCHECKED:
\r
1153 case Token.UNSAFE:
\r
1156 // These can be part of a member access
\r
1161 case Token.USHORT:
\r
1164 case Token.DOUBLE:
\r
1174 public int token ()
\r
1176 current_token = xtoken ();
\r
1178 if (current_token != Token.DEFAULT)
\r
1179 return current_token;
\r
1181 int c = consume_whitespace ();
\r
1183 current_token = Token.ERROR;
\r
1184 else if (c == '(')
\r
1185 current_token = Token.DEFAULT_OPEN_PARENS;
\r
1189 return current_token;
\r
1192 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1194 void get_cmd_arg (out string cmd, out string arg)
\r
1198 tokens_seen = false;
\r
1200 static_cmd_arg.Length = 0;
\r
1202 // skip over white space
\r
1203 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1206 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1207 static_cmd_arg.Append ((char) c);
\r
1211 cmd = static_cmd_arg.ToString ();
\r
1217 } else if (c == '\r')
\r
1220 // skip over white space
\r
1221 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1228 } else if (c == '\r'){
\r
1233 static_cmd_arg.Length = 0;
\r
1234 static_cmd_arg.Append ((char) c);
\r
1236 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1237 static_cmd_arg.Append ((char) c);
\r
1243 } else if (c == '\r')
\r
1245 arg = static_cmd_arg.ToString ().Trim ();
\r
1249 // Handles the #line directive
\r
1251 bool PreProcessLine (string arg)
\r
1256 if (arg == "default"){
\r
1258 ref_name = file_name;
\r
1259 Location.Push (ref_name);
\r
1261 } else if (arg == "hidden"){
\r
1263 // We ignore #line hidden
\r
1271 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1272 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1275 char [] quotes = { '\"' };
\r
1277 string name = arg.Substring (pos). Trim (quotes);
\r
1278 ref_name = Location.LookupFile (name);
\r
1279 file_name.HasLineDirective = true;
\r
1280 ref_name.HasLineDirective = true;
\r
1281 Location.Push (ref_name);
\r
1283 ref_line = System.Int32.Parse (arg);
\r
1293 // Handles #define and #undef
\r
1295 void PreProcessDefinition (bool is_define, string arg)
\r
1297 if (arg == "" || arg == "true" || arg == "false"){
\r
1298 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1302 char[] whitespace = { ' ', '\t' };
\r
1303 if (arg.IndexOfAny (whitespace) != -1){
\r
1304 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1308 if (!is_identifier_start_character (arg [0]))
\r
1309 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1311 foreach (char c in arg.Substring (1)){
\r
1312 if (!is_identifier_part_character (c)){
\r
1313 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1319 if (defines == null)
\r
1320 defines = new Hashtable ();
\r
1323 if (defines == null)
\r
1325 if (defines.Contains (arg))
\r
1326 defines.Remove (arg);
\r
1330 bool eval_val (string s)
\r
1337 if (defines == null)
\r
1339 if (defines.Contains (s))
\r
1345 bool pp_primary (ref string s)
\r
1348 int len = s.Length;
\r
1354 s = s.Substring (1);
\r
1355 bool val = pp_expr (ref s);
\r
1356 if (s.Length > 0 && s [0] == ')'){
\r
1357 s = s.Substring (1);
\r
1360 Error_InvalidDirective ();
\r
1364 if (is_identifier_start_character (c)){
\r
1370 if (is_identifier_part_character (c)){
\r
1374 bool v = eval_val (s.Substring (0, j));
\r
1375 s = s.Substring (j);
\r
1378 bool vv = eval_val (s);
\r
1383 Error_InvalidDirective ();
\r
1387 bool pp_unary (ref string s)
\r
1390 int len = s.Length;
\r
1393 if (s [0] == '!'){
\r
1394 if (len > 1 && s [1] == '='){
\r
1395 Error_InvalidDirective ();
\r
1398 s = s.Substring (1);
\r
1399 return ! pp_primary (ref s);
\r
1401 return pp_primary (ref s);
\r
1403 Error_InvalidDirective ();
\r
1408 bool pp_eq (ref string s)
\r
1410 bool va = pp_unary (ref s);
\r
1413 int len = s.Length;
\r
1415 if (s [0] == '='){
\r
1416 if (len > 2 && s [1] == '='){
\r
1417 s = s.Substring (2);
\r
1418 return va == pp_unary (ref s);
\r
1420 Error_InvalidDirective ();
\r
1423 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1424 s = s.Substring (2);
\r
1426 return va != pp_unary (ref s);
\r
1435 bool pp_and (ref string s)
\r
1437 bool va = pp_eq (ref s);
\r
1440 int len = s.Length;
\r
1442 if (s [0] == '&'){
\r
1443 if (len > 2 && s [1] == '&'){
\r
1444 s = s.Substring (2);
\r
1445 return (va & pp_eq (ref s));
\r
1447 Error_InvalidDirective ();
\r
1456 // Evaluates an expression for `#if' or `#elif'
\r
1458 bool pp_expr (ref string s)
\r
1460 bool va = pp_and (ref s);
\r
1462 int len = s.Length;
\r
1467 if (len > 2 && s [1] == '|'){
\r
1468 s = s.Substring (2);
\r
1469 return va | pp_expr (ref s);
\r
1471 Error_InvalidDirective ();
\r
1480 bool eval (string s)
\r
1482 bool v = pp_expr (ref s);
\r
1484 if (s.Length != 0){
\r
1485 Error_InvalidDirective ();
\r
1492 void Error_InvalidDirective ()
\r
1494 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1497 void Error_UnexpectedDirective (string extra)
\r
1501 "Unexpected processor directive (" + extra + ")");
\r
1504 void Error_TokensSeen ()
\r
1508 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1512 // if true, then the code continues processing the code
\r
1513 // if false, the code stays in a loop until another directive is
\r
1516 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1519 bool region_directive = false;
\r
1521 get_cmd_arg (out cmd, out arg);
\r
1523 // Eat any trailing whitespaces and single-line comments
\r
1524 if (arg.IndexOf ("//") != -1)
\r
1525 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1526 arg = arg.TrimEnd (' ', '\t');
\r
1529 // The first group of pre-processing instructions is always processed
\r
1533 if (RootContext.V2)
\r
1538 if (!PreProcessLine (arg))
\r
1541 "Argument to #line directive is missing or invalid");
\r
1545 region_directive = true;
\r
1550 region_directive = true;
\r
1551 goto case "endif";
\r
1555 Error_InvalidDirective ();
\r
1558 bool taking = false;
\r
1559 if (ifstack == null)
\r
1560 ifstack = new Stack ();
\r
1562 if (ifstack.Count == 0){
\r
1565 int state = (int) ifstack.Peek ();
\r
1566 if ((state & TAKING) != 0)
\r
1570 if (eval (arg) && taking){
\r
1571 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1572 if (region_directive)
\r
1574 ifstack.Push (push);
\r
1577 int push = (taking ? PARENT_TAKING : 0);
\r
1578 if (region_directive)
\r
1580 ifstack.Push (push);
\r
1585 if (ifstack == null || ifstack.Count == 0){
\r
1586 Error_UnexpectedDirective ("no #if for this #endif");
\r
1589 int pop = (int) ifstack.Pop ();
\r
1591 if (region_directive && ((pop & REGION) == 0))
\r
1592 Report.Error (1027, Location, "#endif directive expected");
\r
1593 else if (!region_directive && ((pop & REGION) != 0))
\r
1594 Report.Error (1038, Location, "#endregion directive expected");
\r
1596 if (ifstack.Count == 0)
\r
1599 int state = (int) ifstack.Peek ();
\r
1601 if ((state & TAKING) != 0)
\r
1609 if (ifstack == null || ifstack.Count == 0){
\r
1610 Error_UnexpectedDirective ("no #if for this #elif");
\r
1613 int state = (int) ifstack.Peek ();
\r
1615 if ((state & REGION) != 0) {
\r
1616 Report.Error (1038, Location, "#endregion directive expected");
\r
1620 if ((state & ELSE_SEEN) != 0){
\r
1621 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1625 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1628 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1629 state = (int) ifstack.Pop ();
\r
1630 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1637 if (ifstack == null || ifstack.Count == 0){
\r
1640 "Unexpected processor directive (no #if for this #else)");
\r
1643 int state = (int) ifstack.Peek ();
\r
1645 if ((state & REGION) != 0) {
\r
1646 Report.Error (1038, Location, "#endregion directive expected");
\r
1650 if ((state & ELSE_SEEN) != 0){
\r
1651 Error_UnexpectedDirective ("#else within #else");
\r
1658 if ((state & TAKEN_BEFORE) == 0){
\r
1659 ret = ((state & PARENT_TAKING) != 0);
\r
1668 ifstack.Push (state | ELSE_SEEN);
\r
1675 // These are only processed if we are in a `taking' block
\r
1677 if (!caller_is_taking)
\r
1682 if (any_token_seen){
\r
1683 Error_TokensSeen ();
\r
1686 PreProcessDefinition (true, arg);
\r
1690 if (any_token_seen){
\r
1691 Error_TokensSeen ();
\r
1694 PreProcessDefinition (false, arg);
\r
1698 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1702 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1706 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1711 private int consume_string (bool quoted)
\r
1714 string_builder.Length = 0;
\r
1716 while ((c = getChar ()) != -1){
\r
1718 if (quoted && peekChar () == '"'){
\r
1719 string_builder.Append ((char) c);
\r
1723 val = string_builder.ToString ();
\r
1724 return Token.LITERAL_STRING;
\r
1730 Report.Error (1010, Location, "Newline in constant");
\r
1740 return Token.ERROR;
\r
1742 string_builder.Append ((char) c);
\r
1745 Report.Error (1039, Location, "Unterminated string literal");
\r
1749 private int consume_identifier (int s, bool quoted)
\r
1754 id_builder [0] = (char) s;
\r
1756 while ((c = reader.Read ()) != -1) {
\r
1757 if (is_identifier_part_character ((char) c)){
\r
1758 if (pos == max_id_size){
\r
1759 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1760 return Token.ERROR;
\r
1763 id_builder [pos++] = (char) c;
\r
1764 putback_char = -1;
\r
1773 // Optimization: avoids doing the keyword lookup
\r
1774 // on uppercase letters and _
\r
1777 int keyword = GetKeyword (id_builder, pos);
\r
1778 if (keyword != -1 && !quoted)
\r
1783 // Keep identifiers in an array of hashtables to avoid needless
\r
1787 if (identifiers [pos] != null) {
\r
1788 val = identifiers [pos][id_builder];
\r
1789 if (val != null) {
\r
1790 return Token.IDENTIFIER;
\r
1794 identifiers [pos] = new CharArrayHashtable (pos);
\r
1796 val = new String (id_builder, 0, pos);
\r
1798 char [] chars = new char [pos];
\r
1799 Array.Copy (id_builder, chars, pos);
\r
1801 identifiers [pos] [chars] = val;
\r
1803 return Token.IDENTIFIER;
\r
1806 int consume_whitespace ()
\r
1809 bool doread = false;
\r
1813 // optimization: eliminate col and implement #directive semantic correctly.
\r
1814 for (;(c = getChar ()) != -1; col++) {
\r
1819 col = (((col + 8) / 8) * 8) - 1;
\r
1823 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
\r
1827 if (peekChar () == '\n')
\r
1833 any_token_seen |= tokens_seen;
\r
1834 tokens_seen = false;
\r
1838 // Handle double-slash comments.
\r
1840 int d = peekChar ();
\r
1844 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1851 any_token_seen |= tokens_seen;
\r
1852 tokens_seen = false;
\r
1854 } else if (d == '*'){
\r
1857 while ((d = getChar ()) != -1){
\r
1858 if (d == '*' && peekChar () == '/'){
\r
1867 any_token_seen |= tokens_seen;
\r
1868 tokens_seen = false;
\r
1873 goto is_punct_label;
\r
1882 any_token_seen |= tokens_seen;
\r
1883 tokens_seen = false;
\r
1887 /* For now, ignore pre-processor commands */
\r
1888 // FIXME: In C# the '#' is not limited to appear
\r
1889 // on the first column.
\r
1890 if (c == '#' && !tokens_seen){
\r
1895 cont = handle_preprocessing_directive (cont);
\r
1903 bool skipping = false;
\r
1904 for (;(c = getChar ()) != -1; col++){
\r
1910 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1912 else if (c != '#')
\r
1914 if (c == '#' && !skipping)
\r
1917 any_token_seen |= tokens_seen;
\r
1918 tokens_seen = false;
\r
1920 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1930 public int xtoken ()
\r
1933 bool doread = false;
\r
1937 // optimization: eliminate col and implement #directive semantic correctly.
\r
1939 c = consume_whitespace ();
\r
1943 if (is_identifier_start_character ((char)c)){
\r
1944 tokens_seen = true;
\r
1945 return consume_identifier (c, false);
\r
1949 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1950 tokens_seen = true;
\r
1958 if (c >= '0' && c <= '9'){
\r
1959 tokens_seen = true;
\r
1960 return is_number (c);
\r
1964 tokens_seen = true;
\r
1965 int peek = peekChar ();
\r
1966 if (peek >= '0' && peek <= '9')
\r
1967 return is_number (c);
\r
1972 return consume_string (false);
\r
1976 tokens_seen = true;
\r
1978 error_details = "Empty character literal";
\r
1979 Report.Error (1011, Location, error_details);
\r
1980 return Token.ERROR;
\r
1984 return Token.ERROR;
\r
1985 val = new System.Char ();
\r
1990 error_details = "Too many characters in character literal";
\r
1991 Report.Error (1012, Location, error_details);
\r
1993 // Try to recover, read until newline or next "'"
\r
1994 while ((c = getChar ()) != -1){
\r
1995 if (c == '\n' || c == '\''){
\r
2003 return Token.ERROR;
\r
2005 return Token.LITERAL_CHARACTER;
\r
2011 tokens_seen = true;
\r
2012 return consume_string (true);
\r
2013 } else if (is_identifier_start_character ((char) c)){
\r
2014 return consume_identifier (c, true);
\r
2016 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
2021 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
2022 "character on a line.";
\r
2024 Report.Error (1040, Location, error_details);
\r
2026 return Token.ERROR;
\r
2029 error_details = ((char)c).ToString ();
\r
2031 return Token.ERROR;
\r
2034 public void cleanup ()
\r
2036 if (ifstack != null && ifstack.Count >= 1) {
\r
2037 int state = (int) ifstack.Pop ();
\r
2038 if ((state & REGION) != 0)
\r
2039 Report.Error (1038, "#endregion directive expected");
\r
2041 Report.Error (1027, "#endif directive expected");
\r