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 ("abstract", Token.ABSTRACT);
\r
212 AddKeyword ("as", Token.AS);
\r
213 AddKeyword ("add", Token.ADD);
\r
214 AddKeyword ("assembly", Token.ASSEMBLY);
\r
215 AddKeyword ("base", Token.BASE);
\r
216 AddKeyword ("bool", Token.BOOL);
\r
217 AddKeyword ("break", Token.BREAK);
\r
218 AddKeyword ("byte", Token.BYTE);
\r
219 AddKeyword ("case", Token.CASE);
\r
220 AddKeyword ("catch", Token.CATCH);
\r
221 AddKeyword ("char", Token.CHAR);
\r
222 AddKeyword ("checked", Token.CHECKED);
\r
223 AddKeyword ("class", Token.CLASS);
\r
224 AddKeyword ("const", Token.CONST);
\r
225 AddKeyword ("continue", Token.CONTINUE);
\r
226 AddKeyword ("decimal", Token.DECIMAL);
\r
227 AddKeyword ("default", Token.DEFAULT);
\r
228 AddKeyword ("delegate", Token.DELEGATE);
\r
229 AddKeyword ("do", Token.DO);
\r
230 AddKeyword ("double", Token.DOUBLE);
\r
231 AddKeyword ("else", Token.ELSE);
\r
232 AddKeyword ("enum", Token.ENUM);
\r
233 AddKeyword ("event", Token.EVENT);
\r
234 AddKeyword ("explicit", Token.EXPLICIT);
\r
235 AddKeyword ("extern", Token.EXTERN);
\r
236 AddKeyword ("false", Token.FALSE);
\r
237 AddKeyword ("finally", Token.FINALLY);
\r
238 AddKeyword ("fixed", Token.FIXED);
\r
239 AddKeyword ("float", Token.FLOAT);
\r
240 AddKeyword ("for", Token.FOR);
\r
241 AddKeyword ("foreach", Token.FOREACH);
\r
242 AddKeyword ("goto", Token.GOTO);
\r
243 AddKeyword ("get", Token.GET);
\r
244 AddKeyword ("if", Token.IF);
\r
245 AddKeyword ("implicit", Token.IMPLICIT);
\r
246 AddKeyword ("in", Token.IN);
\r
247 AddKeyword ("int", Token.INT);
\r
248 AddKeyword ("interface", Token.INTERFACE);
\r
249 AddKeyword ("internal", Token.INTERNAL);
\r
250 AddKeyword ("is", Token.IS);
\r
251 AddKeyword ("lock", Token.LOCK);
\r
252 AddKeyword ("long", Token.LONG);
\r
253 AddKeyword ("namespace", Token.NAMESPACE);
\r
254 AddKeyword ("new", Token.NEW);
\r
255 AddKeyword ("null", Token.NULL);
\r
256 AddKeyword ("object", Token.OBJECT);
\r
257 AddKeyword ("operator", Token.OPERATOR);
\r
258 AddKeyword ("out", Token.OUT);
\r
259 AddKeyword ("override", Token.OVERRIDE);
\r
260 AddKeyword ("params", Token.PARAMS);
\r
261 AddKeyword ("private", Token.PRIVATE);
\r
262 AddKeyword ("protected", Token.PROTECTED);
\r
263 AddKeyword ("public", Token.PUBLIC);
\r
264 AddKeyword ("readonly", Token.READONLY);
\r
265 AddKeyword ("ref", Token.REF);
\r
266 AddKeyword ("remove", Token.REMOVE);
\r
267 AddKeyword ("return", Token.RETURN);
\r
268 AddKeyword ("sbyte", Token.SBYTE);
\r
269 AddKeyword ("sealed", Token.SEALED);
\r
270 AddKeyword ("set", Token.SET);
\r
271 AddKeyword ("short", Token.SHORT);
\r
272 AddKeyword ("sizeof", Token.SIZEOF);
\r
273 AddKeyword ("stackalloc", Token.STACKALLOC);
\r
274 AddKeyword ("static", Token.STATIC);
\r
275 AddKeyword ("string", Token.STRING);
\r
276 AddKeyword ("struct", Token.STRUCT);
\r
277 AddKeyword ("switch", Token.SWITCH);
\r
278 AddKeyword ("this", Token.THIS);
\r
279 AddKeyword ("throw", Token.THROW);
\r
280 AddKeyword ("true", Token.TRUE);
\r
281 AddKeyword ("try", Token.TRY);
\r
282 AddKeyword ("typeof", Token.TYPEOF);
\r
283 AddKeyword ("uint", Token.UINT);
\r
284 AddKeyword ("ulong", Token.ULONG);
\r
285 AddKeyword ("unchecked", Token.UNCHECKED);
\r
286 AddKeyword ("unsafe", Token.UNSAFE);
\r
287 AddKeyword ("ushort", Token.USHORT);
\r
288 AddKeyword ("using", Token.USING);
\r
289 AddKeyword ("virtual", Token.VIRTUAL);
\r
290 AddKeyword ("void", Token.VOID);
\r
291 AddKeyword ("volatile", Token.VOLATILE);
\r
292 AddKeyword ("while", Token.WHILE);
\r
296 // Class initializer
\r
298 static Tokenizer ()
\r
301 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
302 styles = NumberStyles.Float;
\r
304 string_builder = new System.Text.StringBuilder ();
\r
307 int GetKeyword (char[] id, int id_len)
\r
310 * Keywords are stored in an array of hashtables grouped by their
\r
314 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
\r
316 object o = keywords [id_len] [id];
\r
323 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
325 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
327 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
333 public Location Location {
\r
335 return new Location (ref_line);
\r
339 void define (string def)
\r
341 if (!RootContext.AllDefines.Contains (def)){
\r
342 RootContext.AllDefines [def] = true;
\r
344 if (defines.Contains (def))
\r
346 defines [def] = true;
\r
349 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
351 this.ref_name = file;
\r
352 this.file_name = file;
\r
358 defines = new Hashtable ();
\r
359 foreach (string def in defs)
\r
364 // FIXME: This could be `Location.Push' but we have to
\r
365 // find out why the MS compiler allows this
\r
367 Mono.CSharp.Location.Push (file);
\r
370 public static void Cleanup () {
\r
371 identifiers = null;
\r
374 static bool is_identifier_start_character (char c)
\r
376 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
379 static bool is_identifier_part_character (char c)
\r
381 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
384 public static bool IsValidIdentifier (string s)
\r
386 if (s == null || s.Length == 0)
\r
389 if (!is_identifier_start_character (s [0]))
\r
392 for (int i = 1; i < s.Length; i ++)
\r
393 if (! is_identifier_part_character (s [i]))
\r
399 int is_punct (char c, ref bool doread)
\r
408 return Token.OPEN_BRACE;
\r
410 return Token.CLOSE_BRACE;
\r
412 return Token.OPEN_BRACKET;
\r
414 return Token.CLOSE_BRACKET;
\r
416 return Token.OPEN_PARENS;
\r
418 if (deambiguate_close_parens == 0)
\r
419 return Token.CLOSE_PARENS;
\r
421 --deambiguate_close_parens;
\r
423 // Save current position and parse next token.
\r
424 int old = reader.Position;
\r
425 int new_token = token ();
\r
426 reader.Position = old;
\r
429 if (new_token == Token.OPEN_PARENS)
\r
430 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
431 else if (new_token == Token.MINUS)
\r
432 return Token.CLOSE_PARENS_MINUS;
\r
433 else if (IsCastToken (new_token))
\r
434 return Token.CLOSE_PARENS_CAST;
\r
436 return Token.CLOSE_PARENS_NO_CAST;
\r
440 return Token.COMMA;
\r
442 return Token.COLON;
\r
444 return Token.SEMICOLON;
\r
446 return Token.TILDE;
\r
448 return Token.INTERR;
\r
457 t = Token.OP_ADD_ASSIGN;
\r
467 t = Token.OP_SUB_ASSIGN;
\r
471 return Token.MINUS;
\r
479 return Token.OP_NE;
\r
487 return Token.OP_EQ;
\r
489 return Token.ASSIGN;
\r
495 return Token.OP_AND;
\r
496 } else if (d == '='){
\r
498 return Token.OP_AND_ASSIGN;
\r
500 return Token.BITWISE_AND;
\r
506 return Token.OP_OR;
\r
507 } else if (d == '='){
\r
509 return Token.OP_OR_ASSIGN;
\r
511 return Token.BITWISE_OR;
\r
517 return Token.OP_MULT_ASSIGN;
\r
525 return Token.OP_DIV_ASSIGN;
\r
533 return Token.OP_MOD_ASSIGN;
\r
535 return Token.PERCENT;
\r
541 return Token.OP_XOR_ASSIGN;
\r
543 return Token.CARRET;
\r
553 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
555 return Token.OP_SHIFT_LEFT;
\r
556 } else if (d == '='){
\r
558 return Token.OP_LE;
\r
560 return Token.OP_LT;
\r
570 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
572 return Token.OP_SHIFT_RIGHT;
\r
573 } else if (d == '='){
\r
575 return Token.OP_GE;
\r
577 return Token.OP_GT;
\r
579 return Token.ERROR;
\r
582 int deambiguate_close_parens = 0;
\r
584 public void Deambiguate_CloseParens ()
\r
587 deambiguate_close_parens++;
\r
590 void Error_NumericConstantTooLong ()
\r
592 Report.Error (1021, Location, "Numeric constant too long");
\r
595 bool decimal_digits (int c)
\r
598 bool seen_digits = false;
\r
601 if (number_pos == max_number_size)
\r
602 Error_NumericConstantTooLong ();
\r
603 number_builder [number_pos++] = (char) c;
\r
607 // We use peekChar2, because decimal_digits needs to do a
\r
608 // 2-character look-ahead (5.ToString for example).
\r
610 while ((d = peekChar2 ()) != -1){
\r
611 if (d >= '0' && d <= '9'){
\r
612 if (number_pos == max_number_size)
\r
613 Error_NumericConstantTooLong ();
\r
614 number_builder [number_pos++] = (char) d;
\r
616 seen_digits = true;
\r
621 return seen_digits;
\r
624 bool is_hex (int e)
\r
626 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
629 void hex_digits (int c)
\r
632 number_builder [number_pos++] = (char) c;
\r
636 int real_type_suffix (int c)
\r
641 case 'F': case 'f':
\r
642 t = Token.LITERAL_FLOAT;
\r
644 case 'D': case 'd':
\r
645 t = Token.LITERAL_DOUBLE;
\r
647 case 'M': case 'm':
\r
648 t= Token.LITERAL_DECIMAL;
\r
656 int integer_type_suffix (ulong ul, int c)
\r
658 bool is_unsigned = false;
\r
659 bool is_long = false;
\r
662 bool scanning = true;
\r
665 case 'U': case 'u':
\r
668 is_unsigned = true;
\r
675 // if we have not seen anything in between
\r
676 // report this error
\r
680 "the 'l' suffix is easily confused with digit `1'," +
\r
681 " use 'L' for clarity");
\r
697 } while (scanning);
\r
700 if (is_long && is_unsigned){
\r
702 return Token.LITERAL_INTEGER;
\r
703 } else if (is_unsigned){
\r
704 // uint if possible, or ulong else.
\r
706 if ((ul & 0xffffffff00000000) == 0)
\r
710 } else if (is_long){
\r
711 // long if possible, ulong otherwise
\r
712 if ((ul & 0x8000000000000000) != 0)
\r
717 // int, uint, long or ulong in that order
\r
718 if ((ul & 0xffffffff00000000) == 0){
\r
719 uint ui = (uint) ul;
\r
721 if ((ui & 0x80000000) != 0)
\r
726 if ((ul & 0x8000000000000000) != 0)
\r
732 return Token.LITERAL_INTEGER;
\r
736 // given `c' as the next char in the input decide whether
\r
737 // we need to convert to a special type, and then choose
\r
738 // the best representation for the integer
\r
740 int adjust_int (int c)
\r
743 if (number_pos > 9){
\r
744 ulong ul = (uint) (number_builder [0] - '0');
\r
746 for (int i = 1; i < number_pos; i++){
\r
747 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
749 return integer_type_suffix (ul, c);
\r
751 uint ui = (uint) (number_builder [0] - '0');
\r
753 for (int i = 1; i < number_pos; i++){
\r
754 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
756 return integer_type_suffix (ui, c);
\r
758 } catch (OverflowException) {
\r
759 error_details = "Integral constant is too large";
\r
760 Report.Error (1021, Location, error_details);
\r
762 return Token.LITERAL_INTEGER;
\r
766 int adjust_real (int t)
\r
768 string s = new String (number_builder, 0, number_pos);
\r
771 case Token.LITERAL_DECIMAL:
\r
773 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
774 } catch (OverflowException) {
\r
776 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
777 Report.Error (594, Location, error_details);
\r
780 case Token.LITERAL_FLOAT:
\r
782 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
783 } catch (OverflowException) {
\r
785 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
786 Report.Error (594, Location, error_details);
\r
790 case Token.LITERAL_DOUBLE:
\r
792 t = Token.LITERAL_DOUBLE;
\r
794 val = System.Double.Parse (s, styles, csharp_format_info);
\r
795 } catch (OverflowException) {
\r
797 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
798 Report.Error (594, Location, error_details);
\r
811 while ((d = peekChar ()) != -1){
\r
813 number_builder [number_pos++] = (char) d;
\r
819 string s = new String (number_builder, 0, number_pos);
\r
821 if (number_pos <= 8)
\r
822 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
824 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
825 } catch (OverflowException){
\r
826 error_details = "Integral constant is too large";
\r
827 Report.Error (1021, Location, error_details);
\r
829 return Token.LITERAL_INTEGER;
\r
832 return integer_type_suffix (ul, peekChar ());
\r
836 // Invoked if we know we have .digits or digits
\r
838 int is_number (int c)
\r
840 bool is_real = false;
\r
845 if (c >= '0' && c <= '9'){
\r
847 int peek = peekChar ();
\r
849 if (peek == 'x' || peek == 'X')
\r
850 return handle_hex ();
\r
852 decimal_digits (c);
\r
857 // We need to handle the case of
\r
858 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
861 if (decimal_digits ('.')){
\r
867 return adjust_int (-1);
\r
871 if (c == 'e' || c == 'E'){
\r
873 if (number_pos == max_number_size)
\r
874 Error_NumericConstantTooLong ();
\r
875 number_builder [number_pos++] = 'e';
\r
879 if (number_pos == max_number_size)
\r
880 Error_NumericConstantTooLong ();
\r
881 number_builder [number_pos++] = '+';
\r
883 } else if (c == '-') {
\r
884 if (number_pos == max_number_size)
\r
885 Error_NumericConstantTooLong ();
\r
886 number_builder [number_pos++] = '-';
\r
889 if (number_pos == max_number_size)
\r
890 Error_NumericConstantTooLong ();
\r
891 number_builder [number_pos++] = '+';
\r
894 decimal_digits (c);
\r
898 type = real_type_suffix (c);
\r
899 if (type == Token.NONE && !is_real){
\r
901 return adjust_int (c);
\r
905 if (type == Token.NONE){
\r
910 return adjust_real (type);
\r
912 Console.WriteLine ("This should not be reached");
\r
913 throw new Exception ("Is Number should never reach this point");
\r
917 // Accepts exactly count (4 or 8) hex, no more no less
\r
919 int getHex (int count, out bool error)
\r
924 int top = count != -1 ? count : 4;
\r
928 for (i = 0; i < top; i++){
\r
931 if (c >= '0' && c <= '9')
\r
932 c = (int) c - (int) '0';
\r
933 else if (c >= 'A' && c <= 'F')
\r
934 c = (int) c - (int) 'A' + 10;
\r
935 else if (c >= 'a' && c <= 'f')
\r
936 c = (int) c - (int) 'a' + 10;
\r
942 total = (total * 16) + c;
\r
944 int p = peekChar ();
\r
947 if (!is_hex ((char)p))
\r
988 v = getHex (-1, out error);
\r
993 v = getHex (4, out error);
\r
998 v = getHex (8, out error);
\r
1003 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1012 if (putback_char != -1){
\r
1013 int x = putback_char;
\r
1014 putback_char = -1;
\r
1018 return reader.Read ();
\r
1023 if (putback_char != -1)
\r
1024 return putback_char;
\r
1025 putback_char = reader.Read ();
\r
1026 return putback_char;
\r
1031 if (putback_char != -1)
\r
1032 return putback_char;
\r
1033 return reader.Peek ();
\r
1036 void putback (int c)
\r
1038 if (putback_char != -1){
\r
1039 Console.WriteLine ("Col: " + col);
\r
1040 Console.WriteLine ("Row: " + line);
\r
1041 Console.WriteLine ("Name: " + ref_name.Name);
\r
1042 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1043 throw new Exception ("This should not happen putback on putback");
\r
1048 public bool advance ()
\r
1050 return peekChar () != -1;
\r
1053 public Object Value {
\r
1059 public Object value ()
\r
1064 bool IsCastToken (int token)
\r
1069 case Token.IDENTIFIER:
\r
1070 case Token.LITERAL_INTEGER:
\r
1071 case Token.LITERAL_FLOAT:
\r
1072 case Token.LITERAL_DOUBLE:
\r
1073 case Token.LITERAL_DECIMAL:
\r
1074 case Token.LITERAL_CHARACTER:
\r
1075 case Token.LITERAL_STRING:
\r
1077 case Token.CHECKED:
\r
1082 case Token.SIZEOF:
\r
1086 case Token.TYPEOF:
\r
1087 case Token.UNCHECKED:
\r
1088 case Token.UNSAFE:
\r
1091 // These can be part of a member access
\r
1096 case Token.USHORT:
\r
1099 case Token.DOUBLE:
\r
1109 public int token ()
\r
1111 current_token = xtoken ();
\r
1112 return current_token;
\r
1115 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1117 void get_cmd_arg (out string cmd, out string arg)
\r
1121 tokens_seen = false;
\r
1123 static_cmd_arg.Length = 0;
\r
1125 // skip over white space
\r
1126 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1130 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1131 if (is_identifier_part_character ((char) c)){
\r
1132 static_cmd_arg.Append ((char) c);
\r
1140 cmd = static_cmd_arg.ToString ();
\r
1146 } else if (c == '\r')
\r
1149 // skip over white space
\r
1150 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1157 } else if (c == '\r'){
\r
1162 static_cmd_arg.Length = 0;
\r
1163 static_cmd_arg.Append ((char) c);
\r
1165 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1166 static_cmd_arg.Append ((char) c);
\r
1172 } else if (c == '\r')
\r
1174 arg = static_cmd_arg.ToString ().Trim ();
\r
1178 // Handles the #line directive
\r
1180 bool PreProcessLine (string arg)
\r
1185 if (arg == "default"){
\r
1187 ref_name = file_name;
\r
1188 Location.Push (ref_name);
\r
1190 } else if (arg == "hidden"){
\r
1192 // We ignore #line hidden
\r
1200 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1201 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1204 char [] quotes = { '\"' };
\r
1206 string name = arg.Substring (pos). Trim (quotes);
\r
1207 ref_name = Location.LookupFile (name);
\r
1208 file_name.HasLineDirective = true;
\r
1209 ref_name.HasLineDirective = true;
\r
1210 Location.Push (ref_name);
\r
1212 ref_line = System.Int32.Parse (arg);
\r
1222 // Handles #define and #undef
\r
1224 void PreProcessDefinition (bool is_define, string arg)
\r
1226 if (arg == "" || arg == "true" || arg == "false"){
\r
1227 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1231 char[] whitespace = { ' ', '\t' };
\r
1232 if (arg.IndexOfAny (whitespace) != -1){
\r
1233 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1237 if (!is_identifier_start_character (arg [0]))
\r
1238 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1240 foreach (char c in arg.Substring (1)){
\r
1241 if (!is_identifier_part_character (c)){
\r
1242 Report.Error (1001, Location, "Identifier expected: " + arg);
\r
1248 if (defines == null)
\r
1249 defines = new Hashtable ();
\r
1252 if (defines == null)
\r
1254 if (defines.Contains (arg))
\r
1255 defines.Remove (arg);
\r
1259 bool eval_val (string s)
\r
1266 if (defines == null)
\r
1268 if (defines.Contains (s))
\r
1274 bool pp_primary (ref string s)
\r
1277 int len = s.Length;
\r
1283 s = s.Substring (1);
\r
1284 bool val = pp_expr (ref s);
\r
1285 if (s.Length > 0 && s [0] == ')'){
\r
1286 s = s.Substring (1);
\r
1289 Error_InvalidDirective ();
\r
1293 if (is_identifier_start_character (c)){
\r
1299 if (is_identifier_part_character (c)){
\r
1303 bool v = eval_val (s.Substring (0, j));
\r
1304 s = s.Substring (j);
\r
1307 bool vv = eval_val (s);
\r
1312 Error_InvalidDirective ();
\r
1316 bool pp_unary (ref string s)
\r
1319 int len = s.Length;
\r
1322 if (s [0] == '!'){
\r
1323 if (len > 1 && s [1] == '='){
\r
1324 Error_InvalidDirective ();
\r
1327 s = s.Substring (1);
\r
1328 return ! pp_primary (ref s);
\r
1330 return pp_primary (ref s);
\r
1332 Error_InvalidDirective ();
\r
1337 bool pp_eq (ref string s)
\r
1339 bool va = pp_unary (ref s);
\r
1342 int len = s.Length;
\r
1344 if (s [0] == '='){
\r
1345 if (len > 2 && s [1] == '='){
\r
1346 s = s.Substring (2);
\r
1347 return va == pp_unary (ref s);
\r
1349 Error_InvalidDirective ();
\r
1352 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1353 s = s.Substring (2);
\r
1355 return va != pp_unary (ref s);
\r
1364 bool pp_and (ref string s)
\r
1366 bool va = pp_eq (ref s);
\r
1369 int len = s.Length;
\r
1371 if (s [0] == '&'){
\r
1372 if (len > 2 && s [1] == '&'){
\r
1373 s = s.Substring (2);
\r
1374 return (va & pp_eq (ref s));
\r
1376 Error_InvalidDirective ();
\r
1385 // Evaluates an expression for `#if' or `#elif'
\r
1387 bool pp_expr (ref string s)
\r
1389 bool va = pp_and (ref s);
\r
1391 int len = s.Length;
\r
1396 if (len > 2 && s [1] == '|'){
\r
1397 s = s.Substring (2);
\r
1398 return va | pp_expr (ref s);
\r
1400 Error_InvalidDirective ();
\r
1409 bool eval (string s)
\r
1411 bool v = pp_expr (ref s);
\r
1413 if (s.Length != 0){
\r
1414 Error_InvalidDirective ();
\r
1421 void Error_InvalidDirective ()
\r
1423 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1426 void Error_UnexpectedDirective (string extra)
\r
1430 "Unexpected processor directive (" + extra + ")");
\r
1433 void Error_TokensSeen ()
\r
1437 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1441 // if true, then the code continues processing the code
\r
1442 // if false, the code stays in a loop until another directive is
\r
1445 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1448 bool region_directive = false;
\r
1450 get_cmd_arg (out cmd, out arg);
\r
1452 // Eat any trailing whitespaces and single-line comments
\r
1453 if (arg.IndexOf ("//") != -1)
\r
1454 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1455 arg = arg.TrimEnd (' ', '\t');
\r
1458 // The first group of pre-processing instructions is always processed
\r
1462 if (RootContext.V2)
\r
1467 if (!PreProcessLine (arg))
\r
1470 "Argument to #line directive is missing or invalid");
\r
1474 region_directive = true;
\r
1479 region_directive = true;
\r
1480 goto case "endif";
\r
1484 Error_InvalidDirective ();
\r
1487 bool taking = false;
\r
1488 if (ifstack == null)
\r
1489 ifstack = new Stack ();
\r
1491 if (ifstack.Count == 0){
\r
1494 int state = (int) ifstack.Peek ();
\r
1495 if ((state & TAKING) != 0)
\r
1499 if (eval (arg) && taking){
\r
1500 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1501 if (region_directive)
\r
1503 ifstack.Push (push);
\r
1506 int push = (taking ? PARENT_TAKING : 0);
\r
1507 if (region_directive)
\r
1509 ifstack.Push (push);
\r
1514 if (ifstack == null || ifstack.Count == 0){
\r
1515 Error_UnexpectedDirective ("no #if for this #endif");
\r
1518 int pop = (int) ifstack.Pop ();
\r
1520 if (region_directive && ((pop & REGION) == 0))
\r
1521 Report.Error (1027, Location, "#endif directive expected");
\r
1522 else if (!region_directive && ((pop & REGION) != 0))
\r
1523 Report.Error (1038, Location, "#endregion directive expected");
\r
1525 if (ifstack.Count == 0)
\r
1528 int state = (int) ifstack.Peek ();
\r
1530 if ((state & TAKING) != 0)
\r
1538 if (ifstack == null || ifstack.Count == 0){
\r
1539 Error_UnexpectedDirective ("no #if for this #elif");
\r
1542 int state = (int) ifstack.Peek ();
\r
1544 if ((state & REGION) != 0) {
\r
1545 Report.Error (1038, Location, "#endregion directive expected");
\r
1549 if ((state & ELSE_SEEN) != 0){
\r
1550 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1554 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1557 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1558 state = (int) ifstack.Pop ();
\r
1559 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1566 if (ifstack == null || ifstack.Count == 0){
\r
1569 "Unexpected processor directive (no #if for this #else)");
\r
1572 int state = (int) ifstack.Peek ();
\r
1574 if ((state & REGION) != 0) {
\r
1575 Report.Error (1038, Location, "#endregion directive expected");
\r
1579 if ((state & ELSE_SEEN) != 0){
\r
1580 Error_UnexpectedDirective ("#else within #else");
\r
1587 if ((state & TAKEN_BEFORE) == 0){
\r
1588 ret = ((state & PARENT_TAKING) != 0);
\r
1597 ifstack.Push (state | ELSE_SEEN);
\r
1604 // These are only processed if we are in a `taking' block
\r
1606 if (!caller_is_taking)
\r
1611 if (any_token_seen){
\r
1612 Error_TokensSeen ();
\r
1615 PreProcessDefinition (true, arg);
\r
1619 if (any_token_seen){
\r
1620 Error_TokensSeen ();
\r
1623 PreProcessDefinition (false, arg);
\r
1627 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1631 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1635 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1640 private int consume_string (bool quoted)
\r
1643 string_builder.Length = 0;
\r
1645 while ((c = getChar ()) != -1){
\r
1647 if (quoted && peekChar () == '"'){
\r
1648 string_builder.Append ((char) c);
\r
1652 val = string_builder.ToString ();
\r
1653 return Token.LITERAL_STRING;
\r
1659 Report.Error (1010, Location, "Newline in constant");
\r
1669 return Token.ERROR;
\r
1671 string_builder.Append ((char) c);
\r
1674 Report.Error (1039, Location, "Unterminated string literal");
\r
1678 private int consume_identifier (int s, bool quoted)
\r
1683 id_builder [0] = (char) s;
\r
1685 while ((c = reader.Read ()) != -1) {
\r
1686 if (is_identifier_part_character ((char) c)){
\r
1687 if (pos == max_id_size){
\r
1688 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1689 return Token.ERROR;
\r
1692 id_builder [pos++] = (char) c;
\r
1693 putback_char = -1;
\r
1702 // Optimization: avoids doing the keyword lookup
\r
1703 // on uppercase letters and _
\r
1706 int keyword = GetKeyword (id_builder, pos);
\r
1707 if (keyword != -1 && !quoted)
\r
1712 // Keep identifiers in an array of hashtables to avoid needless
\r
1716 if (identifiers [pos] != null) {
\r
1717 val = identifiers [pos][id_builder];
\r
1718 if (val != null) {
\r
1719 if (val.Equals ("__arglist"))
\r
1720 return Token.ARGLIST;
\r
1721 return Token.IDENTIFIER;
\r
1725 identifiers [pos] = new CharArrayHashtable (pos);
\r
1727 val = new String (id_builder, 0, pos);
\r
1728 if (val.Equals ("__arglist"))
\r
1729 return Token.ARGLIST;
\r
1731 char [] chars = new char [pos];
\r
1732 Array.Copy (id_builder, chars, pos);
\r
1734 identifiers [pos] [chars] = val;
\r
1736 return Token.IDENTIFIER;
\r
1739 public int xtoken ()
\r
1742 bool doread = false;
\r
1746 // optimization: eliminate col and implement #directive semantic correctly.
\r
1747 for (;(c = getChar ()) != -1; col++) {
\r
1752 col = (((col + 8) / 8) * 8) - 1;
\r
1756 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
\r
1760 if (peekChar () == '\n')
\r
1766 any_token_seen |= tokens_seen;
\r
1767 tokens_seen = false;
\r
1771 // Handle double-slash comments.
\r
1773 int d = peekChar ();
\r
1777 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1784 any_token_seen |= tokens_seen;
\r
1785 tokens_seen = false;
\r
1787 } else if (d == '*'){
\r
1790 while ((d = getChar ()) != -1){
\r
1791 if (d == '*' && peekChar () == '/'){
\r
1800 any_token_seen |= tokens_seen;
\r
1801 tokens_seen = false;
\r
1806 goto is_punct_label;
\r
1810 if (is_identifier_start_character ((char)c)){
\r
1811 tokens_seen = true;
\r
1812 return consume_identifier (c, false);
\r
1816 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1817 tokens_seen = true;
\r
1830 any_token_seen |= tokens_seen;
\r
1831 tokens_seen = false;
\r
1835 if (c >= '0' && c <= '9'){
\r
1836 tokens_seen = true;
\r
1837 return is_number (c);
\r
1841 tokens_seen = true;
\r
1842 int peek = peekChar ();
\r
1843 if (peek >= '0' && peek <= '9')
\r
1844 return is_number (c);
\r
1848 /* For now, ignore pre-processor commands */
\r
1849 // FIXME: In C# the '#' is not limited to appear
\r
1850 // on the first column.
\r
1851 if (c == '#' && !tokens_seen){
\r
1856 cont = handle_preprocessing_directive (cont);
\r
1864 bool skipping = false;
\r
1865 for (;(c = getChar ()) != -1; col++){
\r
1871 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1873 else if (c != '#')
\r
1875 if (c == '#' && !skipping)
\r
1878 any_token_seen |= tokens_seen;
\r
1879 tokens_seen = false;
\r
1881 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1886 return consume_string (false);
\r
1890 tokens_seen = true;
\r
1892 error_details = "Empty character literal";
\r
1893 Report.Error (1011, Location, error_details);
\r
1894 return Token.ERROR;
\r
1898 return Token.ERROR;
\r
1899 val = new System.Char ();
\r
1904 error_details = "Too many characters in character literal";
\r
1905 Report.Error (1012, Location, error_details);
\r
1907 // Try to recover, read until newline or next "'"
\r
1908 while ((c = getChar ()) != -1){
\r
1909 if (c == '\n' || c == '\''){
\r
1918 return Token.ERROR;
\r
1920 return Token.LITERAL_CHARACTER;
\r
1926 tokens_seen = true;
\r
1927 return consume_string (true);
\r
1928 } else if (is_identifier_start_character ((char) c)){
\r
1929 return consume_identifier (c, true);
\r
1931 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1936 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
1937 "character on a line.";
\r
1939 Report.Error (1040, Location, error_details);
\r
1941 return Token.ERROR;
\r
1944 error_details = ((char)c).ToString ();
\r
1946 return Token.ERROR;
\r
1952 public void cleanup ()
\r
1954 if (ifstack != null && ifstack.Count >= 1) {
\r
1955 int state = (int) ifstack.Pop ();
\r
1956 if ((state & REGION) != 0)
\r
1957 Report.Error (1038, "#endregion directive expected");
\r
1959 Report.Error (1027, "#endif directive expected");
\r