2 // cs-tokenizer.cs: The Tokenizer for the C# compiler
3 // This also implements the preprocessor
5 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
17 using System.Collections;
19 using System.Globalization;
20 using System.Reflection;
25 /// Tokenizer for C# source code.
28 public class Tokenizer : yyParser.yyInput
30 SeekableStreamReader reader;
38 bool handle_get_set = false;
39 bool handle_remove_add = false;
40 bool handle_assembly = false;
41 bool handle_constraints = false;
42 bool handle_typeof = false;
43 bool lambda_arguments_parsing;
44 Location current_location;
45 Location current_comment_location = Location.Null;
46 ArrayList escaped_identifiers = new ArrayList ();
52 static bool IsLinqEnabled {
54 return RootContext.Version == LanguageVersion.LINQ;
59 // XML documentation buffer. The save point is used to divide
60 // comments on types and comments on members.
62 StringBuilder xml_comment_buffer;
65 // See comment on XmlCommentState enumeration.
67 XmlCommentState xml_doc_state = XmlCommentState.Allowed;
70 // Whether tokens have been seen on this line
72 bool tokens_seen = false;
75 // Whether a token has been seen on the file
76 // This is needed because `define' is not allowed to be used
77 // after a token has been seen.
79 bool any_token_seen = false;
81 static Hashtable token_values;
82 static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
84 private static Hashtable TokenValueName
87 if (token_values == null)
88 token_values = GetTokenValueNameHash ();
94 private static Hashtable GetTokenValueNameHash ()
96 Type t = typeof (Token);
97 FieldInfo [] fields = t.GetFields ();
98 Hashtable hash = new Hashtable ();
99 foreach (FieldInfo field in fields) {
100 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
101 hash.Add (field.GetValue (null), field.Name);
107 // Returns a verbose representation of the current location
109 public string location {
113 if (current_token == Token.ERROR)
114 det = "detail: " + error_details;
118 // return "Line: "+line+" Col: "+col + "\n" +
119 // "VirtLine: "+ref_line +
120 // " Token: "+current_token + " " + det;
121 string current_token_name = TokenValueName [current_token] as string;
122 if (current_token_name == null)
123 current_token_name = current_token.ToString ();
125 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
133 public bool PropertyParsing {
135 return handle_get_set;
139 handle_get_set = value;
143 public bool AssemblyTargetParsing {
145 return handle_assembly;
149 handle_assembly = value;
153 public bool EventParsing {
155 return handle_remove_add;
159 handle_remove_add = value;
163 public bool ConstraintsParsing {
165 return handle_constraints;
169 handle_constraints = value;
173 public bool TypeOfParsing {
175 return handle_typeof;
179 handle_typeof = value;
183 public XmlCommentState doc_state {
184 get { return xml_doc_state; }
186 if (value == XmlCommentState.Allowed) {
187 check_incorrect_doc_comment ();
188 reset_doc_comment ();
190 xml_doc_state = value;
194 public bool IsEscapedIdentifier (Location loc)
196 foreach (LocatedToken lt in escaped_identifiers)
197 if (lt.Location.Equals (loc))
205 static CharArrayHashtable[] keywords;
206 static Hashtable keyword_strings;
207 static NumberStyles styles;
208 static NumberFormatInfo csharp_format_info;
211 // Values for the associated token returned
221 const int TAKING = 1;
222 const int ELSE_SEEN = 4;
223 const int PARENT_TAKING = 8;
224 const int REGION = 16;
227 // pre-processor if stack state:
231 static System.Text.StringBuilder string_builder;
233 const int max_id_size = 512;
234 static char [] id_builder = new char [max_id_size];
236 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
238 const int max_number_size = 512;
239 static char [] number_builder = new char [max_number_size];
240 static int number_pos;
243 // Details about the error encoutered by the tokenizer
245 string error_details;
247 public string error {
249 return error_details;
266 // This is used when the tokenizer needs to save
267 // the current position as it needs to do some parsing
268 // on its own to deamiguate a token in behalf of the
271 Stack position_stack = new Stack (2);
276 public int putback_char;
277 public int previous_col;
278 public Stack ifstack;
279 public int parsing_generic_less_than;
280 public int current_token;
282 public Position (Tokenizer t)
284 position = t.reader.Position;
285 ref_line = t.ref_line;
287 putback_char = t.putback_char;
288 previous_col = t.previous_col;
289 if (t.ifstack != null && t.ifstack.Count != 0)
290 ifstack = (Stack)t.ifstack.Clone ();
291 parsing_generic_less_than = t.parsing_generic_less_than;
292 current_token = t.current_token;
296 public void PushPosition ()
298 position_stack.Push (new Position (this));
301 public void PopPosition ()
303 Position p = (Position) position_stack.Pop ();
305 reader.Position = p.position;
306 ref_line = p.ref_line;
308 putback_char = p.putback_char;
309 previous_col = p.previous_col;
311 parsing_generic_less_than = p.parsing_generic_less_than;
312 current_token = p.current_token;
315 // Do not reset the position, ignore it.
316 public void DiscardPosition ()
318 position_stack.Pop ();
321 static void AddKeyword (string kw, int token)
323 keyword_strings.Add (kw, kw);
324 if (keywords [kw.Length] == null) {
325 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
327 keywords [kw.Length] [kw.ToCharArray ()] = token;
330 static void InitTokens ()
332 keyword_strings = new Hashtable ();
333 keywords = new CharArrayHashtable [64];
335 AddKeyword ("__arglist", Token.ARGLIST);
336 AddKeyword ("abstract", Token.ABSTRACT);
337 AddKeyword ("as", Token.AS);
338 AddKeyword ("add", Token.ADD);
339 AddKeyword ("assembly", Token.ASSEMBLY);
340 AddKeyword ("base", Token.BASE);
341 AddKeyword ("bool", Token.BOOL);
342 AddKeyword ("break", Token.BREAK);
343 AddKeyword ("byte", Token.BYTE);
344 AddKeyword ("case", Token.CASE);
345 AddKeyword ("catch", Token.CATCH);
346 AddKeyword ("char", Token.CHAR);
347 AddKeyword ("checked", Token.CHECKED);
348 AddKeyword ("class", Token.CLASS);
349 AddKeyword ("const", Token.CONST);
350 AddKeyword ("continue", Token.CONTINUE);
351 AddKeyword ("decimal", Token.DECIMAL);
352 AddKeyword ("default", Token.DEFAULT);
353 AddKeyword ("delegate", Token.DELEGATE);
354 AddKeyword ("do", Token.DO);
355 AddKeyword ("double", Token.DOUBLE);
356 AddKeyword ("else", Token.ELSE);
357 AddKeyword ("enum", Token.ENUM);
358 AddKeyword ("event", Token.EVENT);
359 AddKeyword ("explicit", Token.EXPLICIT);
360 AddKeyword ("extern", Token.EXTERN);
361 AddKeyword ("false", Token.FALSE);
362 AddKeyword ("finally", Token.FINALLY);
363 AddKeyword ("fixed", Token.FIXED);
364 AddKeyword ("float", Token.FLOAT);
365 AddKeyword ("for", Token.FOR);
366 AddKeyword ("foreach", Token.FOREACH);
367 AddKeyword ("goto", Token.GOTO);
368 AddKeyword ("get", Token.GET);
369 AddKeyword ("if", Token.IF);
370 AddKeyword ("implicit", Token.IMPLICIT);
371 AddKeyword ("in", Token.IN);
372 AddKeyword ("int", Token.INT);
373 AddKeyword ("interface", Token.INTERFACE);
374 AddKeyword ("internal", Token.INTERNAL);
375 AddKeyword ("is", Token.IS);
376 AddKeyword ("lock", Token.LOCK);
377 AddKeyword ("long", Token.LONG);
378 AddKeyword ("namespace", Token.NAMESPACE);
379 AddKeyword ("new", Token.NEW);
380 AddKeyword ("null", Token.NULL);
381 AddKeyword ("object", Token.OBJECT);
382 AddKeyword ("operator", Token.OPERATOR);
383 AddKeyword ("out", Token.OUT);
384 AddKeyword ("override", Token.OVERRIDE);
385 AddKeyword ("params", Token.PARAMS);
386 AddKeyword ("private", Token.PRIVATE);
387 AddKeyword ("protected", Token.PROTECTED);
388 AddKeyword ("public", Token.PUBLIC);
389 AddKeyword ("readonly", Token.READONLY);
390 AddKeyword ("ref", Token.REF);
391 AddKeyword ("remove", Token.REMOVE);
392 AddKeyword ("return", Token.RETURN);
393 AddKeyword ("sbyte", Token.SBYTE);
394 AddKeyword ("sealed", Token.SEALED);
395 AddKeyword ("set", Token.SET);
396 AddKeyword ("short", Token.SHORT);
397 AddKeyword ("sizeof", Token.SIZEOF);
398 AddKeyword ("stackalloc", Token.STACKALLOC);
399 AddKeyword ("static", Token.STATIC);
400 AddKeyword ("string", Token.STRING);
401 AddKeyword ("struct", Token.STRUCT);
402 AddKeyword ("switch", Token.SWITCH);
403 AddKeyword ("this", Token.THIS);
404 AddKeyword ("throw", Token.THROW);
405 AddKeyword ("true", Token.TRUE);
406 AddKeyword ("try", Token.TRY);
407 AddKeyword ("typeof", Token.TYPEOF);
408 AddKeyword ("uint", Token.UINT);
409 AddKeyword ("ulong", Token.ULONG);
410 AddKeyword ("unchecked", Token.UNCHECKED);
411 AddKeyword ("unsafe", Token.UNSAFE);
412 AddKeyword ("ushort", Token.USHORT);
413 AddKeyword ("using", Token.USING);
414 AddKeyword ("virtual", Token.VIRTUAL);
415 AddKeyword ("void", Token.VOID);
416 AddKeyword ("volatile", Token.VOLATILE);
417 AddKeyword ("while", Token.WHILE);
418 AddKeyword ("partial", Token.PARTIAL);
420 AddKeyword ("where", Token.WHERE);
425 public static void InitializeLinqKeywords ()
427 AddKeyword ("from", Token.FROM);
428 AddKeyword ("join", Token.JOIN);
429 AddKeyword ("on", Token.ON);
430 AddKeyword ("equals", Token.EQUALS);
431 AddKeyword ("select", Token.SELECT);
432 AddKeyword ("group", Token.GROUP);
433 AddKeyword ("by", Token.BY);
434 AddKeyword ("let", Token.LET);
435 AddKeyword ("orderby", Token.ORDERBY);
436 AddKeyword ("ascending", Token.ASCENDING);
437 AddKeyword ("descending", Token.DESCENDING);
438 AddKeyword ("into", Token.INTO);
450 public static void Reset ()
453 csharp_format_info = NumberFormatInfo.InvariantInfo;
454 styles = NumberStyles.Float;
456 string_builder = new System.Text.StringBuilder ();
459 int GetKeyword (char[] id, int id_len)
462 * Keywords are stored in an array of hashtables grouped by their
466 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
468 object o = keywords [id_len] [id];
475 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
477 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
479 if (handle_assembly == false && res == Token.ASSEMBLY)
483 if (res == Token.FROM &&
484 (current_token == Token.ASSIGN || current_token == Token.OPEN_BRACKET ||
485 current_token == Token.RETURN || current_token == Token.IN)) {
486 query_parsing = true;
490 if (!query_parsing && res > Token.QUERY_FIRST_TOKEN && res < Token.QUERY_LAST_TOKEN)
496 if (!handle_constraints && res == Token.WHERE)
503 public Location Location {
504 get { return current_location; }
507 void define (string def)
509 if (!RootContext.AllDefines.Contains (def)){
510 RootContext.AllDefines [def] = true;
512 if (defines.Contains (def))
514 defines [def] = true;
517 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
519 this.ref_name = file;
520 this.file_name = file;
526 defines = new Hashtable ();
527 foreach (string def in defs)
531 xml_comment_buffer = new StringBuilder ();
534 // FIXME: This could be `Location.Push' but we have to
535 // find out why the MS compiler allows this
537 Mono.CSharp.Location.Push (file);
540 static bool is_identifier_start_character (char c)
542 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter (c);
545 static bool is_identifier_part_character (char c)
547 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') ||
548 Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
551 public static bool IsKeyword (string s)
553 return keyword_strings [s] != null;
557 // Tests whether '(' is beggining of lambda parameters
559 bool IsLambdaOpenParens ()
562 while ((ntoken = xtoken ()) != Token.EOF) {
564 case Token.CLOSE_PARENS:
565 return xtoken () == Token.ARROW;
568 case Token.SEMICOLON:
569 case Token.OPEN_PARENS:
574 Error_TokenExpected (")");
578 public static bool IsValidIdentifier (string s)
580 if (s == null || s.Length == 0)
583 if (!is_identifier_start_character (s [0]))
586 for (int i = 1; i < s.Length; i ++)
587 if (! is_identifier_part_character (s [i]))
593 bool parse_less_than ()
596 int the_token = token ();
597 if (the_token == Token.OPEN_BRACKET) {
599 the_token = token ();
600 } while (the_token != Token.CLOSE_BRACKET);
601 the_token = token ();
604 case Token.IDENTIFIER:
627 the_token = token ();
629 if (the_token == Token.OP_GENERICS_GT)
631 else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
633 else if (the_token == Token.INTERR || the_token == Token.STAR)
635 else if (the_token == Token.OP_GENERICS_LT) {
636 if (!parse_less_than ())
639 } else if (the_token == Token.OPEN_BRACKET) {
641 the_token = token ();
642 if (the_token == Token.CLOSE_BRACKET)
644 else if (the_token == Token.COMMA)
645 goto rank_specifiers;
653 public void PutbackNullable ()
655 if (nullable_pos < 0)
656 throw new Exception ();
660 reader.Position = nullable_pos;
665 public void PutbackCloseParens ()
671 int nullable_pos = -1;
673 public void CheckNullable (bool is_nullable)
676 nullable_pos = reader.Position;
681 public bool QueryParsing {
683 query_parsing = value;
687 bool parse_generic_dimension (out int dimension)
692 int the_token = token ();
693 if (the_token == Token.OP_GENERICS_GT)
695 else if (the_token == Token.COMMA) {
704 public int peek_token ()
709 the_token = token ();
715 int parsing_generic_less_than = 0;
717 int is_punct (char c, ref bool doread)
727 return Token.OPEN_BRACE;
730 return Token.CLOSE_BRACE;
732 // To block doccomment inside attribute declaration.
733 if (doc_state == XmlCommentState.Allowed)
734 doc_state = XmlCommentState.NotAllowed;
735 return Token.OPEN_BRACKET;
737 return Token.CLOSE_BRACKET;
739 if (IsLinqEnabled && !lambda_arguments_parsing) {
740 lambda_arguments_parsing = true;
742 bool lambda_start = IsLambdaOpenParens ();
744 lambda_arguments_parsing = false;
746 return Token.OPEN_PARENS_LAMBDA;
748 return Token.OPEN_PARENS;
750 if (deambiguate_close_parens == 0)
751 return Token.CLOSE_PARENS;
753 --deambiguate_close_parens;
757 int new_token = xtoken ();
761 if (new_token == Token.OPEN_PARENS)
762 return Token.CLOSE_PARENS_OPEN_PARENS;
763 else if (new_token == Token.MINUS)
764 return Token.CLOSE_PARENS_MINUS;
765 else if (IsCastToken (new_token))
766 return Token.CLOSE_PARENS_CAST;
768 return Token.CLOSE_PARENS_NO_CAST;
775 return Token.SEMICOLON;
784 return Token.OP_COALESCING;
791 if (parsing_generic_less_than++ > 0)
792 return Token.OP_GENERICS_LT;
797 if (parse_generic_dimension (out dimension)) {
800 return Token.GENERIC_DIMENSION;
805 // Save current position and parse next token.
807 bool is_generic_lt = parse_less_than ();
811 return Token.OP_GENERICS_LT;
813 parsing_generic_less_than = 0;
822 return Token.OP_SHIFT_LEFT_ASSIGN;
824 return Token.OP_SHIFT_LEFT;
825 } else if (d == '='){
830 } else if (c == '>') {
831 if (parsing_generic_less_than > 0) {
832 parsing_generic_less_than--;
833 return Token.OP_GENERICS_GT;
843 return Token.OP_SHIFT_RIGHT_ASSIGN;
845 return Token.OP_SHIFT_RIGHT;
846 } else if (d == '='){
861 t = Token.OP_ADD_ASSIGN;
875 t = Token.OP_SUB_ASSIGN;
913 } else if (d == '='){
915 return Token.OP_AND_ASSIGN;
918 return Token.BITWISE_AND;
925 } else if (d == '='){
927 return Token.OP_OR_ASSIGN;
929 return Token.BITWISE_OR;
935 return Token.OP_MULT_ASSIGN;
944 return Token.OP_DIV_ASSIGN;
952 return Token.OP_MOD_ASSIGN;
954 return Token.PERCENT;
960 return Token.OP_XOR_ASSIGN;
973 return Token.OP_SHIFT_LEFT_ASSIGN;
975 return Token.OP_SHIFT_LEFT;
976 } else if (d == '='){
990 return Token.OP_SHIFT_RIGHT_ASSIGN;
992 return Token.OP_SHIFT_RIGHT;
993 } else if (d == '='){
1003 return Token.DOUBLE_COLON;
1012 int deambiguate_close_parens = 0;
1014 public void Deambiguate_CloseParens (object expression)
1018 // When any binary operation is used we are sure it is not a cast
1019 if (expression is Binary)
1022 deambiguate_close_parens++;
1025 bool decimal_digits (int c)
1028 bool seen_digits = false;
1031 if (number_pos == max_number_size)
1032 Error_NumericConstantTooLong ();
1033 number_builder [number_pos++] = (char) c;
1037 // We use peek_char2, because decimal_digits needs to do a
1038 // 2-character look-ahead (5.ToString for example).
1040 while ((d = peek_char2 ()) != -1){
1041 if (d >= '0' && d <= '9'){
1042 if (number_pos == max_number_size)
1043 Error_NumericConstantTooLong ();
1044 number_builder [number_pos++] = (char) d;
1054 static bool is_hex (int e)
1056 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1059 static int real_type_suffix (int c)
1065 t = Token.LITERAL_FLOAT;
1068 t = Token.LITERAL_DOUBLE;
1071 t= Token.LITERAL_DECIMAL;
1079 int integer_type_suffix (ulong ul, int c)
1081 bool is_unsigned = false;
1082 bool is_long = false;
1085 bool scanning = true;
1098 // if we have not seen anything in between
1099 // report this error
1101 Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
1104 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
1105 // work around that.
1129 if (is_long && is_unsigned){
1131 return Token.LITERAL_INTEGER;
1132 } else if (is_unsigned){
1133 // uint if possible, or ulong else.
1135 if ((ul & 0xffffffff00000000) == 0)
1139 } else if (is_long){
1140 // long if possible, ulong otherwise
1141 if ((ul & 0x8000000000000000) != 0)
1146 // int, uint, long or ulong in that order
1147 if ((ul & 0xffffffff00000000) == 0){
1148 uint ui = (uint) ul;
1150 if ((ui & 0x80000000) != 0)
1155 if ((ul & 0x8000000000000000) != 0)
1161 return Token.LITERAL_INTEGER;
1165 // given `c' as the next char in the input decide whether
1166 // we need to convert to a special type, and then choose
1167 // the best representation for the integer
1169 int adjust_int (int c)
1172 if (number_pos > 9){
1173 ulong ul = (uint) (number_builder [0] - '0');
1175 for (int i = 1; i < number_pos; i++){
1176 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
1178 return integer_type_suffix (ul, c);
1180 uint ui = (uint) (number_builder [0] - '0');
1182 for (int i = 1; i < number_pos; i++){
1183 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
1185 return integer_type_suffix (ui, c);
1187 } catch (OverflowException) {
1188 error_details = "Integral constant is too large";
1189 Report.Error (1021, Location, error_details);
1191 return Token.LITERAL_INTEGER;
1193 catch (FormatException) {
1194 Report.Error (1013, Location, "Invalid number");
1196 return Token.LITERAL_INTEGER;
1200 int adjust_real (int t)
1202 string s = new String (number_builder, 0, number_pos);
1203 const string error_details = "Floating-point constant is outside the range of type `{0}'";
1206 case Token.LITERAL_DECIMAL:
1208 val = System.Decimal.Parse (s, styles, csharp_format_info);
1209 } catch (OverflowException) {
1211 Report.Error (594, Location, error_details, "decimal");
1214 case Token.LITERAL_FLOAT:
1216 val = float.Parse (s, styles, csharp_format_info);
1217 } catch (OverflowException) {
1219 Report.Error (594, Location, error_details, "float");
1223 case Token.LITERAL_DOUBLE:
1225 t = Token.LITERAL_DOUBLE;
1227 val = System.Double.Parse (s, styles, csharp_format_info);
1228 } catch (OverflowException) {
1230 Report.Error (594, Location, error_details, "double");
1243 while ((d = peek_char ()) != -1){
1245 number_builder [number_pos++] = (char) d;
1251 string s = new String (number_builder, 0, number_pos);
1253 if (number_pos <= 8)
1254 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
1256 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
1257 } catch (OverflowException){
1258 error_details = "Integral constant is too large";
1259 Report.Error (1021, Location, error_details);
1261 return Token.LITERAL_INTEGER;
1263 catch (FormatException) {
1264 Report.Error (1013, Location, "Invalid number");
1266 return Token.LITERAL_INTEGER;
1269 return integer_type_suffix (ul, peek_char ());
1273 // Invoked if we know we have .digits or digits
1275 int is_number (int c)
1277 bool is_real = false;
1282 if (c >= '0' && c <= '9'){
1284 int peek = peek_char ();
1286 if (peek == 'x' || peek == 'X')
1287 return handle_hex ();
1294 // We need to handle the case of
1295 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1298 if (decimal_digits ('.')){
1304 return adjust_int (-1);
1308 if (c == 'e' || c == 'E'){
1310 if (number_pos == max_number_size)
1311 Error_NumericConstantTooLong ();
1312 number_builder [number_pos++] = 'e';
1316 if (number_pos == max_number_size)
1317 Error_NumericConstantTooLong ();
1318 number_builder [number_pos++] = '+';
1320 } else if (c == '-') {
1321 if (number_pos == max_number_size)
1322 Error_NumericConstantTooLong ();
1323 number_builder [number_pos++] = '-';
1326 if (number_pos == max_number_size)
1327 Error_NumericConstantTooLong ();
1328 number_builder [number_pos++] = '+';
1335 type = real_type_suffix (c);
1336 if (type == Token.NONE && !is_real){
1338 return adjust_int (c);
1342 if (type == Token.NONE){
1347 return adjust_real (type);
1349 Console.WriteLine ("This should not be reached");
1350 throw new Exception ("Is Number should never reach this point");
1354 // Accepts exactly count (4 or 8) hex, no more no less
1356 int getHex (int count, out bool error)
1361 int top = count != -1 ? count : 4;
1365 for (i = 0; i < top; i++){
1368 if (c >= '0' && c <= '9')
1369 c = (int) c - (int) '0';
1370 else if (c >= 'A' && c <= 'F')
1371 c = (int) c - (int) 'A' + 10;
1372 else if (c >= 'a' && c <= 'f')
1373 c = (int) c - (int) 'a' + 10;
1379 total = (total * 16) + c;
1381 int p = peek_char ();
1384 if (!is_hex ((char)p))
1425 v = getHex (-1, out error);
1430 v = getHex (4, out error);
1435 v = getHex (8, out error);
1440 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1450 if (putback_char != -1) {
1468 if (putback_char != -1)
1469 return putback_char;
1470 putback_char = reader.Read ();
1471 return putback_char;
1476 if (putback_char != -1)
1477 return putback_char;
1478 return reader.Peek ();
1481 void putback (int c)
1483 if (putback_char != -1){
1484 Console.WriteLine ("Col: " + col);
1485 Console.WriteLine ("Row: " + line);
1486 Console.WriteLine ("Name: " + ref_name.Name);
1487 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
1488 throw new Exception ("This should not happen putback on putback");
1490 if (c == '\n' || col == 0) {
1491 // It won't happen though.
1501 public bool advance ()
1503 return peek_char () != -1;
1506 public Object Value {
1512 public Object value ()
1517 static bool IsCastToken (int token)
1522 case Token.IDENTIFIER:
1523 case Token.LITERAL_INTEGER:
1524 case Token.LITERAL_FLOAT:
1525 case Token.LITERAL_DOUBLE:
1526 case Token.LITERAL_DECIMAL:
1527 case Token.LITERAL_CHARACTER:
1528 case Token.LITERAL_STRING:
1531 case Token.DELEGATE:
1541 case Token.UNCHECKED:
1548 // These can be part of a member access
1568 current_token = xtoken ();
1571 if (current_token != Token.DEFAULT)
1572 return current_token;
1577 current_token = Token.ERROR;
1578 else if (c == Token.OPEN_PARENS)
1579 current_token = Token.DEFAULT_OPEN_PARENS;
1580 else if (c == Token.COLON)
1581 current_token = Token.DEFAULT_COLON;
1585 return current_token;
1588 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
1590 void get_cmd_arg (out string cmd, out string arg)
1594 tokens_seen = false;
1596 static_cmd_arg.Length = 0;
1598 // skip over white space
1599 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1602 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
1603 if (is_identifier_part_character ((char) c)){
1604 static_cmd_arg.Append ((char) c);
1612 cmd = static_cmd_arg.ToString ();
1614 if (c == '\n' || c == '\r'){
1618 // skip over white space
1619 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1624 } else if (c == '\r'){
1626 } else if (c == -1){
1631 static_cmd_arg.Length = 0;
1632 static_cmd_arg.Append ((char) c);
1634 while ((c = get_char ()) != -1 && (c != '\n') && (c != '\r')){
1635 static_cmd_arg.Append ((char) c);
1638 arg = static_cmd_arg.ToString ();
1642 // Handles the #line directive
1644 bool PreProcessLine (string arg)
1646 if (arg.Length == 0)
1649 if (arg == "default"){
1651 ref_name = file_name;
1652 Location.Push (ref_name);
1654 } else if (arg == "hidden"){
1656 // We ignore #line hidden
1664 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
1665 ref_line = System.Int32.Parse (arg.Substring (0, pos));
1668 char [] quotes = { '\"' };
1670 string name = arg.Substring (pos). Trim (quotes);
1671 ref_name = Location.LookupFile (name);
1672 file_name.HasLineDirective = true;
1673 ref_name.HasLineDirective = true;
1674 Location.Push (ref_name);
1676 ref_line = System.Int32.Parse (arg);
1686 // Handles #define and #undef
1688 void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
1690 if (arg.Length == 0 || arg == "true" || arg == "false"){
1691 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
1695 if (arg.IndexOfAny (simple_whitespaces) != -1){
1696 Error_EndLineExpected ();
1700 if (!is_identifier_start_character (arg [0]))
1701 Report.Error (1001, Location, "Identifier expected: " + arg);
1703 foreach (char c in arg.Substring (1)){
1704 if (!is_identifier_part_character (c)){
1705 Report.Error (1001, Location, "Identifier expected: " + arg);
1710 if (!caller_is_taking)
1714 if (defines == null)
1715 defines = new Hashtable ();
1718 if (defines == null)
1720 if (defines.Contains (arg))
1721 defines.Remove (arg);
1726 /// Handles #pragma directive
1728 void PreProcessPragma (string arg)
1730 const string warning = "warning";
1731 const string w_disable = "warning disable";
1732 const string w_restore = "warning restore";
1734 if (arg == w_disable) {
1735 Report.RegisterWarningRegion (Location).WarningDisable (line);
1739 if (arg == w_restore) {
1740 Report.RegisterWarningRegion (Location).WarningEnable (line);
1744 if (arg.StartsWith (w_disable)) {
1745 int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
1746 foreach (int code in codes) {
1748 Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
1753 if (arg.StartsWith (w_restore)) {
1754 int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
1755 Hashtable w_table = Report.warning_ignore_table;
1756 foreach (int code in codes) {
1757 if (w_table != null && w_table.Contains (code))
1758 Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
1759 Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
1764 if (arg.StartsWith (warning)) {
1765 Report.Warning (1634, 1, Location, "Expected disable or restore");
1769 Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
1772 int[] ParseNumbers (string text)
1774 string[] string_array = text.Split (',');
1775 int[] values = new int [string_array.Length];
1777 foreach (string string_code in string_array) {
1779 values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
1781 catch (FormatException) {
1782 Report.Warning (1692, 1, Location, "Invalid number");
1788 bool eval_val (string s)
1795 if (defines == null)
1797 if (defines.Contains (s))
1803 bool pp_primary (ref string s)
1812 s = s.Substring (1);
1813 bool val = pp_expr (ref s, false);
1814 if (s.Length > 0 && s [0] == ')'){
1815 s = s.Substring (1);
1818 Error_InvalidDirective ();
1822 if (is_identifier_start_character (c)){
1828 if (is_identifier_part_character (c)){
1832 bool v = eval_val (s.Substring (0, j));
1833 s = s.Substring (j);
1836 bool vv = eval_val (s);
1841 Error_InvalidDirective ();
1845 bool pp_unary (ref string s)
1852 if (len > 1 && s [1] == '='){
1853 Error_InvalidDirective ();
1856 s = s.Substring (1);
1857 return ! pp_primary (ref s);
1859 return pp_primary (ref s);
1861 Error_InvalidDirective ();
1866 bool pp_eq (ref string s)
1868 bool va = pp_unary (ref s);
1874 if (len > 2 && s [1] == '='){
1875 s = s.Substring (2);
1876 return va == pp_unary (ref s);
1878 Error_InvalidDirective ();
1881 } else if (s [0] == '!' && len > 1 && s [1] == '='){
1882 s = s.Substring (2);
1884 return va != pp_unary (ref s);
1893 bool pp_and (ref string s)
1895 bool va = pp_eq (ref s);
1901 if (len > 2 && s [1] == '&'){
1902 s = s.Substring (2);
1903 return (va & pp_and (ref s));
1905 Error_InvalidDirective ();
1914 // Evaluates an expression for `#if' or `#elif'
1916 bool pp_expr (ref string s, bool isTerm)
1918 bool va = pp_and (ref s);
1925 if (len > 2 && s [1] == '|'){
1926 s = s.Substring (2);
1927 return va | pp_expr (ref s, isTerm);
1929 Error_InvalidDirective ();
1934 Error_EndLineExpected ();
1942 bool eval (string s)
1944 bool v = pp_expr (ref s, true);
1953 void Error_NumericConstantTooLong ()
1955 Report.Error (1021, Location, "Numeric constant too long");
1958 void Error_InvalidDirective ()
1960 Report.Error (1517, Location, "Invalid preprocessor directive");
1963 void Error_UnexpectedDirective (string extra)
1967 "Unexpected processor directive (" + extra + ")");
1970 void Error_TokenExpected (string token)
1972 Report.Error (1026, Location, "Expecting `{0}'", token);
1975 void Error_TokensSeen ()
1977 Report.Error (1032, Location,
1978 "Cannot define or undefine preprocessor symbols after first token in file");
1981 void Eror_WrongPreprocessorLocation ()
1983 Report.Error (1040, Location,
1984 "Preprocessor directives must appear as the first non-whitespace character on a line");
1987 void Error_EndLineExpected ()
1989 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
1993 // if true, then the code continues processing the code
1994 // if false, the code stays in a loop until another directive is
1996 // When caller_is_taking is false we ignore all directives except the ones
1997 // which can help us to identify where the #if block ends
1998 bool handle_preprocessing_directive (bool caller_is_taking)
2001 bool region_directive = false;
2003 get_cmd_arg (out cmd, out arg);
2005 // Eat any trailing whitespaces and single-line comments
2006 if (arg.IndexOf ("//") != -1)
2007 arg = arg.Substring (0, arg.IndexOf ("//"));
2008 arg = arg.Trim (simple_whitespaces);
2011 // The first group of pre-processing instructions is always processed
2015 region_directive = true;
2020 if (ifstack == null || ifstack.Count == 0){
2021 Error_UnexpectedDirective ("no #region for this #endregion");
2024 int pop = (int) ifstack.Pop ();
2026 if ((pop & REGION) == 0)
2027 Report.Error (1027, Location, "Expected `#endif' directive");
2029 return caller_is_taking;
2032 if (ifstack == null)
2033 ifstack = new Stack (2);
2035 int flags = region_directive ? REGION : 0;
2036 if (ifstack.Count == 0){
2037 flags |= PARENT_TAKING;
2039 int state = (int) ifstack.Peek ();
2040 if ((state & TAKING) != 0) {
2041 flags |= PARENT_TAKING;
2045 if (caller_is_taking && eval (arg)) {
2046 ifstack.Push (flags | TAKING);
2049 ifstack.Push (flags);
2053 if (ifstack == null || ifstack.Count == 0){
2054 Error_UnexpectedDirective ("no #if for this #endif");
2057 pop = (int) ifstack.Pop ();
2059 if ((pop & REGION) != 0)
2060 Report.Error (1038, Location, "#endregion directive expected");
2062 if (arg.Length != 0) {
2063 Error_EndLineExpected ();
2066 if (ifstack.Count == 0)
2069 int state = (int) ifstack.Peek ();
2070 return (state & TAKING) != 0;
2074 if (ifstack == null || ifstack.Count == 0){
2075 Error_UnexpectedDirective ("no #if for this #elif");
2078 int state = (int) ifstack.Pop ();
2080 if ((state & REGION) != 0) {
2081 Report.Error (1038, Location, "#endregion directive expected");
2085 if ((state & ELSE_SEEN) != 0){
2086 Error_UnexpectedDirective ("#elif not valid after #else");
2090 if ((state & TAKING) != 0) {
2095 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
2096 ifstack.Push (state | TAKING);
2100 ifstack.Push (state);
2105 if (ifstack == null || ifstack.Count == 0){
2106 Error_UnexpectedDirective ("no #if for this #else");
2109 int state = (int) ifstack.Peek ();
2111 if ((state & REGION) != 0) {
2112 Report.Error (1038, Location, "#endregion directive expected");
2116 if ((state & ELSE_SEEN) != 0){
2117 Error_UnexpectedDirective ("#else within #else");
2123 if (arg.Length != 0) {
2124 Error_EndLineExpected ();
2129 if ((state & PARENT_TAKING) != 0) {
2130 ret = (state & TAKING) == 0;
2138 ifstack.Push (state | ELSE_SEEN);
2143 if (any_token_seen){
2144 Error_TokensSeen ();
2145 return caller_is_taking;
2147 PreProcessDefinition (true, arg, caller_is_taking);
2148 return caller_is_taking;
2151 if (any_token_seen){
2152 Error_TokensSeen ();
2153 return caller_is_taking;
2155 PreProcessDefinition (false, arg, caller_is_taking);
2156 return caller_is_taking;
2160 // These are only processed if we are in a `taking' block
2162 if (!caller_is_taking)
2167 Report.Error (1029, Location, "#error: '" + arg + "'");
2171 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
2175 if (RootContext.Version == LanguageVersion.ISO_1) {
2176 Report.FeatureIsNotISO1 (Location, "#pragma");
2180 PreProcessPragma (arg);
2184 if (!PreProcessLine (arg))
2187 "The line number specified for #line directive is missing or invalid");
2188 return caller_is_taking;
2191 Report.Error (1024, Location, "Wrong preprocessor directive");
2196 private int consume_string (bool quoted)
2199 string_builder.Length = 0;
2201 while ((c = get_char ()) != -1){
2203 if (quoted && peek_char () == '"'){
2204 string_builder.Append ((char) c);
2208 val = string_builder.ToString ();
2209 return Token.LITERAL_STRING;
2215 Report.Error (1010, Location, "Newline in constant");
2223 string_builder.Append ((char) c);
2226 Report.Error (1039, Location, "Unterminated string literal");
2230 private int consume_identifier (int s)
2232 int res = consume_identifier (s, false);
2234 if (doc_state == XmlCommentState.Allowed)
2235 doc_state = XmlCommentState.NotAllowed;
2238 case Token.NAMESPACE:
2239 check_incorrect_doc_comment ();
2243 if (res == Token.PARTIAL) {
2244 // Save current position and parse next token.
2247 int next_token = token ();
2248 bool ok = (next_token == Token.CLASS) ||
2249 (next_token == Token.STRUCT) ||
2250 (next_token == Token.INTERFACE);
2255 if (RootContext.Version == LanguageVersion.ISO_1)
2256 Report.FeatureIsNotISO1 (Location, "partial types");
2261 if (next_token < Token.LAST_KEYWORD)
2262 Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
2264 val = new LocatedToken (Location, "partial");
2265 return Token.IDENTIFIER;
2271 private int consume_identifier (int s, bool quoted)
2276 id_builder [0] = (char) s;
2278 current_location = new Location (ref_line, Col);
2280 while ((c = get_char ()) != -1) {
2282 if (is_identifier_part_character ((char) c)){
2283 if (pos == max_id_size){
2284 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
2288 id_builder [pos++] = (char) c;
2289 // putback_char = -1;
2290 } else if (c == '\\') {
2294 // putback_char = c;
2301 // Optimization: avoids doing the keyword lookup
2302 // on uppercase letters and _
2304 if (!quoted && (s >= 'a' || s == '_')){
2305 int keyword = GetKeyword (id_builder, pos);
2306 if (keyword != -1) {
2313 // Keep identifiers in an array of hashtables to avoid needless
2317 if (identifiers [pos] != null) {
2318 val = identifiers [pos][id_builder];
2320 val = new LocatedToken (Location, (string) val);
2322 escaped_identifiers.Add (val);
2323 return Token.IDENTIFIER;
2327 identifiers [pos] = new CharArrayHashtable (pos);
2329 val = new String (id_builder, 0, pos);
2330 if (RootContext.Version == LanguageVersion.ISO_1) {
2331 for (int i = 1; i < id_builder.Length; i += 3) {
2332 if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
2333 Report.Error (1638, Location,
2334 "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
2340 char [] chars = new char [pos];
2341 Array.Copy (id_builder, chars, pos);
2343 identifiers [pos] [chars] = val;
2345 val = new LocatedToken (Location, (string) val);
2347 escaped_identifiers.Add (val);
2348 return Token.IDENTIFIER;
2351 public int xtoken ()
2354 bool doread = false;
2357 // Whether we have seen comments on the current line
2358 bool comments_seen = false;
2360 for (;(c = get_char ()) != -1;) {
2362 col = ((col + 8) / 8) * 8;
2366 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0 || c == 0)
2370 if (peek_char () == '\n')
2373 any_token_seen |= tokens_seen;
2374 tokens_seen = false;
2375 comments_seen = false;
2379 // Handle double-slash comments.
2381 int d = peek_char ();
2385 if (RootContext.Documentation != null && peek_char () == '/') {
2387 // Don't allow ////.
2388 if ((d = peek_char ()) != '/') {
2389 update_comment_location ();
2390 if (doc_state == XmlCommentState.Allowed)
2391 handle_one_line_xml_comment ();
2392 else if (doc_state == XmlCommentState.NotAllowed)
2393 warn_incorrect_doc_comment ();
2396 while ((d = get_char ()) != -1 && (d != '\n') && d != '\r')
2399 any_token_seen |= tokens_seen;
2400 tokens_seen = false;
2401 comments_seen = false;
2403 } else if (d == '*'){
2405 bool docAppend = false;
2406 if (RootContext.Documentation != null && peek_char () == '*') {
2408 update_comment_location ();
2409 // But when it is /**/, just do nothing.
2410 if (peek_char () == '/') {
2414 if (doc_state == XmlCommentState.Allowed)
2416 else if (doc_state == XmlCommentState.NotAllowed)
2417 warn_incorrect_doc_comment ();
2420 int current_comment_start = 0;
2422 current_comment_start = xml_comment_buffer.Length;
2423 xml_comment_buffer.Append (Environment.NewLine);
2426 Location start_location = Location;
2428 while ((d = get_char ()) != -1){
2429 if (d == '*' && peek_char () == '/'){
2431 comments_seen = true;
2435 xml_comment_buffer.Append ((char) d);
2438 any_token_seen |= tokens_seen;
2439 tokens_seen = false;
2441 // Reset 'comments_seen' just to be consistent.
2442 // It doesn't matter either way, here.
2444 comments_seen = false;
2448 Report.Error (1035, start_location, "End-of-file found, '*/' expected");
2451 update_formatted_doc_comment (current_comment_start);
2454 goto is_punct_label;
2458 if (c == '\\' || is_identifier_start_character ((char)c)){
2460 return consume_identifier (c);
2464 current_location = new Location (ref_line, Col);
2465 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
2475 any_token_seen |= tokens_seen;
2476 tokens_seen = false;
2477 comments_seen = false;
2481 if (c >= '0' && c <= '9'){
2483 return is_number (c);
2488 int peek = peek_char ();
2489 if (peek >= '0' && peek <= '9')
2490 return is_number (c);
2495 if (tokens_seen || comments_seen) {
2496 Eror_WrongPreprocessorLocation ();
2500 if (handle_preprocessing_directive (true))
2503 bool directive_expected = false;
2504 while ((c = get_char ()) != -1) {
2506 directive_expected = true;
2507 } else if (!directive_expected) {
2508 // TODO: Implement comment support for disabled code and uncomment this code
2510 // Eror_WrongPreprocessorLocation ();
2511 // return Token.ERROR;
2516 if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
2520 if (handle_preprocessing_directive (false))
2523 directive_expected = false;
2527 tokens_seen = false;
2535 return consume_string (false);
2541 error_details = "Empty character literal";
2542 Report.Error (1011, Location, error_details);
2545 if (c == '\r' || c == '\n') {
2546 Report.Error (1010, Location, "Newline in constant");
2552 val = new System.Char ();
2557 error_details = "Too many characters in character literal";
2558 Report.Error (1012, Location, error_details);
2560 // Try to recover, read until newline or next "'"
2561 while ((c = get_char ()) != -1){
2570 return Token.LITERAL_CHARACTER;
2577 return consume_string (true);
2578 } else if (is_identifier_start_character ((char) c)){
2579 return consume_identifier (c, true);
2581 Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
2585 error_details = ((char)c).ToString ();
2594 // Handles one line xml comment
2596 private void handle_one_line_xml_comment ()
2599 while ((c = peek_char ()) == ' ')
2600 get_char (); // skip heading whitespaces.
2601 while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') {
2602 xml_comment_buffer.Append ((char) get_char ());
2604 if (c == '\r' || c == '\n')
2605 xml_comment_buffer.Append (Environment.NewLine);
2609 // Remove heading "*" in Javadoc-like xml documentation.
2611 private void update_formatted_doc_comment (int current_comment_start)
2613 int length = xml_comment_buffer.Length - current_comment_start;
2614 string [] lines = xml_comment_buffer.ToString (
2615 current_comment_start,
2616 length).Replace ("\r", "").Split ('\n');
2618 // The first line starts with /**, thus it is not target
2619 // for the format check.
2620 for (int i = 1; i < lines.Length; i++) {
2621 string s = lines [i];
2622 int idx = s.IndexOf ('*');
2625 if (i < lines.Length - 1)
2629 head = s.Substring (0, idx);
2630 foreach (char c in head)
2633 lines [i] = s.Substring (idx + 1);
2635 xml_comment_buffer.Remove (current_comment_start, length);
2636 xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
2640 // Updates current comment location.
2642 private void update_comment_location ()
2644 if (current_comment_location.IsNull) {
2645 // "-2" is for heading "//" or "/*"
2646 current_comment_location =
2647 new Location (ref_line, col - 2);
2652 // Checks if there was incorrect doc comments and raise
2655 public void check_incorrect_doc_comment ()
2657 if (xml_comment_buffer.Length > 0)
2658 warn_incorrect_doc_comment ();
2662 // Raises a warning when tokenizer found incorrect doccomment
2665 private void warn_incorrect_doc_comment ()
2667 if (doc_state != XmlCommentState.Error) {
2668 doc_state = XmlCommentState.Error;
2669 // in csc, it is 'XML comment is not placed on
2670 // a valid language element'. But that does not
2672 Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
2677 // Consumes the saved xml comment lines (if any)
2678 // as for current target member or type.
2680 public string consume_doc_comment ()
2682 if (xml_comment_buffer.Length > 0) {
2683 string ret = xml_comment_buffer.ToString ();
2684 reset_doc_comment ();
2690 void reset_doc_comment ()
2692 xml_comment_buffer.Length = 0;
2693 current_comment_location = Location.Null;
2696 public void cleanup ()
2698 if (ifstack != null && ifstack.Count >= 1) {
2699 current_location = new Location (ref_line, Col);
2700 int state = (int) ifstack.Pop ();
2701 if ((state & REGION) != 0)
2702 Report.Error (1038, Location, "#endregion directive expected");
2704 Report.Error (1027, Location, "Expected `#endif' directive");
2710 // Indicates whether it accepts XML documentation or not.
2712 public enum XmlCommentState {
2713 // comment is allowed in this state.
2715 // comment is not allowed in this state.
2717 // once comments appeared when it is NotAllowed, then the
2718 // state is changed to it, until the state is changed to