1 // -*- coding: dos -*-
\r
3 // cs-tokenizer.cs: The Tokenizer for the C# compiler
\r
4 // This also implements the preprocessor
\r
6 // Author: Miguel de Icaza (miguel@gnu.org)
\r
8 // Licensed under the terms of the GNU GPL
\r
10 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
\r
15 * Make sure we accept the proper Unicode ranges, per the spec.
\r
21 using System.Collections;
\r
23 using System.Globalization;
\r
24 using System.Reflection;
\r
26 namespace Mono.CSharp
\r
29 /// Tokenizer for C# source code.
\r
32 public class Tokenizer : yyParser.yyInput
\r
34 SeekableStreamReader reader;
\r
35 public SourceFile ref_name;
\r
36 public SourceFile file_name;
\r
37 public int ref_line = 1;
\r
38 public int line = 1;
\r
40 public int current_token;
\r
41 bool handle_get_set = false;
\r
42 bool handle_remove_add = false;
\r
43 bool handle_assembly = false;
\r
46 // Whether tokens have been seen on this line
\r
48 bool tokens_seen = false;
\r
51 // Whether a token has been seen on the file
\r
52 // This is needed because `define' is not allowed to be used
\r
53 // after a token has been seen.
\r
55 bool any_token_seen = false;
\r
56 static Hashtable tokenValues;
\r
58 private static Hashtable TokenValueName
\r
61 if (tokenValues == null)
\r
62 tokenValues = GetTokenValueNameHash ();
\r
68 private static Hashtable GetTokenValueNameHash ()
\r
70 Type t = typeof (Token);
\r
71 FieldInfo [] fields = t.GetFields ();
\r
72 Hashtable hash = new Hashtable ();
\r
73 foreach (FieldInfo field in fields) {
\r
74 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
\r
75 hash.Add (field.GetValue (null), field.Name);
\r
81 // Returns a verbose representation of the current location
\r
83 public string location {
\r
87 if (current_token == Token.ERROR)
\r
88 det = "detail: " + error_details;
\r
92 // return "Line: "+line+" Col: "+col + "\n" +
\r
93 // "VirtLine: "+ref_line +
\r
94 // " Token: "+current_token + " " + det;
\r
95 string current_token_name = TokenValueName [current_token] as string;
\r
96 if (current_token_name == null)
\r
97 current_token_name = current_token.ToString ();
\r
99 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
\r
102 current_token_name,
\r
107 public bool PropertyParsing {
\r
109 return handle_get_set;
\r
113 handle_get_set = value;
\r
117 public bool AssemblyTargetParsing {
\r
119 return handle_assembly;
\r
123 handle_assembly = value;
\r
127 public bool EventParsing {
\r
129 return handle_remove_add;
\r
133 handle_remove_add = value;
\r
140 static CharArrayHashtable[] keywords;
\r
141 static NumberStyles styles;
\r
142 static NumberFormatInfo csharp_format_info;
\r
145 // Values for the associated token returned
\r
155 const int TAKING = 1;
\r
156 const int TAKEN_BEFORE = 2;
\r
157 const int ELSE_SEEN = 4;
\r
158 const int PARENT_TAKING = 8;
\r
159 const int REGION = 16;
\r
162 // pre-processor if stack state:
\r
166 static System.Text.StringBuilder string_builder;
\r
168 const int max_id_size = 512;
\r
169 static char [] id_builder = new char [max_id_size];
\r
171 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
\r
173 const int max_number_size = 128;
\r
174 static char [] number_builder = new char [max_number_size];
\r
175 static int number_pos;
\r
178 // Details about the error encoutered by the tokenizer
\r
180 string error_details;
\r
182 public string error {
\r
184 return error_details;
\r
200 static void AddKeyword (string kw, int token) {
\r
201 if (keywords [kw.Length] == null) {
\r
202 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
\r
204 keywords [kw.Length] [kw.ToCharArray ()] = token;
\r
207 static void InitTokens ()
\r
209 keywords = new CharArrayHashtable [64];
\r
211 AddKeyword ("__arglist", Token.ARGLIST);
\r
212 AddKeyword ("abstract", Token.ABSTRACT);
\r
213 AddKeyword ("as", Token.AS);
\r
214 AddKeyword ("add", Token.ADD);
\r
215 AddKeyword ("assembly", Token.ASSEMBLY);
\r
216 AddKeyword ("base", Token.BASE);
\r
217 AddKeyword ("bool", Token.BOOL);
\r
218 AddKeyword ("break", Token.BREAK);
\r
219 AddKeyword ("byte", Token.BYTE);
\r
220 AddKeyword ("case", Token.CASE);
\r
221 AddKeyword ("catch", Token.CATCH);
\r
222 AddKeyword ("char", Token.CHAR);
\r
223 AddKeyword ("checked", Token.CHECKED);
\r
224 AddKeyword ("class", Token.CLASS);
\r
225 AddKeyword ("const", Token.CONST);
\r
226 AddKeyword ("continue", Token.CONTINUE);
\r
227 AddKeyword ("decimal", Token.DECIMAL);
\r
228 AddKeyword ("default", Token.DEFAULT);
\r
229 AddKeyword ("delegate", Token.DELEGATE);
\r
230 AddKeyword ("do", Token.DO);
\r
231 AddKeyword ("double", Token.DOUBLE);
\r
232 AddKeyword ("else", Token.ELSE);
\r
233 AddKeyword ("enum", Token.ENUM);
\r
234 AddKeyword ("event", Token.EVENT);
\r
235 AddKeyword ("explicit", Token.EXPLICIT);
\r
236 AddKeyword ("extern", Token.EXTERN);
\r
237 AddKeyword ("false", Token.FALSE);
\r
238 AddKeyword ("finally", Token.FINALLY);
\r
239 AddKeyword ("fixed", Token.FIXED);
\r
240 AddKeyword ("float", Token.FLOAT);
\r
241 AddKeyword ("for", Token.FOR);
\r
242 AddKeyword ("foreach", Token.FOREACH);
\r
243 AddKeyword ("goto", Token.GOTO);
\r
244 AddKeyword ("get", Token.GET);
\r
245 AddKeyword ("if", Token.IF);
\r
246 AddKeyword ("implicit", Token.IMPLICIT);
\r
247 AddKeyword ("in", Token.IN);
\r
248 AddKeyword ("int", Token.INT);
\r
249 AddKeyword ("interface", Token.INTERFACE);
\r
250 AddKeyword ("internal", Token.INTERNAL);
\r
251 AddKeyword ("is", Token.IS);
\r
252 AddKeyword ("lock", Token.LOCK);
\r
253 AddKeyword ("long", Token.LONG);
\r
254 AddKeyword ("namespace", Token.NAMESPACE);
\r
255 AddKeyword ("new", Token.NEW);
\r
256 AddKeyword ("null", Token.NULL);
\r
257 AddKeyword ("object", Token.OBJECT);
\r
258 AddKeyword ("operator", Token.OPERATOR);
\r
259 AddKeyword ("out", Token.OUT);
\r
260 AddKeyword ("override", Token.OVERRIDE);
\r
261 AddKeyword ("params", Token.PARAMS);
\r
262 AddKeyword ("private", Token.PRIVATE);
\r
263 AddKeyword ("protected", Token.PROTECTED);
\r
264 AddKeyword ("public", Token.PUBLIC);
\r
265 AddKeyword ("readonly", Token.READONLY);
\r
266 AddKeyword ("ref", Token.REF);
\r
267 AddKeyword ("remove", Token.REMOVE);
\r
268 AddKeyword ("return", Token.RETURN);
\r
269 AddKeyword ("sbyte", Token.SBYTE);
\r
270 AddKeyword ("sealed", Token.SEALED);
\r
271 AddKeyword ("set", Token.SET);
\r
272 AddKeyword ("short", Token.SHORT);
\r
273 AddKeyword ("sizeof", Token.SIZEOF);
\r
274 AddKeyword ("stackalloc", Token.STACKALLOC);
\r
275 AddKeyword ("static", Token.STATIC);
\r
276 AddKeyword ("string", Token.STRING);
\r
277 AddKeyword ("struct", Token.STRUCT);
\r
278 AddKeyword ("switch", Token.SWITCH);
\r
279 AddKeyword ("this", Token.THIS);
\r
280 AddKeyword ("throw", Token.THROW);
\r
281 AddKeyword ("true", Token.TRUE);
\r
282 AddKeyword ("try", Token.TRY);
\r
283 AddKeyword ("typeof", Token.TYPEOF);
\r
284 AddKeyword ("uint", Token.UINT);
\r
285 AddKeyword ("ulong", Token.ULONG);
\r
286 AddKeyword ("unchecked", Token.UNCHECKED);
\r
287 AddKeyword ("unsafe", Token.UNSAFE);
\r
288 AddKeyword ("ushort", Token.USHORT);
\r
289 AddKeyword ("using", Token.USING);
\r
290 AddKeyword ("virtual", Token.VIRTUAL);
\r
291 AddKeyword ("void", Token.VOID);
\r
292 AddKeyword ("volatile", Token.VOLATILE);
\r
293 AddKeyword ("while", Token.WHILE);
\r
297 // Class initializer
\r
299 static Tokenizer ()
\r
302 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
303 styles = NumberStyles.Float;
\r
305 string_builder = new System.Text.StringBuilder ();
\r
308 int GetKeyword (char[] id, int id_len)
\r
311 * Keywords are stored in an array of hashtables grouped by their
\r
315 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
\r
317 object o = keywords [id_len] [id];
\r
324 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
326 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
328 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
334 public Location Location {
\r
336 return new Location (ref_line);
\r
340 void define (string def)
\r
342 if (!RootContext.AllDefines.Contains (def)){
\r
343 RootContext.AllDefines [def] = true;
\r
345 if (defines.Contains (def))
\r
347 defines [def] = true;
\r
350 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
352 this.ref_name = file;
\r
353 this.file_name = file;
\r
359 defines = new Hashtable ();
\r
360 foreach (string def in defs)
\r
365 // FIXME: This could be `Location.Push' but we have to
\r
366 // find out why the MS compiler allows this
\r
368 Mono.CSharp.Location.Push (file);
\r
371 public static void Cleanup () {
\r
372 identifiers = null;
\r
375 static bool is_identifier_start_character (char c)
\r
377 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
380 static bool is_identifier_part_character (char c)
\r
382 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
385 public static bool IsValidIdentifier (string s)
\r
387 if (s == null || s.Length == 0)
\r
390 if (!is_identifier_start_character (s [0]))
\r
393 for (int i = 1; i < s.Length; i ++)
\r
394 if (! is_identifier_part_character (s [i]))
\r
400 int is_punct (char c, ref bool doread)
\r
409 return Token.OPEN_BRACE;
\r
411 return Token.CLOSE_BRACE;
\r
413 return Token.OPEN_BRACKET;
\r
415 return Token.CLOSE_BRACKET;
\r
417 return Token.OPEN_PARENS;
\r
419 if (deambiguate_close_parens == 0)
\r
420 return Token.CLOSE_PARENS;
\r
422 --deambiguate_close_parens;
\r
424 // Save current position and parse next token.
\r
425 int old = reader.Position;
\r
426 int new_token = token ();
\r
427 reader.Position = old;
\r
430 if (new_token == Token.OPEN_PARENS)
\r
431 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
432 else if (new_token == Token.MINUS)
\r
433 return Token.CLOSE_PARENS_MINUS;
\r
434 else if (IsCastToken (new_token))
\r
435 return Token.CLOSE_PARENS_CAST;
\r
437 return Token.CLOSE_PARENS_NO_CAST;
\r
441 return Token.COMMA;
\r
443 return Token.COLON;
\r
445 return Token.SEMICOLON;
\r
447 return Token.TILDE;
\r
449 return Token.INTERR;
\r
458 t = Token.OP_ADD_ASSIGN;
\r
468 t = Token.OP_SUB_ASSIGN;
\r
472 return Token.MINUS;
\r
480 return Token.OP_NE;
\r
488 return Token.OP_EQ;
\r
490 return Token.ASSIGN;
\r
496 return Token.OP_AND;
\r
497 } else if (d == '='){
\r
499 return Token.OP_AND_ASSIGN;
\r
501 return Token.BITWISE_AND;
\r
507 return Token.OP_OR;
\r
508 } else if (d == '='){
\r
510 return Token.OP_OR_ASSIGN;
\r
512 return Token.BITWISE_OR;
\r
518 return Token.OP_MULT_ASSIGN;
\r
526 return Token.OP_DIV_ASSIGN;
\r
534 return Token.OP_MOD_ASSIGN;
\r
536 return Token.PERCENT;
\r
542 return Token.OP_XOR_ASSIGN;
\r
544 return Token.CARRET;
\r
554 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
556 return Token.OP_SHIFT_LEFT;
\r
557 } else if (d == '='){
\r
559 return Token.OP_LE;
\r
561 return Token.OP_LT;
\r
571 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
573 return Token.OP_SHIFT_RIGHT;
\r
574 } else if (d == '='){
\r
576 return Token.OP_GE;
\r
578 return Token.OP_GT;
\r
580 return Token.ERROR;
\r
583 int deambiguate_close_parens = 0;
\r
585 public void Deambiguate_CloseParens ()
\r
588 deambiguate_close_parens++;
\r
591 void Error_NumericConstantTooLong ()
\r
593 Report.Error (1021, Location, "Numeric constant too long");
\r
596 bool decimal_digits (int c)
\r
599 bool seen_digits = false;
\r
602 if (number_pos == max_number_size)
\r
603 Error_NumericConstantTooLong ();
\r
604 number_builder [number_pos++] = (char) c;
\r
608 // We use peekChar2, because decimal_digits needs to do a
\r
609 // 2-character look-ahead (5.ToString for example).
\r
611 while ((d = peekChar2 ()) != -1){
\r
612 if (d >= '0' && d <= '9'){
\r
613 if (number_pos == max_number_size)
\r
614 Error_NumericConstantTooLong ();
\r
615 number_builder [number_pos++] = (char) d;
\r
617 seen_digits = true;
\r
622 return seen_digits;
\r
625 bool is_hex (int e)
\r
627 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
630 void hex_digits (int c)
\r
633 number_builder [number_pos++] = (char) c;
\r
637 int real_type_suffix (int c)
\r
642 case 'F': case 'f':
\r
643 t = Token.LITERAL_FLOAT;
\r
645 case 'D': case 'd':
\r
646 t = Token.LITERAL_DOUBLE;
\r
648 case 'M': case 'm':
\r
649 t= Token.LITERAL_DECIMAL;
\r
657 int integer_type_suffix (ulong ul, int c)
\r
659 bool is_unsigned = false;
\r
660 bool is_long = false;
\r
663 bool scanning = true;
\r
666 case 'U': case 'u':
\r
669 is_unsigned = true;
\r
676 // if we have not seen anything in between
\r
677 // report this error
\r
681 "the 'l' suffix is easily confused with digit `1'," +
\r
682 " use 'L' for clarity");
\r
698 } while (scanning);
\r
701 if (is_long && is_unsigned){
\r
703 return Token.LITERAL_INTEGER;
\r
704 } else if (is_unsigned){
\r
705 // uint if possible, or ulong else.
\r
707 if ((ul & 0xffffffff00000000) == 0)
\r
711 } else if (is_long){
\r
712 // long if possible, ulong otherwise
\r
713 if ((ul & 0x8000000000000000) != 0)
\r
718 // int, uint, long or ulong in that order
\r
719 if ((ul & 0xffffffff00000000) == 0){
\r
720 uint ui = (uint) ul;
\r
722 if ((ui & 0x80000000) != 0)
\r
727 if ((ul & 0x8000000000000000) != 0)
\r
733 return Token.LITERAL_INTEGER;
\r
737 // given `c' as the next char in the input decide whether
\r
738 // we need to convert to a special type, and then choose
\r
739 // the best representation for the integer
\r
741 int adjust_int (int c)
\r
744 if (number_pos > 9){
\r
745 ulong ul = (uint) (number_builder [0] - '0');
\r
747 for (int i = 1; i < number_pos; i++){
\r
748 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
750 return integer_type_suffix (ul, c);
\r
752 uint ui = (uint) (number_builder [0] - '0');
\r
754 for (int i = 1; i < number_pos; i++){
\r
755 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
757 return integer_type_suffix (ui, c);
\r
759 } catch (OverflowException) {
\r
760 error_details = "Integral constant is too large";
\r
761 Report.Error (1021, Location, error_details);
\r
763 return Token.LITERAL_INTEGER;
\r
767 int adjust_real (int t)
\r
769 string s = new String (number_builder, 0, number_pos);
\r
772 case Token.LITERAL_DECIMAL:
\r
774 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
775 } catch (OverflowException) {
\r
777 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
778 Report.Error (594, Location, error_details);
\r
781 case Token.LITERAL_FLOAT:
\r
783 val = (float) 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 'float'";
\r
787 Report.Error (594, Location, error_details);
\r
791 case Token.LITERAL_DOUBLE:
\r
793 t = Token.LITERAL_DOUBLE;
\r
795 val = System.Double.Parse (s, styles, csharp_format_info);
\r
796 } catch (OverflowException) {
\r
798 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
799 Report.Error (594, Location, error_details);
\r
812 while ((d = peekChar ()) != -1){
\r
814 number_builder [number_pos++] = (char) d;
\r
820 string s = new String (number_builder, 0, number_pos);
\r
822 if (number_pos <= 8)
\r
823 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
825 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
826 } catch (OverflowException){
\r
827 error_details = "Integral constant is too large";
\r
828 Report.Error (1021, Location, error_details);
\r
830 return Token.LITERAL_INTEGER;
\r
833 return integer_type_suffix (ul, peekChar ());
\r
837 // Invoked if we know we have .digits or digits
\r
839 int is_number (int c)
\r
841 bool is_real = false;
\r
846 if (c >= '0' && c <= '9'){
\r
848 int peek = peekChar ();
\r
850 if (peek == 'x' || peek == 'X')
\r
851 return handle_hex ();
\r
853 decimal_digits (c);
\r
858 // We need to handle the case of
\r
859 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
862 if (decimal_digits ('.')){
\r
868 return adjust_int (-1);
\r
872 if (c == 'e' || c == 'E'){
\r
874 if (number_pos == max_number_size)
\r
875 Error_NumericConstantTooLong ();
\r
876 number_builder [number_pos++] = 'e';
\r
880 if (number_pos == max_number_size)
\r
881 Error_NumericConstantTooLong ();
\r
882 number_builder [number_pos++] = '+';
\r
884 } else if (c == '-') {
\r
885 if (number_pos == max_number_size)
\r
886 Error_NumericConstantTooLong ();
\r
887 number_builder [number_pos++] = '-';
\r
890 if (number_pos == max_number_size)
\r
891 Error_NumericConstantTooLong ();
\r
892 number_builder [number_pos++] = '+';
\r
895 decimal_digits (c);
\r
899 type = real_type_suffix (c);
\r
900 if (type == Token.NONE && !is_real){
\r
902 return adjust_int (c);
\r
906 if (type == Token.NONE){
\r
911 return adjust_real (type);
\r
913 Console.WriteLine ("This should not be reached");
\r
914 throw new Exception ("Is Number should never reach this point");
\r
918 // Accepts exactly count (4 or 8) hex, no more no less
\r
920 int getHex (int count, out bool error)
\r
925 int top = count != -1 ? count : 4;
\r
929 for (i = 0; i < top; i++){
\r
932 if (c >= '0' && c <= '9')
\r
933 c = (int) c - (int) '0';
\r
934 else if (c >= 'A' && c <= 'F')
\r
935 c = (int) c - (int) 'A' + 10;
\r
936 else if (c >= 'a' && c <= 'f')
\r
937 c = (int) c - (int) 'a' + 10;
\r
943 total = (total * 16) + c;
\r
945 int p = peekChar ();
\r
948 if (!is_hex ((char)p))
\r
989 v = getHex (-1, out error);
\r
994 v = getHex (4, out error);
\r
999 v = getHex (8, out error);
\r
1004 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1013 if (putback_char != -1){
\r
1014 int x = putback_char;
\r
1015 putback_char = -1;
\r
1019 return reader.Read ();
\r
1024 if (putback_char != -1)
\r
1025 return putback_char;
\r
1026 putback_char = reader.Read ();
\r
1027 return putback_char;
\r
1032 if (putback_char != -1)
\r
1033 return putback_char;
\r
1034 return reader.Peek ();
\r
1037 void putback (int c)
\r
1039 if (putback_char != -1){
\r
1040 Console.WriteLine ("Col: " + col);
\r
1041 Console.WriteLine ("Row: " + line);
\r
1042 Console.WriteLine ("Name: " + ref_name.Name);
\r
1043 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1044 throw new Exception ("This should not happen putback on putback");
\r
1049 public bool advance ()
\r
1051 return peekChar () != -1;
\r
1054 public Object Value {
\r
1060 public Object value ()
\r
1065 bool IsCastToken (int token)
\r
1070 case Token.IDENTIFIER:
\r
1071 case Token.LITERAL_INTEGER:
\r
1072 case Token.LITERAL_FLOAT:
\r
1073 case Token.LITERAL_DOUBLE:
\r
1074 case Token.LITERAL_DECIMAL:
\r
1075 case Token.LITERAL_CHARACTER:
\r
1076 case Token.LITERAL_STRING:
\r
1078 case Token.CHECKED:
\r
1083 case Token.SIZEOF:
\r
1087 case Token.TYPEOF:
\r
1088 case Token.UNCHECKED:
\r
1089 case Token.UNSAFE:
\r
1092 // These can be part of a member access
\r
1097 case Token.USHORT:
\r
1100 case Token.DOUBLE:
\r
1110 public int token ()
\r
1112 current_token = xtoken ();
\r
1113 return current_token;
\r
1116 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1118 void get_cmd_arg (out string cmd, out string arg)
\r
1122 tokens_seen = false;
\r
1124 static_cmd_arg.Length = 0;
\r
1126 // skip over white space
\r
1127 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1131 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1132 if (is_identifier_part_character ((char) c)){
\r
1133 static_cmd_arg.Append ((char) c);
\r
1141 cmd = static_cmd_arg.ToString ();
\r
1147 } else if (c == '\r')
\r
1150 // skip over white space
\r
1151 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1158 } else if (c == '\r'){
\r
1163 static_cmd_arg.Length = 0;
\r
1164 static_cmd_arg.Append ((char) c);
\r
1166 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1167 static_cmd_arg.Append ((char) c);
\r
1173 } else if (c == '\r')
\r
1175 arg = static_cmd_arg.ToString ().Trim ();
\r
1179 // Handles the #line directive
\r
1181 bool PreProcessLine (string arg)
\r
1186 if (arg == "default"){
\r
1188 ref_name = file_name;
\r
1189 Location.Push (ref_name);
\r
1191 } else if (arg == "hidden"){
\r
1193 // We ignore #line hidden
\r
1201 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1202 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1205 char [] quotes = { '\"' };
\r
1207 string name = arg.Substring (pos). Trim (quotes);
\r
1208 ref_name = Location.LookupFile (name);
\r
1209 file_name.HasLineDirective = true;
\r
1210 ref_name.HasLineDirective = true;
\r
1211 Location.Push (ref_name);
\r
1213 ref_line = System.Int32.Parse (arg);
\r
1223 // Handles #define and #undef
\r
1225 void PreProcessDefinition (bool is_define, string arg)
\r
1227 if (arg == "" || arg == "true" || arg == "false"){
\r
1228 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1232 char[] whitespace = { ' ', '\t' };
\r
1233 if (arg.IndexOfAny (whitespace) != -1){
\r
1234 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1238 if (!is_identifier_start_character (arg [0]))
\r
1239 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1241 foreach (char c in arg.Substring (1)){
\r
1242 if (!is_identifier_part_character (c)){
\r
1243 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1249 if (defines == null)
\r
1250 defines = new Hashtable ();
\r
1253 if (defines == null)
\r
1255 if (defines.Contains (arg))
\r
1256 defines.Remove (arg);
\r
1260 bool eval_val (string s)
\r
1267 if (defines == null)
\r
1269 if (defines.Contains (s))
\r
1275 bool pp_primary (ref string s)
\r
1278 int len = s.Length;
\r
1284 s = s.Substring (1);
\r
1285 bool val = pp_expr (ref s);
\r
1286 if (s.Length > 0 && s [0] == ')'){
\r
1287 s = s.Substring (1);
\r
1290 Error_InvalidDirective ();
\r
1294 if (is_identifier_start_character (c)){
\r
1300 if (is_identifier_part_character (c)){
\r
1304 bool v = eval_val (s.Substring (0, j));
\r
1305 s = s.Substring (j);
\r
1308 bool vv = eval_val (s);
\r
1313 Error_InvalidDirective ();
\r
1317 bool pp_unary (ref string s)
\r
1320 int len = s.Length;
\r
1323 if (s [0] == '!'){
\r
1324 if (len > 1 && s [1] == '='){
\r
1325 Error_InvalidDirective ();
\r
1328 s = s.Substring (1);
\r
1329 return ! pp_primary (ref s);
\r
1331 return pp_primary (ref s);
\r
1333 Error_InvalidDirective ();
\r
1338 bool pp_eq (ref string s)
\r
1340 bool va = pp_unary (ref s);
\r
1343 int len = s.Length;
\r
1345 if (s [0] == '='){
\r
1346 if (len > 2 && s [1] == '='){
\r
1347 s = s.Substring (2);
\r
1348 return va == pp_unary (ref s);
\r
1350 Error_InvalidDirective ();
\r
1353 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1354 s = s.Substring (2);
\r
1356 return va != pp_unary (ref s);
\r
1365 bool pp_and (ref string s)
\r
1367 bool va = pp_eq (ref s);
\r
1370 int len = s.Length;
\r
1372 if (s [0] == '&'){
\r
1373 if (len > 2 && s [1] == '&'){
\r
1374 s = s.Substring (2);
\r
1375 return (va & pp_eq (ref s));
\r
1377 Error_InvalidDirective ();
\r
1386 // Evaluates an expression for `#if' or `#elif'
\r
1388 bool pp_expr (ref string s)
\r
1390 bool va = pp_and (ref s);
\r
1392 int len = s.Length;
\r
1397 if (len > 2 && s [1] == '|'){
\r
1398 s = s.Substring (2);
\r
1399 return va | pp_expr (ref s);
\r
1401 Error_InvalidDirective ();
\r
1410 bool eval (string s)
\r
1412 bool v = pp_expr (ref s);
\r
1414 if (s.Length != 0){
\r
1415 Error_InvalidDirective ();
\r
1422 void Error_InvalidDirective ()
\r
1424 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1427 void Error_UnexpectedDirective (string extra)
\r
1431 "Unexpected processor directive (" + extra + ")");
\r
1434 void Error_TokensSeen ()
\r
1438 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1442 // if true, then the code continues processing the code
\r
1443 // if false, the code stays in a loop until another directive is
\r
1446 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1449 bool region_directive = false;
\r
1451 get_cmd_arg (out cmd, out arg);
\r
1453 // Eat any trailing whitespaces and single-line comments
\r
1454 if (arg.IndexOf ("//") != -1)
\r
1455 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1456 arg = arg.TrimEnd (' ', '\t');
\r
1459 // The first group of pre-processing instructions is always processed
\r
1463 if (RootContext.V2)
\r
1468 if (!PreProcessLine (arg))
\r
1471 "Argument to #line directive is missing or invalid");
\r
1475 region_directive = true;
\r
1480 region_directive = true;
\r
1481 goto case "endif";
\r
1485 Error_InvalidDirective ();
\r
1488 bool taking = false;
\r
1489 if (ifstack == null)
\r
1490 ifstack = new Stack ();
\r
1492 if (ifstack.Count == 0){
\r
1495 int state = (int) ifstack.Peek ();
\r
1496 if ((state & TAKING) != 0)
\r
1500 if (eval (arg) && taking){
\r
1501 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1502 if (region_directive)
\r
1504 ifstack.Push (push);
\r
1507 int push = (taking ? PARENT_TAKING : 0);
\r
1508 if (region_directive)
\r
1510 ifstack.Push (push);
\r
1515 if (ifstack == null || ifstack.Count == 0){
\r
1516 Error_UnexpectedDirective ("no #if for this #endif");
\r
1519 int pop = (int) ifstack.Pop ();
\r
1521 if (region_directive && ((pop & REGION) == 0))
\r
1522 Report.Error (1027, Location, "#endif directive expected");
\r
1523 else if (!region_directive && ((pop & REGION) != 0))
\r
1524 Report.Error (1038, Location, "#endregion directive expected");
\r
1526 if (ifstack.Count == 0)
\r
1529 int state = (int) ifstack.Peek ();
\r
1531 if ((state & TAKING) != 0)
\r
1539 if (ifstack == null || ifstack.Count == 0){
\r
1540 Error_UnexpectedDirective ("no #if for this #elif");
\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 ("#elif not valid after #else");
\r
1555 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1558 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1559 state = (int) ifstack.Pop ();
\r
1560 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1567 if (ifstack == null || ifstack.Count == 0){
\r
1570 "Unexpected processor directive (no #if for this #else)");
\r
1573 int state = (int) ifstack.Peek ();
\r
1575 if ((state & REGION) != 0) {
\r
1576 Report.Error (1038, Location, "#endregion directive expected");
\r
1580 if ((state & ELSE_SEEN) != 0){
\r
1581 Error_UnexpectedDirective ("#else within #else");
\r
1588 if ((state & TAKEN_BEFORE) == 0){
\r
1589 ret = ((state & PARENT_TAKING) != 0);
\r
1598 ifstack.Push (state | ELSE_SEEN);
\r
1605 // These are only processed if we are in a `taking' block
\r
1607 if (!caller_is_taking)
\r
1612 if (any_token_seen){
\r
1613 Error_TokensSeen ();
\r
1616 PreProcessDefinition (true, arg);
\r
1620 if (any_token_seen){
\r
1621 Error_TokensSeen ();
\r
1624 PreProcessDefinition (false, arg);
\r
1628 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1632 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1636 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1641 private int consume_string (bool quoted)
\r
1644 string_builder.Length = 0;
\r
1646 while ((c = getChar ()) != -1){
\r
1648 if (quoted && peekChar () == '"'){
\r
1649 string_builder.Append ((char) c);
\r
1653 val = string_builder.ToString ();
\r
1654 return Token.LITERAL_STRING;
\r
1660 Report.Error (1010, Location, "Newline in constant");
\r
1670 return Token.ERROR;
\r
1672 string_builder.Append ((char) c);
\r
1675 Report.Error (1039, Location, "Unterminated string literal");
\r
1679 private int consume_identifier (int s, bool quoted)
\r
1684 id_builder [0] = (char) s;
\r
1686 while ((c = reader.Read ()) != -1) {
\r
1687 if (is_identifier_part_character ((char) c)){
\r
1688 if (pos == max_id_size){
\r
1689 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1690 return Token.ERROR;
\r
1693 id_builder [pos++] = (char) c;
\r
1694 putback_char = -1;
\r
1703 // Optimization: avoids doing the keyword lookup
\r
1704 // on uppercase letters and _
\r
1706 if (s >= 'a' || s == '_'){
\r
1707 int keyword = GetKeyword (id_builder, pos);
\r
1708 if (keyword != -1 && !quoted)
\r
1713 // Keep identifiers in an array of hashtables to avoid needless
\r
1717 if (identifiers [pos] != null) {
\r
1718 val = identifiers [pos][id_builder];
\r
1719 if (val != null) {
\r
1720 return Token.IDENTIFIER;
\r
1724 identifiers [pos] = new CharArrayHashtable (pos);
\r
1726 val = new String (id_builder, 0, pos);
\r
1728 char [] chars = new char [pos];
\r
1729 Array.Copy (id_builder, chars, pos);
\r
1731 identifiers [pos] [chars] = val;
\r
1733 return Token.IDENTIFIER;
\r
1736 public int xtoken ()
\r
1739 bool doread = false;
\r
1743 // optimization: eliminate col and implement #directive semantic correctly.
\r
1744 for (;(c = getChar ()) != -1; col++) {
\r
1749 col = (((col + 8) / 8) * 8) - 1;
\r
1753 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
\r
1757 if (peekChar () == '\n')
\r
1763 any_token_seen |= tokens_seen;
\r
1764 tokens_seen = false;
\r
1768 // Handle double-slash comments.
\r
1770 int d = peekChar ();
\r
1774 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1781 any_token_seen |= tokens_seen;
\r
1782 tokens_seen = false;
\r
1784 } else if (d == '*'){
\r
1787 while ((d = getChar ()) != -1){
\r
1788 if (d == '*' && peekChar () == '/'){
\r
1797 any_token_seen |= tokens_seen;
\r
1798 tokens_seen = false;
\r
1803 goto is_punct_label;
\r
1807 if (is_identifier_start_character ((char)c)){
\r
1808 tokens_seen = true;
\r
1809 return consume_identifier (c, false);
\r
1813 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1814 tokens_seen = true;
\r
1827 any_token_seen |= tokens_seen;
\r
1828 tokens_seen = false;
\r
1832 if (c >= '0' && c <= '9'){
\r
1833 tokens_seen = true;
\r
1834 return is_number (c);
\r
1838 tokens_seen = true;
\r
1839 int peek = peekChar ();
\r
1840 if (peek >= '0' && peek <= '9')
\r
1841 return is_number (c);
\r
1845 /* For now, ignore pre-processor commands */
\r
1846 // FIXME: In C# the '#' is not limited to appear
\r
1847 // on the first column.
\r
1848 if (c == '#' && !tokens_seen){
\r
1853 cont = handle_preprocessing_directive (cont);
\r
1861 bool skipping = false;
\r
1862 for (;(c = getChar ()) != -1; col++){
\r
1868 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1870 else if (c != '#')
\r
1872 if (c == '#' && !skipping)
\r
1875 any_token_seen |= tokens_seen;
\r
1876 tokens_seen = false;
\r
1878 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1883 return consume_string (false);
\r
1887 tokens_seen = true;
\r
1889 error_details = "Empty character literal";
\r
1890 Report.Error (1011, Location, error_details);
\r
1891 return Token.ERROR;
\r
1895 return Token.ERROR;
\r
1896 val = new System.Char ();
\r
1901 error_details = "Too many characters in character literal";
\r
1902 Report.Error (1012, Location, error_details);
\r
1904 // Try to recover, read until newline or next "'"
\r
1905 while ((c = getChar ()) != -1){
\r
1906 if (c == '\n' || c == '\''){
\r
1915 return Token.ERROR;
\r
1917 return Token.LITERAL_CHARACTER;
\r
1923 tokens_seen = true;
\r
1924 return consume_string (true);
\r
1925 } else if (is_identifier_start_character ((char) c)){
\r
1926 return consume_identifier (c, true);
\r
1928 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1933 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
1934 "character on a line.";
\r
1936 Report.Error (1040, Location, error_details);
\r
1938 return Token.ERROR;
\r
1941 error_details = ((char)c).ToString ();
\r
1943 return Token.ERROR;
\r
1949 public void cleanup ()
\r
1951 if (ifstack != null && ifstack.Count >= 1) {
\r
1952 int state = (int) ifstack.Pop ();
\r
1953 if ((state & REGION) != 0)
\r
1954 Report.Error (1038, "#endregion directive expected");
\r
1956 Report.Error (1027, "#endif directive expected");
\r