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 Hashtable 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 const int max_number_size = 128;
\r
171 static char [] number_builder = new char [max_number_size];
\r
172 static int number_pos;
\r
175 // Details about the error encoutered by the tokenizer
\r
177 string error_details;
\r
179 public string error {
\r
181 return error_details;
\r
197 static void InitTokens ()
\r
199 keywords = new Hashtable ();
\r
201 keywords.Add ("abstract", Token.ABSTRACT);
\r
202 keywords.Add ("as", Token.AS);
\r
203 keywords.Add ("add", Token.ADD);
\r
204 keywords.Add ("assembly", Token.ASSEMBLY);
\r
205 keywords.Add ("base", Token.BASE);
\r
206 keywords.Add ("bool", Token.BOOL);
\r
207 keywords.Add ("break", Token.BREAK);
\r
208 keywords.Add ("byte", Token.BYTE);
\r
209 keywords.Add ("case", Token.CASE);
\r
210 keywords.Add ("catch", Token.CATCH);
\r
211 keywords.Add ("char", Token.CHAR);
\r
212 keywords.Add ("checked", Token.CHECKED);
\r
213 keywords.Add ("class", Token.CLASS);
\r
214 keywords.Add ("const", Token.CONST);
\r
215 keywords.Add ("continue", Token.CONTINUE);
\r
216 keywords.Add ("decimal", Token.DECIMAL);
\r
217 keywords.Add ("default", Token.DEFAULT);
\r
218 keywords.Add ("delegate", Token.DELEGATE);
\r
219 keywords.Add ("do", Token.DO);
\r
220 keywords.Add ("double", Token.DOUBLE);
\r
221 keywords.Add ("else", Token.ELSE);
\r
222 keywords.Add ("enum", Token.ENUM);
\r
223 keywords.Add ("event", Token.EVENT);
\r
224 keywords.Add ("explicit", Token.EXPLICIT);
\r
225 keywords.Add ("extern", Token.EXTERN);
\r
226 keywords.Add ("false", Token.FALSE);
\r
227 keywords.Add ("finally", Token.FINALLY);
\r
228 keywords.Add ("fixed", Token.FIXED);
\r
229 keywords.Add ("float", Token.FLOAT);
\r
230 keywords.Add ("for", Token.FOR);
\r
231 keywords.Add ("foreach", Token.FOREACH);
\r
232 keywords.Add ("goto", Token.GOTO);
\r
233 keywords.Add ("get", Token.GET);
\r
234 keywords.Add ("if", Token.IF);
\r
235 keywords.Add ("implicit", Token.IMPLICIT);
\r
236 keywords.Add ("in", Token.IN);
\r
237 keywords.Add ("int", Token.INT);
\r
238 keywords.Add ("interface", Token.INTERFACE);
\r
239 keywords.Add ("internal", Token.INTERNAL);
\r
240 keywords.Add ("is", Token.IS);
\r
241 keywords.Add ("lock", Token.LOCK);
\r
242 keywords.Add ("long", Token.LONG);
\r
243 keywords.Add ("namespace", Token.NAMESPACE);
\r
244 keywords.Add ("new", Token.NEW);
\r
245 keywords.Add ("null", Token.NULL);
\r
246 keywords.Add ("object", Token.OBJECT);
\r
247 keywords.Add ("operator", Token.OPERATOR);
\r
248 keywords.Add ("out", Token.OUT);
\r
249 keywords.Add ("override", Token.OVERRIDE);
\r
250 keywords.Add ("params", Token.PARAMS);
\r
251 keywords.Add ("private", Token.PRIVATE);
\r
252 keywords.Add ("protected", Token.PROTECTED);
\r
253 keywords.Add ("public", Token.PUBLIC);
\r
254 keywords.Add ("readonly", Token.READONLY);
\r
255 keywords.Add ("ref", Token.REF);
\r
256 keywords.Add ("remove", Token.REMOVE);
\r
257 keywords.Add ("return", Token.RETURN);
\r
258 keywords.Add ("sbyte", Token.SBYTE);
\r
259 keywords.Add ("sealed", Token.SEALED);
\r
260 keywords.Add ("set", Token.SET);
\r
261 keywords.Add ("short", Token.SHORT);
\r
262 keywords.Add ("sizeof", Token.SIZEOF);
\r
263 keywords.Add ("stackalloc", Token.STACKALLOC);
\r
264 keywords.Add ("static", Token.STATIC);
\r
265 keywords.Add ("string", Token.STRING);
\r
266 keywords.Add ("struct", Token.STRUCT);
\r
267 keywords.Add ("switch", Token.SWITCH);
\r
268 keywords.Add ("this", Token.THIS);
\r
269 keywords.Add ("throw", Token.THROW);
\r
270 keywords.Add ("true", Token.TRUE);
\r
271 keywords.Add ("try", Token.TRY);
\r
272 keywords.Add ("typeof", Token.TYPEOF);
\r
273 keywords.Add ("uint", Token.UINT);
\r
274 keywords.Add ("ulong", Token.ULONG);
\r
275 keywords.Add ("unchecked", Token.UNCHECKED);
\r
276 keywords.Add ("unsafe", Token.UNSAFE);
\r
277 keywords.Add ("ushort", Token.USHORT);
\r
278 keywords.Add ("using", Token.USING);
\r
279 keywords.Add ("virtual", Token.VIRTUAL);
\r
280 keywords.Add ("void", Token.VOID);
\r
281 keywords.Add ("volatile", Token.VOLATILE);
\r
282 keywords.Add ("where", Token.WHERE);
\r
283 keywords.Add ("while", Token.WHILE);
\r
285 if (RootContext.V2){
\r
286 keywords.Add ("__yield", Token.YIELD);
\r
287 keywords.Add ("yield", Token.YIELD);
\r
292 // Class initializer
\r
294 static Tokenizer ()
\r
297 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
298 styles = NumberStyles.Float;
\r
300 string_builder = new System.Text.StringBuilder ();
\r
303 int GetKeyword (string name)
\r
305 object o = keywords [name];
\r
312 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
314 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
316 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
322 public Location Location {
\r
324 return new Location (ref_line);
\r
328 void define (string def)
\r
330 if (!RootContext.AllDefines.Contains (def)){
\r
331 RootContext.AllDefines [def] = true;
\r
333 if (defines.Contains (def))
\r
335 defines [def] = true;
\r
338 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
\r
340 this.ref_name = file;
\r
341 this.file_name = file;
\r
347 defines = new Hashtable ();
\r
348 foreach (string def in defs)
\r
353 // FIXME: This could be `Location.Push' but we have to
\r
354 // find out why the MS compiler allows this
\r
356 Mono.CSharp.Location.Push (file);
\r
359 bool is_identifier_start_character (char c)
\r
361 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
364 bool is_identifier_part_character (char c)
\r
366 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
369 bool parse_less_than ()
\r
372 int the_token = token ();
\r
373 switch (the_token) {
\r
374 case Token.IDENTIFIER:
\r
378 case Token.DECIMAL:
\r
397 the_token = token ();
\r
399 if (the_token == Token.OP_GENERICS_GT)
\r
401 else if (the_token == Token.COMMA)
\r
403 else if (the_token == Token.OP_GENERICS_LT) {
\r
404 if (!parse_less_than ())
\r
412 bool parsing_less_than = false;
\r
413 int parsing_generic_less_than = 0;
\r
415 int is_punct (char c, ref bool doread)
\r
424 return Token.OPEN_BRACE;
\r
426 return Token.CLOSE_BRACE;
\r
428 return Token.OPEN_BRACKET;
\r
430 return Token.CLOSE_BRACKET;
\r
432 return Token.OPEN_PARENS;
\r
434 if (deambiguate_close_parens == 0)
\r
435 return Token.CLOSE_PARENS;
\r
437 --deambiguate_close_parens;
\r
439 // Save current position and parse next token.
\r
440 int old = reader.Position;
\r
441 int new_token = token ();
\r
442 reader.Position = old;
\r
445 if (new_token == Token.OPEN_PARENS)
\r
446 return Token.CLOSE_PARENS_OPEN_PARENS;
\r
447 else if (new_token == Token.MINUS)
\r
448 return Token.CLOSE_PARENS_MINUS;
\r
449 else if (IsCastToken (new_token))
\r
450 return Token.CLOSE_PARENS_CAST;
\r
452 return Token.CLOSE_PARENS_NO_CAST;
\r
456 return Token.COMMA;
\r
458 return Token.COLON;
\r
460 return Token.SEMICOLON;
\r
462 return Token.TILDE;
\r
464 return Token.INTERR;
\r
468 if (parsing_generic_less_than++ > 0)
\r
469 return Token.OP_GENERICS_LT;
\r
471 // Save current position and parse next token.
\r
472 int old = reader.Position;
\r
473 bool is_generic_lt = parse_less_than ();
\r
474 reader.Position = old;
\r
477 if (is_generic_lt) {
\r
478 parsing_generic_less_than++;
\r
479 return Token.OP_GENERICS_LT;
\r
481 parsing_generic_less_than = 0;
\r
490 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
492 return Token.OP_SHIFT_LEFT;
\r
493 } else if (d == '='){
\r
495 return Token.OP_LE;
\r
497 return Token.OP_LT;
\r
498 } else if (c == '>') {
\r
499 if (parsing_generic_less_than > 0) {
\r
500 parsing_generic_less_than--;
\r
501 return Token.OP_GENERICS_GT;
\r
511 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
513 return Token.OP_SHIFT_RIGHT;
\r
514 } else if (d == '='){
\r
516 return Token.OP_GE;
\r
518 return Token.OP_GT;
\r
527 t = Token.OP_ADD_ASSIGN;
\r
537 t = Token.OP_SUB_ASSIGN;
\r
541 return Token.MINUS;
\r
549 return Token.OP_NE;
\r
557 return Token.OP_EQ;
\r
559 return Token.ASSIGN;
\r
565 return Token.OP_AND;
\r
566 } else if (d == '='){
\r
568 return Token.OP_AND_ASSIGN;
\r
570 return Token.BITWISE_AND;
\r
576 return Token.OP_OR;
\r
577 } else if (d == '='){
\r
579 return Token.OP_OR_ASSIGN;
\r
581 return Token.BITWISE_OR;
\r
587 return Token.OP_MULT_ASSIGN;
\r
595 return Token.OP_DIV_ASSIGN;
\r
603 return Token.OP_MOD_ASSIGN;
\r
605 return Token.PERCENT;
\r
611 return Token.OP_XOR_ASSIGN;
\r
613 return Token.CARRET;
\r
618 if (deambiguate_greater_than == 0)
\r
619 return Token.OP_GT;
\r
621 --deambiguate_greater_than;
\r
623 // Save current position and parse next token.
\r
624 int old = reader.Position;
\r
625 int new_token = token ();
\r
626 reader.Position = old;
\r
629 switch (new_token) {
\r
630 case Token.OPEN_PARENS:
\r
631 case Token.CLOSE_PARENS:
\r
632 case Token.CLOSE_BRACKET:
\r
635 case Token.SEMICOLON:
\r
639 return Token.OP_GENERICS_GT;
\r
642 return Token.OP_GT;
\r
647 return Token.ERROR;
\r
650 int deambiguate_close_parens = 0;
\r
652 public void Deambiguate_CloseParens ()
\r
655 deambiguate_close_parens++;
\r
658 void Error_NumericConstantTooLong ()
\r
660 Report.Error (1021, Location, "Numeric constant too long");
\r
663 bool decimal_digits (int c)
\r
666 bool seen_digits = false;
\r
669 if (number_pos == max_number_size)
\r
670 Error_NumericConstantTooLong ();
\r
671 number_builder [number_pos++] = (char) c;
\r
675 // We use peekChar2, because decimal_digits needs to do a
\r
676 // 2-character look-ahead (5.ToString for example).
\r
678 while ((d = peekChar2 ()) != -1){
\r
679 if (d >= '0' && d <= '9'){
\r
680 if (number_pos == max_number_size)
\r
681 Error_NumericConstantTooLong ();
\r
682 number_builder [number_pos++] = (char) d;
\r
684 seen_digits = true;
\r
689 return seen_digits;
\r
692 bool is_hex (int e)
\r
694 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
697 void hex_digits (int c)
\r
700 number_builder [number_pos++] = (char) c;
\r
704 int real_type_suffix (int c)
\r
709 case 'F': case 'f':
\r
710 t = Token.LITERAL_FLOAT;
\r
712 case 'D': case 'd':
\r
713 t = Token.LITERAL_DOUBLE;
\r
715 case 'M': case 'm':
\r
716 t= Token.LITERAL_DECIMAL;
\r
724 int integer_type_suffix (ulong ul, int c)
\r
726 bool is_unsigned = false;
\r
727 bool is_long = false;
\r
730 bool scanning = true;
\r
733 case 'U': case 'u':
\r
736 is_unsigned = true;
\r
743 // if we have not seen anything in between
\r
744 // report this error
\r
748 "the 'l' suffix is easily confused with digit `1'," +
\r
749 " use 'L' for clarity");
\r
765 } while (scanning);
\r
768 if (is_long && is_unsigned){
\r
770 return Token.LITERAL_INTEGER;
\r
771 } else if (is_unsigned){
\r
772 // uint if possible, or ulong else.
\r
774 if ((ul & 0xffffffff00000000) == 0)
\r
778 } else if (is_long){
\r
779 // long if possible, ulong otherwise
\r
780 if ((ul & 0x8000000000000000) != 0)
\r
785 // int, uint, long or ulong in that order
\r
786 if ((ul & 0xffffffff00000000) == 0){
\r
787 uint ui = (uint) ul;
\r
789 if ((ui & 0x80000000) != 0)
\r
794 if ((ul & 0x8000000000000000) != 0)
\r
800 return Token.LITERAL_INTEGER;
\r
804 // given `c' as the next char in the input decide whether
\r
805 // we need to convert to a special type, and then choose
\r
806 // the best representation for the integer
\r
808 int adjust_int (int c)
\r
811 if (number_pos > 9){
\r
812 ulong ul = (uint) (number_builder [0] - '0');
\r
814 for (int i = 1; i < number_pos; i++){
\r
815 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
817 return integer_type_suffix (ul, c);
\r
819 uint ui = (uint) (number_builder [0] - '0');
\r
821 for (int i = 1; i < number_pos; i++){
\r
822 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
824 return integer_type_suffix (ui, c);
\r
826 } catch (OverflowException) {
\r
827 error_details = "Integral constant is too large";
\r
828 Report.Error (1021, Location, error_details);
\r
830 return Token.LITERAL_INTEGER;
\r
834 int adjust_real (int t)
\r
836 string s = new String (number_builder, 0, number_pos);
\r
839 case Token.LITERAL_DECIMAL:
\r
841 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
842 } catch (OverflowException) {
\r
844 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
845 Report.Error (594, Location, error_details);
\r
848 case Token.LITERAL_FLOAT:
\r
850 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
851 } catch (OverflowException) {
\r
853 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
854 Report.Error (594, Location, error_details);
\r
858 case Token.LITERAL_DOUBLE:
\r
860 t = Token.LITERAL_DOUBLE;
\r
862 val = System.Double.Parse (s, styles, csharp_format_info);
\r
863 } catch (OverflowException) {
\r
865 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
866 Report.Error (594, Location, error_details);
\r
879 while ((d = peekChar ()) != -1){
\r
881 if (number_pos == 16){
\r
882 Report.Error (1021, Location, "Integral constant too large");
\r
883 return Token.ERROR;
\r
885 number_builder [number_pos++] = (char) d;
\r
891 string s = new String (number_builder, 0, number_pos);
\r
892 if (number_pos <= 8)
\r
893 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
895 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
896 return integer_type_suffix (ul, peekChar ());
\r
900 // Invoked if we know we have .digits or digits
\r
902 int is_number (int c)
\r
904 bool is_real = false;
\r
909 if (c >= '0' && c <= '9'){
\r
911 int peek = peekChar ();
\r
913 if (peek == 'x' || peek == 'X')
\r
914 return handle_hex ();
\r
916 decimal_digits (c);
\r
921 // We need to handle the case of
\r
922 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
925 if (decimal_digits ('.')){
\r
931 return adjust_int (-1);
\r
935 if (c == 'e' || c == 'E'){
\r
937 if (number_pos == max_number_size)
\r
938 Error_NumericConstantTooLong ();
\r
939 number_builder [number_pos++] = 'e';
\r
943 if (number_pos == max_number_size)
\r
944 Error_NumericConstantTooLong ();
\r
945 number_builder [number_pos++] = '+';
\r
947 } else if (c == '-') {
\r
948 if (number_pos == max_number_size)
\r
949 Error_NumericConstantTooLong ();
\r
950 number_builder [number_pos++] = '-';
\r
953 if (number_pos == max_number_size)
\r
954 Error_NumericConstantTooLong ();
\r
955 number_builder [number_pos++] = '+';
\r
958 decimal_digits (c);
\r
962 type = real_type_suffix (c);
\r
963 if (type == Token.NONE && !is_real){
\r
965 return adjust_int (c);
\r
969 if (type == Token.NONE){
\r
974 return adjust_real (type);
\r
976 Console.WriteLine ("This should not be reached");
\r
977 throw new Exception ("Is Number should never reach this point");
\r
981 // Accepts exactly count (4 or 8) hex, no more no less
\r
983 int getHex (int count, out bool error)
\r
985 int [] buffer = new int [8];
\r
989 int top = count != -1 ? count : 4;
\r
993 for (i = 0; i < top; i++){
\r
996 if (c >= '0' && c <= '9')
\r
997 c = (int) c - (int) '0';
\r
998 else if (c >= 'A' && c <= 'F')
\r
999 c = (int) c - (int) 'A' + 10;
\r
1000 else if (c >= 'a' && c <= 'f')
\r
1001 c = (int) c - (int) 'a' + 10;
\r
1007 total = (total * 16) + c;
\r
1009 int p = peekChar ();
\r
1012 if (!is_hex ((char)p))
\r
1019 int escape (int c)
\r
1053 v = getHex (-1, out error);
\r
1058 v = getHex (4, out error);
\r
1063 v = getHex (8, out error);
\r
1068 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
1077 if (putback_char != -1){
\r
1078 int x = putback_char;
\r
1079 putback_char = -1;
\r
1083 return reader.Read ();
\r
1088 if (putback_char != -1)
\r
1089 return putback_char;
\r
1090 putback_char = reader.Read ();
\r
1091 return putback_char;
\r
1096 if (putback_char != -1)
\r
1097 return putback_char;
\r
1098 return reader.Peek ();
\r
1101 void putback (int c)
\r
1103 if (putback_char != -1){
\r
1104 Console.WriteLine ("Col: " + col);
\r
1105 Console.WriteLine ("Row: " + line);
\r
1106 Console.WriteLine ("Name: " + ref_name.Name);
\r
1107 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
1108 throw new Exception ("This should not happen putback on putback");
\r
1113 public bool advance ()
\r
1115 return peekChar () != -1;
\r
1118 public Object Value {
\r
1124 public Object value ()
\r
1129 bool IsCastToken (int token)
\r
1134 case Token.IDENTIFIER:
\r
1135 case Token.LITERAL_INTEGER:
\r
1136 case Token.LITERAL_FLOAT:
\r
1137 case Token.LITERAL_DOUBLE:
\r
1138 case Token.LITERAL_DECIMAL:
\r
1139 case Token.LITERAL_CHARACTER:
\r
1140 case Token.LITERAL_STRING:
\r
1142 case Token.CHECKED:
\r
1147 case Token.SIZEOF:
\r
1151 case Token.TYPEOF:
\r
1152 case Token.UNCHECKED:
\r
1153 case Token.UNSAFE:
\r
1161 public int token ()
\r
1163 current_token = xtoken ();
\r
1164 return current_token;
\r
1167 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1169 void get_cmd_arg (out string cmd, out string arg)
\r
1173 tokens_seen = false;
\r
1175 static_cmd_arg.Length = 0;
\r
1177 while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1178 static_cmd_arg.Append ((char) c);
\r
1181 cmd = static_cmd_arg.ToString ();
\r
1187 } else if (c == '\r')
\r
1190 // skip over white space
\r
1191 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1198 } else if (c == '\r'){
\r
1203 static_cmd_arg.Length = 0;
\r
1204 static_cmd_arg.Append ((char) c);
\r
1206 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1207 static_cmd_arg.Append ((char) c);
\r
1213 } else if (c == '\r')
\r
1215 arg = static_cmd_arg.ToString ().Trim ();
\r
1219 // Handles the #line directive
\r
1221 bool PreProcessLine (string arg)
\r
1226 if (arg == "default"){
\r
1228 ref_name = file_name;
\r
1229 Location.Push (ref_name);
\r
1236 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1237 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1240 char [] quotes = { '\"' };
\r
1242 string name = arg.Substring (pos). Trim (quotes);
\r
1243 ref_name = Location.LookupFile (name);
\r
1244 Location.Push (ref_name);
\r
1246 ref_line = System.Int32.Parse (arg);
\r
1256 // Handles #define and #undef
\r
1258 void PreProcessDefinition (bool is_define, string arg)
\r
1260 if (arg == "" || arg == "true" || arg == "false"){
\r
1261 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1265 char[] whitespace = { ' ', '\t' };
\r
1266 if (arg.IndexOfAny (whitespace) != -1){
\r
1267 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1271 foreach (char c in arg){
\r
1272 if (!Char.IsLetter (c) && (c != '_')){
\r
1273 Report.Error (1001, Location, "Identifier expected");
\r
1279 if (defines == null)
\r
1280 defines = new Hashtable ();
\r
1283 if (defines == null)
\r
1285 if (defines.Contains (arg))
\r
1286 defines.Remove (arg);
\r
1290 bool eval_val (string s)
\r
1297 if (defines == null)
\r
1299 if (defines.Contains (s))
\r
1305 bool pp_primary (ref string s)
\r
1308 int len = s.Length;
\r
1314 s = s.Substring (1);
\r
1315 bool val = pp_expr (ref s);
\r
1316 if (s.Length > 0 && s [0] == ')'){
\r
1317 s = s.Substring (1);
\r
1320 Error_InvalidDirective ();
\r
1324 if (is_identifier_start_character (c)){
\r
1330 if (is_identifier_part_character (c)){
\r
1334 bool v = eval_val (s.Substring (0, j));
\r
1335 s = s.Substring (j);
\r
1338 bool vv = eval_val (s);
\r
1343 Error_InvalidDirective ();
\r
1347 bool pp_unary (ref string s)
\r
1350 int len = s.Length;
\r
1353 if (s [0] == '!'){
\r
1354 if (len > 1 && s [1] == '='){
\r
1355 Error_InvalidDirective ();
\r
1358 s = s.Substring (1);
\r
1359 return ! pp_primary (ref s);
\r
1361 return pp_primary (ref s);
\r
1363 Error_InvalidDirective ();
\r
1368 bool pp_eq (ref string s)
\r
1370 bool va = pp_unary (ref s);
\r
1373 int len = s.Length;
\r
1375 if (s [0] == '='){
\r
1376 if (len > 2 && s [1] == '='){
\r
1377 s = s.Substring (2);
\r
1378 return va == pp_unary (ref s);
\r
1380 Error_InvalidDirective ();
\r
1383 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1384 s = s.Substring (2);
\r
1386 return va != pp_unary (ref s);
\r
1395 bool pp_and (ref string s)
\r
1397 bool va = pp_eq (ref s);
\r
1400 int len = s.Length;
\r
1402 if (s [0] == '&'){
\r
1403 if (len > 2 && s [1] == '&'){
\r
1404 s = s.Substring (2);
\r
1405 return (va & pp_eq (ref s));
\r
1407 Error_InvalidDirective ();
\r
1416 // Evaluates an expression for `#if' or `#elif'
\r
1418 bool pp_expr (ref string s)
\r
1420 bool va = pp_and (ref s);
\r
1422 int len = s.Length;
\r
1427 if (len > 2 && s [1] == '|'){
\r
1428 s = s.Substring (2);
\r
1429 return va | pp_expr (ref s);
\r
1431 Error_InvalidDirective ();
\r
1440 bool eval (string s)
\r
1442 bool v = pp_expr (ref s);
\r
1444 if (s.Length != 0){
\r
1445 Error_InvalidDirective ();
\r
1452 void Error_InvalidDirective ()
\r
1454 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1457 void Error_UnexpectedDirective (string extra)
\r
1461 "Unexpected processor directive (" + extra + ")");
\r
1464 void Error_TokensSeen ()
\r
1468 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1472 // if true, then the code continues processing the code
\r
1473 // if false, the code stays in a loop until another directive is
\r
1476 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1478 char [] blank = { ' ', '\t' };
\r
1480 bool region_directive = false;
\r
1482 get_cmd_arg (out cmd, out arg);
\r
1484 // Eat any trailing whitespaces and single-line comments
\r
1485 if (arg.IndexOf ("//") != -1)
\r
1486 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1487 arg = arg.TrimEnd (' ', '\t');
\r
1490 // The first group of pre-processing instructions is always processed
\r
1494 if (RootContext.V2)
\r
1499 if (!PreProcessLine (arg))
\r
1502 "Argument to #line directive is missing or invalid");
\r
1506 region_directive = true;
\r
1511 region_directive = true;
\r
1512 goto case "endif";
\r
1516 Error_InvalidDirective ();
\r
1519 bool taking = false;
\r
1520 if (ifstack == null)
\r
1521 ifstack = new Stack ();
\r
1523 if (ifstack.Count == 0){
\r
1526 int state = (int) ifstack.Peek ();
\r
1527 if ((state & TAKING) != 0)
\r
1531 if (eval (arg) && taking){
\r
1532 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1533 if (region_directive)
\r
1535 ifstack.Push (push);
\r
1538 int push = (taking ? PARENT_TAKING : 0);
\r
1539 if (region_directive)
\r
1541 ifstack.Push (push);
\r
1546 if (ifstack == null || ifstack.Count == 0){
\r
1547 Error_UnexpectedDirective ("no #if for this #endif");
\r
1550 int pop = (int) ifstack.Pop ();
\r
1552 if (region_directive && ((pop & REGION) == 0))
\r
1553 Report.Error (1027, Location, "#endif directive expected");
\r
1554 else if (!region_directive && ((pop & REGION) != 0))
\r
1555 Report.Error (1038, Location, "#endregion directive expected");
\r
1557 if (ifstack.Count == 0)
\r
1560 int state = (int) ifstack.Peek ();
\r
1562 if ((state & TAKING) != 0)
\r
1570 if (ifstack == null || ifstack.Count == 0){
\r
1571 Error_UnexpectedDirective ("no #if for this #elif");
\r
1574 int state = (int) ifstack.Peek ();
\r
1576 if ((state & REGION) != 0) {
\r
1577 Report.Error (1038, Location, "#endregion directive expected");
\r
1581 if ((state & ELSE_SEEN) != 0){
\r
1582 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1586 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1589 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1590 state = (int) ifstack.Pop ();
\r
1591 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1598 if (ifstack == null || ifstack.Count == 0){
\r
1601 "Unexpected processor directive (no #if for this #else)");
\r
1604 int state = (int) ifstack.Peek ();
\r
1606 if ((state & REGION) != 0) {
\r
1607 Report.Error (1038, Location, "#endregion directive expected");
\r
1611 if ((state & ELSE_SEEN) != 0){
\r
1612 Error_UnexpectedDirective ("#else within #else");
\r
1619 if ((state & TAKEN_BEFORE) == 0){
\r
1620 ret = ((state & PARENT_TAKING) != 0);
\r
1629 ifstack.Push (state | ELSE_SEEN);
\r
1636 // These are only processed if we are in a `taking' block
\r
1638 if (!caller_is_taking)
\r
1643 if (any_token_seen){
\r
1644 Error_TokensSeen ();
\r
1647 PreProcessDefinition (true, arg);
\r
1651 if (any_token_seen){
\r
1652 Error_TokensSeen ();
\r
1655 PreProcessDefinition (false, arg);
\r
1659 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1663 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1667 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1672 private int consume_string (bool quoted)
\r
1675 string_builder.Length = 0;
\r
1677 while ((c = getChar ()) != -1){
\r
1679 if (quoted && peekChar () == '"'){
\r
1680 string_builder.Append ((char) c);
\r
1684 val = string_builder.ToString ();
\r
1685 return Token.LITERAL_STRING;
\r
1691 Report.Error (1010, Location, "Newline in constant");
\r
1701 return Token.ERROR;
\r
1703 string_builder.Append ((char) c);
\r
1706 Report.Error (1039, Location, "Unterminated string literal");
\r
1710 private int consume_identifier (int s, bool quoted)
\r
1715 id_builder [0] = (char) s;
\r
1717 while ((c = reader.Read ()) != -1) {
\r
1718 if (is_identifier_part_character ((char) c)){
\r
1719 if (pos == max_id_size){
\r
1720 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1721 return Token.ERROR;
\r
1724 id_builder [pos++] = (char) c;
\r
1725 putback_char = -1;
\r
1733 string ids = new String (id_builder, 0, pos);
\r
1736 // Optimization: avoids doing the keyword lookup
\r
1737 // on uppercase letters and _
\r
1740 int keyword = GetKeyword (ids);
\r
1741 if (keyword == -1 || quoted){
\r
1743 return Token.IDENTIFIER;
\r
1748 return Token.IDENTIFIER;
\r
1751 public int xtoken ()
\r
1754 bool doread = false;
\r
1758 // optimization: eliminate col and implement #directive semantic correctly.
\r
1759 for (;(c = getChar ()) != -1; col++) {
\r
1760 if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r' || c == 0xa0){
\r
1763 col = (((col + 8) / 8) * 8) - 1;
\r
1767 // Handle double-slash comments.
\r
1769 int d = peekChar ();
\r
1773 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1780 any_token_seen |= tokens_seen;
\r
1781 tokens_seen = false;
\r
1783 } else if (d == '*'){
\r
1786 while ((d = getChar ()) != -1){
\r
1787 if (d == '*' && peekChar () == '/'){
\r
1796 any_token_seen |= tokens_seen;
\r
1797 tokens_seen = false;
\r
1802 goto is_punct_label;
\r
1806 if (is_identifier_start_character ((char)c)){
\r
1807 tokens_seen = true;
\r
1808 return consume_identifier (c, false);
\r
1812 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1813 tokens_seen = true;
\r
1826 any_token_seen |= tokens_seen;
\r
1827 tokens_seen = false;
\r
1831 if (c >= '0' && c <= '9'){
\r
1832 tokens_seen = true;
\r
1833 return is_number (c);
\r
1837 tokens_seen = true;
\r
1838 int peek = peekChar ();
\r
1839 if (peek >= '0' && peek <= '9')
\r
1840 return is_number (c);
\r
1844 /* For now, ignore pre-processor commands */
\r
1845 // FIXME: In C# the '#' is not limited to appear
\r
1846 // on the first column.
\r
1847 if (c == '#' && !tokens_seen){
\r
1852 cont = handle_preprocessing_directive (cont);
\r
1860 bool skipping = false;
\r
1861 for (;(c = getChar ()) != -1; col++){
\r
1867 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1869 else if (c != '#')
\r
1871 if (c == '#' && !skipping)
\r
1874 any_token_seen |= tokens_seen;
\r
1875 tokens_seen = false;
\r
1877 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1882 return consume_string (false);
\r
1886 tokens_seen = true;
\r
1888 error_details = "Empty character literal";
\r
1889 Report.Error (1011, Location, error_details);
\r
1890 return Token.ERROR;
\r
1894 return Token.ERROR;
\r
1895 val = new System.Char ();
\r
1900 error_details = "Too many characters in character literal";
\r
1901 Report.Error (1012, Location, error_details);
\r
1903 // Try to recover, read until newline or next "'"
\r
1904 while ((c = getChar ()) != -1){
\r
1905 if (c == '\n' || c == '\''){
\r
1914 return Token.ERROR;
\r
1916 return Token.LITERAL_CHARACTER;
\r
1922 tokens_seen = true;
\r
1923 return consume_string (true);
\r
1924 } else if (is_identifier_start_character ((char) c)){
\r
1925 return consume_identifier (c, true);
\r
1927 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1932 error_details = "Preprocessor directives must appear as the first non-whitespace " +
\r
1933 "character on a line.";
\r
1935 Report.Error (1040, Location, error_details);
\r
1937 return Token.ERROR;
\r
1940 error_details = ((char)c).ToString ();
\r
1942 return Token.ERROR;
\r
1948 public void cleanup ()
\r
1950 if (ifstack != null && ifstack.Count >= 1) {
\r
1951 int state = (int) ifstack.Pop ();
\r
1952 if ((state & REGION) != 0)
\r
1953 Report.Error (1038, "#endregion directive expected");
\r
1955 Report.Error (1027, "#endif directive expected");
\r