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 CharArrayHashtable[] 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 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
\r
172 const int max_number_size = 128;
\r
173 static char [] number_builder = new char [max_number_size];
\r
174 static int number_pos;
\r
177 // Details about the error encoutered by the tokenizer
\r
179 string error_details;
\r
181 public string error {
\r
183 return error_details;
\r
199 static void AddKeyword (string kw, int token) {
\r
200 if (keywords [kw.Length] == null) {
\r
201 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
\r
203 keywords [kw.Length] [kw.ToCharArray ()] = token;
\r
206 static void InitTokens ()
\r
208 keywords = new CharArrayHashtable [64];
\r
210 AddKeyword ("abstract", Token.ABSTRACT);
\r
211 AddKeyword ("as", Token.AS);
\r
212 AddKeyword ("add", Token.ADD);
\r
213 AddKeyword ("assembly", Token.ASSEMBLY);
\r
214 AddKeyword ("base", Token.BASE);
\r
215 AddKeyword ("bool", Token.BOOL);
\r
216 AddKeyword ("break", Token.BREAK);
\r
217 AddKeyword ("byte", Token.BYTE);
\r
218 AddKeyword ("case", Token.CASE);
\r
219 AddKeyword ("catch", Token.CATCH);
\r
220 AddKeyword ("char", Token.CHAR);
\r
221 AddKeyword ("checked", Token.CHECKED);
\r
222 AddKeyword ("class", Token.CLASS);
\r
223 AddKeyword ("const", Token.CONST);
\r
224 AddKeyword ("continue", Token.CONTINUE);
\r
225 AddKeyword ("decimal", Token.DECIMAL);
\r
226 AddKeyword ("default", Token.DEFAULT);
\r
227 AddKeyword ("delegate", Token.DELEGATE);
\r
228 AddKeyword ("do", Token.DO);
\r
229 AddKeyword ("double", Token.DOUBLE);
\r
230 AddKeyword ("else", Token.ELSE);
\r
231 AddKeyword ("enum", Token.ENUM);
\r
232 AddKeyword ("event", Token.EVENT);
\r
233 AddKeyword ("explicit", Token.EXPLICIT);
\r
234 AddKeyword ("extern", Token.EXTERN);
\r
235 AddKeyword ("false", Token.FALSE);
\r
236 AddKeyword ("finally", Token.FINALLY);
\r
237 AddKeyword ("fixed", Token.FIXED);
\r
238 AddKeyword ("float", Token.FLOAT);
\r
239 AddKeyword ("for", Token.FOR);
\r
240 AddKeyword ("foreach", Token.FOREACH);
\r
241 AddKeyword ("goto", Token.GOTO);
\r
242 AddKeyword ("get", Token.GET);
\r
243 AddKeyword ("if", Token.IF);
\r
244 AddKeyword ("implicit", Token.IMPLICIT);
\r
245 AddKeyword ("in", Token.IN);
\r
246 AddKeyword ("int", Token.INT);
\r
247 AddKeyword ("interface", Token.INTERFACE);
\r
248 AddKeyword ("internal", Token.INTERNAL);
\r
249 AddKeyword ("is", Token.IS);
\r
250 AddKeyword ("lock", Token.LOCK);
\r
251 AddKeyword ("long", Token.LONG);
\r
252 AddKeyword ("namespace", Token.NAMESPACE);
\r
253 AddKeyword ("new", Token.NEW);
\r
254 AddKeyword ("null", Token.NULL);
\r
255 AddKeyword ("object", Token.OBJECT);
\r
256 AddKeyword ("operator", Token.OPERATOR);
\r
257 AddKeyword ("out", Token.OUT);
\r
258 AddKeyword ("override", Token.OVERRIDE);
\r
259 AddKeyword ("params", Token.PARAMS);
\r
260 AddKeyword ("private", Token.PRIVATE);
\r
261 AddKeyword ("protected", Token.PROTECTED);
\r
262 AddKeyword ("public", Token.PUBLIC);
\r
263 AddKeyword ("readonly", Token.READONLY);
\r
264 AddKeyword ("ref", Token.REF);
\r
265 AddKeyword ("remove", Token.REMOVE);
\r
266 AddKeyword ("return", Token.RETURN);
\r
267 AddKeyword ("sbyte", Token.SBYTE);
\r
268 AddKeyword ("sealed", Token.SEALED);
\r
269 AddKeyword ("set", Token.SET);
\r
270 AddKeyword ("short", Token.SHORT);
\r
271 AddKeyword ("sizeof", Token.SIZEOF);
\r
272 AddKeyword ("stackalloc", Token.STACKALLOC);
\r
273 AddKeyword ("static", Token.STATIC);
\r
274 AddKeyword ("string", Token.STRING);
\r
275 AddKeyword ("struct", Token.STRUCT);
\r
276 AddKeyword ("switch", Token.SWITCH);
\r
277 AddKeyword ("this", Token.THIS);
\r
278 AddKeyword ("throw", Token.THROW);
\r
279 AddKeyword ("true", Token.TRUE);
\r
280 AddKeyword ("try", Token.TRY);
\r
281 AddKeyword ("typeof", Token.TYPEOF);
\r
282 AddKeyword ("uint", Token.UINT);
\r
283 AddKeyword ("ulong", Token.ULONG);
\r
284 AddKeyword ("unchecked", Token.UNCHECKED);
\r
285 AddKeyword ("unsafe", Token.UNSAFE);
\r
286 AddKeyword ("ushort", Token.USHORT);
\r
287 AddKeyword ("using", Token.USING);
\r
288 AddKeyword ("virtual", Token.VIRTUAL);
\r
289 AddKeyword ("void", Token.VOID);
\r
290 AddKeyword ("volatile", Token.VOLATILE);
\r
291 AddKeyword ("while", Token.WHILE);
\r
293 if (RootContext.V2){
\r
294 AddKeyword ("__yield", Token.YIELD);
\r
295 AddKeyword ("yield", Token.YIELD);
\r
300 // Class initializer
\r
302 static Tokenizer ()
\r
305 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
306 styles = NumberStyles.Float;
\r
308 string_builder = new System.Text.StringBuilder ();
\r
311 int GetKeyword (char[] id, int id_len)
\r
314 * Keywords are stored in an array of hashtables grouped by their
\r
318 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
\r
320 object o = keywords [id_len] [id];
\r
327 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
329 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
331 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
337 public Location Location {
\r
339 return new Location (ref_line);
\r
343 void define (string def)
\r
345 if (!RootContext.AllDefines.Contains (def)){
\r
346 RootContext.AllDefines [def] = true;
\r
348 if (defines.Contains (def))
\r
350 defines [def] = true;
\r
353 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
355 this.ref_name = file;
\r
356 this.file_name = file;
\r
362 defines = new Hashtable ();
\r
363 foreach (string def in defs)
\r
368 // FIXME: This could be `Location.Push' but we have to
\r
369 // find out why the MS compiler allows this
\r
371 Mono.CSharp.Location.Push (file);
\r
374 public static void Cleanup () {
\r
375 identifiers = null;
\r
378 bool is_identifier_start_character (char c)
\r
380 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
383 bool is_identifier_part_character (char c)
\r
385 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
388 int is_punct (char c, ref bool doread)
\r
397 return Token.OPEN_BRACE;
\r
399 return Token.CLOSE_BRACE;
\r
401 return Token.OPEN_BRACKET;
\r
403 return Token.CLOSE_BRACKET;
\r
405 return Token.OPEN_PARENS;
\r
407 if (deambiguate_close_parens == 0)
\r
408 return Token.CLOSE_PARENS;
\r
410 --deambiguate_close_parens;
\r
412 // Save current position and parse next token.
\r
413 int old = reader.Position;
\r
414 int new_token = token ();
\r
415 reader.Position = old;
\r
418 if (new_token == Token.OPEN_PARENS)
\r
419 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
420 else if (new_token == Token.MINUS)
\r
421 return Token.CLOSE_PARENS_MINUS;
\r
422 else if (IsCastToken (new_token))
\r
423 return Token.CLOSE_PARENS_CAST;
\r
425 return Token.CLOSE_PARENS_NO_CAST;
\r
429 return Token.COMMA;
\r
431 return Token.COLON;
\r
433 return Token.SEMICOLON;
\r
435 return Token.TILDE;
\r
437 return Token.INTERR;
\r
446 t = Token.OP_ADD_ASSIGN;
\r
456 t = Token.OP_SUB_ASSIGN;
\r
460 return Token.MINUS;
\r
468 return Token.OP_NE;
\r
476 return Token.OP_EQ;
\r
478 return Token.ASSIGN;
\r
484 return Token.OP_AND;
\r
485 } else if (d == '='){
\r
487 return Token.OP_AND_ASSIGN;
\r
489 return Token.BITWISE_AND;
\r
495 return Token.OP_OR;
\r
496 } else if (d == '='){
\r
498 return Token.OP_OR_ASSIGN;
\r
500 return Token.BITWISE_OR;
\r
506 return Token.OP_MULT_ASSIGN;
\r
514 return Token.OP_DIV_ASSIGN;
\r
522 return Token.OP_MOD_ASSIGN;
\r
524 return Token.PERCENT;
\r
530 return Token.OP_XOR_ASSIGN;
\r
532 return Token.CARRET;
\r
542 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
544 return Token.OP_SHIFT_LEFT;
\r
545 } else if (d == '='){
\r
547 return Token.OP_LE;
\r
549 return Token.OP_LT;
\r
559 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
561 return Token.OP_SHIFT_RIGHT;
\r
562 } else if (d == '='){
\r
564 return Token.OP_GE;
\r
566 return Token.OP_GT;
\r
568 return Token.ERROR;
\r
571 int deambiguate_close_parens = 0;
\r
573 public void Deambiguate_CloseParens ()
\r
576 deambiguate_close_parens++;
\r
579 void Error_NumericConstantTooLong ()
\r
581 Report.Error (1021, Location, "Numeric constant too long");
\r
584 bool decimal_digits (int c)
\r
587 bool seen_digits = false;
\r
590 if (number_pos == max_number_size)
\r
591 Error_NumericConstantTooLong ();
\r
592 number_builder [number_pos++] = (char) c;
\r
596 // We use peekChar2, because decimal_digits needs to do a
\r
597 // 2-character look-ahead (5.ToString for example).
\r
599 while ((d = peekChar2 ()) != -1){
\r
600 if (d >= '0' && d <= '9'){
\r
601 if (number_pos == max_number_size)
\r
602 Error_NumericConstantTooLong ();
\r
603 number_builder [number_pos++] = (char) d;
\r
605 seen_digits = true;
\r
610 return seen_digits;
\r
613 bool is_hex (int e)
\r
615 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
618 void hex_digits (int c)
\r
621 number_builder [number_pos++] = (char) c;
\r
625 int real_type_suffix (int c)
\r
630 case 'F': case 'f':
\r
631 t = Token.LITERAL_FLOAT;
\r
633 case 'D': case 'd':
\r
634 t = Token.LITERAL_DOUBLE;
\r
636 case 'M': case 'm':
\r
637 t= Token.LITERAL_DECIMAL;
\r
645 int integer_type_suffix (ulong ul, int c)
\r
647 bool is_unsigned = false;
\r
648 bool is_long = false;
\r
651 bool scanning = true;
\r
654 case 'U': case 'u':
\r
657 is_unsigned = true;
\r
664 // if we have not seen anything in between
\r
665 // report this error
\r
669 "the 'l' suffix is easily confused with digit `1'," +
\r
670 " use 'L' for clarity");
\r
686 } while (scanning);
\r
689 if (is_long && is_unsigned){
\r
691 return Token.LITERAL_INTEGER;
\r
692 } else if (is_unsigned){
\r
693 // uint if possible, or ulong else.
\r
695 if ((ul & 0xffffffff00000000) == 0)
\r
699 } else if (is_long){
\r
700 // long if possible, ulong otherwise
\r
701 if ((ul & 0x8000000000000000) != 0)
\r
706 // int, uint, long or ulong in that order
\r
707 if ((ul & 0xffffffff00000000) == 0){
\r
708 uint ui = (uint) ul;
\r
710 if ((ui & 0x80000000) != 0)
\r
715 if ((ul & 0x8000000000000000) != 0)
\r
721 return Token.LITERAL_INTEGER;
\r
725 // given `c' as the next char in the input decide whether
\r
726 // we need to convert to a special type, and then choose
\r
727 // the best representation for the integer
\r
729 int adjust_int (int c)
\r
732 if (number_pos > 9){
\r
733 ulong ul = (uint) (number_builder [0] - '0');
\r
735 for (int i = 1; i < number_pos; i++){
\r
736 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
738 return integer_type_suffix (ul, c);
\r
740 uint ui = (uint) (number_builder [0] - '0');
\r
742 for (int i = 1; i < number_pos; i++){
\r
743 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
745 return integer_type_suffix (ui, c);
\r
747 } catch (OverflowException) {
\r
748 error_details = "Integral constant is too large";
\r
749 Report.Error (1021, Location, error_details);
\r
751 return Token.LITERAL_INTEGER;
\r
755 int adjust_real (int t)
\r
757 string s = new String (number_builder, 0, number_pos);
\r
760 case Token.LITERAL_DECIMAL:
\r
762 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
763 } catch (OverflowException) {
\r
765 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
766 Report.Error (594, Location, error_details);
\r
769 case Token.LITERAL_FLOAT:
\r
771 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
772 } catch (OverflowException) {
\r
774 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
775 Report.Error (594, Location, error_details);
\r
779 case Token.LITERAL_DOUBLE:
\r
781 t = Token.LITERAL_DOUBLE;
\r
783 val = System.Double.Parse (s, styles, csharp_format_info);
\r
784 } catch (OverflowException) {
\r
786 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
787 Report.Error (594, Location, error_details);
\r
800 while ((d = peekChar ()) != -1){
\r
802 number_builder [number_pos++] = (char) d;
\r
808 string s = new String (number_builder, 0, number_pos);
\r
810 if (number_pos <= 8)
\r
811 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
813 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
814 } catch (OverflowException){
\r
815 error_details = "Integral constant is too large";
\r
816 Report.Error (1021, Location, error_details);
\r
818 return Token.LITERAL_INTEGER;
\r
821 return integer_type_suffix (ul, peekChar ());
\r
825 // Invoked if we know we have .digits or digits
\r
827 int is_number (int c)
\r
829 bool is_real = false;
\r
834 if (c >= '0' && c <= '9'){
\r
836 int peek = peekChar ();
\r
838 if (peek == 'x' || peek == 'X')
\r
839 return handle_hex ();
\r
841 decimal_digits (c);
\r
846 // We need to handle the case of
\r
847 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
850 if (decimal_digits ('.')){
\r
856 return adjust_int (-1);
\r
860 if (c == 'e' || c == 'E'){
\r
862 if (number_pos == max_number_size)
\r
863 Error_NumericConstantTooLong ();
\r
864 number_builder [number_pos++] = 'e';
\r
868 if (number_pos == max_number_size)
\r
869 Error_NumericConstantTooLong ();
\r
870 number_builder [number_pos++] = '+';
\r
872 } else if (c == '-') {
\r
873 if (number_pos == max_number_size)
\r
874 Error_NumericConstantTooLong ();
\r
875 number_builder [number_pos++] = '-';
\r
878 if (number_pos == max_number_size)
\r
879 Error_NumericConstantTooLong ();
\r
880 number_builder [number_pos++] = '+';
\r
883 decimal_digits (c);
\r
887 type = real_type_suffix (c);
\r
888 if (type == Token.NONE && !is_real){
\r
890 return adjust_int (c);
\r
894 if (type == Token.NONE){
\r
899 return adjust_real (type);
\r
901 Console.WriteLine ("This should not be reached");
\r
902 throw new Exception ("Is Number should never reach this point");
\r
906 // Accepts exactly count (4 or 8) hex, no more no less
\r
908 int getHex (int count, out bool error)
\r
913 int top = count != -1 ? count : 4;
\r
917 for (i = 0; i < top; i++){
\r
920 if (c >= '0' && c <= '9')
\r
921 c = (int) c - (int) '0';
\r
922 else if (c >= 'A' && c <= 'F')
\r
923 c = (int) c - (int) 'A' + 10;
\r
924 else if (c >= 'a' && c <= 'f')
\r
925 c = (int) c - (int) 'a' + 10;
\r
931 total = (total * 16) + c;
\r
933 int p = peekChar ();
\r
936 if (!is_hex ((char)p))
\r
977 v = getHex (-1, out error);
\r
982 v = getHex (4, out error);
\r
987 v = getHex (8, out error);
\r
992 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1001 if (putback_char != -1){
\r
1002 int x = putback_char;
\r
1003 putback_char = -1;
\r
1007 return reader.Read ();
\r
1012 if (putback_char != -1)
\r
1013 return putback_char;
\r
1014 putback_char = reader.Read ();
\r
1015 return putback_char;
\r
1020 if (putback_char != -1)
\r
1021 return putback_char;
\r
1022 return reader.Peek ();
\r
1025 void putback (int c)
\r
1027 if (putback_char != -1){
\r
1028 Console.WriteLine ("Col: " + col);
\r
1029 Console.WriteLine ("Row: " + line);
\r
1030 Console.WriteLine ("Name: " + ref_name.Name);
\r
1031 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1032 throw new Exception ("This should not happen putback on putback");
\r
1037 public bool advance ()
\r
1039 return peekChar () != -1;
\r
1042 public Object Value {
\r
1048 public Object value ()
\r
1053 bool IsCastToken (int token)
\r
1058 case Token.IDENTIFIER:
\r
1059 case Token.LITERAL_INTEGER:
\r
1060 case Token.LITERAL_FLOAT:
\r
1061 case Token.LITERAL_DOUBLE:
\r
1062 case Token.LITERAL_DECIMAL:
\r
1063 case Token.LITERAL_CHARACTER:
\r
1064 case Token.LITERAL_STRING:
\r
1066 case Token.CHECKED:
\r
1071 case Token.SIZEOF:
\r
1075 case Token.TYPEOF:
\r
1076 case Token.UNCHECKED:
\r
1077 case Token.UNSAFE:
\r
1080 // These can be part of a member access
\r
1085 case Token.USHORT:
\r
1088 case Token.DOUBLE:
\r
1098 public int token ()
\r
1100 current_token = xtoken ();
\r
1101 return current_token;
\r
1104 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1106 void get_cmd_arg (out string cmd, out string arg)
\r
1110 tokens_seen = false;
\r
1112 static_cmd_arg.Length = 0;
\r
1114 while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1115 static_cmd_arg.Append ((char) c);
\r
1118 cmd = static_cmd_arg.ToString ();
\r
1124 } else if (c == '\r')
\r
1127 // skip over white space
\r
1128 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1135 } else if (c == '\r'){
\r
1140 static_cmd_arg.Length = 0;
\r
1141 static_cmd_arg.Append ((char) c);
\r
1143 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1144 static_cmd_arg.Append ((char) c);
\r
1150 } else if (c == '\r')
\r
1152 arg = static_cmd_arg.ToString ().Trim ();
\r
1156 // Handles the #line directive
\r
1158 bool PreProcessLine (string arg)
\r
1163 if (arg == "default"){
\r
1165 ref_name = file_name;
\r
1166 Location.Push (ref_name);
\r
1173 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1174 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1177 char [] quotes = { '\"' };
\r
1179 string name = arg.Substring (pos). Trim (quotes);
\r
1180 ref_name = Location.LookupFile (name);
\r
1181 Location.Push (ref_name);
\r
1183 ref_line = System.Int32.Parse (arg);
\r
1193 // Handles #define and #undef
\r
1195 void PreProcessDefinition (bool is_define, string arg)
\r
1197 if (arg == "" || arg == "true" || arg == "false"){
\r
1198 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1202 char[] whitespace = { ' ', '\t' };
\r
1203 if (arg.IndexOfAny (whitespace) != -1){
\r
1204 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1208 if (!is_identifier_start_character (arg [0]))
\r
1209 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1211 foreach (char c in arg.Substring (1)){
\r
1212 if (!is_identifier_part_character (c)){
\r
1213 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1219 if (defines == null)
\r
1220 defines = new Hashtable ();
\r
1223 if (defines == null)
\r
1225 if (defines.Contains (arg))
\r
1226 defines.Remove (arg);
\r
1230 bool eval_val (string s)
\r
1237 if (defines == null)
\r
1239 if (defines.Contains (s))
\r
1245 bool pp_primary (ref string s)
\r
1248 int len = s.Length;
\r
1254 s = s.Substring (1);
\r
1255 bool val = pp_expr (ref s);
\r
1256 if (s.Length > 0 && s [0] == ')'){
\r
1257 s = s.Substring (1);
\r
1260 Error_InvalidDirective ();
\r
1264 if (is_identifier_start_character (c)){
\r
1270 if (is_identifier_part_character (c)){
\r
1274 bool v = eval_val (s.Substring (0, j));
\r
1275 s = s.Substring (j);
\r
1278 bool vv = eval_val (s);
\r
1283 Error_InvalidDirective ();
\r
1287 bool pp_unary (ref string s)
\r
1290 int len = s.Length;
\r
1293 if (s [0] == '!'){
\r
1294 if (len > 1 && s [1] == '='){
\r
1295 Error_InvalidDirective ();
\r
1298 s = s.Substring (1);
\r
1299 return ! pp_primary (ref s);
\r
1301 return pp_primary (ref s);
\r
1303 Error_InvalidDirective ();
\r
1308 bool pp_eq (ref string s)
\r
1310 bool va = pp_unary (ref s);
\r
1313 int len = s.Length;
\r
1315 if (s [0] == '='){
\r
1316 if (len > 2 && s [1] == '='){
\r
1317 s = s.Substring (2);
\r
1318 return va == pp_unary (ref s);
\r
1320 Error_InvalidDirective ();
\r
1323 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1324 s = s.Substring (2);
\r
1326 return va != pp_unary (ref s);
\r
1335 bool pp_and (ref string s)
\r
1337 bool va = pp_eq (ref s);
\r
1340 int len = s.Length;
\r
1342 if (s [0] == '&'){
\r
1343 if (len > 2 && s [1] == '&'){
\r
1344 s = s.Substring (2);
\r
1345 return (va & pp_eq (ref s));
\r
1347 Error_InvalidDirective ();
\r
1356 // Evaluates an expression for `#if' or `#elif'
\r
1358 bool pp_expr (ref string s)
\r
1360 bool va = pp_and (ref s);
\r
1362 int len = s.Length;
\r
1367 if (len > 2 && s [1] == '|'){
\r
1368 s = s.Substring (2);
\r
1369 return va | pp_expr (ref s);
\r
1371 Error_InvalidDirective ();
\r
1380 bool eval (string s)
\r
1382 bool v = pp_expr (ref s);
\r
1384 if (s.Length != 0){
\r
1385 Error_InvalidDirective ();
\r
1392 void Error_InvalidDirective ()
\r
1394 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1397 void Error_UnexpectedDirective (string extra)
\r
1401 "Unexpected processor directive (" + extra + ")");
\r
1404 void Error_TokensSeen ()
\r
1408 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1412 // if true, then the code continues processing the code
\r
1413 // if false, the code stays in a loop until another directive is
\r
1416 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1419 bool region_directive = false;
\r
1421 get_cmd_arg (out cmd, out arg);
\r
1423 // Eat any trailing whitespaces and single-line comments
\r
1424 if (arg.IndexOf ("//") != -1)
\r
1425 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1426 arg = arg.TrimEnd (' ', '\t');
\r
1429 // The first group of pre-processing instructions is always processed
\r
1433 if (RootContext.V2)
\r
1438 if (!PreProcessLine (arg))
\r
1441 "Argument to #line directive is missing or invalid");
\r
1445 region_directive = true;
\r
1450 region_directive = true;
\r
1451 goto case "endif";
\r
1455 Error_InvalidDirective ();
\r
1458 bool taking = false;
\r
1459 if (ifstack == null)
\r
1460 ifstack = new Stack ();
\r
1462 if (ifstack.Count == 0){
\r
1465 int state = (int) ifstack.Peek ();
\r
1466 if ((state & TAKING) != 0)
\r
1470 if (eval (arg) && taking){
\r
1471 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1472 if (region_directive)
\r
1474 ifstack.Push (push);
\r
1477 int push = (taking ? PARENT_TAKING : 0);
\r
1478 if (region_directive)
\r
1480 ifstack.Push (push);
\r
1485 if (ifstack == null || ifstack.Count == 0){
\r
1486 Error_UnexpectedDirective ("no #if for this #endif");
\r
1489 int pop = (int) ifstack.Pop ();
\r
1491 if (region_directive && ((pop & REGION) == 0))
\r
1492 Report.Error (1027, Location, "#endif directive expected");
\r
1493 else if (!region_directive && ((pop & REGION) != 0))
\r
1494 Report.Error (1038, Location, "#endregion directive expected");
\r
1496 if (ifstack.Count == 0)
\r
1499 int state = (int) ifstack.Peek ();
\r
1501 if ((state & TAKING) != 0)
\r
1509 if (ifstack == null || ifstack.Count == 0){
\r
1510 Error_UnexpectedDirective ("no #if for this #elif");
\r
1513 int state = (int) ifstack.Peek ();
\r
1515 if ((state & REGION) != 0) {
\r
1516 Report.Error (1038, Location, "#endregion directive expected");
\r
1520 if ((state & ELSE_SEEN) != 0){
\r
1521 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1525 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1528 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1529 state = (int) ifstack.Pop ();
\r
1530 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1537 if (ifstack == null || ifstack.Count == 0){
\r
1540 "Unexpected processor directive (no #if for this #else)");
\r
1543 int state = (int) ifstack.Peek ();
\r
1545 if ((state & REGION) != 0) {
\r
1546 Report.Error (1038, Location, "#endregion directive expected");
\r
1550 if ((state & ELSE_SEEN) != 0){
\r
1551 Error_UnexpectedDirective ("#else within #else");
\r
1558 if ((state & TAKEN_BEFORE) == 0){
\r
1559 ret = ((state & PARENT_TAKING) != 0);
\r
1568 ifstack.Push (state | ELSE_SEEN);
\r
1575 // These are only processed if we are in a `taking' block
\r
1577 if (!caller_is_taking)
\r
1582 if (any_token_seen){
\r
1583 Error_TokensSeen ();
\r
1586 PreProcessDefinition (true, arg);
\r
1590 if (any_token_seen){
\r
1591 Error_TokensSeen ();
\r
1594 PreProcessDefinition (false, arg);
\r
1598 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1602 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1606 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1611 private int consume_string (bool quoted)
\r
1614 string_builder.Length = 0;
\r
1616 while ((c = getChar ()) != -1){
\r
1618 if (quoted && peekChar () == '"'){
\r
1619 string_builder.Append ((char) c);
\r
1623 val = string_builder.ToString ();
\r
1624 return Token.LITERAL_STRING;
\r
1630 Report.Error (1010, Location, "Newline in constant");
\r
1640 return Token.ERROR;
\r
1642 string_builder.Append ((char) c);
\r
1645 Report.Error (1039, Location, "Unterminated string literal");
\r
1649 private int consume_identifier (int s, bool quoted)
\r
1654 id_builder [0] = (char) s;
\r
1656 while ((c = reader.Read ()) != -1) {
\r
1657 if (is_identifier_part_character ((char) c)){
\r
1658 if (pos == max_id_size){
\r
1659 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1660 return Token.ERROR;
\r
1663 id_builder [pos++] = (char) c;
\r
1664 putback_char = -1;
\r
1673 // Optimization: avoids doing the keyword lookup
\r
1674 // on uppercase letters and _
\r
1677 int keyword = GetKeyword (id_builder, pos);
\r
1678 if (keyword != -1 && !quoted)
\r
1683 // Keep identifiers in an array of hashtables to avoid needless
\r
1687 if (identifiers [pos] != null) {
\r
1688 val = identifiers [pos][id_builder];
\r
1689 if (val != null) {
\r
1690 return Token.IDENTIFIER;
\r
1694 identifiers [pos] = new CharArrayHashtable (pos);
\r
1696 val = new String (id_builder, 0, pos);
\r
1698 char [] chars = new char [pos];
\r
1699 Array.Copy (id_builder, chars, pos);
\r
1701 identifiers [pos] [chars] = val;
\r
1703 return Token.IDENTIFIER;
\r
1706 public int xtoken ()
\r
1709 bool doread = false;
\r
1713 // optimization: eliminate col and implement #directive semantic correctly.
\r
1714 for (;(c = getChar ()) != -1; col++) {
\r
1715 if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r' || c == 0xa0){
\r
1718 col = (((col + 8) / 8) * 8) - 1;
\r
1722 // Handle double-slash comments.
\r
1724 int d = peekChar ();
\r
1728 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1735 any_token_seen |= tokens_seen;
\r
1736 tokens_seen = false;
\r
1738 } else if (d == '*'){
\r
1741 while ((d = getChar ()) != -1){
\r
1742 if (d == '*' && peekChar () == '/'){
\r
1751 any_token_seen |= tokens_seen;
\r
1752 tokens_seen = false;
\r
1757 goto is_punct_label;
\r
1761 if (is_identifier_start_character ((char)c)){
\r
1762 tokens_seen = true;
\r
1763 return consume_identifier (c, false);
\r
1767 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1768 tokens_seen = true;
\r
1781 any_token_seen |= tokens_seen;
\r
1782 tokens_seen = false;
\r
1786 if (c >= '0' && c <= '9'){
\r
1787 tokens_seen = true;
\r
1788 return is_number (c);
\r
1792 tokens_seen = true;
\r
1793 int peek = peekChar ();
\r
1794 if (peek >= '0' && peek <= '9')
\r
1795 return is_number (c);
\r
1799 /* For now, ignore pre-processor commands */
\r
1800 // FIXME: In C# the '#' is not limited to appear
\r
1801 // on the first column.
\r
1802 if (c == '#' && !tokens_seen){
\r
1807 cont = handle_preprocessing_directive (cont);
\r
1815 bool skipping = false;
\r
1816 for (;(c = getChar ()) != -1; col++){
\r
1822 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1824 else if (c != '#')
\r
1826 if (c == '#' && !skipping)
\r
1829 any_token_seen |= tokens_seen;
\r
1830 tokens_seen = false;
\r
1832 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1837 return consume_string (false);
\r
1841 tokens_seen = true;
\r
1843 error_details = "Empty character literal";
\r
1844 Report.Error (1011, Location, error_details);
\r
1845 return Token.ERROR;
\r
1849 return Token.ERROR;
\r
1850 val = new System.Char ();
\r
1855 error_details = "Too many characters in character literal";
\r
1856 Report.Error (1012, Location, error_details);
\r
1858 // Try to recover, read until newline or next "'"
\r
1859 while ((c = getChar ()) != -1){
\r
1860 if (c == '\n' || c == '\''){
\r
1869 return Token.ERROR;
\r
1871 return Token.LITERAL_CHARACTER;
\r
1877 tokens_seen = true;
\r
1878 return consume_string (true);
\r
1879 } else if (is_identifier_start_character ((char) c)){
\r
1880 return consume_identifier (c, true);
\r
1882 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1887 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
1888 "character on a line.";
\r
1890 Report.Error (1040, Location, error_details);
\r
1892 return Token.ERROR;
\r
1895 error_details = ((char)c).ToString ();
\r
1897 return Token.ERROR;
\r
1903 public void cleanup ()
\r
1905 if (ifstack != null && ifstack.Count >= 1) {
\r
1906 int state = (int) ifstack.Pop ();
\r
1907 if ((state & REGION) != 0)
\r
1908 Report.Error (1038, "#endregion directive expected");
\r
1910 Report.Error (1027, "#endif directive expected");
\r