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 static bool is_identifier_start_character (char c)
\r
389 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
392 static 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 public static bool IsValidIdentifier (string s)
399 if (s == null || s.Length == 0)
402 if (!is_identifier_start_character (s [0]))
405 for (int i = 1; i < s.Length; i ++)
406 if (! is_identifier_part_character (s [i]))
412 bool parse_less_than ()
\r
415 int the_token = token ();
\r
416 switch (the_token) {
\r
417 case Token.IDENTIFIER:
\r
421 case Token.DECIMAL:
\r
440 the_token = token ();
\r
442 if (the_token == Token.OP_GENERICS_GT)
\r
444 else if ((the_token == Token.COMMA) || (the_token == Token.DOT))
\r
446 else if (the_token == Token.OP_GENERICS_LT) {
\r
447 if (!parse_less_than ())
\r
455 bool parsing_less_than = false;
\r
456 int parsing_generic_less_than = 0;
\r
458 int is_punct (char c, ref bool doread)
\r
467 return Token.OPEN_BRACE;
\r
469 return Token.CLOSE_BRACE;
\r
471 return Token.OPEN_BRACKET;
\r
473 return Token.CLOSE_BRACKET;
\r
475 return Token.OPEN_PARENS;
\r
477 if (deambiguate_close_parens == 0)
\r
478 return Token.CLOSE_PARENS;
\r
480 --deambiguate_close_parens;
\r
482 // Save current position and parse next token.
\r
483 int old = reader.Position;
\r
484 int new_token = token ();
\r
485 reader.Position = old;
\r
488 if (new_token == Token.OPEN_PARENS)
\r
489 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
490 else if (new_token == Token.MINUS)
\r
491 return Token.CLOSE_PARENS_MINUS;
\r
492 else if (IsCastToken (new_token))
\r
493 return Token.CLOSE_PARENS_CAST;
\r
495 return Token.CLOSE_PARENS_NO_CAST;
\r
499 return Token.COMMA;
\r
501 return Token.COLON;
\r
503 return Token.SEMICOLON;
\r
505 return Token.TILDE;
\r
507 return Token.INTERR;
\r
511 if (parsing_generic_less_than++ > 0)
\r
512 return Token.OP_GENERICS_LT;
\r
514 // Save current position and parse next token.
\r
515 int old = reader.Position;
\r
516 bool is_generic_lt = parse_less_than ();
\r
517 reader.Position = old;
\r
520 if (is_generic_lt) {
\r
521 parsing_generic_less_than++;
\r
522 return Token.OP_GENERICS_LT;
\r
524 parsing_generic_less_than = 0;
\r
533 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
535 return Token.OP_SHIFT_LEFT;
\r
536 } else if (d == '='){
\r
538 return Token.OP_LE;
\r
540 return Token.OP_LT;
\r
541 } else if (c == '>') {
\r
542 if (parsing_generic_less_than > 0) {
\r
543 parsing_generic_less_than--;
\r
544 return Token.OP_GENERICS_GT;
\r
554 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
556 return Token.OP_SHIFT_RIGHT;
\r
557 } else if (d == '='){
\r
559 return Token.OP_GE;
\r
561 return Token.OP_GT;
\r
570 t = Token.OP_ADD_ASSIGN;
\r
580 t = Token.OP_SUB_ASSIGN;
\r
584 return Token.MINUS;
\r
592 return Token.OP_NE;
\r
600 return Token.OP_EQ;
\r
602 return Token.ASSIGN;
\r
608 return Token.OP_AND;
\r
609 } else if (d == '='){
\r
611 return Token.OP_AND_ASSIGN;
\r
613 return Token.BITWISE_AND;
\r
619 return Token.OP_OR;
\r
620 } else if (d == '='){
\r
622 return Token.OP_OR_ASSIGN;
\r
624 return Token.BITWISE_OR;
\r
630 return Token.OP_MULT_ASSIGN;
\r
638 return Token.OP_DIV_ASSIGN;
\r
646 return Token.OP_MOD_ASSIGN;
\r
648 return Token.PERCENT;
\r
654 return Token.OP_XOR_ASSIGN;
\r
656 return Token.CARRET;
\r
659 return Token.ERROR;
\r
662 int deambiguate_close_parens = 0;
\r
664 public void Deambiguate_CloseParens ()
\r
667 deambiguate_close_parens++;
\r
670 void Error_NumericConstantTooLong ()
\r
672 Report.Error (1021, Location, "Numeric constant too long");
\r
675 bool decimal_digits (int c)
\r
678 bool seen_digits = false;
\r
681 if (number_pos == max_number_size)
\r
682 Error_NumericConstantTooLong ();
\r
683 number_builder [number_pos++] = (char) c;
\r
687 // We use peekChar2, because decimal_digits needs to do a
\r
688 // 2-character look-ahead (5.ToString for example).
\r
690 while ((d = peekChar2 ()) != -1){
\r
691 if (d >= '0' && d <= '9'){
\r
692 if (number_pos == max_number_size)
\r
693 Error_NumericConstantTooLong ();
\r
694 number_builder [number_pos++] = (char) d;
\r
696 seen_digits = true;
\r
701 return seen_digits;
\r
704 bool is_hex (int e)
\r
706 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
709 void hex_digits (int c)
\r
712 number_builder [number_pos++] = (char) c;
\r
716 int real_type_suffix (int c)
\r
721 case 'F': case 'f':
\r
722 t = Token.LITERAL_FLOAT;
\r
724 case 'D': case 'd':
\r
725 t = Token.LITERAL_DOUBLE;
\r
727 case 'M': case 'm':
\r
728 t= Token.LITERAL_DECIMAL;
\r
736 int integer_type_suffix (ulong ul, int c)
\r
738 bool is_unsigned = false;
\r
739 bool is_long = false;
\r
742 bool scanning = true;
\r
745 case 'U': case 'u':
\r
748 is_unsigned = true;
\r
755 // if we have not seen anything in between
\r
756 // report this error
\r
760 "the 'l' suffix is easily confused with digit `1'," +
\r
761 " use 'L' for clarity");
\r
777 } while (scanning);
\r
780 if (is_long && is_unsigned){
\r
782 return Token.LITERAL_INTEGER;
\r
783 } else if (is_unsigned){
\r
784 // uint if possible, or ulong else.
\r
786 if ((ul & 0xffffffff00000000) == 0)
\r
790 } else if (is_long){
\r
791 // long if possible, ulong otherwise
\r
792 if ((ul & 0x8000000000000000) != 0)
\r
797 // int, uint, long or ulong in that order
\r
798 if ((ul & 0xffffffff00000000) == 0){
\r
799 uint ui = (uint) ul;
\r
801 if ((ui & 0x80000000) != 0)
\r
806 if ((ul & 0x8000000000000000) != 0)
\r
812 return Token.LITERAL_INTEGER;
\r
816 // given `c' as the next char in the input decide whether
\r
817 // we need to convert to a special type, and then choose
\r
818 // the best representation for the integer
\r
820 int adjust_int (int c)
\r
823 if (number_pos > 9){
\r
824 ulong ul = (uint) (number_builder [0] - '0');
\r
826 for (int i = 1; i < number_pos; i++){
\r
827 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
829 return integer_type_suffix (ul, c);
\r
831 uint ui = (uint) (number_builder [0] - '0');
\r
833 for (int i = 1; i < number_pos; i++){
\r
834 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
836 return integer_type_suffix (ui, c);
\r
838 } catch (OverflowException) {
\r
839 error_details = "Integral constant is too large";
\r
840 Report.Error (1021, Location, error_details);
\r
842 return Token.LITERAL_INTEGER;
\r
846 int adjust_real (int t)
\r
848 string s = new String (number_builder, 0, number_pos);
\r
851 case Token.LITERAL_DECIMAL:
\r
853 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
854 } catch (OverflowException) {
\r
856 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
857 Report.Error (594, Location, error_details);
\r
860 case Token.LITERAL_FLOAT:
\r
862 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
863 } catch (OverflowException) {
\r
865 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
866 Report.Error (594, Location, error_details);
\r
870 case Token.LITERAL_DOUBLE:
\r
872 t = Token.LITERAL_DOUBLE;
\r
874 val = System.Double.Parse (s, styles, csharp_format_info);
\r
875 } catch (OverflowException) {
\r
877 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
878 Report.Error (594, Location, error_details);
\r
891 while ((d = peekChar ()) != -1){
\r
893 number_builder [number_pos++] = (char) d;
\r
899 string s = new String (number_builder, 0, number_pos);
\r
901 if (number_pos <= 8)
\r
902 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
904 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
905 } catch (OverflowException){
\r
906 error_details = "Integral constant is too large";
\r
907 Report.Error (1021, Location, error_details);
\r
909 return Token.LITERAL_INTEGER;
\r
912 return integer_type_suffix (ul, peekChar ());
\r
916 // Invoked if we know we have .digits or digits
\r
918 int is_number (int c)
\r
920 bool is_real = false;
\r
925 if (c >= '0' && c <= '9'){
\r
927 int peek = peekChar ();
\r
929 if (peek == 'x' || peek == 'X')
\r
930 return handle_hex ();
\r
932 decimal_digits (c);
\r
937 // We need to handle the case of
\r
938 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
941 if (decimal_digits ('.')){
\r
947 return adjust_int (-1);
\r
951 if (c == 'e' || c == 'E'){
\r
953 if (number_pos == max_number_size)
\r
954 Error_NumericConstantTooLong ();
\r
955 number_builder [number_pos++] = 'e';
\r
959 if (number_pos == max_number_size)
\r
960 Error_NumericConstantTooLong ();
\r
961 number_builder [number_pos++] = '+';
\r
963 } else if (c == '-') {
\r
964 if (number_pos == max_number_size)
\r
965 Error_NumericConstantTooLong ();
\r
966 number_builder [number_pos++] = '-';
\r
969 if (number_pos == max_number_size)
\r
970 Error_NumericConstantTooLong ();
\r
971 number_builder [number_pos++] = '+';
\r
974 decimal_digits (c);
\r
978 type = real_type_suffix (c);
\r
979 if (type == Token.NONE && !is_real){
\r
981 return adjust_int (c);
\r
985 if (type == Token.NONE){
\r
990 return adjust_real (type);
\r
992 Console.WriteLine ("This should not be reached");
\r
993 throw new Exception ("Is Number should never reach this point");
\r
997 // Accepts exactly count (4 or 8) hex, no more no less
\r
999 int getHex (int count, out bool error)
\r
1004 int top = count != -1 ? count : 4;
\r
1008 for (i = 0; i < top; i++){
\r
1011 if (c >= '0' && c <= '9')
\r
1012 c = (int) c - (int) '0';
\r
1013 else if (c >= 'A' && c <= 'F')
\r
1014 c = (int) c - (int) 'A' + 10;
\r
1015 else if (c >= 'a' && c <= 'f')
\r
1016 c = (int) c - (int) 'a' + 10;
\r
1022 total = (total * 16) + c;
\r
1024 int p = peekChar ();
\r
1027 if (!is_hex ((char)p))
\r
1034 int escape (int c)
\r
1068 v = getHex (-1, out error);
\r
1073 v = getHex (4, out error);
\r
1078 v = getHex (8, out error);
\r
1083 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1092 if (putback_char != -1){
\r
1093 int x = putback_char;
\r
1094 putback_char = -1;
\r
1098 return reader.Read ();
\r
1103 if (putback_char != -1)
\r
1104 return putback_char;
\r
1105 putback_char = reader.Read ();
\r
1106 return putback_char;
\r
1111 if (putback_char != -1)
\r
1112 return putback_char;
\r
1113 return reader.Peek ();
\r
1116 void putback (int c)
\r
1118 if (putback_char != -1){
\r
1119 Console.WriteLine ("Col: " + col);
\r
1120 Console.WriteLine ("Row: " + line);
\r
1121 Console.WriteLine ("Name: " + ref_name.Name);
\r
1122 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1123 throw new Exception ("This should not happen putback on putback");
\r
1128 public bool advance ()
\r
1130 return peekChar () != -1;
\r
1133 public Object Value {
\r
1139 public Object value ()
\r
1144 bool IsCastToken (int token)
\r
1149 case Token.IDENTIFIER:
\r
1150 case Token.LITERAL_INTEGER:
\r
1151 case Token.LITERAL_FLOAT:
\r
1152 case Token.LITERAL_DOUBLE:
\r
1153 case Token.LITERAL_DECIMAL:
\r
1154 case Token.LITERAL_CHARACTER:
\r
1155 case Token.LITERAL_STRING:
\r
1157 case Token.CHECKED:
\r
1162 case Token.SIZEOF:
\r
1166 case Token.TYPEOF:
\r
1167 case Token.UNCHECKED:
\r
1168 case Token.UNSAFE:
\r
1171 // These can be part of a member access
\r
1176 case Token.USHORT:
\r
1179 case Token.DOUBLE:
\r
1189 public int token ()
\r
1191 current_token = xtoken ();
\r
1193 if (current_token != Token.DEFAULT)
\r
1194 return current_token;
\r
1196 int c = consume_whitespace ();
\r
1198 current_token = Token.ERROR;
\r
1199 else if (c == '(')
\r
1200 current_token = Token.DEFAULT_OPEN_PARENS;
\r
1204 return current_token;
\r
1207 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1209 void get_cmd_arg (out string cmd, out string arg)
\r
1213 tokens_seen = false;
\r
1215 static_cmd_arg.Length = 0;
\r
1217 // skip over white space
\r
1218 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1221 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1222 if (is_identifier_part_character ((char) c)){
\r
1223 static_cmd_arg.Append ((char) c);
\r
1231 cmd = static_cmd_arg.ToString ();
\r
1237 } else if (c == '\r')
\r
1240 // skip over white space
\r
1241 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1248 } else if (c == '\r'){
\r
1253 static_cmd_arg.Length = 0;
\r
1254 static_cmd_arg.Append ((char) c);
\r
1256 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1257 static_cmd_arg.Append ((char) c);
\r
1263 } else if (c == '\r')
\r
1265 arg = static_cmd_arg.ToString ().Trim ();
\r
1269 // Handles the #line directive
\r
1271 bool PreProcessLine (string arg)
\r
1276 if (arg == "default"){
\r
1278 ref_name = file_name;
\r
1279 Location.Push (ref_name);
\r
1281 } else if (arg == "hidden"){
\r
1283 // We ignore #line hidden
\r
1291 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1292 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1295 char [] quotes = { '\"' };
\r
1297 string name = arg.Substring (pos). Trim (quotes);
\r
1298 ref_name = Location.LookupFile (name);
\r
1299 file_name.HasLineDirective = true;
\r
1300 ref_name.HasLineDirective = true;
\r
1301 Location.Push (ref_name);
\r
1303 ref_line = System.Int32.Parse (arg);
\r
1313 // Handles #define and #undef
\r
1315 void PreProcessDefinition (bool is_define, string arg)
\r
1317 if (arg == "" || arg == "true" || arg == "false"){
\r
1318 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1322 char[] whitespace = { ' ', '\t' };
\r
1323 if (arg.IndexOfAny (whitespace) != -1){
\r
1324 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1328 if (!is_identifier_start_character (arg [0]))
\r
1329 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1331 foreach (char c in arg.Substring (1)){
\r
1332 if (!is_identifier_part_character (c)){
\r
1333 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1339 if (defines == null)
\r
1340 defines = new Hashtable ();
\r
1343 if (defines == null)
\r
1345 if (defines.Contains (arg))
\r
1346 defines.Remove (arg);
\r
1350 bool eval_val (string s)
\r
1357 if (defines == null)
\r
1359 if (defines.Contains (s))
\r
1365 bool pp_primary (ref string s)
\r
1368 int len = s.Length;
\r
1374 s = s.Substring (1);
\r
1375 bool val = pp_expr (ref s);
\r
1376 if (s.Length > 0 && s [0] == ')'){
\r
1377 s = s.Substring (1);
\r
1380 Error_InvalidDirective ();
\r
1384 if (is_identifier_start_character (c)){
\r
1390 if (is_identifier_part_character (c)){
\r
1394 bool v = eval_val (s.Substring (0, j));
\r
1395 s = s.Substring (j);
\r
1398 bool vv = eval_val (s);
\r
1403 Error_InvalidDirective ();
\r
1407 bool pp_unary (ref string s)
\r
1410 int len = s.Length;
\r
1413 if (s [0] == '!'){
\r
1414 if (len > 1 && s [1] == '='){
\r
1415 Error_InvalidDirective ();
\r
1418 s = s.Substring (1);
\r
1419 return ! pp_primary (ref s);
\r
1421 return pp_primary (ref s);
\r
1423 Error_InvalidDirective ();
\r
1428 bool pp_eq (ref string s)
\r
1430 bool va = pp_unary (ref s);
\r
1433 int len = s.Length;
\r
1435 if (s [0] == '='){
\r
1436 if (len > 2 && s [1] == '='){
\r
1437 s = s.Substring (2);
\r
1438 return va == pp_unary (ref s);
\r
1440 Error_InvalidDirective ();
\r
1443 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1444 s = s.Substring (2);
\r
1446 return va != pp_unary (ref s);
\r
1455 bool pp_and (ref string s)
\r
1457 bool va = pp_eq (ref s);
\r
1460 int len = s.Length;
\r
1462 if (s [0] == '&'){
\r
1463 if (len > 2 && s [1] == '&'){
\r
1464 s = s.Substring (2);
\r
1465 return (va & pp_eq (ref s));
\r
1467 Error_InvalidDirective ();
\r
1476 // Evaluates an expression for `#if' or `#elif'
\r
1478 bool pp_expr (ref string s)
\r
1480 bool va = pp_and (ref s);
\r
1482 int len = s.Length;
\r
1487 if (len > 2 && s [1] == '|'){
\r
1488 s = s.Substring (2);
\r
1489 return va | pp_expr (ref s);
\r
1491 Error_InvalidDirective ();
\r
1500 bool eval (string s)
\r
1502 bool v = pp_expr (ref s);
\r
1504 if (s.Length != 0){
\r
1505 Error_InvalidDirective ();
\r
1512 void Error_InvalidDirective ()
\r
1514 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1517 void Error_UnexpectedDirective (string extra)
\r
1521 "Unexpected processor directive (" + extra + ")");
\r
1524 void Error_TokensSeen ()
\r
1528 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1532 // if true, then the code continues processing the code
\r
1533 // if false, the code stays in a loop until another directive is
\r
1536 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1539 bool region_directive = false;
\r
1541 get_cmd_arg (out cmd, out arg);
\r
1543 // Eat any trailing whitespaces and single-line comments
\r
1544 if (arg.IndexOf ("//") != -1)
\r
1545 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1546 arg = arg.TrimEnd (' ', '\t');
\r
1549 // The first group of pre-processing instructions is always processed
\r
1553 if (RootContext.V2)
\r
1558 if (!PreProcessLine (arg))
\r
1561 "Argument to #line directive is missing or invalid");
\r
1565 region_directive = true;
\r
1570 region_directive = true;
\r
1571 goto case "endif";
\r
1575 Error_InvalidDirective ();
\r
1578 bool taking = false;
\r
1579 if (ifstack == null)
\r
1580 ifstack = new Stack ();
\r
1582 if (ifstack.Count == 0){
\r
1585 int state = (int) ifstack.Peek ();
\r
1586 if ((state & TAKING) != 0)
\r
1590 if (eval (arg) && taking){
\r
1591 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1592 if (region_directive)
\r
1594 ifstack.Push (push);
\r
1597 int push = (taking ? PARENT_TAKING : 0);
\r
1598 if (region_directive)
\r
1600 ifstack.Push (push);
\r
1605 if (ifstack == null || ifstack.Count == 0){
\r
1606 Error_UnexpectedDirective ("no #if for this #endif");
\r
1609 int pop = (int) ifstack.Pop ();
\r
1611 if (region_directive && ((pop & REGION) == 0))
\r
1612 Report.Error (1027, Location, "#endif directive expected");
\r
1613 else if (!region_directive && ((pop & REGION) != 0))
\r
1614 Report.Error (1038, Location, "#endregion directive expected");
\r
1616 if (ifstack.Count == 0)
\r
1619 int state = (int) ifstack.Peek ();
\r
1621 if ((state & TAKING) != 0)
\r
1629 if (ifstack == null || ifstack.Count == 0){
\r
1630 Error_UnexpectedDirective ("no #if for this #elif");
\r
1633 int state = (int) ifstack.Peek ();
\r
1635 if ((state & REGION) != 0) {
\r
1636 Report.Error (1038, Location, "#endregion directive expected");
\r
1640 if ((state & ELSE_SEEN) != 0){
\r
1641 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1645 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1648 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1649 state = (int) ifstack.Pop ();
\r
1650 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1657 if (ifstack == null || ifstack.Count == 0){
\r
1660 "Unexpected processor directive (no #if for this #else)");
\r
1663 int state = (int) ifstack.Peek ();
\r
1665 if ((state & REGION) != 0) {
\r
1666 Report.Error (1038, Location, "#endregion directive expected");
\r
1670 if ((state & ELSE_SEEN) != 0){
\r
1671 Error_UnexpectedDirective ("#else within #else");
\r
1678 if ((state & TAKEN_BEFORE) == 0){
\r
1679 ret = ((state & PARENT_TAKING) != 0);
\r
1688 ifstack.Push (state | ELSE_SEEN);
\r
1695 // These are only processed if we are in a `taking' block
\r
1697 if (!caller_is_taking)
\r
1702 if (any_token_seen){
\r
1703 Error_TokensSeen ();
\r
1706 PreProcessDefinition (true, arg);
\r
1710 if (any_token_seen){
\r
1711 Error_TokensSeen ();
\r
1714 PreProcessDefinition (false, arg);
\r
1718 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1722 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1726 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1731 private int consume_string (bool quoted)
\r
1734 string_builder.Length = 0;
\r
1736 while ((c = getChar ()) != -1){
\r
1738 if (quoted && peekChar () == '"'){
\r
1739 string_builder.Append ((char) c);
\r
1743 val = string_builder.ToString ();
\r
1744 return Token.LITERAL_STRING;
\r
1750 Report.Error (1010, Location, "Newline in constant");
\r
1760 return Token.ERROR;
\r
1762 string_builder.Append ((char) c);
\r
1765 Report.Error (1039, Location, "Unterminated string literal");
\r
1769 private int consume_identifier (int s, bool quoted)
\r
1774 id_builder [0] = (char) s;
\r
1776 while ((c = reader.Read ()) != -1) {
\r
1777 if (is_identifier_part_character ((char) c)){
\r
1778 if (pos == max_id_size){
\r
1779 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1780 return Token.ERROR;
\r
1783 id_builder [pos++] = (char) c;
\r
1784 putback_char = -1;
\r
1793 // Optimization: avoids doing the keyword lookup
\r
1794 // on uppercase letters and _
\r
1797 int keyword = GetKeyword (id_builder, pos);
\r
1798 if (keyword != -1 && !quoted)
\r
1803 // Keep identifiers in an array of hashtables to avoid needless
\r
1807 if (identifiers [pos] != null) {
\r
1808 val = identifiers [pos][id_builder];
\r
1809 if (val != null) {
\r
1810 return Token.IDENTIFIER;
\r
1814 identifiers [pos] = new CharArrayHashtable (pos);
\r
1816 val = new String (id_builder, 0, pos);
\r
1818 char [] chars = new char [pos];
\r
1819 Array.Copy (id_builder, chars, pos);
\r
1821 identifiers [pos] [chars] = val;
\r
1823 return Token.IDENTIFIER;
\r
1826 int consume_whitespace ()
\r
1829 bool doread = false;
\r
1833 // optimization: eliminate col and implement #directive semantic correctly.
\r
1834 for (;(c = getChar ()) != -1; col++) {
\r
1839 col = (((col + 8) / 8) * 8) - 1;
\r
1843 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
\r
1847 if (peekChar () == '\n')
\r
1853 any_token_seen |= tokens_seen;
\r
1854 tokens_seen = false;
\r
1858 // Handle double-slash comments.
\r
1860 int d = peekChar ();
\r
1864 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1871 any_token_seen |= tokens_seen;
\r
1872 tokens_seen = false;
\r
1874 } else if (d == '*'){
\r
1877 while ((d = getChar ()) != -1){
\r
1878 if (d == '*' && peekChar () == '/'){
\r
1887 any_token_seen |= tokens_seen;
\r
1888 tokens_seen = false;
\r
1893 goto is_punct_label;
\r
1902 any_token_seen |= tokens_seen;
\r
1903 tokens_seen = false;
\r
1907 /* For now, ignore pre-processor commands */
\r
1908 // FIXME: In C# the '#' is not limited to appear
\r
1909 // on the first column.
\r
1910 if (c == '#' && !tokens_seen){
\r
1915 cont = handle_preprocessing_directive (cont);
\r
1923 bool skipping = false;
\r
1924 for (;(c = getChar ()) != -1; col++){
\r
1930 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1932 else if (c != '#')
\r
1934 if (c == '#' && !skipping)
\r
1937 any_token_seen |= tokens_seen;
\r
1938 tokens_seen = false;
\r
1940 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1950 public int xtoken ()
\r
1953 bool doread = false;
\r
1957 // optimization: eliminate col and implement #directive semantic correctly.
\r
1959 c = consume_whitespace ();
\r
1963 if (is_identifier_start_character ((char)c)){
\r
1964 tokens_seen = true;
\r
1965 return consume_identifier (c, false);
\r
1969 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1970 tokens_seen = true;
\r
1978 if (c >= '0' && c <= '9'){
\r
1979 tokens_seen = true;
\r
1980 return is_number (c);
\r
1984 tokens_seen = true;
\r
1985 int peek = peekChar ();
\r
1986 if (peek >= '0' && peek <= '9')
\r
1987 return is_number (c);
\r
1992 return consume_string (false);
\r
1996 tokens_seen = true;
\r
1998 error_details = "Empty character literal";
\r
1999 Report.Error (1011, Location, error_details);
\r
2000 return Token.ERROR;
\r
2004 return Token.ERROR;
\r
2005 val = new System.Char ();
\r
2010 error_details = "Too many characters in character literal";
\r
2011 Report.Error (1012, Location, error_details);
\r
2013 // Try to recover, read until newline or next "'"
\r
2014 while ((c = getChar ()) != -1){
\r
2015 if (c == '\n' || c == '\''){
\r
2023 return Token.ERROR;
\r
2025 return Token.LITERAL_CHARACTER;
\r
2031 tokens_seen = true;
\r
2032 return consume_string (true);
\r
2033 } else if (is_identifier_start_character ((char) c)){
\r
2034 return consume_identifier (c, true);
\r
2036 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
2041 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
2042 "character on a line.";
\r
2044 Report.Error (1040, Location, error_details);
\r
2046 return Token.ERROR;
\r
2049 error_details = ((char)c).ToString ();
\r
2051 return Token.ERROR;
\r
2054 public void cleanup ()
\r
2056 if (ifstack != null && ifstack.Count >= 1) {
\r
2057 int state = (int) ifstack.Pop ();
\r
2058 if ((state & REGION) != 0)
\r
2059 Report.Error (1038, "#endregion directive expected");
\r
2061 Report.Error (1027, "#endif directive expected");
\r