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 number_builder [number_pos++] = (char) d;
\r
788 string s = new String (number_builder, 0, number_pos);
\r
790 if (number_pos <= 8)
\r
791 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
793 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
794 } catch (OverflowException){
\r
795 error_details = "Integral constant is too large";
\r
796 Report.Error (1021, Location, error_details);
\r
798 return Token.LITERAL_INTEGER;
\r
801 return integer_type_suffix (ul, peekChar ());
\r
805 // Invoked if we know we have .digits or digits
\r
807 int is_number (int c)
\r
809 bool is_real = false;
\r
814 if (c >= '0' && c <= '9'){
\r
816 int peek = peekChar ();
\r
818 if (peek == 'x' || peek == 'X')
\r
819 return handle_hex ();
\r
821 decimal_digits (c);
\r
826 // We need to handle the case of
\r
827 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
830 if (decimal_digits ('.')){
\r
836 return adjust_int (-1);
\r
840 if (c == 'e' || c == 'E'){
\r
842 if (number_pos == max_number_size)
\r
843 Error_NumericConstantTooLong ();
\r
844 number_builder [number_pos++] = 'e';
\r
848 if (number_pos == max_number_size)
\r
849 Error_NumericConstantTooLong ();
\r
850 number_builder [number_pos++] = '+';
\r
852 } else if (c == '-') {
\r
853 if (number_pos == max_number_size)
\r
854 Error_NumericConstantTooLong ();
\r
855 number_builder [number_pos++] = '-';
\r
858 if (number_pos == max_number_size)
\r
859 Error_NumericConstantTooLong ();
\r
860 number_builder [number_pos++] = '+';
\r
863 decimal_digits (c);
\r
867 type = real_type_suffix (c);
\r
868 if (type == Token.NONE && !is_real){
\r
870 return adjust_int (c);
\r
874 if (type == Token.NONE){
\r
879 return adjust_real (type);
\r
881 Console.WriteLine ("This should not be reached");
\r
882 throw new Exception ("Is Number should never reach this point");
\r
886 // Accepts exactly count (4 or 8) hex, no more no less
\r
888 int getHex (int count, out bool error)
\r
890 int [] buffer = new int [8];
\r
894 int top = count != -1 ? count : 4;
\r
898 for (i = 0; i < top; i++){
\r
901 if (c >= '0' && c <= '9')
\r
902 c = (int) c - (int) '0';
\r
903 else if (c >= 'A' && c <= 'F')
\r
904 c = (int) c - (int) 'A' + 10;
\r
905 else if (c >= 'a' && c <= 'f')
\r
906 c = (int) c - (int) 'a' + 10;
\r
912 total = (total * 16) + c;
\r
914 int p = peekChar ();
\r
917 if (!is_hex ((char)p))
\r
958 v = getHex (-1, out error);
\r
963 v = getHex (4, out error);
\r
968 v = getHex (8, out error);
\r
973 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
982 if (putback_char != -1){
\r
983 int x = putback_char;
\r
988 return reader.Read ();
\r
993 if (putback_char != -1)
\r
994 return putback_char;
\r
995 putback_char = reader.Read ();
\r
996 return putback_char;
\r
1001 if (putback_char != -1)
\r
1002 return putback_char;
\r
1003 return reader.Peek ();
\r
1006 void putback (int c)
\r
1008 if (putback_char != -1){
\r
1009 Console.WriteLine ("Col: " + col);
\r
1010 Console.WriteLine ("Row: " + line);
\r
1011 Console.WriteLine ("Name: " + ref_name.Name);
\r
1012 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1013 throw new Exception ("This should not happen putback on putback");
\r
1018 public bool advance ()
\r
1020 return peekChar () != -1;
\r
1023 public Object Value {
\r
1029 public Object value ()
\r
1034 bool IsCastToken (int token)
\r
1039 case Token.IDENTIFIER:
\r
1040 case Token.LITERAL_INTEGER:
\r
1041 case Token.LITERAL_FLOAT:
\r
1042 case Token.LITERAL_DOUBLE:
\r
1043 case Token.LITERAL_DECIMAL:
\r
1044 case Token.LITERAL_CHARACTER:
\r
1045 case Token.LITERAL_STRING:
\r
1047 case Token.CHECKED:
\r
1052 case Token.SIZEOF:
\r
1056 case Token.TYPEOF:
\r
1057 case Token.UNCHECKED:
\r
1058 case Token.UNSAFE:
\r
1066 public int token ()
\r
1068 current_token = xtoken ();
\r
1069 return current_token;
\r
1072 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1074 void get_cmd_arg (out string cmd, out string arg)
\r
1078 tokens_seen = false;
\r
1080 static_cmd_arg.Length = 0;
\r
1082 while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1083 static_cmd_arg.Append ((char) c);
\r
1086 cmd = static_cmd_arg.ToString ();
\r
1092 } else if (c == '\r')
\r
1095 // skip over white space
\r
1096 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1103 } else if (c == '\r'){
\r
1108 static_cmd_arg.Length = 0;
\r
1109 static_cmd_arg.Append ((char) c);
\r
1111 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1112 static_cmd_arg.Append ((char) c);
\r
1118 } else if (c == '\r')
\r
1120 arg = static_cmd_arg.ToString ().Trim ();
\r
1124 // Handles the #line directive
\r
1126 bool PreProcessLine (string arg)
\r
1131 if (arg == "default"){
\r
1133 ref_name = file_name;
\r
1134 Location.Push (ref_name);
\r
1141 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1142 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1145 char [] quotes = { '\"' };
\r
1147 string name = arg.Substring (pos). Trim (quotes);
\r
1148 ref_name = Location.LookupFile (name);
\r
1149 Location.Push (ref_name);
\r
1151 ref_line = System.Int32.Parse (arg);
\r
1161 // Handles #define and #undef
\r
1163 void PreProcessDefinition (bool is_define, string arg)
\r
1165 if (arg == "" || arg == "true" || arg == "false"){
\r
1166 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1170 char[] whitespace = { ' ', '\t' };
\r
1171 if (arg.IndexOfAny (whitespace) != -1){
\r
1172 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1176 if (!is_identifier_start_character (arg [0]))
\r
1177 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1179 foreach (char c in arg.Substring (1)){
\r
1180 if (!is_identifier_part_character (c)){
\r
1181 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1187 if (defines == null)
\r
1188 defines = new Hashtable ();
\r
1191 if (defines == null)
\r
1193 if (defines.Contains (arg))
\r
1194 defines.Remove (arg);
\r
1198 bool eval_val (string s)
\r
1205 if (defines == null)
\r
1207 if (defines.Contains (s))
\r
1213 bool pp_primary (ref string s)
\r
1216 int len = s.Length;
\r
1222 s = s.Substring (1);
\r
1223 bool val = pp_expr (ref s);
\r
1224 if (s.Length > 0 && s [0] == ')'){
\r
1225 s = s.Substring (1);
\r
1228 Error_InvalidDirective ();
\r
1232 if (is_identifier_start_character (c)){
\r
1238 if (is_identifier_part_character (c)){
\r
1242 bool v = eval_val (s.Substring (0, j));
\r
1243 s = s.Substring (j);
\r
1246 bool vv = eval_val (s);
\r
1251 Error_InvalidDirective ();
\r
1255 bool pp_unary (ref string s)
\r
1258 int len = s.Length;
\r
1261 if (s [0] == '!'){
\r
1262 if (len > 1 && s [1] == '='){
\r
1263 Error_InvalidDirective ();
\r
1266 s = s.Substring (1);
\r
1267 return ! pp_primary (ref s);
\r
1269 return pp_primary (ref s);
\r
1271 Error_InvalidDirective ();
\r
1276 bool pp_eq (ref string s)
\r
1278 bool va = pp_unary (ref s);
\r
1281 int len = s.Length;
\r
1283 if (s [0] == '='){
\r
1284 if (len > 2 && s [1] == '='){
\r
1285 s = s.Substring (2);
\r
1286 return va == pp_unary (ref s);
\r
1288 Error_InvalidDirective ();
\r
1291 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1292 s = s.Substring (2);
\r
1294 return va != pp_unary (ref s);
\r
1303 bool pp_and (ref string s)
\r
1305 bool va = pp_eq (ref s);
\r
1308 int len = s.Length;
\r
1310 if (s [0] == '&'){
\r
1311 if (len > 2 && s [1] == '&'){
\r
1312 s = s.Substring (2);
\r
1313 return (va & pp_eq (ref s));
\r
1315 Error_InvalidDirective ();
\r
1324 // Evaluates an expression for `#if' or `#elif'
\r
1326 bool pp_expr (ref string s)
\r
1328 bool va = pp_and (ref s);
\r
1330 int len = s.Length;
\r
1335 if (len > 2 && s [1] == '|'){
\r
1336 s = s.Substring (2);
\r
1337 return va | pp_expr (ref s);
\r
1339 Error_InvalidDirective ();
\r
1348 bool eval (string s)
\r
1350 bool v = pp_expr (ref s);
\r
1352 if (s.Length != 0){
\r
1353 Error_InvalidDirective ();
\r
1360 void Error_InvalidDirective ()
\r
1362 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1365 void Error_UnexpectedDirective (string extra)
\r
1369 "Unexpected processor directive (" + extra + ")");
\r
1372 void Error_TokensSeen ()
\r
1376 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1380 // if true, then the code continues processing the code
\r
1381 // if false, the code stays in a loop until another directive is
\r
1384 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1386 char [] blank = { ' ', '\t' };
\r
1388 bool region_directive = false;
\r
1390 get_cmd_arg (out cmd, out arg);
\r
1392 // Eat any trailing whitespaces and single-line comments
\r
1393 if (arg.IndexOf ("//") != -1)
\r
1394 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1395 arg = arg.TrimEnd (' ', '\t');
\r
1398 // The first group of pre-processing instructions is always processed
\r
1402 if (RootContext.V2)
\r
1407 if (!PreProcessLine (arg))
\r
1410 "Argument to #line directive is missing or invalid");
\r
1414 region_directive = true;
\r
1419 region_directive = true;
\r
1420 goto case "endif";
\r
1424 Error_InvalidDirective ();
\r
1427 bool taking = false;
\r
1428 if (ifstack == null)
\r
1429 ifstack = new Stack ();
\r
1431 if (ifstack.Count == 0){
\r
1434 int state = (int) ifstack.Peek ();
\r
1435 if ((state & TAKING) != 0)
\r
1439 if (eval (arg) && taking){
\r
1440 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1441 if (region_directive)
\r
1443 ifstack.Push (push);
\r
1446 int push = (taking ? PARENT_TAKING : 0);
\r
1447 if (region_directive)
\r
1449 ifstack.Push (push);
\r
1454 if (ifstack == null || ifstack.Count == 0){
\r
1455 Error_UnexpectedDirective ("no #if for this #endif");
\r
1458 int pop = (int) ifstack.Pop ();
\r
1460 if (region_directive && ((pop & REGION) == 0))
\r
1461 Report.Error (1027, Location, "#endif directive expected");
\r
1462 else if (!region_directive && ((pop & REGION) != 0))
\r
1463 Report.Error (1038, Location, "#endregion directive expected");
\r
1465 if (ifstack.Count == 0)
\r
1468 int state = (int) ifstack.Peek ();
\r
1470 if ((state & TAKING) != 0)
\r
1478 if (ifstack == null || ifstack.Count == 0){
\r
1479 Error_UnexpectedDirective ("no #if for this #elif");
\r
1482 int state = (int) ifstack.Peek ();
\r
1484 if ((state & REGION) != 0) {
\r
1485 Report.Error (1038, Location, "#endregion directive expected");
\r
1489 if ((state & ELSE_SEEN) != 0){
\r
1490 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1494 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1497 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1498 state = (int) ifstack.Pop ();
\r
1499 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1506 if (ifstack == null || ifstack.Count == 0){
\r
1509 "Unexpected processor directive (no #if for this #else)");
\r
1512 int state = (int) ifstack.Peek ();
\r
1514 if ((state & REGION) != 0) {
\r
1515 Report.Error (1038, Location, "#endregion directive expected");
\r
1519 if ((state & ELSE_SEEN) != 0){
\r
1520 Error_UnexpectedDirective ("#else within #else");
\r
1527 if ((state & TAKEN_BEFORE) == 0){
\r
1528 ret = ((state & PARENT_TAKING) != 0);
\r
1537 ifstack.Push (state | ELSE_SEEN);
\r
1544 // These are only processed if we are in a `taking' block
\r
1546 if (!caller_is_taking)
\r
1551 if (any_token_seen){
\r
1552 Error_TokensSeen ();
\r
1555 PreProcessDefinition (true, arg);
\r
1559 if (any_token_seen){
\r
1560 Error_TokensSeen ();
\r
1563 PreProcessDefinition (false, arg);
\r
1567 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1571 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1575 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1580 private int consume_string (bool quoted)
\r
1583 string_builder.Length = 0;
\r
1585 while ((c = getChar ()) != -1){
\r
1587 if (quoted && peekChar () == '"'){
\r
1588 string_builder.Append ((char) c);
\r
1592 val = string_builder.ToString ();
\r
1593 return Token.LITERAL_STRING;
\r
1599 Report.Error (1010, Location, "Newline in constant");
\r
1609 return Token.ERROR;
\r
1611 string_builder.Append ((char) c);
\r
1614 Report.Error (1039, Location, "Unterminated string literal");
\r
1618 private int consume_identifier (int s, bool quoted)
\r
1623 id_builder [0] = (char) s;
\r
1625 while ((c = reader.Read ()) != -1) {
\r
1626 if (is_identifier_part_character ((char) c)){
\r
1627 if (pos == max_id_size){
\r
1628 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1629 return Token.ERROR;
\r
1632 id_builder [pos++] = (char) c;
\r
1633 putback_char = -1;
\r
1641 string ids = new String (id_builder, 0, pos);
\r
1644 // Optimization: avoids doing the keyword lookup
\r
1645 // on uppercase letters and _
\r
1648 int keyword = GetKeyword (ids);
\r
1649 if (keyword == -1 || quoted){
\r
1651 return Token.IDENTIFIER;
\r
1656 return Token.IDENTIFIER;
\r
1659 public int xtoken ()
\r
1662 bool doread = false;
\r
1666 // optimization: eliminate col and implement #directive semantic correctly.
\r
1667 for (;(c = getChar ()) != -1; col++) {
\r
1668 if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r' || c == 0xa0){
\r
1671 col = (((col + 8) / 8) * 8) - 1;
\r
1675 // Handle double-slash comments.
\r
1677 int d = peekChar ();
\r
1681 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1688 any_token_seen |= tokens_seen;
\r
1689 tokens_seen = false;
\r
1691 } else if (d == '*'){
\r
1694 while ((d = getChar ()) != -1){
\r
1695 if (d == '*' && peekChar () == '/'){
\r
1704 any_token_seen |= tokens_seen;
\r
1705 tokens_seen = false;
\r
1710 goto is_punct_label;
\r
1714 if (is_identifier_start_character ((char)c)){
\r
1715 tokens_seen = true;
\r
1716 return consume_identifier (c, false);
\r
1720 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1721 tokens_seen = true;
\r
1734 any_token_seen |= tokens_seen;
\r
1735 tokens_seen = false;
\r
1739 if (c >= '0' && c <= '9'){
\r
1740 tokens_seen = true;
\r
1741 return is_number (c);
\r
1745 tokens_seen = true;
\r
1746 int peek = peekChar ();
\r
1747 if (peek >= '0' && peek <= '9')
\r
1748 return is_number (c);
\r
1752 /* For now, ignore pre-processor commands */
\r
1753 // FIXME: In C# the '#' is not limited to appear
\r
1754 // on the first column.
\r
1755 if (c == '#' && !tokens_seen){
\r
1760 cont = handle_preprocessing_directive (cont);
\r
1768 bool skipping = false;
\r
1769 for (;(c = getChar ()) != -1; col++){
\r
1775 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1777 else if (c != '#')
\r
1779 if (c == '#' && !skipping)
\r
1782 any_token_seen |= tokens_seen;
\r
1783 tokens_seen = false;
\r
1785 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1790 return consume_string (false);
\r
1794 tokens_seen = true;
\r
1796 error_details = "Empty character literal";
\r
1797 Report.Error (1011, Location, error_details);
\r
1798 return Token.ERROR;
\r
1802 return Token.ERROR;
\r
1803 val = new System.Char ();
\r
1808 error_details = "Too many characters in character literal";
\r
1809 Report.Error (1012, Location, error_details);
\r
1811 // Try to recover, read until newline or next "'"
\r
1812 while ((c = getChar ()) != -1){
\r
1813 if (c == '\n' || c == '\''){
\r
1822 return Token.ERROR;
\r
1824 return Token.LITERAL_CHARACTER;
\r
1830 tokens_seen = true;
\r
1831 return consume_string (true);
\r
1832 } else if (is_identifier_start_character ((char) c)){
\r
1833 return consume_identifier (c, true);
\r
1835 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1840 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
1841 "character on a line.";
\r
1843 Report.Error (1040, Location, error_details);
\r
1845 return Token.ERROR;
\r
1848 error_details = ((char)c).ToString ();
\r
1850 return Token.ERROR;
\r
1856 public void cleanup ()
\r
1858 if (ifstack != null && ifstack.Count >= 1) {
\r
1859 int state = (int) ifstack.Pop ();
\r
1860 if ((state & REGION) != 0)
\r
1861 Report.Error (1038, "#endregion directive expected");
\r
1863 Report.Error (1027, "#endif directive expected");
\r