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 StreamReader 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 ("while", Token.WHILE);
\r
286 // Class initializer
\r
288 static Tokenizer ()
\r
291 csharp_format_info = NumberFormatInfo.InvariantInfo;
\r
292 styles = NumberStyles.Float;
\r
294 string_builder = new System.Text.StringBuilder ();
\r
297 int GetKeyword (string name)
\r
299 object o = keywords [name];
\r
306 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
\r
308 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
\r
310 if (handle_assembly == false && res == Token.ASSEMBLY)
\r
316 public Location Location {
\r
318 return new Location (ref_line);
\r
322 void define (string def)
\r
324 if (!RootContext.AllDefines.Contains (def)){
\r
325 RootContext.AllDefines [def] = true;
\r
327 if (defines.Contains (def))
\r
329 defines [def] = true;
\r
332 public Tokenizer (StreamReader input, SourceFile file, ArrayList defs)
\r
334 this.ref_name = file;
\r
335 this.file_name = file;
\r
341 defines = new Hashtable ();
\r
342 foreach (string def in defs)
\r
347 // FIXME: This could be `Location.Push' but we have to
\r
348 // find out why the MS compiler allows this
\r
350 Mono.CSharp.Location.Push (file);
\r
353 bool is_identifier_start_character (char c)
\r
355 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
\r
358 bool is_identifier_part_character (char c)
\r
360 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
\r
363 int is_punct (char c, ref bool doread)
\r
372 return Token.OPEN_BRACE;
\r
374 return Token.CLOSE_BRACE;
\r
376 return Token.OPEN_BRACKET;
\r
378 return Token.CLOSE_BRACKET;
\r
380 return Token.OPEN_PARENS;
\r
382 return Token.CLOSE_PARENS;
\r
384 return Token.COMMA;
\r
386 return Token.COLON;
\r
388 return Token.SEMICOLON;
\r
390 return Token.TILDE;
\r
392 return Token.INTERR;
\r
401 t = Token.OP_ADD_ASSIGN;
\r
411 t = Token.OP_SUB_ASSIGN;
\r
415 return Token.MINUS;
\r
423 return Token.OP_NE;
\r
431 return Token.OP_EQ;
\r
433 return Token.ASSIGN;
\r
439 return Token.OP_AND;
\r
440 } else if (d == '='){
\r
442 return Token.OP_AND_ASSIGN;
\r
444 return Token.BITWISE_AND;
\r
450 return Token.OP_OR;
\r
451 } else if (d == '='){
\r
453 return Token.OP_OR_ASSIGN;
\r
455 return Token.BITWISE_OR;
\r
461 return Token.OP_MULT_ASSIGN;
\r
469 return Token.OP_DIV_ASSIGN;
\r
477 return Token.OP_MOD_ASSIGN;
\r
479 return Token.PERCENT;
\r
485 return Token.OP_XOR_ASSIGN;
\r
487 return Token.CARRET;
\r
497 return Token.OP_SHIFT_LEFT_ASSIGN;
\r
499 return Token.OP_SHIFT_LEFT;
\r
500 } else if (d == '='){
\r
502 return Token.OP_LE;
\r
504 return Token.OP_LT;
\r
514 return Token.OP_SHIFT_RIGHT_ASSIGN;
\r
516 return Token.OP_SHIFT_RIGHT;
\r
517 } else if (d == '='){
\r
519 return Token.OP_GE;
\r
521 return Token.OP_GT;
\r
523 return Token.ERROR;
\r
526 void Error_NumericConstantTooLong ()
\r
528 Report.Error (1021, Location, "Numeric constant too long");
\r
531 bool decimal_digits (int c)
\r
534 bool seen_digits = false;
\r
537 if (number_pos == max_number_size)
\r
538 Error_NumericConstantTooLong ();
\r
539 number_builder [number_pos++] = (char) c;
\r
543 // We use peekChar2, because decimal_digits needs to do a
\r
544 // 2-character look-ahead (5.ToString for example).
\r
546 while ((d = peekChar2 ()) != -1){
\r
547 if (d >= '0' && d <= '9'){
\r
548 if (number_pos == max_number_size)
\r
549 Error_NumericConstantTooLong ();
\r
550 number_builder [number_pos++] = (char) d;
\r
552 seen_digits = true;
\r
557 return seen_digits;
\r
560 bool is_hex (int e)
\r
562 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
\r
565 void hex_digits (int c)
\r
568 number_builder [number_pos++] = (char) c;
\r
572 int real_type_suffix (int c)
\r
577 case 'F': case 'f':
\r
578 t = Token.LITERAL_FLOAT;
\r
580 case 'D': case 'd':
\r
581 t = Token.LITERAL_DOUBLE;
\r
583 case 'M': case 'm':
\r
584 t= Token.LITERAL_DECIMAL;
\r
592 int integer_type_suffix (ulong ul, int c)
\r
594 bool is_unsigned = false;
\r
595 bool is_long = false;
\r
598 bool scanning = true;
\r
601 case 'U': case 'u':
\r
604 is_unsigned = true;
\r
611 // if we have not seen anything in between
\r
612 // report this error
\r
616 "the 'l' suffix is easily confused with digit `1'," +
\r
617 " use 'L' for clarity");
\r
633 } while (scanning);
\r
636 if (is_long && is_unsigned){
\r
638 return Token.LITERAL_INTEGER;
\r
639 } else if (is_unsigned){
\r
640 // uint if possible, or ulong else.
\r
642 if ((ul & 0xffffffff00000000) == 0)
\r
646 } else if (is_long){
\r
647 // long if possible, ulong otherwise
\r
648 if ((ul & 0x8000000000000000) != 0)
\r
653 // int, uint, long or ulong in that order
\r
654 if ((ul & 0xffffffff00000000) == 0){
\r
655 uint ui = (uint) ul;
\r
657 if ((ui & 0x80000000) != 0)
\r
662 if ((ul & 0x8000000000000000) != 0)
\r
668 return Token.LITERAL_INTEGER;
\r
672 // given `c' as the next char in the input decide whether
\r
673 // we need to convert to a special type, and then choose
\r
674 // the best representation for the integer
\r
676 int adjust_int (int c)
\r
679 if (number_pos > 9){
\r
680 ulong ul = (uint) (number_builder [0] - '0');
\r
682 for (int i = 1; i < number_pos; i++){
\r
683 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
\r
685 return integer_type_suffix (ul, c);
\r
687 uint ui = (uint) (number_builder [0] - '0');
\r
689 for (int i = 1; i < number_pos; i++){
\r
690 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
\r
692 return integer_type_suffix (ui, c);
\r
694 } catch (OverflowException) {
\r
695 error_details = "Integral constant is too large";
\r
696 Report.Error (1021, Location, error_details);
\r
698 return Token.LITERAL_INTEGER;
\r
702 int adjust_real (int t)
\r
704 string s = new String (number_builder, 0, number_pos);
\r
707 case Token.LITERAL_DECIMAL:
\r
709 val = System.Decimal.Parse (s, styles, csharp_format_info);
\r
710 } catch (OverflowException) {
\r
712 error_details = "Floating-point constant is outside the range of the type 'decimal'";
\r
713 Report.Error (594, Location, error_details);
\r
716 case Token.LITERAL_FLOAT:
\r
718 val = (float) System.Double.Parse (s, styles, csharp_format_info);
\r
719 } catch (OverflowException) {
\r
721 error_details = "Floating-point constant is outside the range of the type 'float'";
\r
722 Report.Error (594, Location, error_details);
\r
726 case Token.LITERAL_DOUBLE:
\r
728 t = Token.LITERAL_DOUBLE;
\r
730 val = System.Double.Parse (s, styles, csharp_format_info);
\r
731 } catch (OverflowException) {
\r
733 error_details = "Floating-point constant is outside the range of the type 'double'";
\r
734 Report.Error (594, Location, error_details);
\r
747 while ((d = peekChar ()) != -1){
\r
749 if (number_pos == 16){
\r
750 Report.Error (1021, Location, "Integral constant too large");
\r
751 return Token.ERROR;
\r
753 number_builder [number_pos++] = (char) d;
\r
759 string s = new String (number_builder, 0, number_pos);
\r
760 if (number_pos <= 8)
\r
761 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
\r
763 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
\r
764 return integer_type_suffix (ul, peekChar ());
\r
768 // Invoked if we know we have .digits or digits
\r
770 int is_number (int c)
\r
772 bool is_real = false;
\r
777 if (c >= '0' && c <= '9'){
\r
779 int peek = peekChar ();
\r
781 if (peek == 'x' || peek == 'X')
\r
782 return handle_hex ();
\r
784 decimal_digits (c);
\r
789 // We need to handle the case of
\r
790 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
\r
793 if (decimal_digits ('.')){
\r
799 return adjust_int (-1);
\r
803 if (c == 'e' || c == 'E'){
\r
805 if (number_pos == max_number_size)
\r
806 Error_NumericConstantTooLong ();
\r
807 number_builder [number_pos++] = 'e';
\r
811 if (number_pos == max_number_size)
\r
812 Error_NumericConstantTooLong ();
\r
813 number_builder [number_pos++] = '+';
\r
815 } else if (c == '-') {
\r
816 if (number_pos == max_number_size)
\r
817 Error_NumericConstantTooLong ();
\r
818 number_builder [number_pos++] = '-';
\r
821 if (number_pos == max_number_size)
\r
822 Error_NumericConstantTooLong ();
\r
823 number_builder [number_pos++] = '+';
\r
826 decimal_digits (c);
\r
830 type = real_type_suffix (c);
\r
831 if (type == Token.NONE && !is_real){
\r
833 return adjust_int (c);
\r
837 if (type == Token.NONE){
\r
842 return adjust_real (type);
\r
844 Console.WriteLine ("This should not be reached");
\r
845 throw new Exception ("Is Number should never reach this point");
\r
849 // Accepts exactly count (4 or 8) hex, no more no less
\r
851 int getHex (int count, out bool error)
\r
853 int [] buffer = new int [8];
\r
857 int top = count != -1 ? count : 4;
\r
861 for (i = 0; i < top; i++){
\r
864 if (c >= '0' && c <= '9')
\r
865 c = (int) c - (int) '0';
\r
866 else if (c >= 'A' && c <= 'F')
\r
867 c = (int) c - (int) 'A' + 10;
\r
868 else if (c >= 'a' && c <= 'f')
\r
869 c = (int) c - (int) 'a' + 10;
\r
875 total = (total * 16) + c;
\r
877 int p = peekChar ();
\r
880 if (!is_hex ((char)p))
\r
921 v = getHex (-1, out error);
\r
926 v = getHex (4, out error);
\r
931 v = getHex (8, out error);
\r
936 Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
\r
945 if (putback_char != -1){
\r
946 int x = putback_char;
\r
951 return reader.Read ();
\r
956 if (putback_char != -1)
\r
957 return putback_char;
\r
958 putback_char = reader.Read ();
\r
959 return putback_char;
\r
964 if (putback_char != -1)
\r
965 return putback_char;
\r
966 return reader.Peek ();
\r
969 void putback (int c)
\r
971 if (putback_char != -1){
\r
972 Console.WriteLine ("Col: " + col);
\r
973 Console.WriteLine ("Row: " + line);
\r
974 Console.WriteLine ("Name: " + ref_name.Name);
\r
975 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
\r
976 throw new Exception ("This should not happen putback on putback");
\r
981 public bool advance ()
\r
983 return peekChar () != -1;
\r
986 public Object Value {
\r
992 public Object value ()
\r
997 public int token ()
\r
999 current_token = xtoken ();
\r
1000 return current_token;
\r
1003 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
\r
1005 void get_cmd_arg (out string cmd, out string arg)
\r
1009 tokens_seen = false;
\r
1011 static_cmd_arg.Length = 0;
\r
1013 while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
\r
1014 static_cmd_arg.Append ((char) c);
\r
1017 cmd = static_cmd_arg.ToString ();
\r
1023 } else if (c == '\r')
\r
1026 // skip over white space
\r
1027 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
\r
1034 } else if (c == '\r'){
\r
1039 static_cmd_arg.Length = 0;
\r
1040 static_cmd_arg.Append ((char) c);
\r
1042 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
\r
1043 static_cmd_arg.Append ((char) c);
\r
1049 } else if (c == '\r')
\r
1051 arg = static_cmd_arg.ToString ().Trim ();
\r
1055 // Handles the #line directive
\r
1057 bool PreProcessLine (string arg)
\r
1062 if (arg == "default"){
\r
1064 ref_name = file_name;
\r
1065 Location.Push (ref_name);
\r
1072 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
\r
1073 ref_line = System.Int32.Parse (arg.Substring (0, pos));
\r
1076 char [] quotes = { '\"' };
\r
1078 string name = arg.Substring (pos). Trim (quotes);
\r
1079 ref_name = Location.LookupFile (name);
\r
1080 Location.Push (ref_name);
\r
1082 ref_line = System.Int32.Parse (arg);
\r
1094 // Handles #define and #undef
\r
1096 void PreProcessDefinition (bool is_define, string arg)
\r
1098 if (arg == "" || arg == "true" || arg == "false"){
\r
1099 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
\r
1103 char[] whitespace = { ' ', '\t' };
\r
1104 if (arg.IndexOfAny (whitespace) != -1){
\r
1105 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
\r
1109 foreach (char c in arg){
\r
1110 if (!Char.IsLetter (c) && (c != '_')){
\r
1111 Report.Error (1001, Location, "Identifier expected");
\r
1117 if (defines == null)
\r
1118 defines = new Hashtable ();
\r
1121 if (defines == null)
\r
1123 if (defines.Contains (arg))
\r
1124 defines.Remove (arg);
\r
1128 bool eval_val (string s)
\r
1135 if (defines == null)
\r
1137 if (defines.Contains (s))
\r
1143 bool pp_primary (ref string s)
\r
1146 int len = s.Length;
\r
1152 s = s.Substring (1);
\r
1153 bool val = pp_expr (ref s);
\r
1154 if (s.Length > 0 && s [0] == ')'){
\r
1155 s = s.Substring (1);
\r
1158 Error_InvalidDirective ();
\r
1162 if (is_identifier_start_character (c)){
\r
1168 if (is_identifier_part_character (c)){
\r
1172 bool v = eval_val (s.Substring (0, j));
\r
1173 s = s.Substring (j);
\r
1176 bool vv = eval_val (s);
\r
1181 Error_InvalidDirective ();
\r
1185 bool pp_unary (ref string s)
\r
1188 int len = s.Length;
\r
1191 if (s [0] == '!'){
\r
1192 if (len > 1 && s [1] == '='){
\r
1193 Error_InvalidDirective ();
\r
1196 s = s.Substring (1);
\r
1197 return ! pp_primary (ref s);
\r
1199 return pp_primary (ref s);
\r
1201 Error_InvalidDirective ();
\r
1206 bool pp_eq (ref string s)
\r
1208 bool va = pp_unary (ref s);
\r
1211 int len = s.Length;
\r
1213 if (s [0] == '='){
\r
1214 if (len > 2 && s [1] == '='){
\r
1215 s = s.Substring (2);
\r
1216 return va == pp_unary (ref s);
\r
1218 Error_InvalidDirective ();
\r
1221 } else if (s [0] == '!' && len > 1 && s [1] == '='){
\r
1222 s = s.Substring (2);
\r
1224 return va != pp_unary (ref s);
\r
1233 bool pp_and (ref string s)
\r
1235 bool va = pp_eq (ref s);
\r
1238 int len = s.Length;
\r
1240 if (s [0] == '&'){
\r
1241 if (len > 2 && s [1] == '&'){
\r
1242 s = s.Substring (2);
\r
1243 return (va & pp_eq (ref s));
\r
1245 Error_InvalidDirective ();
\r
1254 // Evaluates an expression for `#if' or `#elif'
\r
1256 bool pp_expr (ref string s)
\r
1258 bool va = pp_and (ref s);
\r
1260 int len = s.Length;
\r
1265 if (len > 2 && s [1] == '|'){
\r
1266 s = s.Substring (2);
\r
1267 return va | pp_expr (ref s);
\r
1269 Error_InvalidDirective ();
\r
1278 bool eval (string s)
\r
1280 bool v = pp_expr (ref s);
\r
1282 if (s.Length != 0){
\r
1283 Error_InvalidDirective ();
\r
1290 void Error_InvalidDirective ()
\r
1292 Report.Error (1517, Location, "Invalid pre-processor directive");
\r
1295 void Error_UnexpectedDirective (string extra)
\r
1299 "Unexpected processor directive (" + extra + ")");
\r
1302 void Error_TokensSeen ()
\r
1306 "Cannot define or undefine pre-processor symbols after a token in the file");
\r
1310 // if true, then the code continues processing the code
\r
1311 // if false, the code stays in a loop until another directive is
\r
1314 bool handle_preprocessing_directive (bool caller_is_taking)
\r
1316 char [] blank = { ' ', '\t' };
\r
1318 bool region_directive = false;
\r
1320 get_cmd_arg (out cmd, out arg);
\r
1322 // Eat any trailing whitespaces and single-line comments
\r
1323 if (arg.IndexOf ("//") != -1)
\r
1324 arg = arg.Substring (0, arg.IndexOf ("//"));
\r
1325 arg = arg.TrimEnd (' ', '\t');
\r
1328 // The first group of pre-processing instructions is always processed
\r
1332 if (!PreProcessLine (arg))
\r
1335 "Argument to #line directive is missing or invalid");
\r
1339 region_directive = true;
\r
1344 region_directive = true;
\r
1345 goto case "endif";
\r
1349 Error_InvalidDirective ();
\r
1352 bool taking = false;
\r
1353 if (ifstack == null)
\r
1354 ifstack = new Stack ();
\r
1356 if (ifstack.Count == 0){
\r
1359 int state = (int) ifstack.Peek ();
\r
1360 if ((state & TAKING) != 0)
\r
1364 if (eval (arg) && taking){
\r
1365 int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
\r
1366 if (region_directive)
\r
1368 ifstack.Push (push);
\r
1371 int push = (taking ? PARENT_TAKING : 0);
\r
1372 if (region_directive)
\r
1374 ifstack.Push (push);
\r
1379 if (ifstack == null || ifstack.Count == 0){
\r
1380 Error_UnexpectedDirective ("no #if for this #endif");
\r
1383 int pop = (int) ifstack.Pop ();
\r
1385 if (region_directive && ((pop & REGION) == 0))
\r
1386 Report.Error (1027, Location, "#endif directive expected");
\r
1387 else if (!region_directive && ((pop & REGION) != 0))
\r
1388 Report.Error (1038, Location, "#endregion directive expected");
\r
1390 if (ifstack.Count == 0)
\r
1393 int state = (int) ifstack.Peek ();
\r
1395 if ((state & TAKING) != 0)
\r
1403 if (ifstack == null || ifstack.Count == 0){
\r
1404 Error_UnexpectedDirective ("no #if for this #elif");
\r
1407 int state = (int) ifstack.Peek ();
\r
1409 if ((state & REGION) != 0) {
\r
1410 Report.Error (1038, Location, "#endregion directive expected");
\r
1414 if ((state & ELSE_SEEN) != 0){
\r
1415 Error_UnexpectedDirective ("#elif not valid after #else");
\r
1419 if ((state & (TAKEN_BEFORE | TAKING)) != 0)
\r
1422 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
\r
1423 state = (int) ifstack.Pop ();
\r
1424 ifstack.Push (state | TAKING | TAKEN_BEFORE);
\r
1431 if (ifstack == null || ifstack.Count == 0){
\r
1434 "Unexpected processor directive (no #if for this #else)");
\r
1437 int state = (int) ifstack.Peek ();
\r
1439 if ((state & REGION) != 0) {
\r
1440 Report.Error (1038, Location, "#endregion directive expected");
\r
1444 if ((state & ELSE_SEEN) != 0){
\r
1445 Error_UnexpectedDirective ("#else within #else");
\r
1452 if ((state & TAKEN_BEFORE) == 0){
\r
1453 ret = ((state & PARENT_TAKING) != 0);
\r
1462 ifstack.Push (state | ELSE_SEEN);
\r
1469 // These are only processed if we are in a `taking' block
\r
1471 if (!caller_is_taking)
\r
1476 if (any_token_seen){
\r
1477 Error_TokensSeen ();
\r
1480 PreProcessDefinition (true, arg);
\r
1484 if (any_token_seen){
\r
1485 Error_TokensSeen ();
\r
1488 PreProcessDefinition (false, arg);
\r
1492 Report.Error (1029, Location, "#error: '" + arg + "'");
\r
1496 Report.Warning (1030, Location, "#warning: '" + arg + "'");
\r
1500 Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
\r
1505 private int consume_string (bool quoted)
\r
1508 string_builder.Length = 0;
\r
1510 while ((c = getChar ()) != -1){
\r
1512 if (quoted && peekChar () == '"'){
\r
1513 string_builder.Append ((char) c);
\r
1517 val = string_builder.ToString ();
\r
1518 return Token.LITERAL_STRING;
\r
1524 Report.Error (1010, Location, "Newline in constant");
\r
1534 return Token.ERROR;
\r
1536 string_builder.Append ((char) c);
\r
1539 Report.Error (1039, Location, "Unterminated string literal");
\r
1543 private int consume_identifier (int s, bool quoted)
\r
1548 id_builder [0] = (char) s;
\r
1550 while ((c = reader.Read ()) != -1) {
\r
1551 if (is_identifier_part_character ((char) c)){
\r
1552 if (pos == max_id_size){
\r
1553 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
\r
1554 return Token.ERROR;
\r
1557 id_builder [pos++] = (char) c;
\r
1558 putback_char = -1;
\r
1566 string ids = new String (id_builder, 0, pos);
\r
1569 int keyword = GetKeyword (ids);
\r
1570 if (keyword == -1 || quoted){
\r
1572 return Token.IDENTIFIER;
\r
1577 return Token.IDENTIFIER;
\r
1580 public int xtoken ()
\r
1583 bool doread = false;
\r
1587 // optimization: eliminate col and implement #directive semantic correctly.
\r
1588 for (;(c = getChar ()) != -1; col++) {
\r
1589 if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r' || c == 0xa0){
\r
1592 col = (((col + 8) / 8) * 8) - 1;
\r
1596 // Handle double-slash comments.
\r
1598 int d = peekChar ();
\r
1602 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
\r
1609 any_token_seen |= tokens_seen;
\r
1610 tokens_seen = false;
\r
1612 } else if (d == '*'){
\r
1615 while ((d = getChar ()) != -1){
\r
1616 if (d == '*' && peekChar () == '/'){
\r
1625 any_token_seen |= tokens_seen;
\r
1626 tokens_seen = false;
\r
1631 goto is_punct_label;
\r
1635 if (is_identifier_start_character ((char)c)){
\r
1636 tokens_seen = true;
\r
1637 return consume_identifier (c, false);
\r
1641 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
\r
1642 tokens_seen = true;
\r
1655 any_token_seen |= tokens_seen;
\r
1656 tokens_seen = false;
\r
1660 if (c >= '0' && c <= '9'){
\r
1661 tokens_seen = true;
\r
1662 return is_number (c);
\r
1666 tokens_seen = true;
\r
1667 int peek = peekChar ();
\r
1668 if (peek >= '0' && peek <= '9')
\r
1669 return is_number (c);
\r
1673 /* For now, ignore pre-processor commands */
\r
1674 // FIXME: In C# the '#' is not limited to appear
\r
1675 // on the first column.
\r
1676 if (c == '#' && !tokens_seen){
\r
1681 cont = handle_preprocessing_directive (cont);
\r
1689 bool skipping = false;
\r
1690 for (;(c = getChar ()) != -1; col++){
\r
1696 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
\r
1698 else if (c != '#')
\r
1700 if (c == '#' && !skipping)
\r
1703 any_token_seen |= tokens_seen;
\r
1704 tokens_seen = false;
\r
1706 Report.Error (1027, Location, "#endif/#endregion expected");
\r
1711 return consume_string (false);
\r
1715 tokens_seen = true;
\r
1717 error_details = "Empty character literal";
\r
1718 Report.Error (1011, Location, error_details);
\r
1719 return Token.ERROR;
\r
1723 return Token.ERROR;
\r
1724 val = new System.Char ();
\r
1729 error_details = "Too many characters in character literal";
\r
1730 Report.Error (1012, Location, error_details);
\r
1732 // Try to recover, read until newline or next "'"
\r
1733 while ((c = getChar ()) != -1){
\r
1734 if (c == '\n' || c == '\''){
\r
1743 return Token.ERROR;
\r
1745 return Token.LITERAL_CHARACTER;
\r
1751 tokens_seen = true;
\r
1752 return consume_string (true);
\r
1753 } else if (is_identifier_start_character ((char) c)){
\r
1754 return consume_identifier (c, true);
\r
1756 Report.Error (1033, Location, "'@' must be followed by string constant or identifier");
\r
1760 error_details = ((char)c).ToString ();
\r
1762 return Token.ERROR;
\r
1768 public void cleanup ()
\r
1770 if (ifstack != null && ifstack.Count >= 1) {
\r
1771 int state = (int) ifstack.Pop ();
\r
1772 if ((state & REGION) != 0)
\r
1773 Report.Error (1038, "#endregion directive expected");
\r
1775 Report.Error (1027, "#endif directive expected");
\r