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
45 // Whether tokens have been seen on this line
\r
47 bool tokens_seen = false;
\r
50 // Whether a token has been seen on the file
\r
51 // This is needed because `define' is not allowed to be used
\r
52 // after a token has been seen.
\r
54 bool any_token_seen = false;
\r
55 static Hashtable tokenValues;
\r
57 private static Hashtable TokenValueName
\r
60 if (tokenValues == null)
\r
61 tokenValues = GetTokenValueNameHash ();
\r
67 private static Hashtable GetTokenValueNameHash ()
\r
69 Type t = typeof (Token);
\r
70 FieldInfo [] fields = t.GetFields ();
\r
71 Hashtable hash = new Hashtable ();
\r
72 foreach (FieldInfo field in fields) {
\r
73 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
\r
74 hash.Add (field.GetValue (null), field.Name);
\r
80 // Returns a verbose representation of the current location
\r
82 public string location {
\r
86 if (current_token == Token.ERROR)
\r
87 det = "detail: " + error_details;
\r
91 // return "Line: "+line+" Col: "+col + "\n" +
\r
92 // "VirtLine: "+ref_line +
\r
93 // " Token: "+current_token + " " + det;
\r
94 string current_token_name = TokenValueName [current_token] as string;
\r
95 if (current_token_name == null)
\r
96 current_token_name = current_token.ToString ();
\r
98 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
\r
101 current_token_name,
\r
106 public bool PropertyParsing {
\r
108 return handle_get_set;
\r
112 handle_get_set = value;
\r
116 public bool AssemblyTargetParsing {
\r
118 return handle_assembly;
\r
122 handle_assembly = value;
\r
126 public bool EventParsing {
\r
128 return handle_remove_add;
\r
132 handle_remove_add = value;
\r
139 static Hashtable keywords;
\r
140 static NumberStyles styles;
\r
141 static NumberFormatInfo csharp_format_info;
\r
144 // Values for the associated token returned
\r
154 const int TAKING = 1;
\r
155 const int TAKEN_BEFORE = 2;
\r
156 const int ELSE_SEEN = 4;
\r
157 const int PARENT_TAKING = 8;
\r
158 const int REGION = 16;
\r
161 // pre-processor if stack state:
\r
165 static System.Text.StringBuilder string_builder;
\r
167 const int max_id_size = 512;
\r
168 static char [] id_builder = new char [max_id_size];
\r
170 const int max_number_size = 128;
\r
171 static char [] number_builder = new char [max_number_size];
\r
172 static int number_pos;
\r
175 // Details about the error encoutered by the tokenizer
\r
177 string error_details;
\r
179 public string error {
\r
181 return error_details;
\r
197 static void InitTokens ()
\r
199 keywords = new Hashtable ();
\r
201 keywords.Add ("abstract", Token.ABSTRACT);
\r
202 keywords.Add ("as", Token.AS);
\r
203 keywords.Add ("add", Token.ADD);
\r
204 keywords.Add ("assembly", Token.ASSEMBLY);
\r
205 keywords.Add ("base", Token.BASE);
\r
206 keywords.Add ("bool", Token.BOOL);
\r
207 keywords.Add ("break", Token.BREAK);
\r
208 keywords.Add ("byte", Token.BYTE);
\r
209 keywords.Add ("case", Token.CASE);
\r
210 keywords.Add ("catch", Token.CATCH);
\r
211 keywords.Add ("char", Token.CHAR);
\r
212 keywords.Add ("checked", Token.CHECKED);
\r
213 keywords.Add ("class", Token.CLASS);
\r
214 keywords.Add ("const", Token.CONST);
\r
215 keywords.Add ("continue", Token.CONTINUE);
\r
216 keywords.Add ("decimal", Token.DECIMAL);
\r
217 keywords.Add ("default", Token.DEFAULT);
\r
218 keywords.Add ("delegate", Token.DELEGATE);
\r
219 keywords.Add ("do", Token.DO);
\r
220 keywords.Add ("double", Token.DOUBLE);
\r
221 keywords.Add ("else", Token.ELSE);
\r
222 keywords.Add ("enum", Token.ENUM);
\r
223 keywords.Add ("event", Token.EVENT);
\r
224 keywords.Add ("explicit", Token.EXPLICIT);
\r
225 keywords.Add ("extern", Token.EXTERN);
\r
226 keywords.Add ("false", Token.FALSE);
\r
227 keywords.Add ("finally", Token.FINALLY);
\r
228 keywords.Add ("fixed", Token.FIXED);
\r
229 keywords.Add ("float", Token.FLOAT);
\r
230 keywords.Add ("for", Token.FOR);
\r
231 keywords.Add ("foreach", Token.FOREACH);
\r
232 keywords.Add ("goto", Token.GOTO);
\r
233 keywords.Add ("get", Token.GET);
\r
234 keywords.Add ("if", Token.IF);
\r
235 keywords.Add ("implicit", Token.IMPLICIT);
\r
236 keywords.Add ("in", Token.IN);
\r
237 keywords.Add ("int", Token.INT);
\r
238 keywords.Add ("interface", Token.INTERFACE);
\r
239 keywords.Add ("internal", Token.INTERNAL);
\r
240 keywords.Add ("is", Token.IS);
\r
241 keywords.Add ("lock", Token.LOCK);
\r
242 keywords.Add ("long", Token.LONG);
\r
243 keywords.Add ("namespace", Token.NAMESPACE);
\r
244 keywords.Add ("new", Token.NEW);
\r
245 keywords.Add ("null", Token.NULL);
\r
246 keywords.Add ("object", Token.OBJECT);
\r
247 keywords.Add ("operator", Token.OPERATOR);
\r
248 keywords.Add ("out", Token.OUT);
\r
249 keywords.Add ("override", Token.OVERRIDE);
\r
250 keywords.Add ("params", Token.PARAMS);
\r
251 keywords.Add ("private", Token.PRIVATE);
\r
252 keywords.Add ("protected", Token.PROTECTED);
\r
253 keywords.Add ("public", Token.PUBLIC);
\r
254 keywords.Add ("readonly", Token.READONLY);
\r
255 keywords.Add ("ref", Token.REF);
\r
256 keywords.Add ("remove", Token.REMOVE);
\r
257 keywords.Add ("return", Token.RETURN);
\r
258 keywords.Add ("sbyte", Token.SBYTE);
\r
259 keywords.Add ("sealed", Token.SEALED);
\r
260 keywords.Add ("set", Token.SET);
\r
261 keywords.Add ("short", Token.SHORT);
\r
262 keywords.Add ("sizeof", Token.SIZEOF);
\r
263 keywords.Add ("stackalloc", Token.STACKALLOC);
\r
264 keywords.Add ("static", Token.STATIC);
\r
265 keywords.Add ("string", Token.STRING);
\r
266 keywords.Add ("struct", Token.STRUCT);
\r
267 keywords.Add ("switch", Token.SWITCH);
\r
268 keywords.Add ("this", Token.THIS);
\r
269 keywords.Add ("throw", Token.THROW);
\r
270 keywords.Add ("true", Token.TRUE);
\r
271 keywords.Add ("try", Token.TRY);
\r
272 keywords.Add ("typeof", Token.TYPEOF);
\r
273 keywords.Add ("uint", Token.UINT);
\r
274 keywords.Add ("ulong", Token.ULONG);
\r
275 keywords.Add ("unchecked", Token.UNCHECKED);
\r
276 keywords.Add ("unsafe", Token.UNSAFE);
\r
277 keywords.Add ("ushort", Token.USHORT);
\r
278 keywords.Add ("using", Token.USING);
\r
279 keywords.Add ("virtual", Token.VIRTUAL);
\r
280 keywords.Add ("void", Token.VOID);
\r
281 keywords.Add ("volatile", Token.VOLATILE);
\r
282 keywords.Add ("while", Token.WHILE);
\r
284 if (RootContext.V2){
\r
285 keywords.Add ("__yield", Token.YIELD);
\r
286 keywords.Add ("yield", Token.YIELD);
\r
291 // Class initializer
\r
293 static Tokenizer ()
\r
296 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
297 styles = NumberStyles.Float;
\r
299 string_builder = new System.Text.StringBuilder ();
\r
302 int GetKeyword (string name)
\r
304 object o = keywords [name];
\r
311 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
313 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
315 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
321 public Location Location {
\r
323 return new Location (ref_line);
\r
327 void define (string def)
\r
329 if (!RootContext.AllDefines.Contains (def)){
\r
330 RootContext.AllDefines [def] = true;
\r
332 if (defines.Contains (def))
\r
334 defines [def] = true;
\r
337 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
339 this.ref_name = file;
\r
340 this.file_name = file;
\r
346 defines = new Hashtable ();
\r
347 foreach (string def in defs)
\r
352 // FIXME: This could be `Location.Push' but we have to
\r
353 // find out why the MS compiler allows this
\r
355 Mono.CSharp.Location.Push (file);
\r
358 bool is_identifier_start_character (char c)
\r
360 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
363 bool is_identifier_part_character (char c)
\r
365 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
368 int is_punct (char c, ref bool doread)
\r
377 return Token.OPEN_BRACE;
\r
379 return Token.CLOSE_BRACE;
\r
381 return Token.OPEN_BRACKET;
\r
383 return Token.CLOSE_BRACKET;
\r
385 return Token.OPEN_PARENS;
\r
387 if (deambiguate_close_parens == 0)
\r
388 return Token.CLOSE_PARENS;
\r
390 --deambiguate_close_parens;
\r
392 // Save current position and parse next token.
\r
393 int old = reader.Position;
\r
394 int new_token = token ();
\r
395 reader.Position = old;
\r
398 if (new_token == Token.OPEN_PARENS)
\r
399 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
400 else if (new_token == Token.MINUS)
\r
401 return Token.CLOSE_PARENS_MINUS;
\r
402 else if (IsCastToken (new_token))
\r
403 return Token.CLOSE_PARENS_CAST;
\r
405 return Token.CLOSE_PARENS_NO_CAST;
\r
409 return Token.COMMA;
\r
411 return Token.COLON;
\r
413 return Token.SEMICOLON;
\r
415 return Token.TILDE;
\r
417 return Token.INTERR;
\r
426 t = Token.OP_ADD_ASSIGN;
\r
436 t = Token.OP_SUB_ASSIGN;
\r
440 return Token.MINUS;
\r
448 return Token.OP_NE;
\r
456 return Token.OP_EQ;
\r
458 return Token.ASSIGN;
\r
464 return Token.OP_AND;
\r
465 } else if (d == '='){
\r
467 return Token.OP_AND_ASSIGN;
\r
469 return Token.BITWISE_AND;
\r
475 return Token.OP_OR;
\r
476 } else if (d == '='){
\r
478 return Token.OP_OR_ASSIGN;
\r
480 return Token.BITWISE_OR;
\r
486 return Token.OP_MULT_ASSIGN;
\r
494 return Token.OP_DIV_ASSIGN;
\r
502 return Token.OP_MOD_ASSIGN;
\r
504 return Token.PERCENT;
\r
510 return Token.OP_XOR_ASSIGN;
\r
512 return Token.CARRET;
\r
522 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
524 return Token.OP_SHIFT_LEFT;
\r
525 } else if (d == '='){
\r
527 return Token.OP_LE;
\r
529 return Token.OP_LT;
\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
548 return Token.ERROR;
\r
551 int deambiguate_close_parens = 0;
\r
553 public void Deambiguate_CloseParens ()
\r
556 deambiguate_close_parens++;
\r
559 void Error_NumericConstantTooLong ()
\r
561 Report.Error (1021, Location, "Numeric constant too long");
\r
564 bool decimal_digits (int c)
\r
567 bool seen_digits = false;
\r
570 if (number_pos == max_number_size)
\r
571 Error_NumericConstantTooLong ();
\r
572 number_builder [number_pos++] = (char) c;
\r
576 // We use peekChar2, because decimal_digits needs to do a
\r
577 // 2-character look-ahead (5.ToString for example).
\r
579 while ((d = peekChar2 ()) != -1){
\r
580 if (d >= '0' && d <= '9'){
\r
581 if (number_pos == max_number_size)
\r
582 Error_NumericConstantTooLong ();
\r
583 number_builder [number_pos++] = (char) d;
\r
585 seen_digits = true;
\r
590 return seen_digits;
\r
593 bool is_hex (int e)
\r
595 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
598 void hex_digits (int c)
\r
601 number_builder [number_pos++] = (char) c;
\r
605 int real_type_suffix (int c)
\r
610 case 'F': case 'f':
\r
611 t = Token.LITERAL_FLOAT;
\r
613 case 'D': case 'd':
\r
614 t = Token.LITERAL_DOUBLE;
\r
616 case 'M': case 'm':
\r
617 t= Token.LITERAL_DECIMAL;
\r
625 int integer_type_suffix (ulong ul, int c)
\r
627 bool is_unsigned = false;
\r
628 bool is_long = false;
\r
631 bool scanning = true;
\r
634 case 'U': case 'u':
\r
637 is_unsigned = true;
\r
644 // if we have not seen anything in between
\r
645 // report this error
\r
649 "the 'l' suffix is easily confused with digit `1'," +
\r
650 " use 'L' for clarity");
\r
666 } while (scanning);
\r
669 if (is_long && is_unsigned){
\r
671 return Token.LITERAL_INTEGER;
\r
672 } else if (is_unsigned){
\r
673 // uint if possible, or ulong else.
\r
675 if ((ul & 0xffffffff00000000) == 0)
\r
679 } else if (is_long){
\r
680 // long if possible, ulong otherwise
\r
681 if ((ul & 0x8000000000000000) != 0)
\r
686 // int, uint, long or ulong in that order
\r
687 if ((ul & 0xffffffff00000000) == 0){
\r
688 uint ui = (uint) ul;
\r
690 if ((ui & 0x80000000) != 0)
\r
695 if ((ul & 0x8000000000000000) != 0)
\r
701 return Token.LITERAL_INTEGER;
\r
705 // given `c' as the next char in the input decide whether
\r
706 // we need to convert to a special type, and then choose
\r
707 // the best representation for the integer
\r
709 int adjust_int (int c)
\r
712 if (number_pos > 9){
\r
713 ulong ul = (uint) (number_builder [0] - '0');
\r
715 for (int i = 1; i < number_pos; i++){
\r
716 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
718 return integer_type_suffix (ul, c);
\r
720 uint ui = (uint) (number_builder [0] - '0');
\r
722 for (int i = 1; i < number_pos; i++){
\r
723 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
725 return integer_type_suffix (ui, c);
\r
727 } catch (OverflowException) {
\r
728 error_details = "Integral constant is too large";
\r
729 Report.Error (1021, Location, error_details);
\r
731 return Token.LITERAL_INTEGER;
\r
735 int adjust_real (int t)
\r
737 string s = new String (number_builder, 0, number_pos);
\r
740 case Token.LITERAL_DECIMAL:
\r
742 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
743 } catch (OverflowException) {
\r
745 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
746 Report.Error (594, Location, error_details);
\r
749 case Token.LITERAL_FLOAT:
\r
751 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
752 } catch (OverflowException) {
\r
754 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
755 Report.Error (594, Location, error_details);
\r
759 case Token.LITERAL_DOUBLE:
\r
761 t = Token.LITERAL_DOUBLE;
\r
763 val = System.Double.Parse (s, styles, csharp_format_info);
\r
764 } catch (OverflowException) {
\r
766 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
767 Report.Error (594, Location, error_details);
\r
780 while ((d = peekChar ()) != -1){
\r
782 if (number_pos == 16){
\r
783 Report.Error (1021, Location, "Integral constant too large");
\r
784 return Token.ERROR;
\r
786 number_builder [number_pos++] = (char) d;
\r
792 string s = new String (number_builder, 0, number_pos);
\r
793 if (number_pos <= 8)
\r
794 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
796 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
797 return integer_type_suffix (ul, peekChar ());
\r
801 // Invoked if we know we have .digits or digits
\r
803 int is_number (int c)
\r
805 bool is_real = false;
\r
810 if (c >= '0' && c <= '9'){
\r
812 int peek = peekChar ();
\r
814 if (peek == 'x' || peek == 'X')
\r
815 return handle_hex ();
\r
817 decimal_digits (c);
\r
822 // We need to handle the case of
\r
823 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
826 if (decimal_digits ('.')){
\r
832 return adjust_int (-1);
\r
836 if (c == 'e' || c == 'E'){
\r
838 if (number_pos == max_number_size)
\r
839 Error_NumericConstantTooLong ();
\r
840 number_builder [number_pos++] = 'e';
\r
844 if (number_pos == max_number_size)
\r
845 Error_NumericConstantTooLong ();
\r
846 number_builder [number_pos++] = '+';
\r
848 } else if (c == '-') {
\r
849 if (number_pos == max_number_size)
\r
850 Error_NumericConstantTooLong ();
\r
851 number_builder [number_pos++] = '-';
\r
854 if (number_pos == max_number_size)
\r
855 Error_NumericConstantTooLong ();
\r
856 number_builder [number_pos++] = '+';
\r
859 decimal_digits (c);
\r
863 type = real_type_suffix (c);
\r
864 if (type == Token.NONE && !is_real){
\r
866 return adjust_int (c);
\r
870 if (type == Token.NONE){
\r
875 return adjust_real (type);
\r
877 Console.WriteLine ("This should not be reached");
\r
878 throw new Exception ("Is Number should never reach this point");
\r
882 // Accepts exactly count (4 or 8) hex, no more no less
\r
884 int getHex (int count, out bool error)
\r
886 int [] buffer = new int [8];
\r
890 int top = count != -1 ? count : 4;
\r
894 for (i = 0; i < top; i++){
\r
897 if (c >= '0' && c <= '9')
\r
898 c = (int) c - (int) '0';
\r
899 else if (c >= 'A' && c <= 'F')
\r
900 c = (int) c - (int) 'A' + 10;
\r
901 else if (c >= 'a' && c <= 'f')
\r
902 c = (int) c - (int) 'a' + 10;
\r
908 total = (total * 16) + c;
\r
910 int p = peekChar ();
\r
913 if (!is_hex ((char)p))
\r
954 v = getHex (-1, out error);
\r
959 v = getHex (4, out error);
\r
964 v = getHex (8, out error);
\r
969 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
978 if (putback_char != -1){
\r
979 int x = putback_char;
\r
984 return reader.Read ();
\r
989 if (putback_char != -1)
\r
990 return putback_char;
\r
991 putback_char = reader.Read ();
\r
992 return putback_char;
\r
997 if (putback_char != -1)
\r
998 return putback_char;
\r
999 return reader.Peek ();
\r
1002 void putback (int c)
\r
1004 if (putback_char != -1){
\r
1005 Console.WriteLine ("Col: " + col);
\r
1006 Console.WriteLine ("Row: " + line);
\r
1007 Console.WriteLine ("Name: " + ref_name.Name);
\r
1008 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1009 throw new Exception ("This should not happen putback on putback");
\r
1014 public bool advance ()
\r
1016 return peekChar () != -1;
\r
1019 public Object Value {
\r
1025 public Object value ()
\r
1030 bool IsCastToken (int token)
\r
1035 case Token.IDENTIFIER:
\r
1036 case Token.LITERAL_INTEGER:
\r
1037 case Token.LITERAL_FLOAT:
\r
1038 case Token.LITERAL_DOUBLE:
\r
1039 case Token.LITERAL_DECIMAL:
\r
1040 case Token.LITERAL_CHARACTER:
\r
1041 case Token.LITERAL_STRING:
\r
1043 case Token.CHECKED:
\r
1048 case Token.SIZEOF:
\r
1052 case Token.TYPEOF:
\r
1053 case Token.UNCHECKED:
\r
1054 case Token.UNSAFE:
\r
1062 public int token ()
\r
1064 current_token = xtoken ();
\r
1065 return current_token;
\r
1068 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1070 void get_cmd_arg (out string cmd, out string arg)
\r
1074 tokens_seen = false;
\r
1076 static_cmd_arg.Length = 0;
\r
1078 while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1079 static_cmd_arg.Append ((char) c);
\r
1082 cmd = static_cmd_arg.ToString ();
\r
1088 } else if (c == '\r')
\r
1091 // skip over white space
\r
1092 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1099 } else if (c == '\r'){
\r
1104 static_cmd_arg.Length = 0;
\r
1105 static_cmd_arg.Append ((char) c);
\r
1107 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1108 static_cmd_arg.Append ((char) c);
\r
1114 } else if (c == '\r')
\r
1116 arg = static_cmd_arg.ToString ().Trim ();
\r
1120 // Handles the #line directive
\r
1122 bool PreProcessLine (string arg)
\r
1127 if (arg == "default"){
\r
1129 ref_name = file_name;
\r
1130 Location.Push (ref_name);
\r
1137 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1138 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1141 char [] quotes = { '\"' };
\r
1143 string name = arg.Substring (pos). Trim (quotes);
\r
1144 ref_name = Location.LookupFile (name);
\r
1145 Location.Push (ref_name);
\r
1147 ref_line = System.Int32.Parse (arg);
\r
1157 // Handles #define and #undef
\r
1159 void PreProcessDefinition (bool is_define, string arg)
\r
1161 if (arg == "" || arg == "true" || arg == "false"){
\r
1162 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1166 char[] whitespace = { ' ', '\t' };
\r
1167 if (arg.IndexOfAny (whitespace) != -1){
\r
1168 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1172 foreach (char c in arg){
\r
1173 if (!Char.IsLetter (c) && (c != '_')){
\r
1174 Report.Error (1001, Location, "Identifier expected");
\r
1180 if (defines == null)
\r
1181 defines = new Hashtable ();
\r
1184 if (defines == null)
\r
1186 if (defines.Contains (arg))
\r
1187 defines.Remove (arg);
\r
1191 bool eval_val (string s)
\r
1198 if (defines == null)
\r
1200 if (defines.Contains (s))
\r
1206 bool pp_primary (ref string s)
\r
1209 int len = s.Length;
\r
1215 s = s.Substring (1);
\r
1216 bool val = pp_expr (ref s);
\r
1217 if (s.Length > 0 && s [0] == ')'){
\r
1218 s = s.Substring (1);
\r
1221 Error_InvalidDirective ();
\r
1225 if (is_identifier_start_character (c)){
\r
1231 if (is_identifier_part_character (c)){
\r
1235 bool v = eval_val (s.Substring (0, j));
\r
1236 s = s.Substring (j);
\r
1239 bool vv = eval_val (s);
\r
1244 Error_InvalidDirective ();
\r
1248 bool pp_unary (ref string s)
\r
1251 int len = s.Length;
\r
1254 if (s [0] == '!'){
\r
1255 if (len > 1 && s [1] == '='){
\r
1256 Error_InvalidDirective ();
\r
1259 s = s.Substring (1);
\r
1260 return ! pp_primary (ref s);
\r
1262 return pp_primary (ref s);
\r
1264 Error_InvalidDirective ();
\r
1269 bool pp_eq (ref string s)
\r
1271 bool va = pp_unary (ref s);
\r
1274 int len = s.Length;
\r
1276 if (s [0] == '='){
\r
1277 if (len > 2 && s [1] == '='){
\r
1278 s = s.Substring (2);
\r
1279 return va == pp_unary (ref s);
\r
1281 Error_InvalidDirective ();
\r
1284 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1285 s = s.Substring (2);
\r
1287 return va != pp_unary (ref s);
\r
1296 bool pp_and (ref string s)
\r
1298 bool va = pp_eq (ref s);
\r
1301 int len = s.Length;
\r
1303 if (s [0] == '&'){
\r
1304 if (len > 2 && s [1] == '&'){
\r
1305 s = s.Substring (2);
\r
1306 return (va & pp_eq (ref s));
\r
1308 Error_InvalidDirective ();
\r
1317 // Evaluates an expression for `#if' or `#elif'
\r
1319 bool pp_expr (ref string s)
\r
1321 bool va = pp_and (ref s);
\r
1323 int len = s.Length;
\r
1328 if (len > 2 && s [1] == '|'){
\r
1329 s = s.Substring (2);
\r
1330 return va | pp_expr (ref s);
\r
1332 Error_InvalidDirective ();
\r
1341 bool eval (string s)
\r
1343 bool v = pp_expr (ref s);
\r
1345 if (s.Length != 0){
\r
1346 Error_InvalidDirective ();
\r
1353 void Error_InvalidDirective ()
\r
1355 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1358 void Error_UnexpectedDirective (string extra)
\r
1362 "Unexpected processor directive (" + extra + ")");
\r
1365 void Error_TokensSeen ()
\r
1369 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1373 // if true, then the code continues processing the code
\r
1374 // if false, the code stays in a loop until another directive is
\r
1377 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1379 char [] blank = { ' ', '\t' };
\r
1381 bool region_directive = false;
\r
1383 get_cmd_arg (out cmd, out arg);
\r
1385 // Eat any trailing whitespaces and single-line comments
\r
1386 if (arg.IndexOf ("//") != -1)
\r
1387 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1388 arg = arg.TrimEnd (' ', '\t');
\r
1391 // The first group of pre-processing instructions is always processed
\r
1395 if (RootContext.V2)
\r
1400 if (!PreProcessLine (arg))
\r
1403 "Argument to #line directive is missing or invalid");
\r
1407 region_directive = true;
\r
1412 region_directive = true;
\r
1413 goto case "endif";
\r
1417 Error_InvalidDirective ();
\r
1420 bool taking = false;
\r
1421 if (ifstack == null)
\r
1422 ifstack = new Stack ();
\r
1424 if (ifstack.Count == 0){
\r
1427 int state = (int) ifstack.Peek ();
\r
1428 if ((state & TAKING) != 0)
\r
1432 if (eval (arg) && taking){
\r
1433 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1434 if (region_directive)
\r
1436 ifstack.Push (push);
\r
1439 int push = (taking ? PARENT_TAKING : 0);
\r
1440 if (region_directive)
\r
1442 ifstack.Push (push);
\r
1447 if (ifstack == null || ifstack.Count == 0){
\r
1448 Error_UnexpectedDirective ("no #if for this #endif");
\r
1451 int pop = (int) ifstack.Pop ();
\r
1453 if (region_directive && ((pop & REGION) == 0))
\r
1454 Report.Error (1027, Location, "#endif directive expected");
\r
1455 else if (!region_directive && ((pop & REGION) != 0))
\r
1456 Report.Error (1038, Location, "#endregion directive expected");
\r
1458 if (ifstack.Count == 0)
\r
1461 int state = (int) ifstack.Peek ();
\r
1463 if ((state & TAKING) != 0)
\r
1471 if (ifstack == null || ifstack.Count == 0){
\r
1472 Error_UnexpectedDirective ("no #if for this #elif");
\r
1475 int state = (int) ifstack.Peek ();
\r
1477 if ((state & REGION) != 0) {
\r
1478 Report.Error (1038, Location, "#endregion directive expected");
\r
1482 if ((state & ELSE_SEEN) != 0){
\r
1483 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1487 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1490 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1491 state = (int) ifstack.Pop ();
\r
1492 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1499 if (ifstack == null || ifstack.Count == 0){
\r
1502 "Unexpected processor directive (no #if for this #else)");
\r
1505 int state = (int) ifstack.Peek ();
\r
1507 if ((state & REGION) != 0) {
\r
1508 Report.Error (1038, Location, "#endregion directive expected");
\r
1512 if ((state & ELSE_SEEN) != 0){
\r
1513 Error_UnexpectedDirective ("#else within #else");
\r
1520 if ((state & TAKEN_BEFORE) == 0){
\r
1521 ret = ((state & PARENT_TAKING) != 0);
\r
1530 ifstack.Push (state | ELSE_SEEN);
\r
1537 // These are only processed if we are in a `taking' block
\r
1539 if (!caller_is_taking)
\r
1544 if (any_token_seen){
\r
1545 Error_TokensSeen ();
\r
1548 PreProcessDefinition (true, arg);
\r
1552 if (any_token_seen){
\r
1553 Error_TokensSeen ();
\r
1556 PreProcessDefinition (false, arg);
\r
1560 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1564 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1568 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1573 private int consume_string (bool quoted)
\r
1576 string_builder.Length = 0;
\r
1578 while ((c = getChar ()) != -1){
\r
1580 if (quoted && peekChar () == '"'){
\r
1581 string_builder.Append ((char) c);
\r
1585 val = string_builder.ToString ();
\r
1586 return Token.LITERAL_STRING;
\r
1592 Report.Error (1010, Location, "Newline in constant");
\r
1602 return Token.ERROR;
\r
1604 string_builder.Append ((char) c);
\r
1607 Report.Error (1039, Location, "Unterminated string literal");
\r
1611 private int consume_identifier (int s, bool quoted)
\r
1616 id_builder [0] = (char) s;
\r
1618 while ((c = reader.Read ()) != -1) {
\r
1619 if (is_identifier_part_character ((char) c)){
\r
1620 if (pos == max_id_size){
\r
1621 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1622 return Token.ERROR;
\r
1625 id_builder [pos++] = (char) c;
\r
1626 putback_char = -1;
\r
1634 string ids = new String (id_builder, 0, pos);
\r
1637 // Optimization: avoids doing the keyword lookup
\r
1638 // on uppercase letters and _
\r
1641 int keyword = GetKeyword (ids);
\r
1642 if (keyword == -1 || quoted){
\r
1644 return Token.IDENTIFIER;
\r
1649 return Token.IDENTIFIER;
\r
1652 public int xtoken ()
\r
1655 bool doread = false;
\r
1659 // optimization: eliminate col and implement #directive semantic correctly.
\r
1660 for (;(c = getChar ()) != -1; col++) {
\r
1661 if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r' || c == 0xa0){
\r
1664 col = (((col + 8) / 8) * 8) - 1;
\r
1668 // Handle double-slash comments.
\r
1670 int d = peekChar ();
\r
1674 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1681 any_token_seen |= tokens_seen;
\r
1682 tokens_seen = false;
\r
1684 } else if (d == '*'){
\r
1687 while ((d = getChar ()) != -1){
\r
1688 if (d == '*' && peekChar () == '/'){
\r
1697 any_token_seen |= tokens_seen;
\r
1698 tokens_seen = false;
\r
1703 goto is_punct_label;
\r
1707 if (is_identifier_start_character ((char)c)){
\r
1708 tokens_seen = true;
\r
1709 return consume_identifier (c, false);
\r
1713 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1714 tokens_seen = true;
\r
1727 any_token_seen |= tokens_seen;
\r
1728 tokens_seen = false;
\r
1732 if (c >= '0' && c <= '9'){
\r
1733 tokens_seen = true;
\r
1734 return is_number (c);
\r
1738 tokens_seen = true;
\r
1739 int peek = peekChar ();
\r
1740 if (peek >= '0' && peek <= '9')
\r
1741 return is_number (c);
\r
1745 /* For now, ignore pre-processor commands */
\r
1746 // FIXME: In C# the '#' is not limited to appear
\r
1747 // on the first column.
\r
1748 if (c == '#' && !tokens_seen){
\r
1753 cont = handle_preprocessing_directive (cont);
\r
1761 bool skipping = false;
\r
1762 for (;(c = getChar ()) != -1; col++){
\r
1768 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1770 else if (c != '#')
\r
1772 if (c == '#' && !skipping)
\r
1775 any_token_seen |= tokens_seen;
\r
1776 tokens_seen = false;
\r
1778 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1783 return consume_string (false);
\r
1787 tokens_seen = true;
\r
1789 error_details = "Empty character literal";
\r
1790 Report.Error (1011, Location, error_details);
\r
1791 return Token.ERROR;
\r
1795 return Token.ERROR;
\r
1796 val = new System.Char ();
\r
1801 error_details = "Too many characters in character literal";
\r
1802 Report.Error (1012, Location, error_details);
\r
1804 // Try to recover, read until newline or next "'"
\r
1805 while ((c = getChar ()) != -1){
\r
1806 if (c == '\n' || c == '\''){
\r
1815 return Token.ERROR;
\r
1817 return Token.LITERAL_CHARACTER;
\r
1823 tokens_seen = true;
\r
1824 return consume_string (true);
\r
1825 } else if (is_identifier_start_character ((char) c)){
\r
1826 return consume_identifier (c, true);
\r
1828 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1833 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
1834 "character on a line.";
\r
1836 Report.Error (1040, Location, error_details);
\r
1838 return Token.ERROR;
\r
1841 error_details = ((char)c).ToString ();
\r
1843 return Token.ERROR;
\r
1849 public void cleanup ()
\r
1851 if (ifstack != null && ifstack.Count >= 1) {
\r
1852 int state = (int) ifstack.Pop ();
\r
1853 if ((state & REGION) != 0)
\r
1854 Report.Error (1038, "#endregion directive expected");
\r
1856 Report.Error (1027, "#endif directive expected");
\r