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_where = 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 ();
47 public int parsing_block;
53 static bool IsLinqEnabled {
55 return RootContext.Version == LanguageVersion.LINQ;
60 // XML documentation buffer. The save point is used to divide
61 // comments on types and comments on members.
63 StringBuilder xml_comment_buffer;
66 // See comment on XmlCommentState enumeration.
68 XmlCommentState xml_doc_state = XmlCommentState.Allowed;
71 // Whether tokens have been seen on this line
73 bool tokens_seen = false;
76 // Whether a token has been seen on the file
77 // This is needed because `define' is not allowed to be used
78 // after a token has been seen.
80 bool any_token_seen = false;
82 static Hashtable token_values;
83 static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
85 private static Hashtable TokenValueName
88 if (token_values == null)
89 token_values = GetTokenValueNameHash ();
95 private static Hashtable GetTokenValueNameHash ()
97 Type t = typeof (Token);
98 FieldInfo [] fields = t.GetFields ();
99 Hashtable hash = new Hashtable ();
100 foreach (FieldInfo field in fields) {
101 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
102 hash.Add (field.GetValue (null), field.Name);
108 // Returns a verbose representation of the current location
110 public string location {
114 if (current_token == Token.ERROR)
115 det = "detail: " + error_details;
119 // return "Line: "+line+" Col: "+col + "\n" +
120 // "VirtLine: "+ref_line +
121 // " Token: "+current_token + " " + det;
122 string current_token_name = TokenValueName [current_token] as string;
123 if (current_token_name == null)
124 current_token_name = current_token.ToString ();
126 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
134 public bool PropertyParsing {
135 get { return handle_get_set; }
136 set { handle_get_set = value; }
139 public bool AssemblyTargetParsing {
140 get { return handle_assembly; }
141 set { handle_assembly = value; }
144 public bool EventParsing {
145 get { return handle_remove_add; }
146 set { handle_remove_add = value; }
149 public bool ConstraintsParsing {
150 get { return handle_where; }
151 set { handle_where = value; }
154 public bool TypeOfParsing {
155 get { return handle_typeof; }
156 set { handle_typeof = value; }
159 public XmlCommentState doc_state {
160 get { return xml_doc_state; }
162 if (value == XmlCommentState.Allowed) {
163 check_incorrect_doc_comment ();
164 reset_doc_comment ();
166 xml_doc_state = value;
170 public bool IsEscapedIdentifier (Location loc)
172 foreach (LocatedToken lt in escaped_identifiers)
173 if (lt.Location.Equals (loc))
181 static CharArrayHashtable[] keywords;
182 static Hashtable keyword_strings;
183 static NumberStyles styles;
184 static NumberFormatInfo csharp_format_info;
187 // Values for the associated token returned
197 const int TAKING = 1;
198 const int ELSE_SEEN = 4;
199 const int PARENT_TAKING = 8;
200 const int REGION = 16;
203 // pre-processor if stack state:
207 static System.Text.StringBuilder string_builder;
209 const int max_id_size = 512;
210 static char [] id_builder = new char [max_id_size];
212 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
214 const int max_number_size = 512;
215 static char [] number_builder = new char [max_number_size];
216 static int number_pos;
219 // Details about the error encoutered by the tokenizer
221 string error_details;
223 public string error {
225 return error_details;
242 // This is used when the tokenizer needs to save
243 // the current position as it needs to do some parsing
244 // on its own to deamiguate a token in behalf of the
247 Stack position_stack = new Stack (2);
252 public int putback_char;
253 public int previous_col;
254 public Stack ifstack;
255 public int parsing_generic_less_than;
256 public int current_token;
258 public Position (Tokenizer t)
260 position = t.reader.Position;
261 ref_line = t.ref_line;
263 putback_char = t.putback_char;
264 previous_col = t.previous_col;
265 if (t.ifstack != null && t.ifstack.Count != 0)
266 ifstack = (Stack)t.ifstack.Clone ();
267 parsing_generic_less_than = t.parsing_generic_less_than;
268 current_token = t.current_token;
272 public void PushPosition ()
274 position_stack.Push (new Position (this));
277 public void PopPosition ()
279 Position p = (Position) position_stack.Pop ();
281 reader.Position = p.position;
282 ref_line = p.ref_line;
284 putback_char = p.putback_char;
285 previous_col = p.previous_col;
287 parsing_generic_less_than = p.parsing_generic_less_than;
288 current_token = p.current_token;
291 // Do not reset the position, ignore it.
292 public void DiscardPosition ()
294 position_stack.Pop ();
297 static void AddKeyword (string kw, int token)
299 keyword_strings.Add (kw, kw);
300 if (keywords [kw.Length] == null) {
301 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
303 keywords [kw.Length] [kw.ToCharArray ()] = token;
306 static void InitTokens ()
308 keyword_strings = new Hashtable ();
309 keywords = new CharArrayHashtable [64];
311 AddKeyword ("__arglist", Token.ARGLIST);
312 AddKeyword ("abstract", Token.ABSTRACT);
313 AddKeyword ("as", Token.AS);
314 AddKeyword ("add", Token.ADD);
315 AddKeyword ("assembly", Token.ASSEMBLY);
316 AddKeyword ("base", Token.BASE);
317 AddKeyword ("bool", Token.BOOL);
318 AddKeyword ("break", Token.BREAK);
319 AddKeyword ("byte", Token.BYTE);
320 AddKeyword ("case", Token.CASE);
321 AddKeyword ("catch", Token.CATCH);
322 AddKeyword ("char", Token.CHAR);
323 AddKeyword ("checked", Token.CHECKED);
324 AddKeyword ("class", Token.CLASS);
325 AddKeyword ("const", Token.CONST);
326 AddKeyword ("continue", Token.CONTINUE);
327 AddKeyword ("decimal", Token.DECIMAL);
328 AddKeyword ("default", Token.DEFAULT);
329 AddKeyword ("delegate", Token.DELEGATE);
330 AddKeyword ("do", Token.DO);
331 AddKeyword ("double", Token.DOUBLE);
332 AddKeyword ("else", Token.ELSE);
333 AddKeyword ("enum", Token.ENUM);
334 AddKeyword ("event", Token.EVENT);
335 AddKeyword ("explicit", Token.EXPLICIT);
336 AddKeyword ("extern", Token.EXTERN);
337 AddKeyword ("false", Token.FALSE);
338 AddKeyword ("finally", Token.FINALLY);
339 AddKeyword ("fixed", Token.FIXED);
340 AddKeyword ("float", Token.FLOAT);
341 AddKeyword ("for", Token.FOR);
342 AddKeyword ("foreach", Token.FOREACH);
343 AddKeyword ("goto", Token.GOTO);
344 AddKeyword ("get", Token.GET);
345 AddKeyword ("if", Token.IF);
346 AddKeyword ("implicit", Token.IMPLICIT);
347 AddKeyword ("in", Token.IN);
348 AddKeyword ("int", Token.INT);
349 AddKeyword ("interface", Token.INTERFACE);
350 AddKeyword ("internal", Token.INTERNAL);
351 AddKeyword ("is", Token.IS);
352 AddKeyword ("lock", Token.LOCK);
353 AddKeyword ("long", Token.LONG);
354 AddKeyword ("namespace", Token.NAMESPACE);
355 AddKeyword ("new", Token.NEW);
356 AddKeyword ("null", Token.NULL);
357 AddKeyword ("object", Token.OBJECT);
358 AddKeyword ("operator", Token.OPERATOR);
359 AddKeyword ("out", Token.OUT);
360 AddKeyword ("override", Token.OVERRIDE);
361 AddKeyword ("params", Token.PARAMS);
362 AddKeyword ("private", Token.PRIVATE);
363 AddKeyword ("protected", Token.PROTECTED);
364 AddKeyword ("public", Token.PUBLIC);
365 AddKeyword ("readonly", Token.READONLY);
366 AddKeyword ("ref", Token.REF);
367 AddKeyword ("remove", Token.REMOVE);
368 AddKeyword ("return", Token.RETURN);
369 AddKeyword ("sbyte", Token.SBYTE);
370 AddKeyword ("sealed", Token.SEALED);
371 AddKeyword ("set", Token.SET);
372 AddKeyword ("short", Token.SHORT);
373 AddKeyword ("sizeof", Token.SIZEOF);
374 AddKeyword ("stackalloc", Token.STACKALLOC);
375 AddKeyword ("static", Token.STATIC);
376 AddKeyword ("string", Token.STRING);
377 AddKeyword ("struct", Token.STRUCT);
378 AddKeyword ("switch", Token.SWITCH);
379 AddKeyword ("this", Token.THIS);
380 AddKeyword ("throw", Token.THROW);
381 AddKeyword ("true", Token.TRUE);
382 AddKeyword ("try", Token.TRY);
383 AddKeyword ("typeof", Token.TYPEOF);
384 AddKeyword ("uint", Token.UINT);
385 AddKeyword ("ulong", Token.ULONG);
386 AddKeyword ("unchecked", Token.UNCHECKED);
387 AddKeyword ("unsafe", Token.UNSAFE);
388 AddKeyword ("ushort", Token.USHORT);
389 AddKeyword ("using", Token.USING);
390 AddKeyword ("virtual", Token.VIRTUAL);
391 AddKeyword ("void", Token.VOID);
392 AddKeyword ("volatile", Token.VOLATILE);
393 AddKeyword ("while", Token.WHILE);
394 AddKeyword ("partial", Token.PARTIAL);
396 AddKeyword ("where", Token.WHERE);
401 public static void InitializeLinqKeywords ()
403 AddKeyword ("from", Token.FROM);
404 AddKeyword ("join", Token.JOIN);
405 AddKeyword ("on", Token.ON);
406 AddKeyword ("equals", Token.EQUALS);
407 AddKeyword ("select", Token.SELECT);
408 AddKeyword ("group", Token.GROUP);
409 AddKeyword ("by", Token.BY);
410 AddKeyword ("let", Token.LET);
411 AddKeyword ("orderby", Token.ORDERBY);
412 AddKeyword ("ascending", Token.ASCENDING);
413 AddKeyword ("descending", Token.DESCENDING);
414 AddKeyword ("into", Token.INTO);
426 public static void Reset ()
429 csharp_format_info = NumberFormatInfo.InvariantInfo;
430 styles = NumberStyles.Float;
432 string_builder = new System.Text.StringBuilder ();
435 int GetKeyword (char[] id, int id_len)
438 * Keywords are stored in an array of hashtables grouped by their
442 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
444 object o = keywords [id_len] [id];
451 if (!handle_get_set && (res == Token.GET || res == Token.SET))
453 if (!handle_remove_add && (res == Token.REMOVE || res == Token.ADD))
455 if (!handle_assembly && res == Token.ASSEMBLY)
459 if (res == Token.FROM &&
460 (current_token == Token.ASSIGN || current_token == Token.OPEN_BRACKET ||
461 current_token == Token.RETURN || current_token == Token.IN)) {
462 query_parsing = true;
466 if (!query_parsing && res > Token.QUERY_FIRST_TOKEN && res < Token.QUERY_LAST_TOKEN)
472 if (!handle_where && res == Token.WHERE)
479 public Location Location {
480 get { return current_location; }
483 void define (string def)
485 if (!RootContext.AllDefines.Contains (def)){
486 RootContext.AllDefines [def] = true;
488 if (defines.Contains (def))
490 defines [def] = true;
493 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
495 this.ref_name = file;
496 this.file_name = file;
502 defines = new Hashtable ();
503 foreach (string def in defs)
507 xml_comment_buffer = new StringBuilder ();
510 // FIXME: This could be `Location.Push' but we have to
511 // find out why the MS compiler allows this
513 Mono.CSharp.Location.Push (file);
516 static bool is_identifier_start_character (char c)
518 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter (c);
521 static bool is_identifier_part_character (char c)
523 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') ||
524 Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
527 public static bool IsKeyword (string s)
529 return keyword_strings [s] != null;
533 // Tests whether '(' is beggining of lambda parameters
535 bool IsLambdaOpenParens ()
538 while ((ntoken = xtoken ()) != Token.EOF) {
540 case Token.CLOSE_PARENS:
541 return xtoken () == Token.ARROW;
544 case Token.SEMICOLON:
545 case Token.OPEN_PARENS:
550 Error_TokenExpected (")");
554 public static bool IsValidIdentifier (string s)
556 if (s == null || s.Length == 0)
559 if (!is_identifier_start_character (s [0]))
562 for (int i = 1; i < s.Length; i ++)
563 if (! is_identifier_part_character (s [i]))
569 bool parse_less_than ()
572 int the_token = token ();
573 if (the_token == Token.OPEN_BRACKET) {
575 the_token = token ();
576 } while (the_token != Token.CLOSE_BRACKET);
577 the_token = token ();
580 case Token.IDENTIFIER:
603 the_token = token ();
605 if (the_token == Token.OP_GENERICS_GT)
607 else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
609 else if (the_token == Token.INTERR || the_token == Token.STAR)
611 else if (the_token == Token.OP_GENERICS_LT) {
612 if (!parse_less_than ())
615 } else if (the_token == Token.OPEN_BRACKET) {
617 the_token = token ();
618 if (the_token == Token.CLOSE_BRACKET)
620 else if (the_token == Token.COMMA)
621 goto rank_specifiers;
629 public void PutbackNullable ()
631 if (nullable_pos < 0)
632 throw new Exception ();
636 reader.Position = nullable_pos;
641 public void PutbackCloseParens ()
647 int nullable_pos = -1;
649 public void CheckNullable (bool is_nullable)
652 nullable_pos = reader.Position;
657 public bool QueryParsing {
659 query_parsing = value;
663 bool parse_generic_dimension (out int dimension)
668 int the_token = token ();
669 if (the_token == Token.OP_GENERICS_GT)
671 else if (the_token == Token.COMMA) {
680 public int peek_token ()
685 the_token = token ();
691 int parsing_generic_less_than = 0;
693 int is_punct (char c, ref bool doread)
703 return Token.OPEN_BRACE;
706 return Token.CLOSE_BRACE;
708 // To block doccomment inside attribute declaration.
709 if (doc_state == XmlCommentState.Allowed)
710 doc_state = XmlCommentState.NotAllowed;
711 return Token.OPEN_BRACKET;
713 return Token.CLOSE_BRACKET;
715 if (IsLinqEnabled && !lambda_arguments_parsing) {
716 lambda_arguments_parsing = true;
718 bool lambda_start = IsLambdaOpenParens ();
720 lambda_arguments_parsing = false;
722 return Token.OPEN_PARENS_LAMBDA;
724 return Token.OPEN_PARENS;
726 if (deambiguate_close_parens == 0)
727 return Token.CLOSE_PARENS;
729 --deambiguate_close_parens;
733 int new_token = xtoken ();
737 if (new_token == Token.OPEN_PARENS)
738 return Token.CLOSE_PARENS_OPEN_PARENS;
739 else if (new_token == Token.MINUS)
740 return Token.CLOSE_PARENS_MINUS;
741 else if (IsCastToken (new_token))
742 return Token.CLOSE_PARENS_CAST;
744 return Token.CLOSE_PARENS_NO_CAST;
751 return Token.SEMICOLON;
760 return Token.OP_COALESCING;
767 if (parsing_generic_less_than++ > 0)
768 return Token.OP_GENERICS_LT;
773 if (parse_generic_dimension (out dimension)) {
776 return Token.GENERIC_DIMENSION;
781 // Save current position and parse next token.
783 bool is_generic_lt = parse_less_than ();
787 return Token.OP_GENERICS_LT;
789 parsing_generic_less_than = 0;
798 return Token.OP_SHIFT_LEFT_ASSIGN;
800 return Token.OP_SHIFT_LEFT;
801 } else if (d == '='){
806 } else if (c == '>') {
807 if (parsing_generic_less_than > 0) {
808 parsing_generic_less_than--;
809 return Token.OP_GENERICS_GT;
819 return Token.OP_SHIFT_RIGHT_ASSIGN;
821 return Token.OP_SHIFT_RIGHT;
822 } else if (d == '='){
837 t = Token.OP_ADD_ASSIGN;
851 t = Token.OP_SUB_ASSIGN;
889 } else if (d == '='){
891 return Token.OP_AND_ASSIGN;
894 return Token.BITWISE_AND;
901 } else if (d == '='){
903 return Token.OP_OR_ASSIGN;
905 return Token.BITWISE_OR;
911 return Token.OP_MULT_ASSIGN;
920 return Token.OP_DIV_ASSIGN;
928 return Token.OP_MOD_ASSIGN;
930 return Token.PERCENT;
936 return Token.OP_XOR_ASSIGN;
949 return Token.OP_SHIFT_LEFT_ASSIGN;
951 return Token.OP_SHIFT_LEFT;
952 } else if (d == '='){
966 return Token.OP_SHIFT_RIGHT_ASSIGN;
968 return Token.OP_SHIFT_RIGHT;
969 } else if (d == '='){
979 return Token.DOUBLE_COLON;
988 int deambiguate_close_parens = 0;
990 public void Deambiguate_CloseParens (object expression)
994 // When any binary operation is used we are sure it is not a cast
995 if (expression is Binary)
998 deambiguate_close_parens++;
1001 bool decimal_digits (int c)
1004 bool seen_digits = false;
1007 if (number_pos == max_number_size)
1008 Error_NumericConstantTooLong ();
1009 number_builder [number_pos++] = (char) c;
1013 // We use peek_char2, because decimal_digits needs to do a
1014 // 2-character look-ahead (5.ToString for example).
1016 while ((d = peek_char2 ()) != -1){
1017 if (d >= '0' && d <= '9'){
1018 if (number_pos == max_number_size)
1019 Error_NumericConstantTooLong ();
1020 number_builder [number_pos++] = (char) d;
1030 static bool is_hex (int e)
1032 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1035 static int real_type_suffix (int c)
1041 t = Token.LITERAL_FLOAT;
1044 t = Token.LITERAL_DOUBLE;
1047 t= Token.LITERAL_DECIMAL;
1055 int integer_type_suffix (ulong ul, int c)
1057 bool is_unsigned = false;
1058 bool is_long = false;
1061 bool scanning = true;
1074 // if we have not seen anything in between
1075 // report this error
1077 Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
1080 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
1081 // work around that.
1105 if (is_long && is_unsigned){
1107 return Token.LITERAL_INTEGER;
1108 } else if (is_unsigned){
1109 // uint if possible, or ulong else.
1111 if ((ul & 0xffffffff00000000) == 0)
1115 } else if (is_long){
1116 // long if possible, ulong otherwise
1117 if ((ul & 0x8000000000000000) != 0)
1122 // int, uint, long or ulong in that order
1123 if ((ul & 0xffffffff00000000) == 0){
1124 uint ui = (uint) ul;
1126 if ((ui & 0x80000000) != 0)
1131 if ((ul & 0x8000000000000000) != 0)
1137 return Token.LITERAL_INTEGER;
1141 // given `c' as the next char in the input decide whether
1142 // we need to convert to a special type, and then choose
1143 // the best representation for the integer
1145 int adjust_int (int c)
1148 if (number_pos > 9){
1149 ulong ul = (uint) (number_builder [0] - '0');
1151 for (int i = 1; i < number_pos; i++){
1152 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
1154 return integer_type_suffix (ul, c);
1156 uint ui = (uint) (number_builder [0] - '0');
1158 for (int i = 1; i < number_pos; i++){
1159 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
1161 return integer_type_suffix (ui, c);
1163 } catch (OverflowException) {
1164 error_details = "Integral constant is too large";
1165 Report.Error (1021, Location, error_details);
1167 return Token.LITERAL_INTEGER;
1169 catch (FormatException) {
1170 Report.Error (1013, Location, "Invalid number");
1172 return Token.LITERAL_INTEGER;
1176 int adjust_real (int t)
1178 string s = new String (number_builder, 0, number_pos);
1179 const string error_details = "Floating-point constant is outside the range of type `{0}'";
1182 case Token.LITERAL_DECIMAL:
1184 val = System.Decimal.Parse (s, styles, csharp_format_info);
1185 } catch (OverflowException) {
1187 Report.Error (594, Location, error_details, "decimal");
1190 case Token.LITERAL_FLOAT:
1192 val = float.Parse (s, styles, csharp_format_info);
1193 } catch (OverflowException) {
1195 Report.Error (594, Location, error_details, "float");
1199 case Token.LITERAL_DOUBLE:
1201 t = Token.LITERAL_DOUBLE;
1203 val = System.Double.Parse (s, styles, csharp_format_info);
1204 } catch (OverflowException) {
1206 Report.Error (594, Location, error_details, "double");
1219 while ((d = peek_char ()) != -1){
1221 number_builder [number_pos++] = (char) d;
1227 string s = new String (number_builder, 0, number_pos);
1229 if (number_pos <= 8)
1230 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
1232 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
1233 } catch (OverflowException){
1234 error_details = "Integral constant is too large";
1235 Report.Error (1021, Location, error_details);
1237 return Token.LITERAL_INTEGER;
1239 catch (FormatException) {
1240 Report.Error (1013, Location, "Invalid number");
1242 return Token.LITERAL_INTEGER;
1245 return integer_type_suffix (ul, peek_char ());
1249 // Invoked if we know we have .digits or digits
1251 int is_number (int c)
1253 bool is_real = false;
1258 if (c >= '0' && c <= '9'){
1260 int peek = peek_char ();
1262 if (peek == 'x' || peek == 'X')
1263 return handle_hex ();
1270 // We need to handle the case of
1271 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1274 if (decimal_digits ('.')){
1280 return adjust_int (-1);
1284 if (c == 'e' || c == 'E'){
1286 if (number_pos == max_number_size)
1287 Error_NumericConstantTooLong ();
1288 number_builder [number_pos++] = 'e';
1292 if (number_pos == max_number_size)
1293 Error_NumericConstantTooLong ();
1294 number_builder [number_pos++] = '+';
1296 } else if (c == '-') {
1297 if (number_pos == max_number_size)
1298 Error_NumericConstantTooLong ();
1299 number_builder [number_pos++] = '-';
1302 if (number_pos == max_number_size)
1303 Error_NumericConstantTooLong ();
1304 number_builder [number_pos++] = '+';
1311 type = real_type_suffix (c);
1312 if (type == Token.NONE && !is_real){
1314 return adjust_int (c);
1318 if (type == Token.NONE){
1323 return adjust_real (type);
1325 Console.WriteLine ("This should not be reached");
1326 throw new Exception ("Is Number should never reach this point");
1330 // Accepts exactly count (4 or 8) hex, no more no less
1332 int getHex (int count, out bool error)
1337 int top = count != -1 ? count : 4;
1341 for (i = 0; i < top; i++){
1344 if (c >= '0' && c <= '9')
1345 c = (int) c - (int) '0';
1346 else if (c >= 'A' && c <= 'F')
1347 c = (int) c - (int) 'A' + 10;
1348 else if (c >= 'a' && c <= 'f')
1349 c = (int) c - (int) 'a' + 10;
1355 total = (total * 16) + c;
1357 int p = peek_char ();
1360 if (!is_hex ((char)p))
1401 v = getHex (-1, out error);
1406 v = getHex (4, out error);
1411 v = getHex (8, out error);
1416 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1426 if (putback_char != -1) {
1444 if (putback_char != -1)
1445 return putback_char;
1446 putback_char = reader.Read ();
1447 return putback_char;
1452 if (putback_char != -1)
1453 return putback_char;
1454 return reader.Peek ();
1457 void putback (int c)
1459 if (putback_char != -1){
1460 Console.WriteLine ("Col: " + col);
1461 Console.WriteLine ("Row: " + line);
1462 Console.WriteLine ("Name: " + ref_name.Name);
1463 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
1464 throw new Exception ("This should not happen putback on putback");
1466 if (c == '\n' || col == 0) {
1467 // It won't happen though.
1477 public bool advance ()
1479 return peek_char () != -1;
1482 public Object Value {
1488 public Object value ()
1493 static bool IsCastToken (int token)
1498 case Token.IDENTIFIER:
1499 case Token.LITERAL_INTEGER:
1500 case Token.LITERAL_FLOAT:
1501 case Token.LITERAL_DOUBLE:
1502 case Token.LITERAL_DECIMAL:
1503 case Token.LITERAL_CHARACTER:
1504 case Token.LITERAL_STRING:
1507 case Token.DELEGATE:
1517 case Token.UNCHECKED:
1524 // These can be part of a member access
1544 current_token = xtoken ();
1547 if (current_token != Token.DEFAULT)
1548 return current_token;
1553 current_token = Token.ERROR;
1554 else if (c == Token.OPEN_PARENS)
1555 current_token = Token.DEFAULT_OPEN_PARENS;
1556 else if (c == Token.COLON)
1557 current_token = Token.DEFAULT_COLON;
1561 return current_token;
1564 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
1566 void get_cmd_arg (out string cmd, out string arg)
1570 tokens_seen = false;
1572 static_cmd_arg.Length = 0;
1574 // skip over white space
1575 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1578 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
1579 if (is_identifier_part_character ((char) c)){
1580 static_cmd_arg.Append ((char) c);
1588 cmd = static_cmd_arg.ToString ();
1590 if (c == '\n' || c == '\r'){
1594 // skip over white space
1595 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1600 } else if (c == '\r'){
1602 } else if (c == -1){
1607 static_cmd_arg.Length = 0;
1608 static_cmd_arg.Append ((char) c);
1610 while ((c = get_char ()) != -1 && (c != '\n') && (c != '\r')){
1611 static_cmd_arg.Append ((char) c);
1614 arg = static_cmd_arg.ToString ();
1618 // Handles the #line directive
1620 bool PreProcessLine (string arg)
1622 if (arg.Length == 0)
1625 if (arg == "default"){
1627 ref_name = file_name;
1628 Location.Push (ref_name);
1630 } else if (arg == "hidden"){
1632 // We ignore #line hidden
1640 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
1641 ref_line = System.Int32.Parse (arg.Substring (0, pos));
1644 char [] quotes = { '\"' };
1646 string name = arg.Substring (pos). Trim (quotes);
1647 ref_name = Location.LookupFile (name);
1648 file_name.HasLineDirective = true;
1649 ref_name.HasLineDirective = true;
1650 Location.Push (ref_name);
1652 ref_line = System.Int32.Parse (arg);
1662 // Handles #define and #undef
1664 void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
1666 if (arg.Length == 0 || arg == "true" || arg == "false"){
1667 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
1671 if (arg.IndexOfAny (simple_whitespaces) != -1){
1672 Error_EndLineExpected ();
1676 if (!is_identifier_start_character (arg [0]))
1677 Report.Error (1001, Location, "Identifier expected: " + arg);
1679 foreach (char c in arg.Substring (1)){
1680 if (!is_identifier_part_character (c)){
1681 Report.Error (1001, Location, "Identifier expected: " + arg);
1686 if (!caller_is_taking)
1690 if (defines == null)
1691 defines = new Hashtable ();
1694 if (defines == null)
1696 if (defines.Contains (arg))
1697 defines.Remove (arg);
1702 /// Handles #pragma directive
1704 void PreProcessPragma (string arg)
1706 const string warning = "warning";
1707 const string w_disable = "warning disable";
1708 const string w_restore = "warning restore";
1710 if (arg == w_disable) {
1711 Report.RegisterWarningRegion (Location).WarningDisable (line);
1715 if (arg == w_restore) {
1716 Report.RegisterWarningRegion (Location).WarningEnable (line);
1720 if (arg.StartsWith (w_disable)) {
1721 int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
1722 foreach (int code in codes) {
1724 Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
1729 if (arg.StartsWith (w_restore)) {
1730 int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
1731 Hashtable w_table = Report.warning_ignore_table;
1732 foreach (int code in codes) {
1733 if (w_table != null && w_table.Contains (code))
1734 Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
1735 Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
1740 if (arg.StartsWith (warning)) {
1741 Report.Warning (1634, 1, Location, "Expected disable or restore");
1745 Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
1748 int[] ParseNumbers (string text)
1750 string[] string_array = text.Split (',');
1751 int[] values = new int [string_array.Length];
1753 foreach (string string_code in string_array) {
1755 values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
1757 catch (FormatException) {
1758 Report.Warning (1692, 1, Location, "Invalid number");
1764 bool eval_val (string s)
1771 if (defines == null)
1773 if (defines.Contains (s))
1779 bool pp_primary (ref string s)
1788 s = s.Substring (1);
1789 bool val = pp_expr (ref s, false);
1790 if (s.Length > 0 && s [0] == ')'){
1791 s = s.Substring (1);
1794 Error_InvalidDirective ();
1798 if (is_identifier_start_character (c)){
1804 if (is_identifier_part_character (c)){
1808 bool v = eval_val (s.Substring (0, j));
1809 s = s.Substring (j);
1812 bool vv = eval_val (s);
1817 Error_InvalidDirective ();
1821 bool pp_unary (ref string s)
1828 if (len > 1 && s [1] == '='){
1829 Error_InvalidDirective ();
1832 s = s.Substring (1);
1833 return ! pp_primary (ref s);
1835 return pp_primary (ref s);
1837 Error_InvalidDirective ();
1842 bool pp_eq (ref string s)
1844 bool va = pp_unary (ref s);
1850 if (len > 2 && s [1] == '='){
1851 s = s.Substring (2);
1852 return va == pp_unary (ref s);
1854 Error_InvalidDirective ();
1857 } else if (s [0] == '!' && len > 1 && s [1] == '='){
1858 s = s.Substring (2);
1860 return va != pp_unary (ref s);
1869 bool pp_and (ref string s)
1871 bool va = pp_eq (ref s);
1877 if (len > 2 && s [1] == '&'){
1878 s = s.Substring (2);
1879 return (va & pp_and (ref s));
1881 Error_InvalidDirective ();
1890 // Evaluates an expression for `#if' or `#elif'
1892 bool pp_expr (ref string s, bool isTerm)
1894 bool va = pp_and (ref s);
1901 if (len > 2 && s [1] == '|'){
1902 s = s.Substring (2);
1903 return va | pp_expr (ref s, isTerm);
1905 Error_InvalidDirective ();
1910 Error_EndLineExpected ();
1918 bool eval (string s)
1920 bool v = pp_expr (ref s, true);
1929 void Error_NumericConstantTooLong ()
1931 Report.Error (1021, Location, "Numeric constant too long");
1934 void Error_InvalidDirective ()
1936 Report.Error (1517, Location, "Invalid preprocessor directive");
1939 void Error_UnexpectedDirective (string extra)
1943 "Unexpected processor directive (" + extra + ")");
1946 void Error_TokenExpected (string token)
1948 Report.Error (1026, Location, "Expecting `{0}'", token);
1951 void Error_TokensSeen ()
1953 Report.Error (1032, Location,
1954 "Cannot define or undefine preprocessor symbols after first token in file");
1957 void Eror_WrongPreprocessorLocation ()
1959 Report.Error (1040, Location,
1960 "Preprocessor directives must appear as the first non-whitespace character on a line");
1963 void Error_EndLineExpected ()
1965 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
1969 // if true, then the code continues processing the code
1970 // if false, the code stays in a loop until another directive is
1972 // When caller_is_taking is false we ignore all directives except the ones
1973 // which can help us to identify where the #if block ends
1974 bool handle_preprocessing_directive (bool caller_is_taking)
1977 bool region_directive = false;
1979 get_cmd_arg (out cmd, out arg);
1981 // Eat any trailing whitespaces and single-line comments
1982 if (arg.IndexOf ("//") != -1)
1983 arg = arg.Substring (0, arg.IndexOf ("//"));
1984 arg = arg.Trim (simple_whitespaces);
1987 // The first group of pre-processing instructions is always processed
1991 region_directive = true;
1996 if (ifstack == null || ifstack.Count == 0){
1997 Error_UnexpectedDirective ("no #region for this #endregion");
2000 int pop = (int) ifstack.Pop ();
2002 if ((pop & REGION) == 0)
2003 Report.Error (1027, Location, "Expected `#endif' directive");
2005 return caller_is_taking;
2008 if (ifstack == null)
2009 ifstack = new Stack (2);
2011 int flags = region_directive ? REGION : 0;
2012 if (ifstack.Count == 0){
2013 flags |= PARENT_TAKING;
2015 int state = (int) ifstack.Peek ();
2016 if ((state & TAKING) != 0) {
2017 flags |= PARENT_TAKING;
2021 if (caller_is_taking && eval (arg)) {
2022 ifstack.Push (flags | TAKING);
2025 ifstack.Push (flags);
2029 if (ifstack == null || ifstack.Count == 0){
2030 Error_UnexpectedDirective ("no #if for this #endif");
2033 pop = (int) ifstack.Pop ();
2035 if ((pop & REGION) != 0)
2036 Report.Error (1038, Location, "#endregion directive expected");
2038 if (arg.Length != 0) {
2039 Error_EndLineExpected ();
2042 if (ifstack.Count == 0)
2045 int state = (int) ifstack.Peek ();
2046 return (state & TAKING) != 0;
2050 if (ifstack == null || ifstack.Count == 0){
2051 Error_UnexpectedDirective ("no #if for this #elif");
2054 int state = (int) ifstack.Pop ();
2056 if ((state & REGION) != 0) {
2057 Report.Error (1038, Location, "#endregion directive expected");
2061 if ((state & ELSE_SEEN) != 0){
2062 Error_UnexpectedDirective ("#elif not valid after #else");
2066 if ((state & TAKING) != 0) {
2071 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
2072 ifstack.Push (state | TAKING);
2076 ifstack.Push (state);
2081 if (ifstack == null || ifstack.Count == 0){
2082 Error_UnexpectedDirective ("no #if for this #else");
2085 int state = (int) ifstack.Peek ();
2087 if ((state & REGION) != 0) {
2088 Report.Error (1038, Location, "#endregion directive expected");
2092 if ((state & ELSE_SEEN) != 0){
2093 Error_UnexpectedDirective ("#else within #else");
2099 if (arg.Length != 0) {
2100 Error_EndLineExpected ();
2105 if ((state & PARENT_TAKING) != 0) {
2106 ret = (state & TAKING) == 0;
2114 ifstack.Push (state | ELSE_SEEN);
2119 if (any_token_seen){
2120 Error_TokensSeen ();
2121 return caller_is_taking;
2123 PreProcessDefinition (true, arg, caller_is_taking);
2124 return caller_is_taking;
2127 if (any_token_seen){
2128 Error_TokensSeen ();
2129 return caller_is_taking;
2131 PreProcessDefinition (false, arg, caller_is_taking);
2132 return caller_is_taking;
2136 // These are only processed if we are in a `taking' block
2138 if (!caller_is_taking)
2143 Report.Error (1029, Location, "#error: '" + arg + "'");
2147 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
2151 if (RootContext.Version == LanguageVersion.ISO_1) {
2152 Report.FeatureIsNotISO1 (Location, "#pragma");
2156 PreProcessPragma (arg);
2160 if (!PreProcessLine (arg))
2163 "The line number specified for #line directive is missing or invalid");
2164 return caller_is_taking;
2167 Report.Error (1024, Location, "Wrong preprocessor directive");
2172 private int consume_string (bool quoted)
2175 string_builder.Length = 0;
2177 while ((c = get_char ()) != -1){
2179 if (quoted && peek_char () == '"'){
2180 string_builder.Append ((char) c);
2184 val = string_builder.ToString ();
2185 return Token.LITERAL_STRING;
2191 Report.Error (1010, Location, "Newline in constant");
2199 string_builder.Append ((char) c);
2202 Report.Error (1039, Location, "Unterminated string literal");
2206 private int consume_identifier (int s)
2208 int res = consume_identifier (s, false);
2210 if (doc_state == XmlCommentState.Allowed)
2211 doc_state = XmlCommentState.NotAllowed;
2214 case Token.NAMESPACE:
2215 check_incorrect_doc_comment ();
2219 if (res == Token.PARTIAL) {
2220 if (parsing_block > 0) {
2221 val = new LocatedToken (Location, "partial");
2222 return Token.IDENTIFIER;
2225 // Save current position and parse next token.
2228 int next_token = token ();
2229 bool ok = (next_token == Token.CLASS) ||
2230 (next_token == Token.STRUCT) ||
2231 (next_token == Token.INTERFACE);
2236 if (RootContext.Version == LanguageVersion.ISO_1)
2237 Report.FeatureIsNotISO1 (Location, "partial types");
2242 if (next_token < Token.LAST_KEYWORD)
2243 Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
2245 val = new LocatedToken (Location, "partial");
2246 return Token.IDENTIFIER;
2252 private int consume_identifier (int s, bool quoted)
2257 id_builder [0] = (char) s;
2259 current_location = new Location (ref_line, Col);
2261 while ((c = get_char ()) != -1) {
2263 if (is_identifier_part_character ((char) c)){
2264 if (pos == max_id_size){
2265 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
2269 id_builder [pos++] = (char) c;
2270 // putback_char = -1;
2271 } else if (c == '\\') {
2275 // putback_char = c;
2282 // Optimization: avoids doing the keyword lookup
2283 // on uppercase letters and _
2285 if (!quoted && (s >= 'a' || s == '_')){
2286 int keyword = GetKeyword (id_builder, pos);
2287 if (keyword != -1) {
2294 // Keep identifiers in an array of hashtables to avoid needless
2298 if (identifiers [pos] != null) {
2299 val = identifiers [pos][id_builder];
2301 val = new LocatedToken (Location, (string) val);
2303 escaped_identifiers.Add (val);
2304 return Token.IDENTIFIER;
2308 identifiers [pos] = new CharArrayHashtable (pos);
2310 val = new String (id_builder, 0, pos);
2311 if (RootContext.Version == LanguageVersion.ISO_1) {
2312 for (int i = 1; i < id_builder.Length; i += 3) {
2313 if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
2314 Report.Error (1638, Location,
2315 "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
2321 char [] chars = new char [pos];
2322 Array.Copy (id_builder, chars, pos);
2324 identifiers [pos] [chars] = val;
2326 val = new LocatedToken (Location, (string) val);
2328 escaped_identifiers.Add (val);
2329 return Token.IDENTIFIER;
2332 public int xtoken ()
2335 bool doread = false;
2338 // Whether we have seen comments on the current line
2339 bool comments_seen = false;
2341 for (;(c = get_char ()) != -1;) {
2343 col = ((col + 8) / 8) * 8;
2347 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0 || c == 0)
2351 if (peek_char () == '\n')
2354 any_token_seen |= tokens_seen;
2355 tokens_seen = false;
2356 comments_seen = false;
2360 // Handle double-slash comments.
2362 int d = peek_char ();
2366 if (RootContext.Documentation != null && peek_char () == '/') {
2368 // Don't allow ////.
2369 if ((d = peek_char ()) != '/') {
2370 update_comment_location ();
2371 if (doc_state == XmlCommentState.Allowed)
2372 handle_one_line_xml_comment ();
2373 else if (doc_state == XmlCommentState.NotAllowed)
2374 warn_incorrect_doc_comment ();
2377 while ((d = get_char ()) != -1 && (d != '\n') && d != '\r')
2380 any_token_seen |= tokens_seen;
2381 tokens_seen = false;
2382 comments_seen = false;
2384 } else if (d == '*'){
2386 bool docAppend = false;
2387 if (RootContext.Documentation != null && peek_char () == '*') {
2389 update_comment_location ();
2390 // But when it is /**/, just do nothing.
2391 if (peek_char () == '/') {
2395 if (doc_state == XmlCommentState.Allowed)
2397 else if (doc_state == XmlCommentState.NotAllowed)
2398 warn_incorrect_doc_comment ();
2401 int current_comment_start = 0;
2403 current_comment_start = xml_comment_buffer.Length;
2404 xml_comment_buffer.Append (Environment.NewLine);
2407 Location start_location = Location;
2409 while ((d = get_char ()) != -1){
2410 if (d == '*' && peek_char () == '/'){
2412 comments_seen = true;
2416 xml_comment_buffer.Append ((char) d);
2419 any_token_seen |= tokens_seen;
2420 tokens_seen = false;
2422 // Reset 'comments_seen' just to be consistent.
2423 // It doesn't matter either way, here.
2425 comments_seen = false;
2429 Report.Error (1035, start_location, "End-of-file found, '*/' expected");
2432 update_formatted_doc_comment (current_comment_start);
2435 goto is_punct_label;
2439 if (c == '\\' || is_identifier_start_character ((char)c)){
2441 return consume_identifier (c);
2445 current_location = new Location (ref_line, Col);
2446 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
2456 any_token_seen |= tokens_seen;
2457 tokens_seen = false;
2458 comments_seen = false;
2462 if (c >= '0' && c <= '9'){
2464 return is_number (c);
2469 int peek = peek_char ();
2470 if (peek >= '0' && peek <= '9')
2471 return is_number (c);
2476 if (tokens_seen || comments_seen) {
2477 Eror_WrongPreprocessorLocation ();
2481 if (handle_preprocessing_directive (true))
2484 bool directive_expected = false;
2485 while ((c = get_char ()) != -1) {
2487 directive_expected = true;
2488 } else if (!directive_expected) {
2489 // TODO: Implement comment support for disabled code and uncomment this code
2491 // Eror_WrongPreprocessorLocation ();
2492 // return Token.ERROR;
2497 if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
2501 if (handle_preprocessing_directive (false))
2504 directive_expected = false;
2508 tokens_seen = false;
2516 return consume_string (false);
2522 error_details = "Empty character literal";
2523 Report.Error (1011, Location, error_details);
2526 if (c == '\r' || c == '\n') {
2527 Report.Error (1010, Location, "Newline in constant");
2533 val = new System.Char ();
2538 error_details = "Too many characters in character literal";
2539 Report.Error (1012, Location, error_details);
2541 // Try to recover, read until newline or next "'"
2542 while ((c = get_char ()) != -1){
2551 return Token.LITERAL_CHARACTER;
2558 return consume_string (true);
2559 } else if (is_identifier_start_character ((char) c)){
2560 return consume_identifier (c, true);
2562 Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
2566 error_details = ((char)c).ToString ();
2575 // Handles one line xml comment
2577 private void handle_one_line_xml_comment ()
2580 while ((c = peek_char ()) == ' ')
2581 get_char (); // skip heading whitespaces.
2582 while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') {
2583 xml_comment_buffer.Append ((char) get_char ());
2585 if (c == '\r' || c == '\n')
2586 xml_comment_buffer.Append (Environment.NewLine);
2590 // Remove heading "*" in Javadoc-like xml documentation.
2592 private void update_formatted_doc_comment (int current_comment_start)
2594 int length = xml_comment_buffer.Length - current_comment_start;
2595 string [] lines = xml_comment_buffer.ToString (
2596 current_comment_start,
2597 length).Replace ("\r", "").Split ('\n');
2599 // The first line starts with /**, thus it is not target
2600 // for the format check.
2601 for (int i = 1; i < lines.Length; i++) {
2602 string s = lines [i];
2603 int idx = s.IndexOf ('*');
2606 if (i < lines.Length - 1)
2610 head = s.Substring (0, idx);
2611 foreach (char c in head)
2614 lines [i] = s.Substring (idx + 1);
2616 xml_comment_buffer.Remove (current_comment_start, length);
2617 xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
2621 // Updates current comment location.
2623 private void update_comment_location ()
2625 if (current_comment_location.IsNull) {
2626 // "-2" is for heading "//" or "/*"
2627 current_comment_location =
2628 new Location (ref_line, col - 2);
2633 // Checks if there was incorrect doc comments and raise
2636 public void check_incorrect_doc_comment ()
2638 if (xml_comment_buffer.Length > 0)
2639 warn_incorrect_doc_comment ();
2643 // Raises a warning when tokenizer found incorrect doccomment
2646 private void warn_incorrect_doc_comment ()
2648 if (doc_state != XmlCommentState.Error) {
2649 doc_state = XmlCommentState.Error;
2650 // in csc, it is 'XML comment is not placed on
2651 // a valid language element'. But that does not
2653 Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
2658 // Consumes the saved xml comment lines (if any)
2659 // as for current target member or type.
2661 public string consume_doc_comment ()
2663 if (xml_comment_buffer.Length > 0) {
2664 string ret = xml_comment_buffer.ToString ();
2665 reset_doc_comment ();
2671 void reset_doc_comment ()
2673 xml_comment_buffer.Length = 0;
2674 current_comment_location = Location.Null;
2677 public void cleanup ()
2679 if (ifstack != null && ifstack.Count >= 1) {
2680 current_location = new Location (ref_line, Col);
2681 int state = (int) ifstack.Pop ();
2682 if ((state & REGION) != 0)
2683 Report.Error (1038, Location, "#endregion directive expected");
2685 Report.Error (1027, Location, "Expected `#endif' directive");
2691 // Indicates whether it accepts XML documentation or not.
2693 public enum XmlCommentState {
2694 // comment is allowed in this state.
2696 // comment is not allowed in this state.
2698 // once comments appeared when it is NotAllowed, then the
2699 // state is changed to it, until the state is changed to