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
43 bool handle_constraints = 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
137 public bool ConstraintsParsing {
\r
139 return handle_constraints;
\r
143 handle_constraints = value;
\r
150 static CharArrayHashtable[] keywords;
\r
151 static NumberStyles styles;
\r
152 static NumberFormatInfo csharp_format_info;
\r
155 // Values for the associated token returned
\r
165 const int TAKING = 1;
\r
166 const int TAKEN_BEFORE = 2;
\r
167 const int ELSE_SEEN = 4;
\r
168 const int PARENT_TAKING = 8;
\r
169 const int REGION = 16;
\r
172 // pre-processor if stack state:
\r
176 static System.Text.StringBuilder string_builder;
\r
178 const int max_id_size = 512;
\r
179 static char [] id_builder = new char [max_id_size];
\r
181 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
\r
183 const int max_number_size = 128;
\r
184 static char [] number_builder = new char [max_number_size];
\r
185 static int number_pos;
\r
188 // Details about the error encoutered by the tokenizer
\r
190 string error_details;
\r
192 public string error {
\r
194 return error_details;
\r
210 static void AddKeyword (string kw, int token) {
\r
211 if (keywords [kw.Length] == null) {
\r
212 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
\r
214 keywords [kw.Length] [kw.ToCharArray ()] = token;
\r
217 static void InitTokens ()
\r
219 keywords = new CharArrayHashtable [64];
\r
221 AddKeyword ("abstract", Token.ABSTRACT);
\r
222 AddKeyword ("as", Token.AS);
\r
223 AddKeyword ("add", Token.ADD);
\r
224 AddKeyword ("assembly", Token.ASSEMBLY);
\r
225 AddKeyword ("base", Token.BASE);
\r
226 AddKeyword ("bool", Token.BOOL);
\r
227 AddKeyword ("break", Token.BREAK);
\r
228 AddKeyword ("byte", Token.BYTE);
\r
229 AddKeyword ("case", Token.CASE);
\r
230 AddKeyword ("catch", Token.CATCH);
\r
231 AddKeyword ("char", Token.CHAR);
\r
232 AddKeyword ("checked", Token.CHECKED);
\r
233 AddKeyword ("class", Token.CLASS);
\r
234 AddKeyword ("const", Token.CONST);
\r
235 AddKeyword ("continue", Token.CONTINUE);
\r
236 AddKeyword ("decimal", Token.DECIMAL);
\r
237 AddKeyword ("default", Token.DEFAULT);
\r
238 AddKeyword ("delegate", Token.DELEGATE);
\r
239 AddKeyword ("do", Token.DO);
\r
240 AddKeyword ("double", Token.DOUBLE);
\r
241 AddKeyword ("else", Token.ELSE);
\r
242 AddKeyword ("enum", Token.ENUM);
\r
243 AddKeyword ("event", Token.EVENT);
\r
244 AddKeyword ("explicit", Token.EXPLICIT);
\r
245 AddKeyword ("extern", Token.EXTERN);
\r
246 AddKeyword ("false", Token.FALSE);
\r
247 AddKeyword ("finally", Token.FINALLY);
\r
248 AddKeyword ("fixed", Token.FIXED);
\r
249 AddKeyword ("float", Token.FLOAT);
\r
250 AddKeyword ("for", Token.FOR);
\r
251 AddKeyword ("foreach", Token.FOREACH);
\r
252 AddKeyword ("goto", Token.GOTO);
\r
253 AddKeyword ("get", Token.GET);
\r
254 AddKeyword ("if", Token.IF);
\r
255 AddKeyword ("implicit", Token.IMPLICIT);
\r
256 AddKeyword ("in", Token.IN);
\r
257 AddKeyword ("int", Token.INT);
\r
258 AddKeyword ("interface", Token.INTERFACE);
\r
259 AddKeyword ("internal", Token.INTERNAL);
\r
260 AddKeyword ("is", Token.IS);
\r
261 AddKeyword ("lock", Token.LOCK);
\r
262 AddKeyword ("long", Token.LONG);
\r
263 AddKeyword ("namespace", Token.NAMESPACE);
\r
264 AddKeyword ("new", Token.NEW);
\r
265 AddKeyword ("null", Token.NULL);
\r
266 AddKeyword ("object", Token.OBJECT);
\r
267 AddKeyword ("operator", Token.OPERATOR);
\r
268 AddKeyword ("out", Token.OUT);
\r
269 AddKeyword ("override", Token.OVERRIDE);
\r
270 AddKeyword ("params", Token.PARAMS);
\r
271 AddKeyword ("private", Token.PRIVATE);
\r
272 AddKeyword ("protected", Token.PROTECTED);
\r
273 AddKeyword ("public", Token.PUBLIC);
\r
274 AddKeyword ("readonly", Token.READONLY);
\r
275 AddKeyword ("ref", Token.REF);
\r
276 AddKeyword ("remove", Token.REMOVE);
\r
277 AddKeyword ("return", Token.RETURN);
\r
278 AddKeyword ("sbyte", Token.SBYTE);
\r
279 AddKeyword ("sealed", Token.SEALED);
\r
280 AddKeyword ("set", Token.SET);
\r
281 AddKeyword ("short", Token.SHORT);
\r
282 AddKeyword ("sizeof", Token.SIZEOF);
\r
283 AddKeyword ("stackalloc", Token.STACKALLOC);
\r
284 AddKeyword ("static", Token.STATIC);
\r
285 AddKeyword ("string", Token.STRING);
\r
286 AddKeyword ("struct", Token.STRUCT);
\r
287 AddKeyword ("switch", Token.SWITCH);
\r
288 AddKeyword ("this", Token.THIS);
\r
289 AddKeyword ("throw", Token.THROW);
\r
290 AddKeyword ("true", Token.TRUE);
\r
291 AddKeyword ("try", Token.TRY);
\r
292 AddKeyword ("typeof", Token.TYPEOF);
\r
293 AddKeyword ("uint", Token.UINT);
\r
294 AddKeyword ("ulong", Token.ULONG);
\r
295 AddKeyword ("unchecked", Token.UNCHECKED);
\r
296 AddKeyword ("unsafe", Token.UNSAFE);
\r
297 AddKeyword ("ushort", Token.USHORT);
\r
298 AddKeyword ("using", Token.USING);
\r
299 AddKeyword ("virtual", Token.VIRTUAL);
\r
300 AddKeyword ("void", Token.VOID);
\r
301 AddKeyword ("volatile", Token.VOLATILE);
\r
302 AddKeyword ("where", Token.WHERE);
\r
303 AddKeyword ("while", Token.WHILE);
\r
305 if (RootContext.V2){
\r
306 AddKeyword ("__yield", Token.YIELD);
\r
307 AddKeyword ("yield", Token.YIELD);
\r
312 // Class initializer
\r
314 static Tokenizer ()
\r
317 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
318 styles = NumberStyles.Float;
\r
320 string_builder = new System.Text.StringBuilder ();
\r
323 int GetKeyword (char[] id, int id_len)
\r
326 * Keywords are stored in an array of hashtables grouped by their
\r
330 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
\r
332 object o = keywords [id_len] [id];
\r
339 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
341 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
343 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
345 if (handle_constraints == false && res == Token.WHERE)
\r
351 public Location Location {
\r
353 return new Location (ref_line);
\r
357 void define (string def)
\r
359 if (!RootContext.AllDefines.Contains (def)){
\r
360 RootContext.AllDefines [def] = true;
\r
362 if (defines.Contains (def))
\r
364 defines [def] = true;
\r
367 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
369 this.ref_name = file;
\r
370 this.file_name = file;
\r
376 defines = new Hashtable ();
\r
377 foreach (string def in defs)
\r
382 // FIXME: This could be `Location.Push' but we have to
\r
383 // find out why the MS compiler allows this
\r
385 Mono.CSharp.Location.Push (file);
\r
388 public static void Cleanup () {
\r
389 identifiers = null;
\r
392 bool is_identifier_start_character (char c)
\r
394 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
397 bool is_identifier_part_character (char c)
\r
399 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
402 bool parse_less_than ()
\r
405 int the_token = token ();
\r
406 switch (the_token) {
\r
407 case Token.IDENTIFIER:
\r
411 case Token.DECIMAL:
\r
430 the_token = token ();
\r
432 if (the_token == Token.OP_GENERICS_GT)
\r
434 else if (the_token == Token.COMMA)
\r
436 else if (the_token == Token.OP_GENERICS_LT) {
\r
437 if (!parse_less_than ())
\r
445 bool parsing_less_than = false;
\r
446 int parsing_generic_less_than = 0;
\r
448 int is_punct (char c, ref bool doread)
\r
457 return Token.OPEN_BRACE;
\r
459 return Token.CLOSE_BRACE;
\r
461 return Token.OPEN_BRACKET;
\r
463 return Token.CLOSE_BRACKET;
\r
465 return Token.OPEN_PARENS;
\r
467 if (deambiguate_close_parens == 0)
\r
468 return Token.CLOSE_PARENS;
\r
470 --deambiguate_close_parens;
\r
472 // Save current position and parse next token.
\r
473 int old = reader.Position;
\r
474 int new_token = token ();
\r
475 reader.Position = old;
\r
478 if (new_token == Token.OPEN_PARENS)
\r
479 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
480 else if (new_token == Token.MINUS)
\r
481 return Token.CLOSE_PARENS_MINUS;
\r
482 else if (IsCastToken (new_token))
\r
483 return Token.CLOSE_PARENS_CAST;
\r
485 return Token.CLOSE_PARENS_NO_CAST;
\r
489 return Token.COMMA;
\r
491 return Token.COLON;
\r
493 return Token.SEMICOLON;
\r
495 return Token.TILDE;
\r
497 return Token.INTERR;
\r
501 if (parsing_generic_less_than++ > 0)
\r
502 return Token.OP_GENERICS_LT;
\r
504 // Save current position and parse next token.
\r
505 int old = reader.Position;
\r
506 bool is_generic_lt = parse_less_than ();
\r
507 reader.Position = old;
\r
510 if (is_generic_lt) {
\r
511 parsing_generic_less_than++;
\r
512 return Token.OP_GENERICS_LT;
\r
514 parsing_generic_less_than = 0;
\r
523 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
525 return Token.OP_SHIFT_LEFT;
\r
526 } else if (d == '='){
\r
528 return Token.OP_LE;
\r
530 return Token.OP_LT;
\r
531 } else if (c == '>') {
\r
532 if (parsing_generic_less_than > 0) {
\r
533 parsing_generic_less_than--;
\r
534 return Token.OP_GENERICS_GT;
\r
544 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
546 return Token.OP_SHIFT_RIGHT;
\r
547 } else if (d == '='){
\r
549 return Token.OP_GE;
\r
551 return Token.OP_GT;
\r
560 t = Token.OP_ADD_ASSIGN;
\r
570 t = Token.OP_SUB_ASSIGN;
\r
574 return Token.MINUS;
\r
582 return Token.OP_NE;
\r
590 return Token.OP_EQ;
\r
592 return Token.ASSIGN;
\r
598 return Token.OP_AND;
\r
599 } else if (d == '='){
\r
601 return Token.OP_AND_ASSIGN;
\r
603 return Token.BITWISE_AND;
\r
609 return Token.OP_OR;
\r
610 } else if (d == '='){
\r
612 return Token.OP_OR_ASSIGN;
\r
614 return Token.BITWISE_OR;
\r
620 return Token.OP_MULT_ASSIGN;
\r
628 return Token.OP_DIV_ASSIGN;
\r
636 return Token.OP_MOD_ASSIGN;
\r
638 return Token.PERCENT;
\r
644 return Token.OP_XOR_ASSIGN;
\r
646 return Token.CARRET;
\r
651 if (deambiguate_greater_than == 0)
\r
652 return Token.OP_GT;
\r
654 --deambiguate_greater_than;
\r
656 // Save current position and parse next token.
\r
657 int old = reader.Position;
\r
658 int new_token = token ();
\r
659 reader.Position = old;
\r
662 switch (new_token) {
\r
663 case Token.OPEN_PARENS:
\r
664 case Token.CLOSE_PARENS:
\r
665 case Token.CLOSE_BRACKET:
\r
668 case Token.SEMICOLON:
\r
672 return Token.OP_GENERICS_GT;
\r
675 return Token.OP_GT;
\r
680 return Token.ERROR;
\r
683 int deambiguate_close_parens = 0;
\r
685 public void Deambiguate_CloseParens ()
\r
688 deambiguate_close_parens++;
\r
691 void Error_NumericConstantTooLong ()
\r
693 Report.Error (1021, Location, "Numeric constant too long");
\r
696 bool decimal_digits (int c)
\r
699 bool seen_digits = false;
\r
702 if (number_pos == max_number_size)
\r
703 Error_NumericConstantTooLong ();
\r
704 number_builder [number_pos++] = (char) c;
\r
708 // We use peekChar2, because decimal_digits needs to do a
\r
709 // 2-character look-ahead (5.ToString for example).
\r
711 while ((d = peekChar2 ()) != -1){
\r
712 if (d >= '0' && d <= '9'){
\r
713 if (number_pos == max_number_size)
\r
714 Error_NumericConstantTooLong ();
\r
715 number_builder [number_pos++] = (char) d;
\r
717 seen_digits = true;
\r
722 return seen_digits;
\r
725 bool is_hex (int e)
\r
727 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
730 void hex_digits (int c)
\r
733 number_builder [number_pos++] = (char) c;
\r
737 int real_type_suffix (int c)
\r
742 case 'F': case 'f':
\r
743 t = Token.LITERAL_FLOAT;
\r
745 case 'D': case 'd':
\r
746 t = Token.LITERAL_DOUBLE;
\r
748 case 'M': case 'm':
\r
749 t= Token.LITERAL_DECIMAL;
\r
757 int integer_type_suffix (ulong ul, int c)
\r
759 bool is_unsigned = false;
\r
760 bool is_long = false;
\r
763 bool scanning = true;
\r
766 case 'U': case 'u':
\r
769 is_unsigned = true;
\r
776 // if we have not seen anything in between
\r
777 // report this error
\r
781 "the 'l' suffix is easily confused with digit `1'," +
\r
782 " use 'L' for clarity");
\r
798 } while (scanning);
\r
801 if (is_long && is_unsigned){
\r
803 return Token.LITERAL_INTEGER;
\r
804 } else if (is_unsigned){
\r
805 // uint if possible, or ulong else.
\r
807 if ((ul & 0xffffffff00000000) == 0)
\r
811 } else if (is_long){
\r
812 // long if possible, ulong otherwise
\r
813 if ((ul & 0x8000000000000000) != 0)
\r
818 // int, uint, long or ulong in that order
\r
819 if ((ul & 0xffffffff00000000) == 0){
\r
820 uint ui = (uint) ul;
\r
822 if ((ui & 0x80000000) != 0)
\r
827 if ((ul & 0x8000000000000000) != 0)
\r
833 return Token.LITERAL_INTEGER;
\r
837 // given `c' as the next char in the input decide whether
\r
838 // we need to convert to a special type, and then choose
\r
839 // the best representation for the integer
\r
841 int adjust_int (int c)
\r
844 if (number_pos > 9){
\r
845 ulong ul = (uint) (number_builder [0] - '0');
\r
847 for (int i = 1; i < number_pos; i++){
\r
848 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
850 return integer_type_suffix (ul, c);
\r
852 uint ui = (uint) (number_builder [0] - '0');
\r
854 for (int i = 1; i < number_pos; i++){
\r
855 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
857 return integer_type_suffix (ui, c);
\r
859 } catch (OverflowException) {
\r
860 error_details = "Integral constant is too large";
\r
861 Report.Error (1021, Location, error_details);
\r
863 return Token.LITERAL_INTEGER;
\r
867 int adjust_real (int t)
\r
869 string s = new String (number_builder, 0, number_pos);
\r
872 case Token.LITERAL_DECIMAL:
\r
874 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
875 } catch (OverflowException) {
\r
877 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
878 Report.Error (594, Location, error_details);
\r
881 case Token.LITERAL_FLOAT:
\r
883 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
884 } catch (OverflowException) {
\r
886 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
887 Report.Error (594, Location, error_details);
\r
891 case Token.LITERAL_DOUBLE:
\r
893 t = Token.LITERAL_DOUBLE;
\r
895 val = System.Double.Parse (s, styles, csharp_format_info);
\r
896 } catch (OverflowException) {
\r
898 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
899 Report.Error (594, Location, error_details);
\r
912 while ((d = peekChar ()) != -1){
\r
914 number_builder [number_pos++] = (char) d;
\r
920 string s = new String (number_builder, 0, number_pos);
\r
922 if (number_pos <= 8)
\r
923 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
925 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
926 } catch (OverflowException){
\r
927 error_details = "Integral constant is too large";
\r
928 Report.Error (1021, Location, error_details);
\r
930 return Token.LITERAL_INTEGER;
\r
933 return integer_type_suffix (ul, peekChar ());
\r
937 // Invoked if we know we have .digits or digits
\r
939 int is_number (int c)
\r
941 bool is_real = false;
\r
946 if (c >= '0' && c <= '9'){
\r
948 int peek = peekChar ();
\r
950 if (peek == 'x' || peek == 'X')
\r
951 return handle_hex ();
\r
953 decimal_digits (c);
\r
958 // We need to handle the case of
\r
959 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
962 if (decimal_digits ('.')){
\r
968 return adjust_int (-1);
\r
972 if (c == 'e' || c == 'E'){
\r
974 if (number_pos == max_number_size)
\r
975 Error_NumericConstantTooLong ();
\r
976 number_builder [number_pos++] = 'e';
\r
980 if (number_pos == max_number_size)
\r
981 Error_NumericConstantTooLong ();
\r
982 number_builder [number_pos++] = '+';
\r
984 } else if (c == '-') {
\r
985 if (number_pos == max_number_size)
\r
986 Error_NumericConstantTooLong ();
\r
987 number_builder [number_pos++] = '-';
\r
990 if (number_pos == max_number_size)
\r
991 Error_NumericConstantTooLong ();
\r
992 number_builder [number_pos++] = '+';
\r
995 decimal_digits (c);
\r
999 type = real_type_suffix (c);
\r
1000 if (type == Token.NONE && !is_real){
\r
1002 return adjust_int (c);
\r
1006 if (type == Token.NONE){
\r
1011 return adjust_real (type);
\r
1013 Console.WriteLine ("This should not be reached");
\r
1014 throw new Exception ("Is Number should never reach this point");
\r
1018 // Accepts exactly count (4 or 8) hex, no more no less
\r
1020 int getHex (int count, out bool error)
\r
1025 int top = count != -1 ? count : 4;
\r
1029 for (i = 0; i < top; i++){
\r
1032 if (c >= '0' && c <= '9')
\r
1033 c = (int) c - (int) '0';
\r
1034 else if (c >= 'A' && c <= 'F')
\r
1035 c = (int) c - (int) 'A' + 10;
\r
1036 else if (c >= 'a' && c <= 'f')
\r
1037 c = (int) c - (int) 'a' + 10;
\r
1043 total = (total * 16) + c;
\r
1045 int p = peekChar ();
\r
1048 if (!is_hex ((char)p))
\r
1055 int escape (int c)
\r
1089 v = getHex (-1, out error);
\r
1094 v = getHex (4, out error);
\r
1099 v = getHex (8, out error);
\r
1104 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1113 if (putback_char != -1){
\r
1114 int x = putback_char;
\r
1115 putback_char = -1;
\r
1119 return reader.Read ();
\r
1124 if (putback_char != -1)
\r
1125 return putback_char;
\r
1126 putback_char = reader.Read ();
\r
1127 return putback_char;
\r
1132 if (putback_char != -1)
\r
1133 return putback_char;
\r
1134 return reader.Peek ();
\r
1137 void putback (int c)
\r
1139 if (putback_char != -1){
\r
1140 Console.WriteLine ("Col: " + col);
\r
1141 Console.WriteLine ("Row: " + line);
\r
1142 Console.WriteLine ("Name: " + ref_name.Name);
\r
1143 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1144 throw new Exception ("This should not happen putback on putback");
\r
1149 public bool advance ()
\r
1151 return peekChar () != -1;
\r
1154 public Object Value {
\r
1160 public Object value ()
\r
1165 bool IsCastToken (int token)
\r
1170 case Token.IDENTIFIER:
\r
1171 case Token.LITERAL_INTEGER:
\r
1172 case Token.LITERAL_FLOAT:
\r
1173 case Token.LITERAL_DOUBLE:
\r
1174 case Token.LITERAL_DECIMAL:
\r
1175 case Token.LITERAL_CHARACTER:
\r
1176 case Token.LITERAL_STRING:
\r
1178 case Token.CHECKED:
\r
1183 case Token.SIZEOF:
\r
1187 case Token.TYPEOF:
\r
1188 case Token.UNCHECKED:
\r
1189 case Token.UNSAFE:
\r
1192 // These can be part of a member access
\r
1197 case Token.USHORT:
\r
1200 case Token.DOUBLE:
\r
1210 public int token ()
\r
1212 current_token = xtoken ();
\r
1213 return current_token;
\r
1216 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1218 void get_cmd_arg (out string cmd, out string arg)
\r
1222 tokens_seen = false;
\r
1224 static_cmd_arg.Length = 0;
\r
1226 while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1227 static_cmd_arg.Append ((char) c);
\r
1230 cmd = static_cmd_arg.ToString ();
\r
1236 } else if (c == '\r')
\r
1239 // skip over white space
\r
1240 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1247 } else if (c == '\r'){
\r
1252 static_cmd_arg.Length = 0;
\r
1253 static_cmd_arg.Append ((char) c);
\r
1255 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1256 static_cmd_arg.Append ((char) c);
\r
1262 } else if (c == '\r')
\r
1264 arg = static_cmd_arg.ToString ().Trim ();
\r
1268 // Handles the #line directive
\r
1270 bool PreProcessLine (string arg)
\r
1275 if (arg == "default"){
\r
1277 ref_name = file_name;
\r
1278 Location.Push (ref_name);
\r
1285 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1286 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1289 char [] quotes = { '\"' };
\r
1291 string name = arg.Substring (pos). Trim (quotes);
\r
1292 ref_name = Location.LookupFile (name);
\r
1293 Location.Push (ref_name);
\r
1295 ref_line = System.Int32.Parse (arg);
\r
1305 // Handles #define and #undef
\r
1307 void PreProcessDefinition (bool is_define, string arg)
\r
1309 if (arg == "" || arg == "true" || arg == "false"){
\r
1310 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1314 char[] whitespace = { ' ', '\t' };
\r
1315 if (arg.IndexOfAny (whitespace) != -1){
\r
1316 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1320 if (!is_identifier_start_character (arg [0]))
\r
1321 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1323 foreach (char c in arg.Substring (1)){
\r
1324 if (!is_identifier_part_character (c)){
\r
1325 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1331 if (defines == null)
\r
1332 defines = new Hashtable ();
\r
1335 if (defines == null)
\r
1337 if (defines.Contains (arg))
\r
1338 defines.Remove (arg);
\r
1342 bool eval_val (string s)
\r
1349 if (defines == null)
\r
1351 if (defines.Contains (s))
\r
1357 bool pp_primary (ref string s)
\r
1360 int len = s.Length;
\r
1366 s = s.Substring (1);
\r
1367 bool val = pp_expr (ref s);
\r
1368 if (s.Length > 0 && s [0] == ')'){
\r
1369 s = s.Substring (1);
\r
1372 Error_InvalidDirective ();
\r
1376 if (is_identifier_start_character (c)){
\r
1382 if (is_identifier_part_character (c)){
\r
1386 bool v = eval_val (s.Substring (0, j));
\r
1387 s = s.Substring (j);
\r
1390 bool vv = eval_val (s);
\r
1395 Error_InvalidDirective ();
\r
1399 bool pp_unary (ref string s)
\r
1402 int len = s.Length;
\r
1405 if (s [0] == '!'){
\r
1406 if (len > 1 && s [1] == '='){
\r
1407 Error_InvalidDirective ();
\r
1410 s = s.Substring (1);
\r
1411 return ! pp_primary (ref s);
\r
1413 return pp_primary (ref s);
\r
1415 Error_InvalidDirective ();
\r
1420 bool pp_eq (ref string s)
\r
1422 bool va = pp_unary (ref s);
\r
1425 int len = s.Length;
\r
1427 if (s [0] == '='){
\r
1428 if (len > 2 && s [1] == '='){
\r
1429 s = s.Substring (2);
\r
1430 return va == pp_unary (ref s);
\r
1432 Error_InvalidDirective ();
\r
1435 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1436 s = s.Substring (2);
\r
1438 return va != pp_unary (ref s);
\r
1447 bool pp_and (ref string s)
\r
1449 bool va = pp_eq (ref s);
\r
1452 int len = s.Length;
\r
1454 if (s [0] == '&'){
\r
1455 if (len > 2 && s [1] == '&'){
\r
1456 s = s.Substring (2);
\r
1457 return (va & pp_eq (ref s));
\r
1459 Error_InvalidDirective ();
\r
1468 // Evaluates an expression for `#if' or `#elif'
\r
1470 bool pp_expr (ref string s)
\r
1472 bool va = pp_and (ref s);
\r
1474 int len = s.Length;
\r
1479 if (len > 2 && s [1] == '|'){
\r
1480 s = s.Substring (2);
\r
1481 return va | pp_expr (ref s);
\r
1483 Error_InvalidDirective ();
\r
1492 bool eval (string s)
\r
1494 bool v = pp_expr (ref s);
\r
1496 if (s.Length != 0){
\r
1497 Error_InvalidDirective ();
\r
1504 void Error_InvalidDirective ()
\r
1506 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1509 void Error_UnexpectedDirective (string extra)
\r
1513 "Unexpected processor directive (" + extra + ")");
\r
1516 void Error_TokensSeen ()
\r
1520 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1524 // if true, then the code continues processing the code
\r
1525 // if false, the code stays in a loop until another directive is
\r
1528 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1531 bool region_directive = false;
\r
1533 get_cmd_arg (out cmd, out arg);
\r
1535 // Eat any trailing whitespaces and single-line comments
\r
1536 if (arg.IndexOf ("//") != -1)
\r
1537 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1538 arg = arg.TrimEnd (' ', '\t');
\r
1541 // The first group of pre-processing instructions is always processed
\r
1545 if (RootContext.V2)
\r
1550 if (!PreProcessLine (arg))
\r
1553 "Argument to #line directive is missing or invalid");
\r
1557 region_directive = true;
\r
1562 region_directive = true;
\r
1563 goto case "endif";
\r
1567 Error_InvalidDirective ();
\r
1570 bool taking = false;
\r
1571 if (ifstack == null)
\r
1572 ifstack = new Stack ();
\r
1574 if (ifstack.Count == 0){
\r
1577 int state = (int) ifstack.Peek ();
\r
1578 if ((state & TAKING) != 0)
\r
1582 if (eval (arg) && taking){
\r
1583 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1584 if (region_directive)
\r
1586 ifstack.Push (push);
\r
1589 int push = (taking ? PARENT_TAKING : 0);
\r
1590 if (region_directive)
\r
1592 ifstack.Push (push);
\r
1597 if (ifstack == null || ifstack.Count == 0){
\r
1598 Error_UnexpectedDirective ("no #if for this #endif");
\r
1601 int pop = (int) ifstack.Pop ();
\r
1603 if (region_directive && ((pop & REGION) == 0))
\r
1604 Report.Error (1027, Location, "#endif directive expected");
\r
1605 else if (!region_directive && ((pop & REGION) != 0))
\r
1606 Report.Error (1038, Location, "#endregion directive expected");
\r
1608 if (ifstack.Count == 0)
\r
1611 int state = (int) ifstack.Peek ();
\r
1613 if ((state & TAKING) != 0)
\r
1621 if (ifstack == null || ifstack.Count == 0){
\r
1622 Error_UnexpectedDirective ("no #if for this #elif");
\r
1625 int state = (int) ifstack.Peek ();
\r
1627 if ((state & REGION) != 0) {
\r
1628 Report.Error (1038, Location, "#endregion directive expected");
\r
1632 if ((state & ELSE_SEEN) != 0){
\r
1633 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1637 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1640 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1641 state = (int) ifstack.Pop ();
\r
1642 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1649 if (ifstack == null || ifstack.Count == 0){
\r
1652 "Unexpected processor directive (no #if for this #else)");
\r
1655 int state = (int) ifstack.Peek ();
\r
1657 if ((state & REGION) != 0) {
\r
1658 Report.Error (1038, Location, "#endregion directive expected");
\r
1662 if ((state & ELSE_SEEN) != 0){
\r
1663 Error_UnexpectedDirective ("#else within #else");
\r
1670 if ((state & TAKEN_BEFORE) == 0){
\r
1671 ret = ((state & PARENT_TAKING) != 0);
\r
1680 ifstack.Push (state | ELSE_SEEN);
\r
1687 // These are only processed if we are in a `taking' block
\r
1689 if (!caller_is_taking)
\r
1694 if (any_token_seen){
\r
1695 Error_TokensSeen ();
\r
1698 PreProcessDefinition (true, arg);
\r
1702 if (any_token_seen){
\r
1703 Error_TokensSeen ();
\r
1706 PreProcessDefinition (false, arg);
\r
1710 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1714 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1718 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1723 private int consume_string (bool quoted)
\r
1726 string_builder.Length = 0;
\r
1728 while ((c = getChar ()) != -1){
\r
1730 if (quoted && peekChar () == '"'){
\r
1731 string_builder.Append ((char) c);
\r
1735 val = string_builder.ToString ();
\r
1736 return Token.LITERAL_STRING;
\r
1742 Report.Error (1010, Location, "Newline in constant");
\r
1752 return Token.ERROR;
\r
1754 string_builder.Append ((char) c);
\r
1757 Report.Error (1039, Location, "Unterminated string literal");
\r
1761 private int consume_identifier (int s, bool quoted)
\r
1766 id_builder [0] = (char) s;
\r
1768 while ((c = reader.Read ()) != -1) {
\r
1769 if (is_identifier_part_character ((char) c)){
\r
1770 if (pos == max_id_size){
\r
1771 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1772 return Token.ERROR;
\r
1775 id_builder [pos++] = (char) c;
\r
1776 putback_char = -1;
\r
1785 // Optimization: avoids doing the keyword lookup
\r
1786 // on uppercase letters and _
\r
1789 int keyword = GetKeyword (id_builder, pos);
\r
1790 if (keyword != -1 && !quoted)
\r
1795 // Keep identifiers in an array of hashtables to avoid needless
\r
1799 if (identifiers [pos] != null) {
\r
1800 val = identifiers [pos][id_builder];
\r
1801 if (val != null) {
\r
1802 return Token.IDENTIFIER;
\r
1806 identifiers [pos] = new CharArrayHashtable (pos);
\r
1808 val = new String (id_builder, 0, pos);
\r
1810 char [] chars = new char [pos];
\r
1811 Array.Copy (id_builder, chars, pos);
\r
1813 identifiers [pos] [chars] = val;
\r
1815 return Token.IDENTIFIER;
\r
1818 public int xtoken ()
\r
1821 bool doread = false;
\r
1825 // optimization: eliminate col and implement #directive semantic correctly.
\r
1826 for (;(c = getChar ()) != -1; col++) {
\r
1827 if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r' || c == 0xa0){
\r
1830 col = (((col + 8) / 8) * 8) - 1;
\r
1834 // Handle double-slash comments.
\r
1836 int d = peekChar ();
\r
1840 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1847 any_token_seen |= tokens_seen;
\r
1848 tokens_seen = false;
\r
1850 } else if (d == '*'){
\r
1853 while ((d = getChar ()) != -1){
\r
1854 if (d == '*' && peekChar () == '/'){
\r
1863 any_token_seen |= tokens_seen;
\r
1864 tokens_seen = false;
\r
1869 goto is_punct_label;
\r
1873 if (is_identifier_start_character ((char)c)){
\r
1874 tokens_seen = true;
\r
1875 return consume_identifier (c, false);
\r
1879 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1880 tokens_seen = true;
\r
1893 any_token_seen |= tokens_seen;
\r
1894 tokens_seen = false;
\r
1898 if (c >= '0' && c <= '9'){
\r
1899 tokens_seen = true;
\r
1900 return is_number (c);
\r
1904 tokens_seen = true;
\r
1905 int peek = peekChar ();
\r
1906 if (peek >= '0' && peek <= '9')
\r
1907 return is_number (c);
\r
1911 /* For now, ignore pre-processor commands */
\r
1912 // FIXME: In C# the '#' is not limited to appear
\r
1913 // on the first column.
\r
1914 if (c == '#' && !tokens_seen){
\r
1919 cont = handle_preprocessing_directive (cont);
\r
1927 bool skipping = false;
\r
1928 for (;(c = getChar ()) != -1; col++){
\r
1934 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1936 else if (c != '#')
\r
1938 if (c == '#' && !skipping)
\r
1941 any_token_seen |= tokens_seen;
\r
1942 tokens_seen = false;
\r
1944 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1949 return consume_string (false);
\r
1953 tokens_seen = true;
\r
1955 error_details = "Empty character literal";
\r
1956 Report.Error (1011, Location, error_details);
\r
1957 return Token.ERROR;
\r
1961 return Token.ERROR;
\r
1962 val = new System.Char ();
\r
1967 error_details = "Too many characters in character literal";
\r
1968 Report.Error (1012, Location, error_details);
\r
1970 // Try to recover, read until newline or next "'"
\r
1971 while ((c = getChar ()) != -1){
\r
1972 if (c == '\n' || c == '\''){
\r
1981 return Token.ERROR;
\r
1983 return Token.LITERAL_CHARACTER;
\r
1989 tokens_seen = true;
\r
1990 return consume_string (true);
\r
1991 } else if (is_identifier_start_character ((char) c)){
\r
1992 return consume_identifier (c, true);
\r
1994 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1999 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
2000 "character on a line.";
\r
2002 Report.Error (1040, Location, error_details);
\r
2004 return Token.ERROR;
\r
2007 error_details = ((char)c).ToString ();
\r
2009 return Token.ERROR;
\r
2015 public void cleanup ()
\r
2017 if (ifstack != null && ifstack.Count >= 1) {
\r
2018 int state = (int) ifstack.Pop ();
\r
2019 if ((state & REGION) != 0)
\r
2020 Report.Error (1038, "#endregion directive expected");
\r
2022 Report.Error (1027, "#endif directive expected");
\r