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 Location current_location;
44 Location current_comment_location = Location.Null;
45 ArrayList escaped_identifiers = new ArrayList ();
51 static bool IsLinqEnabled {
53 return RootContext.Version == LanguageVersion.LINQ;
58 // XML documentation buffer. The save point is used to divide
59 // comments on types and comments on members.
61 StringBuilder xml_comment_buffer;
64 // See comment on XmlCommentState enumeration.
66 XmlCommentState xml_doc_state = XmlCommentState.Allowed;
69 // Whether tokens have been seen on this line
71 bool tokens_seen = false;
74 // Whether a token has been seen on the file
75 // This is needed because `define' is not allowed to be used
76 // after a token has been seen.
78 bool any_token_seen = false;
80 static Hashtable token_values;
81 static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
83 private static Hashtable TokenValueName
86 if (token_values == null)
87 token_values = GetTokenValueNameHash ();
93 private static Hashtable GetTokenValueNameHash ()
95 Type t = typeof (Token);
96 FieldInfo [] fields = t.GetFields ();
97 Hashtable hash = new Hashtable ();
98 foreach (FieldInfo field in fields) {
99 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
100 hash.Add (field.GetValue (null), field.Name);
106 // Returns a verbose representation of the current location
108 public string location {
112 if (current_token == Token.ERROR)
113 det = "detail: " + error_details;
117 // return "Line: "+line+" Col: "+col + "\n" +
118 // "VirtLine: "+ref_line +
119 // " Token: "+current_token + " " + det;
120 string current_token_name = TokenValueName [current_token] as string;
121 if (current_token_name == null)
122 current_token_name = current_token.ToString ();
124 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
132 public bool PropertyParsing {
134 return handle_get_set;
138 handle_get_set = value;
142 public bool AssemblyTargetParsing {
144 return handle_assembly;
148 handle_assembly = value;
152 public bool EventParsing {
154 return handle_remove_add;
158 handle_remove_add = value;
162 public bool ConstraintsParsing {
164 return handle_constraints;
168 handle_constraints = value;
172 public bool TypeOfParsing {
174 return handle_typeof;
178 handle_typeof = value;
182 public XmlCommentState doc_state {
183 get { return xml_doc_state; }
185 if (value == XmlCommentState.Allowed) {
186 check_incorrect_doc_comment ();
187 reset_doc_comment ();
189 xml_doc_state = value;
193 public bool IsEscapedIdentifier (Location loc)
195 foreach (LocatedToken lt in escaped_identifiers)
196 if (lt.Location.Equals (loc))
204 static CharArrayHashtable[] keywords;
205 static Hashtable keyword_strings;
206 static NumberStyles styles;
207 static NumberFormatInfo csharp_format_info;
210 // Values for the associated token returned
220 const int TAKING = 1;
221 const int ELSE_SEEN = 4;
222 const int PARENT_TAKING = 8;
223 const int REGION = 16;
226 // pre-processor if stack state:
230 static System.Text.StringBuilder string_builder;
232 const int max_id_size = 512;
233 static char [] id_builder = new char [max_id_size];
235 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
237 const int max_number_size = 512;
238 static char [] number_builder = new char [max_number_size];
239 static int number_pos;
242 // Details about the error encoutered by the tokenizer
244 string error_details;
246 public string error {
248 return error_details;
265 // This is used when the tokenizer needs to save
266 // the current position as it needs to do some parsing
267 // on its own to deamiguate a token in behalf of the
270 Stack position_stack = new Stack (2);
275 public int putback_char;
276 public int previous_col;
277 public Stack ifstack;
278 public int parsing_generic_less_than;
279 public int current_token;
281 public Position (Tokenizer t)
283 position = t.reader.Position;
284 ref_line = t.ref_line;
286 putback_char = t.putback_char;
287 previous_col = t.previous_col;
288 if (t.ifstack != null && t.ifstack.Count != 0)
289 ifstack = (Stack)t.ifstack.Clone ();
290 parsing_generic_less_than = t.parsing_generic_less_than;
291 current_token = t.current_token;
295 public void PushPosition ()
297 position_stack.Push (new Position (this));
300 public void PopPosition ()
302 Position p = (Position) position_stack.Pop ();
304 reader.Position = p.position;
305 ref_line = p.ref_line;
307 putback_char = p.putback_char;
308 previous_col = p.previous_col;
310 parsing_generic_less_than = p.parsing_generic_less_than;
311 current_token = p.current_token;
314 // Do not reset the position, ignore it.
315 public void DiscardPosition ()
317 position_stack.Pop ();
320 static void AddKeyword (string kw, int token)
322 keyword_strings.Add (kw, kw);
323 if (keywords [kw.Length] == null) {
324 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
326 keywords [kw.Length] [kw.ToCharArray ()] = token;
329 static void InitTokens ()
331 keyword_strings = new Hashtable ();
332 keywords = new CharArrayHashtable [64];
334 AddKeyword ("__arglist", Token.ARGLIST);
335 AddKeyword ("abstract", Token.ABSTRACT);
336 AddKeyword ("as", Token.AS);
337 AddKeyword ("add", Token.ADD);
338 AddKeyword ("assembly", Token.ASSEMBLY);
339 AddKeyword ("base", Token.BASE);
340 AddKeyword ("bool", Token.BOOL);
341 AddKeyword ("break", Token.BREAK);
342 AddKeyword ("byte", Token.BYTE);
343 AddKeyword ("case", Token.CASE);
344 AddKeyword ("catch", Token.CATCH);
345 AddKeyword ("char", Token.CHAR);
346 AddKeyword ("checked", Token.CHECKED);
347 AddKeyword ("class", Token.CLASS);
348 AddKeyword ("const", Token.CONST);
349 AddKeyword ("continue", Token.CONTINUE);
350 AddKeyword ("decimal", Token.DECIMAL);
351 AddKeyword ("default", Token.DEFAULT);
352 AddKeyword ("delegate", Token.DELEGATE);
353 AddKeyword ("do", Token.DO);
354 AddKeyword ("double", Token.DOUBLE);
355 AddKeyword ("else", Token.ELSE);
356 AddKeyword ("enum", Token.ENUM);
357 AddKeyword ("event", Token.EVENT);
358 AddKeyword ("explicit", Token.EXPLICIT);
359 AddKeyword ("extern", Token.EXTERN);
360 AddKeyword ("false", Token.FALSE);
361 AddKeyword ("finally", Token.FINALLY);
362 AddKeyword ("fixed", Token.FIXED);
363 AddKeyword ("float", Token.FLOAT);
364 AddKeyword ("for", Token.FOR);
365 AddKeyword ("foreach", Token.FOREACH);
366 AddKeyword ("goto", Token.GOTO);
367 AddKeyword ("get", Token.GET);
368 AddKeyword ("if", Token.IF);
369 AddKeyword ("implicit", Token.IMPLICIT);
370 AddKeyword ("in", Token.IN);
371 AddKeyword ("int", Token.INT);
372 AddKeyword ("interface", Token.INTERFACE);
373 AddKeyword ("internal", Token.INTERNAL);
374 AddKeyword ("is", Token.IS);
375 AddKeyword ("lock", Token.LOCK);
376 AddKeyword ("long", Token.LONG);
377 AddKeyword ("namespace", Token.NAMESPACE);
378 AddKeyword ("new", Token.NEW);
379 AddKeyword ("null", Token.NULL);
380 AddKeyword ("object", Token.OBJECT);
381 AddKeyword ("operator", Token.OPERATOR);
382 AddKeyword ("out", Token.OUT);
383 AddKeyword ("override", Token.OVERRIDE);
384 AddKeyword ("params", Token.PARAMS);
385 AddKeyword ("private", Token.PRIVATE);
386 AddKeyword ("protected", Token.PROTECTED);
387 AddKeyword ("public", Token.PUBLIC);
388 AddKeyword ("readonly", Token.READONLY);
389 AddKeyword ("ref", Token.REF);
390 AddKeyword ("remove", Token.REMOVE);
391 AddKeyword ("return", Token.RETURN);
392 AddKeyword ("sbyte", Token.SBYTE);
393 AddKeyword ("sealed", Token.SEALED);
394 AddKeyword ("set", Token.SET);
395 AddKeyword ("short", Token.SHORT);
396 AddKeyword ("sizeof", Token.SIZEOF);
397 AddKeyword ("stackalloc", Token.STACKALLOC);
398 AddKeyword ("static", Token.STATIC);
399 AddKeyword ("string", Token.STRING);
400 AddKeyword ("struct", Token.STRUCT);
401 AddKeyword ("switch", Token.SWITCH);
402 AddKeyword ("this", Token.THIS);
403 AddKeyword ("throw", Token.THROW);
404 AddKeyword ("true", Token.TRUE);
405 AddKeyword ("try", Token.TRY);
406 AddKeyword ("typeof", Token.TYPEOF);
407 AddKeyword ("uint", Token.UINT);
408 AddKeyword ("ulong", Token.ULONG);
409 AddKeyword ("unchecked", Token.UNCHECKED);
410 AddKeyword ("unsafe", Token.UNSAFE);
411 AddKeyword ("ushort", Token.USHORT);
412 AddKeyword ("using", Token.USING);
413 AddKeyword ("virtual", Token.VIRTUAL);
414 AddKeyword ("void", Token.VOID);
415 AddKeyword ("volatile", Token.VOLATILE);
416 AddKeyword ("while", Token.WHILE);
417 AddKeyword ("partial", Token.PARTIAL);
419 AddKeyword ("where", Token.WHERE);
424 public static void InitializeLinqKeywords ()
426 AddKeyword ("from", Token.FROM);
427 AddKeyword ("join", Token.JOIN);
428 AddKeyword ("on", Token.ON);
429 AddKeyword ("equals", Token.EQUALS);
430 AddKeyword ("select", Token.SELECT);
431 AddKeyword ("group", Token.GROUP);
432 AddKeyword ("by", Token.BY);
433 AddKeyword ("let", Token.LET);
434 AddKeyword ("orderby", Token.ORDERBY);
435 AddKeyword ("ascending", Token.ASCENDING);
436 AddKeyword ("descending", Token.DESCENDING);
437 AddKeyword ("into", Token.INTO);
449 public static void Reset ()
452 csharp_format_info = NumberFormatInfo.InvariantInfo;
453 styles = NumberStyles.Float;
455 string_builder = new System.Text.StringBuilder ();
458 int GetKeyword (char[] id, int id_len)
461 * Keywords are stored in an array of hashtables grouped by their
465 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
467 object o = keywords [id_len] [id];
474 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
476 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
478 if (handle_assembly == false && res == Token.ASSEMBLY)
482 if (res == Token.FROM &&
483 (current_token == Token.ASSIGN || current_token == Token.OPEN_BRACKET ||
484 current_token == Token.RETURN || current_token == Token.IN)) {
485 query_parsing = true;
489 if (!query_parsing && res > Token.QUERY_FIRST_TOKEN && res < Token.QUERY_LAST_TOKEN)
495 if (!handle_constraints && res == Token.WHERE)
502 public Location Location {
503 get { return current_location; }
506 void define (string def)
508 if (!RootContext.AllDefines.Contains (def)){
509 RootContext.AllDefines [def] = true;
511 if (defines.Contains (def))
513 defines [def] = true;
516 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
518 this.ref_name = file;
519 this.file_name = file;
525 defines = new Hashtable ();
526 foreach (string def in defs)
530 xml_comment_buffer = new StringBuilder ();
533 // FIXME: This could be `Location.Push' but we have to
534 // find out why the MS compiler allows this
536 Mono.CSharp.Location.Push (file);
539 static bool is_identifier_start_character (char c)
541 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter (c);
544 static bool is_identifier_part_character (char c)
546 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') ||
547 Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
550 public static bool IsKeyword (string s)
552 return keyword_strings [s] != null;
555 public static bool IsValidIdentifier (string s)
557 if (s == null || s.Length == 0)
560 if (!is_identifier_start_character (s [0]))
563 for (int i = 1; i < s.Length; i ++)
564 if (! is_identifier_part_character (s [i]))
570 bool parse_less_than ()
573 int the_token = token ();
574 if (the_token == Token.OPEN_BRACKET) {
576 the_token = token ();
577 } while (the_token != Token.CLOSE_BRACKET);
578 the_token = token ();
581 case Token.IDENTIFIER:
604 the_token = token ();
606 if (the_token == Token.OP_GENERICS_GT)
608 else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
610 else if (the_token == Token.INTERR || the_token == Token.STAR)
612 else if (the_token == Token.OP_GENERICS_LT) {
613 if (!parse_less_than ())
616 } else if (the_token == Token.OPEN_BRACKET) {
618 the_token = token ();
619 if (the_token == Token.CLOSE_BRACKET)
621 else if (the_token == Token.COMMA)
622 goto rank_specifiers;
630 public void PutbackNullable ()
632 if (nullable_pos < 0)
633 throw new Exception ();
637 reader.Position = nullable_pos;
642 public void PutbackCloseParens ()
648 int nullable_pos = -1;
650 public void CheckNullable (bool is_nullable)
653 nullable_pos = reader.Position;
658 public bool QueryParsing {
660 query_parsing = value;
664 bool parse_generic_dimension (out int dimension)
669 int the_token = token ();
670 if (the_token == Token.OP_GENERICS_GT)
672 else if (the_token == Token.COMMA) {
681 public int peek_token ()
686 the_token = token ();
692 bool parse_namespace_or_typename (int next)
695 next = peek_token ();
696 while (next == Token.IDENTIFIER){
699 next = peek_token ();
700 if (next == Token.DOT || next == Token.DOUBLE_COLON){
702 next = peek_token ();
705 if (next == Token.OP_GENERICS_LT){
707 if (!parse_less_than ())
717 bool is_simple_type (int token)
719 return (token == Token.BOOL ||
720 token == Token.DECIMAL ||
721 token == Token.SBYTE ||
722 token == Token.BYTE ||
723 token == Token.SHORT ||
724 token == Token.USHORT ||
725 token == Token.INT ||
726 token == Token.UINT ||
727 token == Token.LONG ||
728 token == Token.ULONG ||
729 token == Token.CHAR ||
730 token == Token.FLOAT ||
731 token == Token.DOUBLE);
734 bool is_builtin_reference_type (int token)
736 return (token == Token.OBJECT || token == Token.STRING);
739 bool parse_opt_rank (int next)
742 if (next != Token.OPEN_BRACKET)
748 if (next == Token.CLOSE_BRACKET){
749 next = peek_token ();
752 if (next == Token.COMMA)
762 int next = peek_token ();
764 if (is_simple_type (next)){
766 next = peek_token ();
767 if (next == Token.INTERR)
769 return parse_opt_rank (peek_token ());
771 if (parse_namespace_or_typename (next)){
772 next = peek_token ();
773 if (next == Token.INTERR)
775 return parse_opt_rank (peek_token ());
776 } else if (is_builtin_reference_type (next)){
778 return parse_opt_rank (peek_token ());
785 // Invoked after '(' has been seen and tries to parse:
786 // type identifier [, type identifier]*
788 // if this is the case, instead of returning an
789 // OPEN_PARENS token we return a special token that
790 // triggers lambda parsing.
792 // This is needed because we can not introduce the
793 // explicitly_typed_lambda_parameter_list after a '(' in the
794 // grammar without introducing reduce/reduce conflicts.
796 // We need to parse a type and if it is followed by an
797 // identifier, we know it has to be parsed as a lambda
800 // the type expression can be prefixed with `ref' or `out'
802 public bool parse_lambda_parameters ()
805 int next = peek_token ();
807 if (next == Token.REF || next == Token.OUT)
811 next = peek_token ();
812 if (next == Token.IDENTIFIER){
814 next = peek_token ();
815 if (next == Token.COMMA){
819 if (next == Token.CLOSE_PARENS)
827 int parsing_generic_less_than = 0;
829 int is_punct (char c, ref bool doread)
839 return Token.OPEN_BRACE;
842 return Token.CLOSE_BRACE;
844 // To block doccomment inside attribute declaration.
845 if (doc_state == XmlCommentState.Allowed)
846 doc_state = XmlCommentState.NotAllowed;
847 return Token.OPEN_BRACKET;
849 return Token.CLOSE_BRACKET;
853 bool have_lambda_parameter = parse_lambda_parameters ();
856 if (have_lambda_parameter)
857 return Token.OPEN_PARENS_LAMBDA;
859 return Token.OPEN_PARENS;
861 return Token.OPEN_PARENS;
863 if (deambiguate_close_parens == 0)
864 return Token.CLOSE_PARENS;
866 --deambiguate_close_parens;
870 int new_token = xtoken ();
874 if (new_token == Token.OPEN_PARENS)
875 return Token.CLOSE_PARENS_OPEN_PARENS;
876 else if (new_token == Token.MINUS)
877 return Token.CLOSE_PARENS_MINUS;
878 else if (IsCastToken (new_token))
879 return Token.CLOSE_PARENS_CAST;
881 return Token.CLOSE_PARENS_NO_CAST;
888 return Token.SEMICOLON;
897 return Token.OP_COALESCING;
904 if (parsing_generic_less_than++ > 0)
905 return Token.OP_GENERICS_LT;
910 if (parse_generic_dimension (out dimension)) {
913 return Token.GENERIC_DIMENSION;
918 // Save current position and parse next token.
920 bool is_generic_lt = parse_less_than ();
924 return Token.OP_GENERICS_LT;
926 parsing_generic_less_than = 0;
935 return Token.OP_SHIFT_LEFT_ASSIGN;
937 return Token.OP_SHIFT_LEFT;
938 } else if (d == '='){
943 } else if (c == '>') {
944 if (parsing_generic_less_than > 0) {
945 parsing_generic_less_than--;
946 return Token.OP_GENERICS_GT;
956 return Token.OP_SHIFT_RIGHT_ASSIGN;
958 return Token.OP_SHIFT_RIGHT;
959 } else if (d == '='){
974 t = Token.OP_ADD_ASSIGN;
988 t = Token.OP_SUB_ASSIGN;
1018 return Token.ASSIGN;
1024 return Token.OP_AND;
1025 } else if (d == '='){
1027 return Token.OP_AND_ASSIGN;
1030 return Token.BITWISE_AND;
1037 } else if (d == '='){
1039 return Token.OP_OR_ASSIGN;
1041 return Token.BITWISE_OR;
1047 return Token.OP_MULT_ASSIGN;
1056 return Token.OP_DIV_ASSIGN;
1064 return Token.OP_MOD_ASSIGN;
1066 return Token.PERCENT;
1072 return Token.OP_XOR_ASSIGN;
1074 return Token.CARRET;
1085 return Token.OP_SHIFT_LEFT_ASSIGN;
1087 return Token.OP_SHIFT_LEFT;
1088 } else if (d == '='){
1102 return Token.OP_SHIFT_RIGHT_ASSIGN;
1104 return Token.OP_SHIFT_RIGHT;
1105 } else if (d == '='){
1115 return Token.DOUBLE_COLON;
1124 int deambiguate_close_parens = 0;
1126 public void Deambiguate_CloseParens (object expression)
1130 // When any binary operation is used we are sure it is not a cast
1131 if (expression is Binary)
1134 deambiguate_close_parens++;
1137 bool decimal_digits (int c)
1140 bool seen_digits = false;
1143 if (number_pos == max_number_size)
1144 Error_NumericConstantTooLong ();
1145 number_builder [number_pos++] = (char) c;
1149 // We use peek_char2, because decimal_digits needs to do a
1150 // 2-character look-ahead (5.ToString for example).
1152 while ((d = peek_char2 ()) != -1){
1153 if (d >= '0' && d <= '9'){
1154 if (number_pos == max_number_size)
1155 Error_NumericConstantTooLong ();
1156 number_builder [number_pos++] = (char) d;
1166 static bool is_hex (int e)
1168 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1171 static int real_type_suffix (int c)
1177 t = Token.LITERAL_FLOAT;
1180 t = Token.LITERAL_DOUBLE;
1183 t= Token.LITERAL_DECIMAL;
1191 int integer_type_suffix (ulong ul, int c)
1193 bool is_unsigned = false;
1194 bool is_long = false;
1197 bool scanning = true;
1208 if (!is_unsigned && (RootContext.WarningLevel >= 4)){
1210 // if we have not seen anything in between
1211 // report this error
1213 Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
1216 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
1217 // work around that.
1241 if (is_long && is_unsigned){
1243 return Token.LITERAL_INTEGER;
1244 } else if (is_unsigned){
1245 // uint if possible, or ulong else.
1247 if ((ul & 0xffffffff00000000) == 0)
1251 } else if (is_long){
1252 // long if possible, ulong otherwise
1253 if ((ul & 0x8000000000000000) != 0)
1258 // int, uint, long or ulong in that order
1259 if ((ul & 0xffffffff00000000) == 0){
1260 uint ui = (uint) ul;
1262 if ((ui & 0x80000000) != 0)
1267 if ((ul & 0x8000000000000000) != 0)
1273 return Token.LITERAL_INTEGER;
1277 // given `c' as the next char in the input decide whether
1278 // we need to convert to a special type, and then choose
1279 // the best representation for the integer
1281 int adjust_int (int c)
1284 if (number_pos > 9){
1285 ulong ul = (uint) (number_builder [0] - '0');
1287 for (int i = 1; i < number_pos; i++){
1288 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
1290 return integer_type_suffix (ul, c);
1292 uint ui = (uint) (number_builder [0] - '0');
1294 for (int i = 1; i < number_pos; i++){
1295 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
1297 return integer_type_suffix (ui, c);
1299 } catch (OverflowException) {
1300 error_details = "Integral constant is too large";
1301 Report.Error (1021, Location, error_details);
1303 return Token.LITERAL_INTEGER;
1305 catch (FormatException) {
1306 Report.Error (1013, Location, "Invalid number");
1308 return Token.LITERAL_INTEGER;
1312 int adjust_real (int t)
1314 string s = new String (number_builder, 0, number_pos);
1315 const string error_details = "Floating-point constant is outside the range of type `{0}'";
1318 case Token.LITERAL_DECIMAL:
1320 val = System.Decimal.Parse (s, styles, csharp_format_info);
1321 } catch (OverflowException) {
1323 Report.Error (594, Location, error_details, "decimal");
1326 case Token.LITERAL_FLOAT:
1328 val = float.Parse (s, styles, csharp_format_info);
1329 } catch (OverflowException) {
1331 Report.Error (594, Location, error_details, "float");
1335 case Token.LITERAL_DOUBLE:
1337 t = Token.LITERAL_DOUBLE;
1339 val = System.Double.Parse (s, styles, csharp_format_info);
1340 } catch (OverflowException) {
1342 Report.Error (594, Location, error_details, "double");
1355 while ((d = peek_char ()) != -1){
1357 number_builder [number_pos++] = (char) d;
1363 string s = new String (number_builder, 0, number_pos);
1365 if (number_pos <= 8)
1366 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
1368 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
1369 } catch (OverflowException){
1370 error_details = "Integral constant is too large";
1371 Report.Error (1021, Location, error_details);
1373 return Token.LITERAL_INTEGER;
1375 catch (FormatException) {
1376 Report.Error (1013, Location, "Invalid number");
1378 return Token.LITERAL_INTEGER;
1381 return integer_type_suffix (ul, peek_char ());
1385 // Invoked if we know we have .digits or digits
1387 int is_number (int c)
1389 bool is_real = false;
1394 if (c >= '0' && c <= '9'){
1396 int peek = peek_char ();
1398 if (peek == 'x' || peek == 'X')
1399 return handle_hex ();
1406 // We need to handle the case of
1407 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1410 if (decimal_digits ('.')){
1416 return adjust_int (-1);
1420 if (c == 'e' || c == 'E'){
1422 if (number_pos == max_number_size)
1423 Error_NumericConstantTooLong ();
1424 number_builder [number_pos++] = 'e';
1428 if (number_pos == max_number_size)
1429 Error_NumericConstantTooLong ();
1430 number_builder [number_pos++] = '+';
1432 } else if (c == '-') {
1433 if (number_pos == max_number_size)
1434 Error_NumericConstantTooLong ();
1435 number_builder [number_pos++] = '-';
1438 if (number_pos == max_number_size)
1439 Error_NumericConstantTooLong ();
1440 number_builder [number_pos++] = '+';
1447 type = real_type_suffix (c);
1448 if (type == Token.NONE && !is_real){
1450 return adjust_int (c);
1454 if (type == Token.NONE){
1459 return adjust_real (type);
1461 Console.WriteLine ("This should not be reached");
1462 throw new Exception ("Is Number should never reach this point");
1466 // Accepts exactly count (4 or 8) hex, no more no less
1468 int getHex (int count, out bool error)
1473 int top = count != -1 ? count : 4;
1477 for (i = 0; i < top; i++){
1480 if (c >= '0' && c <= '9')
1481 c = (int) c - (int) '0';
1482 else if (c >= 'A' && c <= 'F')
1483 c = (int) c - (int) 'A' + 10;
1484 else if (c >= 'a' && c <= 'f')
1485 c = (int) c - (int) 'a' + 10;
1491 total = (total * 16) + c;
1493 int p = peek_char ();
1496 if (!is_hex ((char)p))
1537 v = getHex (-1, out error);
1542 v = getHex (4, out error);
1547 v = getHex (8, out error);
1552 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1562 if (putback_char != -1) {
1580 if (putback_char != -1)
1581 return putback_char;
1582 putback_char = reader.Read ();
1583 return putback_char;
1588 if (putback_char != -1)
1589 return putback_char;
1590 return reader.Peek ();
1593 void putback (int c)
1595 if (putback_char != -1){
1596 Console.WriteLine ("Col: " + col);
1597 Console.WriteLine ("Row: " + line);
1598 Console.WriteLine ("Name: " + ref_name.Name);
1599 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
1600 throw new Exception ("This should not happen putback on putback");
1602 if (c == '\n' || col == 0) {
1603 // It won't happen though.
1613 public bool advance ()
1615 return peek_char () != -1;
1618 public Object Value {
1624 public Object value ()
1629 static bool IsCastToken (int token)
1634 case Token.IDENTIFIER:
1635 case Token.LITERAL_INTEGER:
1636 case Token.LITERAL_FLOAT:
1637 case Token.LITERAL_DOUBLE:
1638 case Token.LITERAL_DECIMAL:
1639 case Token.LITERAL_CHARACTER:
1640 case Token.LITERAL_STRING:
1643 case Token.DELEGATE:
1653 case Token.UNCHECKED:
1660 // These can be part of a member access
1680 current_token = xtoken ();
1683 if (current_token != Token.DEFAULT)
1684 return current_token;
1689 current_token = Token.ERROR;
1690 else if (c == Token.OPEN_PARENS)
1691 current_token = Token.DEFAULT_OPEN_PARENS;
1692 else if (c == Token.COLON)
1693 current_token = Token.DEFAULT_COLON;
1697 return current_token;
1700 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
1702 void get_cmd_arg (out string cmd, out string arg)
1706 tokens_seen = false;
1708 static_cmd_arg.Length = 0;
1710 // skip over white space
1711 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1714 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
1715 if (is_identifier_part_character ((char) c)){
1716 static_cmd_arg.Append ((char) c);
1724 cmd = static_cmd_arg.ToString ();
1726 if (c == '\n' || c == '\r'){
1730 // skip over white space
1731 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1736 } else if (c == '\r'){
1738 } else if (c == -1){
1743 static_cmd_arg.Length = 0;
1744 static_cmd_arg.Append ((char) c);
1746 while ((c = get_char ()) != -1 && (c != '\n') && (c != '\r')){
1747 static_cmd_arg.Append ((char) c);
1750 arg = static_cmd_arg.ToString ();
1754 // Handles the #line directive
1756 bool PreProcessLine (string arg)
1758 if (arg.Length == 0)
1761 if (arg == "default"){
1763 ref_name = file_name;
1764 Location.Push (ref_name);
1766 } else if (arg == "hidden"){
1768 // We ignore #line hidden
1776 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
1777 ref_line = System.Int32.Parse (arg.Substring (0, pos));
1780 char [] quotes = { '\"' };
1782 string name = arg.Substring (pos). Trim (quotes);
1783 ref_name = Location.LookupFile (name);
1784 file_name.HasLineDirective = true;
1785 ref_name.HasLineDirective = true;
1786 Location.Push (ref_name);
1788 ref_line = System.Int32.Parse (arg);
1798 // Handles #define and #undef
1800 void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
1802 if (arg.Length == 0 || arg == "true" || arg == "false"){
1803 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
1807 if (arg.IndexOfAny (simple_whitespaces) != -1){
1808 Error_EndLineExpected ();
1812 if (!is_identifier_start_character (arg [0]))
1813 Report.Error (1001, Location, "Identifier expected: " + arg);
1815 foreach (char c in arg.Substring (1)){
1816 if (!is_identifier_part_character (c)){
1817 Report.Error (1001, Location, "Identifier expected: " + arg);
1822 if (!caller_is_taking)
1826 if (defines == null)
1827 defines = new Hashtable ();
1830 if (defines == null)
1832 if (defines.Contains (arg))
1833 defines.Remove (arg);
1838 /// Handles #pragma directive
1840 void PreProcessPragma (string arg)
1842 const string warning = "warning";
1843 const string w_disable = "warning disable";
1844 const string w_restore = "warning restore";
1846 if (arg == w_disable) {
1847 Report.RegisterWarningRegion (Location).WarningDisable (line);
1851 if (arg == w_restore) {
1852 Report.RegisterWarningRegion (Location).WarningEnable (line);
1856 if (arg.StartsWith (w_disable)) {
1857 int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
1858 foreach (int code in codes) {
1860 Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
1865 if (arg.StartsWith (w_restore)) {
1866 int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
1867 Hashtable w_table = Report.warning_ignore_table;
1868 foreach (int code in codes) {
1869 if (w_table != null && w_table.Contains (code))
1870 Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
1871 Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
1876 if (arg.StartsWith (warning)) {
1877 Report.Warning (1634, 1, Location, "Expected disable or restore");
1881 Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
1884 int[] ParseNumbers (string text)
1886 string[] string_array = text.Split (',');
1887 int[] values = new int [string_array.Length];
1889 foreach (string string_code in string_array) {
1891 values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
1893 catch (FormatException) {
1894 Report.Warning (1692, 1, Location, "Invalid number");
1900 bool eval_val (string s)
1907 if (defines == null)
1909 if (defines.Contains (s))
1915 bool pp_primary (ref string s)
1924 s = s.Substring (1);
1925 bool val = pp_expr (ref s, false);
1926 if (s.Length > 0 && s [0] == ')'){
1927 s = s.Substring (1);
1930 Error_InvalidDirective ();
1934 if (is_identifier_start_character (c)){
1940 if (is_identifier_part_character (c)){
1944 bool v = eval_val (s.Substring (0, j));
1945 s = s.Substring (j);
1948 bool vv = eval_val (s);
1953 Error_InvalidDirective ();
1957 bool pp_unary (ref string s)
1964 if (len > 1 && s [1] == '='){
1965 Error_InvalidDirective ();
1968 s = s.Substring (1);
1969 return ! pp_primary (ref s);
1971 return pp_primary (ref s);
1973 Error_InvalidDirective ();
1978 bool pp_eq (ref string s)
1980 bool va = pp_unary (ref s);
1986 if (len > 2 && s [1] == '='){
1987 s = s.Substring (2);
1988 return va == pp_unary (ref s);
1990 Error_InvalidDirective ();
1993 } else if (s [0] == '!' && len > 1 && s [1] == '='){
1994 s = s.Substring (2);
1996 return va != pp_unary (ref s);
2005 bool pp_and (ref string s)
2007 bool va = pp_eq (ref s);
2013 if (len > 2 && s [1] == '&'){
2014 s = s.Substring (2);
2015 return (va & pp_and (ref s));
2017 Error_InvalidDirective ();
2026 // Evaluates an expression for `#if' or `#elif'
2028 bool pp_expr (ref string s, bool isTerm)
2030 bool va = pp_and (ref s);
2037 if (len > 2 && s [1] == '|'){
2038 s = s.Substring (2);
2039 return va | pp_expr (ref s, isTerm);
2041 Error_InvalidDirective ();
2046 Error_EndLineExpected ();
2054 bool eval (string s)
2056 bool v = pp_expr (ref s, true);
2065 void Error_NumericConstantTooLong ()
2067 Report.Error (1021, Location, "Numeric constant too long");
2070 void Error_InvalidDirective ()
2072 Report.Error (1517, Location, "Invalid preprocessor directive");
2075 void Error_UnexpectedDirective (string extra)
2079 "Unexpected processor directive (" + extra + ")");
2082 void Error_TokensSeen ()
2084 Report.Error (1032, Location,
2085 "Cannot define or undefine preprocessor symbols after first token in file");
2088 void Eror_WrongPreprocessorLocation ()
2090 Report.Error (1040, Location,
2091 "Preprocessor directives must appear as the first non-whitespace character on a line");
2094 void Error_EndLineExpected ()
2096 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
2100 // if true, then the code continues processing the code
2101 // if false, the code stays in a loop until another directive is
2103 // When caller_is_taking is false we ignore all directives except the ones
2104 // which can help us to identify where the #if block ends
2105 bool handle_preprocessing_directive (bool caller_is_taking)
2108 bool region_directive = false;
2110 get_cmd_arg (out cmd, out arg);
2112 // Eat any trailing whitespaces and single-line comments
2113 if (arg.IndexOf ("//") != -1)
2114 arg = arg.Substring (0, arg.IndexOf ("//"));
2115 arg = arg.Trim (simple_whitespaces);
2118 // The first group of pre-processing instructions is always processed
2122 region_directive = true;
2127 if (ifstack == null || ifstack.Count == 0){
2128 Error_UnexpectedDirective ("no #region for this #endregion");
2131 int pop = (int) ifstack.Pop ();
2133 if ((pop & REGION) == 0)
2134 Report.Error (1027, Location, "Expected `#endif' directive");
2136 return caller_is_taking;
2139 if (ifstack == null)
2140 ifstack = new Stack (2);
2142 int flags = region_directive ? REGION : 0;
2143 if (ifstack.Count == 0){
2144 flags |= PARENT_TAKING;
2146 int state = (int) ifstack.Peek ();
2147 if ((state & TAKING) != 0) {
2148 flags |= PARENT_TAKING;
2152 if (caller_is_taking && eval (arg)) {
2153 ifstack.Push (flags | TAKING);
2156 ifstack.Push (flags);
2160 if (ifstack == null || ifstack.Count == 0){
2161 Error_UnexpectedDirective ("no #if for this #endif");
2164 pop = (int) ifstack.Pop ();
2166 if ((pop & REGION) != 0)
2167 Report.Error (1038, Location, "#endregion directive expected");
2169 if (arg.Length != 0) {
2170 Error_EndLineExpected ();
2173 if (ifstack.Count == 0)
2176 int state = (int) ifstack.Peek ();
2177 return (state & TAKING) != 0;
2181 if (ifstack == null || ifstack.Count == 0){
2182 Error_UnexpectedDirective ("no #if for this #elif");
2185 int state = (int) ifstack.Pop ();
2187 if ((state & REGION) != 0) {
2188 Report.Error (1038, Location, "#endregion directive expected");
2192 if ((state & ELSE_SEEN) != 0){
2193 Error_UnexpectedDirective ("#elif not valid after #else");
2197 if ((state & TAKING) != 0) {
2202 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
2203 ifstack.Push (state | TAKING);
2207 ifstack.Push (state);
2212 if (ifstack == null || ifstack.Count == 0){
2213 Error_UnexpectedDirective ("no #if for this #else");
2216 int state = (int) ifstack.Peek ();
2218 if ((state & REGION) != 0) {
2219 Report.Error (1038, Location, "#endregion directive expected");
2223 if ((state & ELSE_SEEN) != 0){
2224 Error_UnexpectedDirective ("#else within #else");
2230 if (arg.Length != 0) {
2231 Error_EndLineExpected ();
2236 if ((state & PARENT_TAKING) != 0) {
2237 ret = (state & TAKING) == 0;
2245 ifstack.Push (state | ELSE_SEEN);
2250 if (any_token_seen){
2251 Error_TokensSeen ();
2252 return caller_is_taking;
2254 PreProcessDefinition (true, arg, caller_is_taking);
2255 return caller_is_taking;
2258 if (any_token_seen){
2259 Error_TokensSeen ();
2260 return caller_is_taking;
2262 PreProcessDefinition (false, arg, caller_is_taking);
2263 return caller_is_taking;
2267 // These are only processed if we are in a `taking' block
2269 if (!caller_is_taking)
2274 Report.Error (1029, Location, "#error: '" + arg + "'");
2278 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
2282 if (RootContext.Version == LanguageVersion.ISO_1) {
2283 Report.FeatureIsNotISO1 (Location, "#pragma");
2287 PreProcessPragma (arg);
2291 if (!PreProcessLine (arg))
2294 "The line number specified for #line directive is missing or invalid");
2295 return caller_is_taking;
2298 Report.Error (1024, Location, "Wrong preprocessor directive");
2303 private int consume_string (bool quoted)
2306 string_builder.Length = 0;
2308 while ((c = get_char ()) != -1){
2310 if (quoted && peek_char () == '"'){
2311 string_builder.Append ((char) c);
2315 val = string_builder.ToString ();
2316 return Token.LITERAL_STRING;
2322 Report.Error (1010, Location, "Newline in constant");
2330 string_builder.Append ((char) c);
2333 Report.Error (1039, Location, "Unterminated string literal");
2337 private int consume_identifier (int s)
2339 int res = consume_identifier (s, false);
2341 if (doc_state == XmlCommentState.Allowed)
2342 doc_state = XmlCommentState.NotAllowed;
2345 case Token.NAMESPACE:
2346 check_incorrect_doc_comment ();
2350 if (res == Token.PARTIAL) {
2351 // Save current position and parse next token.
2354 int next_token = token ();
2355 bool ok = (next_token == Token.CLASS) ||
2356 (next_token == Token.STRUCT) ||
2357 (next_token == Token.INTERFACE);
2362 if (RootContext.Version == LanguageVersion.ISO_1)
2363 Report.FeatureIsNotISO1 (Location, "partial types");
2368 if (next_token < Token.LAST_KEYWORD)
2369 Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
2371 val = new LocatedToken (Location, "partial");
2372 return Token.IDENTIFIER;
2378 private int consume_identifier (int s, bool quoted)
2383 id_builder [0] = (char) s;
2385 current_location = new Location (ref_line, Col);
2387 while ((c = get_char ()) != -1) {
2389 if (is_identifier_part_character ((char) c)){
2390 if (pos == max_id_size){
2391 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
2395 id_builder [pos++] = (char) c;
2396 // putback_char = -1;
2397 } else if (c == '\\') {
2401 // putback_char = c;
2408 // Optimization: avoids doing the keyword lookup
2409 // on uppercase letters and _
2411 if (!quoted && (s >= 'a' || s == '_')){
2412 int keyword = GetKeyword (id_builder, pos);
2413 if (keyword != -1) {
2420 // Keep identifiers in an array of hashtables to avoid needless
2424 if (identifiers [pos] != null) {
2425 val = identifiers [pos][id_builder];
2427 val = new LocatedToken (Location, (string) val);
2429 escaped_identifiers.Add (val);
2430 return Token.IDENTIFIER;
2434 identifiers [pos] = new CharArrayHashtable (pos);
2436 val = new String (id_builder, 0, pos);
2437 if (RootContext.Version == LanguageVersion.ISO_1) {
2438 for (int i = 1; i < id_builder.Length; i += 3) {
2439 if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
2440 Report.Error (1638, Location,
2441 "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
2447 char [] chars = new char [pos];
2448 Array.Copy (id_builder, chars, pos);
2450 identifiers [pos] [chars] = val;
2452 val = new LocatedToken (Location, (string) val);
2454 escaped_identifiers.Add (val);
2455 return Token.IDENTIFIER;
2458 public int xtoken ()
2461 bool doread = false;
2464 // Whether we have seen comments on the current line
2465 bool comments_seen = false;
2467 for (;(c = get_char ()) != -1;) {
2469 col = ((col + 8) / 8) * 8;
2473 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0 || c == 0)
2477 if (peek_char () == '\n')
2480 any_token_seen |= tokens_seen;
2481 tokens_seen = false;
2482 comments_seen = false;
2486 // Handle double-slash comments.
2488 int d = peek_char ();
2492 if (RootContext.Documentation != null && peek_char () == '/') {
2494 // Don't allow ////.
2495 if ((d = peek_char ()) != '/') {
2496 update_comment_location ();
2497 if (doc_state == XmlCommentState.Allowed)
2498 handle_one_line_xml_comment ();
2499 else if (doc_state == XmlCommentState.NotAllowed)
2500 warn_incorrect_doc_comment ();
2503 while ((d = get_char ()) != -1 && (d != '\n') && d != '\r')
2506 any_token_seen |= tokens_seen;
2507 tokens_seen = false;
2508 comments_seen = false;
2510 } else if (d == '*'){
2512 bool docAppend = false;
2513 if (RootContext.Documentation != null && peek_char () == '*') {
2515 update_comment_location ();
2516 // But when it is /**/, just do nothing.
2517 if (peek_char () == '/') {
2521 if (doc_state == XmlCommentState.Allowed)
2523 else if (doc_state == XmlCommentState.NotAllowed)
2524 warn_incorrect_doc_comment ();
2527 int current_comment_start = 0;
2529 current_comment_start = xml_comment_buffer.Length;
2530 xml_comment_buffer.Append (Environment.NewLine);
2533 Location start_location = Location;
2535 while ((d = get_char ()) != -1){
2536 if (d == '*' && peek_char () == '/'){
2538 comments_seen = true;
2542 xml_comment_buffer.Append ((char) d);
2545 any_token_seen |= tokens_seen;
2546 tokens_seen = false;
2548 // Reset 'comments_seen' just to be consistent.
2549 // It doesn't matter either way, here.
2551 comments_seen = false;
2555 Report.Error (1035, start_location, "End-of-file found, '*/' expected");
2558 update_formatted_doc_comment (current_comment_start);
2561 goto is_punct_label;
2565 if (c == '\\' || is_identifier_start_character ((char)c)){
2567 return consume_identifier (c);
2571 current_location = new Location (ref_line, Col);
2572 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
2582 any_token_seen |= tokens_seen;
2583 tokens_seen = false;
2584 comments_seen = false;
2588 if (c >= '0' && c <= '9'){
2590 return is_number (c);
2595 int peek = peek_char ();
2596 if (peek >= '0' && peek <= '9')
2597 return is_number (c);
2602 if (tokens_seen || comments_seen) {
2603 Eror_WrongPreprocessorLocation ();
2607 if (handle_preprocessing_directive (true))
2610 bool directive_expected = false;
2611 while ((c = get_char ()) != -1) {
2613 directive_expected = true;
2614 } else if (!directive_expected) {
2615 // TODO: Implement comment support for disabled code and uncomment this code
2617 // Eror_WrongPreprocessorLocation ();
2618 // return Token.ERROR;
2623 if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
2627 if (handle_preprocessing_directive (false))
2630 directive_expected = false;
2634 tokens_seen = false;
2642 return consume_string (false);
2648 error_details = "Empty character literal";
2649 Report.Error (1011, Location, error_details);
2652 if (c == '\r' || c == '\n') {
2653 Report.Error (1010, Location, "Newline in constant");
2659 val = new System.Char ();
2664 error_details = "Too many characters in character literal";
2665 Report.Error (1012, Location, error_details);
2667 // Try to recover, read until newline or next "'"
2668 while ((c = get_char ()) != -1){
2677 return Token.LITERAL_CHARACTER;
2684 return consume_string (true);
2685 } else if (is_identifier_start_character ((char) c)){
2686 return consume_identifier (c, true);
2688 Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
2692 error_details = ((char)c).ToString ();
2701 // Handles one line xml comment
2703 private void handle_one_line_xml_comment ()
2706 while ((c = peek_char ()) == ' ')
2707 get_char (); // skip heading whitespaces.
2708 while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') {
2709 xml_comment_buffer.Append ((char) get_char ());
2711 if (c == '\r' || c == '\n')
2712 xml_comment_buffer.Append (Environment.NewLine);
2716 // Remove heading "*" in Javadoc-like xml documentation.
2718 private void update_formatted_doc_comment (int current_comment_start)
2720 int length = xml_comment_buffer.Length - current_comment_start;
2721 string [] lines = xml_comment_buffer.ToString (
2722 current_comment_start,
2723 length).Replace ("\r", "").Split ('\n');
2725 // The first line starts with /**, thus it is not target
2726 // for the format check.
2727 for (int i = 1; i < lines.Length; i++) {
2728 string s = lines [i];
2729 int idx = s.IndexOf ('*');
2732 if (i < lines.Length - 1)
2736 head = s.Substring (0, idx);
2737 foreach (char c in head)
2740 lines [i] = s.Substring (idx + 1);
2742 xml_comment_buffer.Remove (current_comment_start, length);
2743 xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
2747 // Updates current comment location.
2749 private void update_comment_location ()
2751 if (current_comment_location.IsNull) {
2752 // "-2" is for heading "//" or "/*"
2753 current_comment_location =
2754 new Location (ref_line, col - 2);
2759 // Checks if there was incorrect doc comments and raise
2762 public void check_incorrect_doc_comment ()
2764 if (xml_comment_buffer.Length > 0)
2765 warn_incorrect_doc_comment ();
2769 // Raises a warning when tokenizer found incorrect doccomment
2772 private void warn_incorrect_doc_comment ()
2774 if (doc_state != XmlCommentState.Error) {
2775 doc_state = XmlCommentState.Error;
2776 // in csc, it is 'XML comment is not placed on
2777 // a valid language element'. But that does not
2779 Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
2784 // Consumes the saved xml comment lines (if any)
2785 // as for current target member or type.
2787 public string consume_doc_comment ()
2789 if (xml_comment_buffer.Length > 0) {
2790 string ret = xml_comment_buffer.ToString ();
2791 reset_doc_comment ();
2797 void reset_doc_comment ()
2799 xml_comment_buffer.Length = 0;
2800 current_comment_location = Location.Null;
2803 public void cleanup ()
2805 if (ifstack != null && ifstack.Count >= 1) {
2806 current_location = new Location (ref_line, Col);
2807 int state = (int) ifstack.Pop ();
2808 if ((state & REGION) != 0)
2809 Report.Error (1038, Location, "#endregion directive expected");
2811 Report.Error (1027, Location, "Expected `#endif' directive");
2817 // Indicates whether it accepts XML documentation or not.
2819 public enum XmlCommentState {
2820 // comment is allowed in this state.
2822 // comment is not allowed in this state.
2824 // once comments appeared when it is NotAllowed, then the
2825 // state is changed to it, until the state is changed to