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
295 // Class initializer
\r
297 static Tokenizer ()
\r
300 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
301 styles = NumberStyles.Float;
\r
303 string_builder = new System.Text.StringBuilder ();
\r
306 int GetKeyword (char[] id, int id_len)
\r
309 * Keywords are stored in an array of hashtables grouped by their
\r
313 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
\r
315 object o = keywords [id_len] [id];
\r
322 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
324 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
326 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
332 public Location Location {
\r
334 return new Location (ref_line);
\r
338 void define (string def)
\r
340 if (!RootContext.AllDefines.Contains (def)){
\r
341 RootContext.AllDefines [def] = true;
\r
343 if (defines.Contains (def))
\r
345 defines [def] = true;
\r
348 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
350 this.ref_name = file;
\r
351 this.file_name = file;
\r
357 defines = new Hashtable ();
\r
358 foreach (string def in defs)
\r
363 // FIXME: This could be `Location.Push' but we have to
\r
364 // find out why the MS compiler allows this
\r
366 Mono.CSharp.Location.Push (file);
\r
369 public static void Cleanup () {
\r
370 identifiers = null;
\r
373 static bool is_identifier_start_character (char c)
\r
375 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
378 static bool is_identifier_part_character (char c)
\r
380 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
383 public static bool IsValidIdentifier (string s)
385 if (s == null || s.Length == 0)
388 if (!is_identifier_start_character (s [0]))
391 for (int i = 1; i < s.Length; i ++)
392 if (! is_identifier_part_character (s [i]))
398 int is_punct (char c, ref bool doread)
\r
407 return Token.OPEN_BRACE;
\r
409 return Token.CLOSE_BRACE;
\r
411 return Token.OPEN_BRACKET;
\r
413 return Token.CLOSE_BRACKET;
\r
415 return Token.OPEN_PARENS;
\r
417 if (deambiguate_close_parens == 0)
\r
418 return Token.CLOSE_PARENS;
\r
420 --deambiguate_close_parens;
\r
422 // Save current position and parse next token.
\r
423 int old = reader.Position;
\r
424 int new_token = token ();
\r
425 reader.Position = old;
\r
428 if (new_token == Token.OPEN_PARENS)
\r
429 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
430 else if (new_token == Token.MINUS)
\r
431 return Token.CLOSE_PARENS_MINUS;
\r
432 else if (IsCastToken (new_token))
\r
433 return Token.CLOSE_PARENS_CAST;
\r
435 return Token.CLOSE_PARENS_NO_CAST;
\r
439 return Token.COMMA;
\r
441 return Token.COLON;
\r
443 return Token.SEMICOLON;
\r
445 return Token.TILDE;
\r
447 return Token.INTERR;
\r
456 t = Token.OP_ADD_ASSIGN;
\r
466 t = Token.OP_SUB_ASSIGN;
\r
470 return Token.MINUS;
\r
478 return Token.OP_NE;
\r
486 return Token.OP_EQ;
\r
488 return Token.ASSIGN;
\r
494 return Token.OP_AND;
\r
495 } else if (d == '='){
\r
497 return Token.OP_AND_ASSIGN;
\r
499 return Token.BITWISE_AND;
\r
505 return Token.OP_OR;
\r
506 } else if (d == '='){
\r
508 return Token.OP_OR_ASSIGN;
\r
510 return Token.BITWISE_OR;
\r
516 return Token.OP_MULT_ASSIGN;
\r
524 return Token.OP_DIV_ASSIGN;
\r
532 return Token.OP_MOD_ASSIGN;
\r
534 return Token.PERCENT;
\r
540 return Token.OP_XOR_ASSIGN;
\r
542 return Token.CARRET;
\r
552 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
554 return Token.OP_SHIFT_LEFT;
\r
555 } else if (d == '='){
\r
557 return Token.OP_LE;
\r
559 return Token.OP_LT;
\r
569 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
571 return Token.OP_SHIFT_RIGHT;
\r
572 } else if (d == '='){
\r
574 return Token.OP_GE;
\r
576 return Token.OP_GT;
\r
578 return Token.ERROR;
\r
581 int deambiguate_close_parens = 0;
\r
583 public void Deambiguate_CloseParens ()
\r
586 deambiguate_close_parens++;
\r
589 void Error_NumericConstantTooLong ()
\r
591 Report.Error (1021, Location, "Numeric constant too long");
\r
594 bool decimal_digits (int c)
\r
597 bool seen_digits = false;
\r
600 if (number_pos == max_number_size)
\r
601 Error_NumericConstantTooLong ();
\r
602 number_builder [number_pos++] = (char) c;
\r
606 // We use peekChar2, because decimal_digits needs to do a
\r
607 // 2-character look-ahead (5.ToString for example).
\r
609 while ((d = peekChar2 ()) != -1){
\r
610 if (d >= '0' && d <= '9'){
\r
611 if (number_pos == max_number_size)
\r
612 Error_NumericConstantTooLong ();
\r
613 number_builder [number_pos++] = (char) d;
\r
615 seen_digits = true;
\r
620 return seen_digits;
\r
623 bool is_hex (int e)
\r
625 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
628 void hex_digits (int c)
\r
631 number_builder [number_pos++] = (char) c;
\r
635 int real_type_suffix (int c)
\r
640 case 'F': case 'f':
\r
641 t = Token.LITERAL_FLOAT;
\r
643 case 'D': case 'd':
\r
644 t = Token.LITERAL_DOUBLE;
\r
646 case 'M': case 'm':
\r
647 t= Token.LITERAL_DECIMAL;
\r
655 int integer_type_suffix (ulong ul, int c)
\r
657 bool is_unsigned = false;
\r
658 bool is_long = false;
\r
661 bool scanning = true;
\r
664 case 'U': case 'u':
\r
667 is_unsigned = true;
\r
674 // if we have not seen anything in between
\r
675 // report this error
\r
679 "the 'l' suffix is easily confused with digit `1'," +
\r
680 " use 'L' for clarity");
\r
696 } while (scanning);
\r
699 if (is_long && is_unsigned){
\r
701 return Token.LITERAL_INTEGER;
\r
702 } else if (is_unsigned){
\r
703 // uint if possible, or ulong else.
\r
705 if ((ul & 0xffffffff00000000) == 0)
\r
709 } else if (is_long){
\r
710 // long if possible, ulong otherwise
\r
711 if ((ul & 0x8000000000000000) != 0)
\r
716 // int, uint, long or ulong in that order
\r
717 if ((ul & 0xffffffff00000000) == 0){
\r
718 uint ui = (uint) ul;
\r
720 if ((ui & 0x80000000) != 0)
\r
725 if ((ul & 0x8000000000000000) != 0)
\r
731 return Token.LITERAL_INTEGER;
\r
735 // given `c' as the next char in the input decide whether
\r
736 // we need to convert to a special type, and then choose
\r
737 // the best representation for the integer
\r
739 int adjust_int (int c)
\r
742 if (number_pos > 9){
\r
743 ulong ul = (uint) (number_builder [0] - '0');
\r
745 for (int i = 1; i < number_pos; i++){
\r
746 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
748 return integer_type_suffix (ul, c);
\r
750 uint ui = (uint) (number_builder [0] - '0');
\r
752 for (int i = 1; i < number_pos; i++){
\r
753 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
755 return integer_type_suffix (ui, c);
\r
757 } catch (OverflowException) {
\r
758 error_details = "Integral constant is too large";
\r
759 Report.Error (1021, Location, error_details);
\r
761 return Token.LITERAL_INTEGER;
\r
765 int adjust_real (int t)
\r
767 string s = new String (number_builder, 0, number_pos);
\r
770 case Token.LITERAL_DECIMAL:
\r
772 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
773 } catch (OverflowException) {
\r
775 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
776 Report.Error (594, Location, error_details);
\r
779 case Token.LITERAL_FLOAT:
\r
781 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
782 } catch (OverflowException) {
\r
784 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
785 Report.Error (594, Location, error_details);
\r
789 case Token.LITERAL_DOUBLE:
\r
791 t = Token.LITERAL_DOUBLE;
\r
793 val = System.Double.Parse (s, styles, csharp_format_info);
\r
794 } catch (OverflowException) {
\r
796 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
797 Report.Error (594, Location, error_details);
\r
810 while ((d = peekChar ()) != -1){
\r
812 number_builder [number_pos++] = (char) d;
\r
818 string s = new String (number_builder, 0, number_pos);
\r
820 if (number_pos <= 8)
\r
821 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
823 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
824 } catch (OverflowException){
\r
825 error_details = "Integral constant is too large";
\r
826 Report.Error (1021, Location, error_details);
\r
828 return Token.LITERAL_INTEGER;
\r
831 return integer_type_suffix (ul, peekChar ());
\r
835 // Invoked if we know we have .digits or digits
\r
837 int is_number (int c)
\r
839 bool is_real = false;
\r
844 if (c >= '0' && c <= '9'){
\r
846 int peek = peekChar ();
\r
848 if (peek == 'x' || peek == 'X')
\r
849 return handle_hex ();
\r
851 decimal_digits (c);
\r
856 // We need to handle the case of
\r
857 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
860 if (decimal_digits ('.')){
\r
866 return adjust_int (-1);
\r
870 if (c == 'e' || c == 'E'){
\r
872 if (number_pos == max_number_size)
\r
873 Error_NumericConstantTooLong ();
\r
874 number_builder [number_pos++] = 'e';
\r
878 if (number_pos == max_number_size)
\r
879 Error_NumericConstantTooLong ();
\r
880 number_builder [number_pos++] = '+';
\r
882 } else if (c == '-') {
\r
883 if (number_pos == max_number_size)
\r
884 Error_NumericConstantTooLong ();
\r
885 number_builder [number_pos++] = '-';
\r
888 if (number_pos == max_number_size)
\r
889 Error_NumericConstantTooLong ();
\r
890 number_builder [number_pos++] = '+';
\r
893 decimal_digits (c);
\r
897 type = real_type_suffix (c);
\r
898 if (type == Token.NONE && !is_real){
\r
900 return adjust_int (c);
\r
904 if (type == Token.NONE){
\r
909 return adjust_real (type);
\r
911 Console.WriteLine ("This should not be reached");
\r
912 throw new Exception ("Is Number should never reach this point");
\r
916 // Accepts exactly count (4 or 8) hex, no more no less
\r
918 int getHex (int count, out bool error)
\r
923 int top = count != -1 ? count : 4;
\r
927 for (i = 0; i < top; i++){
\r
930 if (c >= '0' && c <= '9')
\r
931 c = (int) c - (int) '0';
\r
932 else if (c >= 'A' && c <= 'F')
\r
933 c = (int) c - (int) 'A' + 10;
\r
934 else if (c >= 'a' && c <= 'f')
\r
935 c = (int) c - (int) 'a' + 10;
\r
941 total = (total * 16) + c;
\r
943 int p = peekChar ();
\r
946 if (!is_hex ((char)p))
\r
987 v = getHex (-1, out error);
\r
992 v = getHex (4, out error);
\r
997 v = getHex (8, out error);
\r
1002 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1011 if (putback_char != -1){
\r
1012 int x = putback_char;
\r
1013 putback_char = -1;
\r
1017 return reader.Read ();
\r
1022 if (putback_char != -1)
\r
1023 return putback_char;
\r
1024 putback_char = reader.Read ();
\r
1025 return putback_char;
\r
1030 if (putback_char != -1)
\r
1031 return putback_char;
\r
1032 return reader.Peek ();
\r
1035 void putback (int c)
\r
1037 if (putback_char != -1){
\r
1038 Console.WriteLine ("Col: " + col);
\r
1039 Console.WriteLine ("Row: " + line);
\r
1040 Console.WriteLine ("Name: " + ref_name.Name);
\r
1041 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1042 throw new Exception ("This should not happen putback on putback");
\r
1047 public bool advance ()
\r
1049 return peekChar () != -1;
\r
1052 public Object Value {
\r
1058 public Object value ()
\r
1063 bool IsCastToken (int token)
\r
1068 case Token.IDENTIFIER:
\r
1069 case Token.LITERAL_INTEGER:
\r
1070 case Token.LITERAL_FLOAT:
\r
1071 case Token.LITERAL_DOUBLE:
\r
1072 case Token.LITERAL_DECIMAL:
\r
1073 case Token.LITERAL_CHARACTER:
\r
1074 case Token.LITERAL_STRING:
\r
1076 case Token.CHECKED:
\r
1081 case Token.SIZEOF:
\r
1085 case Token.TYPEOF:
\r
1086 case Token.UNCHECKED:
\r
1087 case Token.UNSAFE:
\r
1090 // These can be part of a member access
\r
1095 case Token.USHORT:
\r
1098 case Token.DOUBLE:
\r
1108 public int token ()
\r
1110 current_token = xtoken ();
\r
1111 return current_token;
\r
1114 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1116 void get_cmd_arg (out string cmd, out string arg)
\r
1120 tokens_seen = false;
\r
1122 static_cmd_arg.Length = 0;
\r
1124 // skip over white space
\r
1125 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1129 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1130 if (is_identifier_part_character ((char) c)){
\r
1131 static_cmd_arg.Append ((char) c);
\r
1139 cmd = static_cmd_arg.ToString ();
\r
1145 } else if (c == '\r')
\r
1148 // skip over white space
\r
1149 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1156 } else if (c == '\r'){
\r
1161 static_cmd_arg.Length = 0;
\r
1162 static_cmd_arg.Append ((char) c);
\r
1164 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1165 static_cmd_arg.Append ((char) c);
\r
1171 } else if (c == '\r')
\r
1173 arg = static_cmd_arg.ToString ().Trim ();
\r
1177 // Handles the #line directive
\r
1179 bool PreProcessLine (string arg)
\r
1184 if (arg == "default"){
\r
1186 ref_name = file_name;
\r
1187 Location.Push (ref_name);
\r
1189 } else if (arg == "hidden"){
\r
1191 // We ignore #line hidden
\r
1199 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1200 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1203 char [] quotes = { '\"' };
\r
1205 string name = arg.Substring (pos). Trim (quotes);
\r
1206 ref_name = Location.LookupFile (name);
\r
1207 file_name.HasLineDirective = true;
\r
1208 ref_name.HasLineDirective = true;
\r
1209 Location.Push (ref_name);
\r
1211 ref_line = System.Int32.Parse (arg);
\r
1221 // Handles #define and #undef
\r
1223 void PreProcessDefinition (bool is_define, string arg)
\r
1225 if (arg == "" || arg == "true" || arg == "false"){
\r
1226 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1230 char[] whitespace = { ' ', '\t' };
\r
1231 if (arg.IndexOfAny (whitespace) != -1){
\r
1232 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1236 if (!is_identifier_start_character (arg [0]))
\r
1237 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1239 foreach (char c in arg.Substring (1)){
\r
1240 if (!is_identifier_part_character (c)){
\r
1241 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1247 if (defines == null)
\r
1248 defines = new Hashtable ();
\r
1251 if (defines == null)
\r
1253 if (defines.Contains (arg))
\r
1254 defines.Remove (arg);
\r
1258 bool eval_val (string s)
\r
1265 if (defines == null)
\r
1267 if (defines.Contains (s))
\r
1273 bool pp_primary (ref string s)
\r
1276 int len = s.Length;
\r
1282 s = s.Substring (1);
\r
1283 bool val = pp_expr (ref s);
\r
1284 if (s.Length > 0 && s [0] == ')'){
\r
1285 s = s.Substring (1);
\r
1288 Error_InvalidDirective ();
\r
1292 if (is_identifier_start_character (c)){
\r
1298 if (is_identifier_part_character (c)){
\r
1302 bool v = eval_val (s.Substring (0, j));
\r
1303 s = s.Substring (j);
\r
1306 bool vv = eval_val (s);
\r
1311 Error_InvalidDirective ();
\r
1315 bool pp_unary (ref string s)
\r
1318 int len = s.Length;
\r
1321 if (s [0] == '!'){
\r
1322 if (len > 1 && s [1] == '='){
\r
1323 Error_InvalidDirective ();
\r
1326 s = s.Substring (1);
\r
1327 return ! pp_primary (ref s);
\r
1329 return pp_primary (ref s);
\r
1331 Error_InvalidDirective ();
\r
1336 bool pp_eq (ref string s)
\r
1338 bool va = pp_unary (ref s);
\r
1341 int len = s.Length;
\r
1343 if (s [0] == '='){
\r
1344 if (len > 2 && s [1] == '='){
\r
1345 s = s.Substring (2);
\r
1346 return va == pp_unary (ref s);
\r
1348 Error_InvalidDirective ();
\r
1351 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1352 s = s.Substring (2);
\r
1354 return va != pp_unary (ref s);
\r
1363 bool pp_and (ref string s)
\r
1365 bool va = pp_eq (ref s);
\r
1368 int len = s.Length;
\r
1370 if (s [0] == '&'){
\r
1371 if (len > 2 && s [1] == '&'){
\r
1372 s = s.Substring (2);
\r
1373 return (va & pp_eq (ref s));
\r
1375 Error_InvalidDirective ();
\r
1384 // Evaluates an expression for `#if' or `#elif'
\r
1386 bool pp_expr (ref string s)
\r
1388 bool va = pp_and (ref s);
\r
1390 int len = s.Length;
\r
1395 if (len > 2 && s [1] == '|'){
\r
1396 s = s.Substring (2);
\r
1397 return va | pp_expr (ref s);
\r
1399 Error_InvalidDirective ();
\r
1408 bool eval (string s)
\r
1410 bool v = pp_expr (ref s);
\r
1412 if (s.Length != 0){
\r
1413 Error_InvalidDirective ();
\r
1420 void Error_InvalidDirective ()
\r
1422 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1425 void Error_UnexpectedDirective (string extra)
\r
1429 "Unexpected processor directive (" + extra + ")");
\r
1432 void Error_TokensSeen ()
\r
1436 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1440 // if true, then the code continues processing the code
\r
1441 // if false, the code stays in a loop until another directive is
\r
1444 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1447 bool region_directive = false;
\r
1449 get_cmd_arg (out cmd, out arg);
\r
1451 // Eat any trailing whitespaces and single-line comments
\r
1452 if (arg.IndexOf ("//") != -1)
\r
1453 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1454 arg = arg.TrimEnd (' ', '\t');
\r
1457 // The first group of pre-processing instructions is always processed
\r
1461 if (RootContext.V2)
\r
1466 if (!PreProcessLine (arg))
\r
1469 "Argument to #line directive is missing or invalid");
\r
1473 region_directive = true;
\r
1478 region_directive = true;
\r
1479 goto case "endif";
\r
1483 Error_InvalidDirective ();
\r
1486 bool taking = false;
\r
1487 if (ifstack == null)
\r
1488 ifstack = new Stack ();
\r
1490 if (ifstack.Count == 0){
\r
1493 int state = (int) ifstack.Peek ();
\r
1494 if ((state & TAKING) != 0)
\r
1498 if (eval (arg) && taking){
\r
1499 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1500 if (region_directive)
\r
1502 ifstack.Push (push);
\r
1505 int push = (taking ? PARENT_TAKING : 0);
\r
1506 if (region_directive)
\r
1508 ifstack.Push (push);
\r
1513 if (ifstack == null || ifstack.Count == 0){
\r
1514 Error_UnexpectedDirective ("no #if for this #endif");
\r
1517 int pop = (int) ifstack.Pop ();
\r
1519 if (region_directive && ((pop & REGION) == 0))
\r
1520 Report.Error (1027, Location, "#endif directive expected");
\r
1521 else if (!region_directive && ((pop & REGION) != 0))
\r
1522 Report.Error (1038, Location, "#endregion directive expected");
\r
1524 if (ifstack.Count == 0)
\r
1527 int state = (int) ifstack.Peek ();
\r
1529 if ((state & TAKING) != 0)
\r
1537 if (ifstack == null || ifstack.Count == 0){
\r
1538 Error_UnexpectedDirective ("no #if for this #elif");
\r
1541 int state = (int) ifstack.Peek ();
\r
1543 if ((state & REGION) != 0) {
\r
1544 Report.Error (1038, Location, "#endregion directive expected");
\r
1548 if ((state & ELSE_SEEN) != 0){
\r
1549 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1553 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1556 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1557 state = (int) ifstack.Pop ();
\r
1558 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1565 if (ifstack == null || ifstack.Count == 0){
\r
1568 "Unexpected processor directive (no #if for this #else)");
\r
1571 int state = (int) ifstack.Peek ();
\r
1573 if ((state & REGION) != 0) {
\r
1574 Report.Error (1038, Location, "#endregion directive expected");
\r
1578 if ((state & ELSE_SEEN) != 0){
\r
1579 Error_UnexpectedDirective ("#else within #else");
\r
1586 if ((state & TAKEN_BEFORE) == 0){
\r
1587 ret = ((state & PARENT_TAKING) != 0);
\r
1596 ifstack.Push (state | ELSE_SEEN);
\r
1603 // These are only processed if we are in a `taking' block
\r
1605 if (!caller_is_taking)
\r
1610 if (any_token_seen){
\r
1611 Error_TokensSeen ();
\r
1614 PreProcessDefinition (true, arg);
\r
1618 if (any_token_seen){
\r
1619 Error_TokensSeen ();
\r
1622 PreProcessDefinition (false, arg);
\r
1626 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1630 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1634 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1639 private int consume_string (bool quoted)
\r
1642 string_builder.Length = 0;
\r
1644 while ((c = getChar ()) != -1){
\r
1646 if (quoted && peekChar () == '"'){
\r
1647 string_builder.Append ((char) c);
\r
1651 val = string_builder.ToString ();
\r
1652 return Token.LITERAL_STRING;
\r
1658 Report.Error (1010, Location, "Newline in constant");
\r
1668 return Token.ERROR;
\r
1670 string_builder.Append ((char) c);
\r
1673 Report.Error (1039, Location, "Unterminated string literal");
\r
1677 private int consume_identifier (int s, bool quoted)
\r
1682 id_builder [0] = (char) s;
\r
1684 while ((c = reader.Read ()) != -1) {
\r
1685 if (is_identifier_part_character ((char) c)){
\r
1686 if (pos == max_id_size){
\r
1687 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1688 return Token.ERROR;
\r
1691 id_builder [pos++] = (char) c;
\r
1692 putback_char = -1;
\r
1701 // Optimization: avoids doing the keyword lookup
\r
1702 // on uppercase letters and _
\r
1705 int keyword = GetKeyword (id_builder, pos);
\r
1706 if (keyword != -1 && !quoted)
\r
1711 // Keep identifiers in an array of hashtables to avoid needless
\r
1715 if (identifiers [pos] != null) {
\r
1716 val = identifiers [pos][id_builder];
\r
1717 if (val != null) {
\r
1718 return Token.IDENTIFIER;
\r
1722 identifiers [pos] = new CharArrayHashtable (pos);
\r
1724 val = new String (id_builder, 0, pos);
\r
1726 char [] chars = new char [pos];
\r
1727 Array.Copy (id_builder, chars, pos);
\r
1729 identifiers [pos] [chars] = val;
\r
1731 return Token.IDENTIFIER;
\r
1734 public int xtoken ()
\r
1737 bool doread = false;
\r
1741 // optimization: eliminate col and implement #directive semantic correctly.
\r
1742 for (;(c = getChar ()) != -1; col++) {
\r
1747 col = (((col + 8) / 8) * 8) - 1;
\r
1751 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
\r
1755 if (peekChar () == '\n')
\r
1761 any_token_seen |= tokens_seen;
\r
1762 tokens_seen = false;
\r
1766 // Handle double-slash comments.
\r
1768 int d = peekChar ();
\r
1772 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1779 any_token_seen |= tokens_seen;
\r
1780 tokens_seen = false;
\r
1782 } else if (d == '*'){
\r
1785 while ((d = getChar ()) != -1){
\r
1786 if (d == '*' && peekChar () == '/'){
\r
1795 any_token_seen |= tokens_seen;
\r
1796 tokens_seen = false;
\r
1801 goto is_punct_label;
\r
1805 if (is_identifier_start_character ((char)c)){
\r
1806 tokens_seen = true;
\r
1807 return consume_identifier (c, false);
\r
1811 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1812 tokens_seen = true;
\r
1825 any_token_seen |= tokens_seen;
\r
1826 tokens_seen = false;
\r
1830 if (c >= '0' && c <= '9'){
\r
1831 tokens_seen = true;
\r
1832 return is_number (c);
\r
1836 tokens_seen = true;
\r
1837 int peek = peekChar ();
\r
1838 if (peek >= '0' && peek <= '9')
\r
1839 return is_number (c);
\r
1843 /* For now, ignore pre-processor commands */
\r
1844 // FIXME: In C# the '#' is not limited to appear
\r
1845 // on the first column.
\r
1846 if (c == '#' && !tokens_seen){
\r
1851 cont = handle_preprocessing_directive (cont);
\r
1859 bool skipping = false;
\r
1860 for (;(c = getChar ()) != -1; col++){
\r
1866 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1868 else if (c != '#')
\r
1870 if (c == '#' && !skipping)
\r
1873 any_token_seen |= tokens_seen;
\r
1874 tokens_seen = false;
\r
1876 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1881 return consume_string (false);
\r
1885 tokens_seen = true;
\r
1887 error_details = "Empty character literal";
\r
1888 Report.Error (1011, Location, error_details);
\r
1889 return Token.ERROR;
\r
1893 return Token.ERROR;
\r
1894 val = new System.Char ();
\r
1899 error_details = "Too many characters in character literal";
\r
1900 Report.Error (1012, Location, error_details);
\r
1902 // Try to recover, read until newline or next "'"
\r
1903 while ((c = getChar ()) != -1){
\r
1904 if (c == '\n' || c == '\''){
\r
1913 return Token.ERROR;
\r
1915 return Token.LITERAL_CHARACTER;
\r
1921 tokens_seen = true;
\r
1922 return consume_string (true);
\r
1923 } else if (is_identifier_start_character ((char) c)){
\r
1924 return consume_identifier (c, true);
\r
1926 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1931 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
1932 "character on a line.";
\r
1934 Report.Error (1040, Location, error_details);
\r
1936 return Token.ERROR;
\r
1939 error_details = ((char)c).ToString ();
\r
1941 return Token.ERROR;
\r
1947 public void cleanup ()
\r
1949 if (ifstack != null && ifstack.Count >= 1) {
\r
1950 int state = (int) ifstack.Pop ();
\r
1951 if ((state & REGION) != 0)
\r
1952 Report.Error (1038, "#endregion directive expected");
\r
1954 Report.Error (1027, "#endif directive expected");
\r