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;
44 Location current_location;
45 Location current_comment_location = Location.Null;
46 ArrayList escaped_identifiers = new ArrayList ();
48 static bool IsLinqEnabled {
50 return RootContext.Version == LanguageVersion.LINQ;
55 // XML documentation buffer. The save point is used to divide
56 // comments on types and comments on members.
58 StringBuilder xml_comment_buffer;
61 // See comment on XmlCommentState enumeration.
63 XmlCommentState xml_doc_state = XmlCommentState.Allowed;
66 // Whether tokens have been seen on this line
68 bool tokens_seen = false;
71 // Whether a token has been seen on the file
72 // This is needed because `define' is not allowed to be used
73 // after a token has been seen.
75 bool any_token_seen = false;
77 static Hashtable token_values;
78 static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
80 private static Hashtable TokenValueName
83 if (token_values == null)
84 token_values = GetTokenValueNameHash ();
90 private static Hashtable GetTokenValueNameHash ()
92 Type t = typeof (Token);
93 FieldInfo [] fields = t.GetFields ();
94 Hashtable hash = new Hashtable ();
95 foreach (FieldInfo field in fields) {
96 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
97 hash.Add (field.GetValue (null), field.Name);
103 // Returns a verbose representation of the current location
105 public string location {
109 if (current_token == Token.ERROR)
110 det = "detail: " + error_details;
114 // return "Line: "+line+" Col: "+col + "\n" +
115 // "VirtLine: "+ref_line +
116 // " Token: "+current_token + " " + det;
117 string current_token_name = TokenValueName [current_token] as string;
118 if (current_token_name == null)
119 current_token_name = current_token.ToString ();
121 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
129 public bool PropertyParsing {
131 return handle_get_set;
135 handle_get_set = value;
139 public bool AssemblyTargetParsing {
141 return handle_assembly;
145 handle_assembly = value;
149 public bool EventParsing {
151 return handle_remove_add;
155 handle_remove_add = value;
159 public bool ConstraintsParsing {
161 return handle_constraints;
165 handle_constraints = value;
169 public bool TypeOfParsing {
171 return handle_typeof;
175 handle_typeof = value;
179 public bool QueryParsing {
181 query_parsing = value;
185 public XmlCommentState doc_state {
186 get { return xml_doc_state; }
188 if (value == XmlCommentState.Allowed) {
189 check_incorrect_doc_comment ();
190 reset_doc_comment ();
192 xml_doc_state = value;
196 public bool IsEscapedIdentifier (Location loc)
198 foreach (LocatedToken lt in escaped_identifiers)
199 if (lt.Location.Equals (loc))
207 static CharArrayHashtable[] keywords;
208 static Hashtable keyword_strings;
209 static NumberStyles styles;
210 static NumberFormatInfo csharp_format_info;
213 // Values for the associated token returned
223 const int TAKING = 1;
224 const int ELSE_SEEN = 4;
225 const int PARENT_TAKING = 8;
226 const int REGION = 16;
229 // pre-processor if stack state:
233 static System.Text.StringBuilder string_builder;
235 const int max_id_size = 512;
236 static char [] id_builder = new char [max_id_size];
238 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
240 const int max_number_size = 512;
241 static char [] number_builder = new char [max_number_size];
242 static int number_pos;
245 // Details about the error encoutered by the tokenizer
247 string error_details;
249 public string error {
251 return error_details;
268 // This is used when the tokenizer needs to save
269 // the current position as it needs to do some parsing
270 // on its own to deamiguate a token in behalf of the
273 Stack position_stack = new Stack (2);
278 public int putback_char;
279 public int previous_col;
280 public Stack ifstack;
281 public int parsing_generic_less_than;
282 public int current_token;
284 public Position (Tokenizer t)
286 position = t.reader.Position;
287 ref_line = t.ref_line;
289 putback_char = t.putback_char;
290 previous_col = t.previous_col;
291 if (t.ifstack != null && t.ifstack.Count != 0)
292 ifstack = (Stack)t.ifstack.Clone ();
293 parsing_generic_less_than = t.parsing_generic_less_than;
294 current_token = t.current_token;
298 public void PushPosition ()
300 position_stack.Push (new Position (this));
303 public void PopPosition ()
305 Position p = (Position) position_stack.Pop ();
307 reader.Position = p.position;
308 ref_line = p.ref_line;
310 putback_char = p.putback_char;
311 previous_col = p.previous_col;
313 parsing_generic_less_than = p.parsing_generic_less_than;
314 current_token = p.current_token;
317 // Do not reset the position, ignore it.
318 public void DiscardPosition ()
320 position_stack.Pop ();
323 static void AddKeyword (string kw, int token)
325 keyword_strings.Add (kw, kw);
326 if (keywords [kw.Length] == null) {
327 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
329 keywords [kw.Length] [kw.ToCharArray ()] = token;
332 static void InitTokens ()
334 keyword_strings = new Hashtable ();
335 keywords = new CharArrayHashtable [64];
337 AddKeyword ("__arglist", Token.ARGLIST);
338 AddKeyword ("abstract", Token.ABSTRACT);
339 AddKeyword ("as", Token.AS);
340 AddKeyword ("add", Token.ADD);
341 AddKeyword ("assembly", Token.ASSEMBLY);
342 AddKeyword ("base", Token.BASE);
343 AddKeyword ("bool", Token.BOOL);
344 AddKeyword ("break", Token.BREAK);
345 AddKeyword ("byte", Token.BYTE);
346 AddKeyword ("case", Token.CASE);
347 AddKeyword ("catch", Token.CATCH);
348 AddKeyword ("char", Token.CHAR);
349 AddKeyword ("checked", Token.CHECKED);
350 AddKeyword ("class", Token.CLASS);
351 AddKeyword ("const", Token.CONST);
352 AddKeyword ("continue", Token.CONTINUE);
353 AddKeyword ("decimal", Token.DECIMAL);
354 AddKeyword ("default", Token.DEFAULT);
355 AddKeyword ("delegate", Token.DELEGATE);
356 AddKeyword ("do", Token.DO);
357 AddKeyword ("double", Token.DOUBLE);
358 AddKeyword ("else", Token.ELSE);
359 AddKeyword ("enum", Token.ENUM);
360 AddKeyword ("event", Token.EVENT);
361 AddKeyword ("explicit", Token.EXPLICIT);
362 AddKeyword ("extern", Token.EXTERN);
363 AddKeyword ("false", Token.FALSE);
364 AddKeyword ("finally", Token.FINALLY);
365 AddKeyword ("fixed", Token.FIXED);
366 AddKeyword ("float", Token.FLOAT);
367 AddKeyword ("for", Token.FOR);
368 AddKeyword ("foreach", Token.FOREACH);
369 AddKeyword ("goto", Token.GOTO);
370 AddKeyword ("get", Token.GET);
371 AddKeyword ("if", Token.IF);
372 AddKeyword ("implicit", Token.IMPLICIT);
373 AddKeyword ("in", Token.IN);
374 AddKeyword ("int", Token.INT);
375 AddKeyword ("interface", Token.INTERFACE);
376 AddKeyword ("internal", Token.INTERNAL);
377 AddKeyword ("is", Token.IS);
378 AddKeyword ("lock", Token.LOCK);
379 AddKeyword ("long", Token.LONG);
380 AddKeyword ("namespace", Token.NAMESPACE);
381 AddKeyword ("new", Token.NEW);
382 AddKeyword ("null", Token.NULL);
383 AddKeyword ("object", Token.OBJECT);
384 AddKeyword ("operator", Token.OPERATOR);
385 AddKeyword ("out", Token.OUT);
386 AddKeyword ("override", Token.OVERRIDE);
387 AddKeyword ("params", Token.PARAMS);
388 AddKeyword ("private", Token.PRIVATE);
389 AddKeyword ("protected", Token.PROTECTED);
390 AddKeyword ("public", Token.PUBLIC);
391 AddKeyword ("readonly", Token.READONLY);
392 AddKeyword ("ref", Token.REF);
393 AddKeyword ("remove", Token.REMOVE);
394 AddKeyword ("return", Token.RETURN);
395 AddKeyword ("sbyte", Token.SBYTE);
396 AddKeyword ("sealed", Token.SEALED);
397 AddKeyword ("set", Token.SET);
398 AddKeyword ("short", Token.SHORT);
399 AddKeyword ("sizeof", Token.SIZEOF);
400 AddKeyword ("stackalloc", Token.STACKALLOC);
401 AddKeyword ("static", Token.STATIC);
402 AddKeyword ("string", Token.STRING);
403 AddKeyword ("struct", Token.STRUCT);
404 AddKeyword ("switch", Token.SWITCH);
405 AddKeyword ("this", Token.THIS);
406 AddKeyword ("throw", Token.THROW);
407 AddKeyword ("true", Token.TRUE);
408 AddKeyword ("try", Token.TRY);
409 AddKeyword ("typeof", Token.TYPEOF);
410 AddKeyword ("uint", Token.UINT);
411 AddKeyword ("ulong", Token.ULONG);
412 AddKeyword ("unchecked", Token.UNCHECKED);
413 AddKeyword ("unsafe", Token.UNSAFE);
414 AddKeyword ("ushort", Token.USHORT);
415 AddKeyword ("using", Token.USING);
416 AddKeyword ("virtual", Token.VIRTUAL);
417 AddKeyword ("void", Token.VOID);
418 AddKeyword ("volatile", Token.VOLATILE);
419 AddKeyword ("while", Token.WHILE);
420 AddKeyword ("partial", Token.PARTIAL);
422 AddKeyword ("where", Token.WHERE);
427 public static void InitializeLinqKeywords ()
429 AddKeyword ("from", Token.FROM);
430 AddKeyword ("join", Token.JOIN);
431 AddKeyword ("on", Token.ON);
432 AddKeyword ("equals", Token.EQUALS);
433 AddKeyword ("select", Token.SELECT);
434 AddKeyword ("group", Token.GROUP);
435 AddKeyword ("by", Token.BY);
436 AddKeyword ("let", Token.LET);
437 AddKeyword ("orderby", Token.ORDERBY);
438 AddKeyword ("ascending", Token.ASCENDING);
439 AddKeyword ("descending", Token.DESCENDING);
440 AddKeyword ("into", Token.INTO);
452 public static void Reset ()
455 csharp_format_info = NumberFormatInfo.InvariantInfo;
456 styles = NumberStyles.Float;
458 string_builder = new System.Text.StringBuilder ();
461 int GetKeyword (char[] id, int id_len)
464 * Keywords are stored in an array of hashtables grouped by their
468 if ((id_len >= keywords.Length) || (keywords [id_len] == null))
470 object o = keywords [id_len] [id];
477 if (handle_get_set == false && (res == Token.GET || res == Token.SET))
479 if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
481 if (handle_assembly == false && res == Token.ASSEMBLY)
485 if (res == Token.FROM &&
486 (current_token == Token.ASSIGN || current_token == Token.OPEN_BRACKET ||
487 current_token == Token.RETURN || current_token == Token.IN)) {
488 query_parsing = true;
492 if (!query_parsing && res > Token.QUERY_FIRST_TOKEN && res < Token.QUERY_LAST_TOKEN)
498 if (!handle_constraints && res == Token.WHERE)
505 public Location Location {
506 get { return current_location; }
509 void define (string def)
511 if (!RootContext.AllDefines.Contains (def)){
512 RootContext.AllDefines [def] = true;
514 if (defines.Contains (def))
516 defines [def] = true;
519 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
521 this.ref_name = file;
522 this.file_name = file;
528 defines = new Hashtable ();
529 foreach (string def in defs)
533 xml_comment_buffer = new StringBuilder ();
536 // FIXME: This could be `Location.Push' but we have to
537 // find out why the MS compiler allows this
539 Mono.CSharp.Location.Push (file);
542 static bool is_identifier_start_character (char c)
544 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter (c);
547 static bool is_identifier_part_character (char c)
549 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') ||
550 Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
553 public static bool IsKeyword (string s)
555 return keyword_strings [s] != null;
558 public static bool IsValidIdentifier (string s)
560 if (s == null || s.Length == 0)
563 if (!is_identifier_start_character (s [0]))
566 for (int i = 1; i < s.Length; i ++)
567 if (! is_identifier_part_character (s [i]))
573 bool parse_generic_dimension (out int dimension)
578 int the_token = token ();
579 if (the_token == Token.OP_GENERICS_GT)
581 else if (the_token == Token.COMMA) {
589 bool parse_less_than ()
592 int the_token = token ();
593 if (the_token == Token.OPEN_BRACKET) {
595 the_token = token ();
596 } while (the_token != Token.CLOSE_BRACKET);
597 the_token = token ();
600 case Token.IDENTIFIER:
623 the_token = token ();
625 if (the_token == Token.OP_GENERICS_GT)
627 else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
629 else if (the_token == Token.INTERR || the_token == Token.STAR)
631 else if (the_token == Token.OP_GENERICS_LT) {
632 if (!parse_less_than ())
635 } else if (the_token == Token.OPEN_BRACKET) {
637 the_token = token ();
638 if (the_token == Token.CLOSE_BRACKET)
640 else if (the_token == Token.COMMA)
641 goto rank_specifiers;
649 public void PutbackNullable ()
651 if (nullable_pos < 0)
652 throw new Exception ();
656 reader.Position = nullable_pos;
661 public void PutbackCloseParens ()
667 int nullable_pos = -1;
669 public void CheckNullable (bool is_nullable)
672 nullable_pos = reader.Position;
678 public int peek_token ()
683 the_token = token ();
689 bool parse_namespace_or_typename (int next)
692 next = peek_token ();
693 while (next == Token.IDENTIFIER){
696 next = peek_token ();
697 if (next == Token.DOT || next == Token.DOUBLE_COLON){
699 next = peek_token ();
702 if (next == Token.OP_GENERICS_LT){
704 if (!parse_less_than ())
714 bool is_simple_type (int token)
716 return (token == Token.BOOL ||
717 token == Token.DECIMAL ||
718 token == Token.SBYTE ||
719 token == Token.BYTE ||
720 token == Token.SHORT ||
721 token == Token.USHORT ||
722 token == Token.INT ||
723 token == Token.UINT ||
724 token == Token.LONG ||
725 token == Token.ULONG ||
726 token == Token.CHAR ||
727 token == Token.FLOAT ||
728 token == Token.DOUBLE);
731 bool is_builtin_reference_type (int token)
733 return (token == Token.OBJECT || token == Token.STRING);
736 bool parse_opt_rank (int next)
739 if (next != Token.OPEN_BRACKET)
745 if (next == Token.CLOSE_BRACKET){
746 next = peek_token ();
749 if (next == Token.COMMA)
759 int next = peek_token ();
761 if (is_simple_type (next)){
763 next = peek_token ();
764 if (next == Token.INTERR)
766 return parse_opt_rank (peek_token ());
768 if (parse_namespace_or_typename (next)){
769 next = peek_token ();
770 if (next == Token.INTERR)
772 return parse_opt_rank (peek_token ());
773 } else if (is_builtin_reference_type (next)){
775 return parse_opt_rank (peek_token ());
782 // Invoked after '(' has been seen and tries to parse:
783 // type identifier [, type identifier]*
785 // if this is the case, instead of returning an
786 // OPEN_PARENS token we return a special token that
787 // triggers lambda parsing.
789 // This is needed because we can not introduce the
790 // explicitly_typed_lambda_parameter_list after a '(' in the
791 // grammar without introducing reduce/reduce conflicts.
793 // We need to parse a type and if it is followed by an
794 // identifier, we know it has to be parsed as a lambda
797 // the type expression can be prefixed with `ref' or `out'
799 public bool parse_lambda_parameters ()
802 int next = peek_token ();
804 if (next == Token.REF || next == Token.OUT)
808 next = peek_token ();
809 if (next == Token.IDENTIFIER){
811 next = peek_token ();
812 if (next == Token.COMMA){
816 if (next == Token.CLOSE_PARENS)
824 int parsing_generic_less_than = 0;
826 int is_punct (char c, ref bool doread)
836 return Token.OPEN_BRACE;
839 return Token.CLOSE_BRACE;
841 // To block doccomment inside attribute declaration.
842 if (doc_state == XmlCommentState.Allowed)
843 doc_state = XmlCommentState.NotAllowed;
844 return Token.OPEN_BRACKET;
846 return Token.CLOSE_BRACKET;
850 bool have_lambda_parameter = parse_lambda_parameters ();
853 if (have_lambda_parameter)
854 return Token.OPEN_PARENS_LAMBDA;
856 return Token.OPEN_PARENS;
858 return Token.OPEN_PARENS;
860 if (deambiguate_close_parens == 0)
861 return Token.CLOSE_PARENS;
863 --deambiguate_close_parens;
867 int new_token = xtoken ();
871 if (new_token == Token.OPEN_PARENS)
872 return Token.CLOSE_PARENS_OPEN_PARENS;
873 else if (new_token == Token.MINUS)
874 return Token.CLOSE_PARENS_MINUS;
875 else if (IsCastToken (new_token))
876 return Token.CLOSE_PARENS_CAST;
878 return Token.CLOSE_PARENS_NO_CAST;
885 return Token.SEMICOLON;
894 return Token.OP_COALESCING;
901 if (parsing_generic_less_than++ > 0)
902 return Token.OP_GENERICS_LT;
907 if (parse_generic_dimension (out dimension)) {
910 return Token.GENERIC_DIMENSION;
915 // Save current position and parse next token.
917 bool is_generic_lt = parse_less_than ();
921 return Token.OP_GENERICS_LT;
923 parsing_generic_less_than = 0;
932 return Token.OP_SHIFT_LEFT_ASSIGN;
934 return Token.OP_SHIFT_LEFT;
935 } else if (d == '='){
940 } else if (c == '>') {
941 if (parsing_generic_less_than > 0) {
942 parsing_generic_less_than--;
943 return Token.OP_GENERICS_GT;
953 return Token.OP_SHIFT_RIGHT_ASSIGN;
955 return Token.OP_SHIFT_RIGHT;
956 } else if (d == '='){
971 t = Token.OP_ADD_ASSIGN;
985 t = Token.OP_SUB_ASSIGN;
1015 return Token.ASSIGN;
1021 return Token.OP_AND;
1022 } else if (d == '='){
1024 return Token.OP_AND_ASSIGN;
1027 return Token.BITWISE_AND;
1034 } else if (d == '='){
1036 return Token.OP_OR_ASSIGN;
1038 return Token.BITWISE_OR;
1044 return Token.OP_MULT_ASSIGN;
1053 return Token.OP_DIV_ASSIGN;
1061 return Token.OP_MOD_ASSIGN;
1063 return Token.PERCENT;
1069 return Token.OP_XOR_ASSIGN;
1071 return Token.CARRET;
1082 return Token.OP_SHIFT_LEFT_ASSIGN;
1084 return Token.OP_SHIFT_LEFT;
1085 } else if (d == '='){
1099 return Token.OP_SHIFT_RIGHT_ASSIGN;
1101 return Token.OP_SHIFT_RIGHT;
1102 } else if (d == '='){
1112 return Token.DOUBLE_COLON;
1121 int deambiguate_close_parens = 0;
1123 public void Deambiguate_CloseParens (object expression)
1127 // When any binary operation is used we are sure it is not a cast
1128 if (expression is Binary)
1131 deambiguate_close_parens++;
1134 bool decimal_digits (int c)
1137 bool seen_digits = false;
1140 if (number_pos == max_number_size)
1141 Error_NumericConstantTooLong ();
1142 number_builder [number_pos++] = (char) c;
1146 // We use peek_char2, because decimal_digits needs to do a
1147 // 2-character look-ahead (5.ToString for example).
1149 while ((d = peek_char2 ()) != -1){
1150 if (d >= '0' && d <= '9'){
1151 if (number_pos == max_number_size)
1152 Error_NumericConstantTooLong ();
1153 number_builder [number_pos++] = (char) d;
1163 static bool is_hex (int e)
1165 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1168 static int real_type_suffix (int c)
1174 t = Token.LITERAL_FLOAT;
1177 t = Token.LITERAL_DOUBLE;
1180 t= Token.LITERAL_DECIMAL;
1188 int integer_type_suffix (ulong ul, int c)
1190 bool is_unsigned = false;
1191 bool is_long = false;
1194 bool scanning = true;
1205 if (!is_unsigned && (RootContext.WarningLevel >= 4)){
1207 // if we have not seen anything in between
1208 // report this error
1210 Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
1213 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
1214 // work around that.
1238 if (is_long && is_unsigned){
1240 return Token.LITERAL_INTEGER;
1241 } else if (is_unsigned){
1242 // uint if possible, or ulong else.
1244 if ((ul & 0xffffffff00000000) == 0)
1248 } else if (is_long){
1249 // long if possible, ulong otherwise
1250 if ((ul & 0x8000000000000000) != 0)
1255 // int, uint, long or ulong in that order
1256 if ((ul & 0xffffffff00000000) == 0){
1257 uint ui = (uint) ul;
1259 if ((ui & 0x80000000) != 0)
1264 if ((ul & 0x8000000000000000) != 0)
1270 return Token.LITERAL_INTEGER;
1274 // given `c' as the next char in the input decide whether
1275 // we need to convert to a special type, and then choose
1276 // the best representation for the integer
1278 int adjust_int (int c)
1281 if (number_pos > 9){
1282 ulong ul = (uint) (number_builder [0] - '0');
1284 for (int i = 1; i < number_pos; i++){
1285 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
1287 return integer_type_suffix (ul, c);
1289 uint ui = (uint) (number_builder [0] - '0');
1291 for (int i = 1; i < number_pos; i++){
1292 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
1294 return integer_type_suffix (ui, c);
1296 } catch (OverflowException) {
1297 error_details = "Integral constant is too large";
1298 Report.Error (1021, Location, error_details);
1300 return Token.LITERAL_INTEGER;
1302 catch (FormatException) {
1303 Report.Error (1013, Location, "Invalid number");
1305 return Token.LITERAL_INTEGER;
1309 int adjust_real (int t)
1311 string s = new String (number_builder, 0, number_pos);
1312 const string error_details = "Floating-point constant is outside the range of type `{0}'";
1315 case Token.LITERAL_DECIMAL:
1317 val = System.Decimal.Parse (s, styles, csharp_format_info);
1318 } catch (OverflowException) {
1320 Report.Error (594, Location, error_details, "decimal");
1323 case Token.LITERAL_FLOAT:
1325 val = float.Parse (s, styles, csharp_format_info);
1326 } catch (OverflowException) {
1328 Report.Error (594, Location, error_details, "float");
1332 case Token.LITERAL_DOUBLE:
1334 t = Token.LITERAL_DOUBLE;
1336 val = System.Double.Parse (s, styles, csharp_format_info);
1337 } catch (OverflowException) {
1339 Report.Error (594, Location, error_details, "double");
1352 while ((d = peek_char ()) != -1){
1354 number_builder [number_pos++] = (char) d;
1360 string s = new String (number_builder, 0, number_pos);
1362 if (number_pos <= 8)
1363 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
1365 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
1366 } catch (OverflowException){
1367 error_details = "Integral constant is too large";
1368 Report.Error (1021, Location, error_details);
1370 return Token.LITERAL_INTEGER;
1372 catch (FormatException) {
1373 Report.Error (1013, Location, "Invalid number");
1375 return Token.LITERAL_INTEGER;
1378 return integer_type_suffix (ul, peek_char ());
1382 // Invoked if we know we have .digits or digits
1384 int is_number (int c)
1386 bool is_real = false;
1391 if (c >= '0' && c <= '9'){
1393 int peek = peek_char ();
1395 if (peek == 'x' || peek == 'X')
1396 return handle_hex ();
1403 // We need to handle the case of
1404 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1407 if (decimal_digits ('.')){
1413 return adjust_int (-1);
1417 if (c == 'e' || c == 'E'){
1419 if (number_pos == max_number_size)
1420 Error_NumericConstantTooLong ();
1421 number_builder [number_pos++] = 'e';
1425 if (number_pos == max_number_size)
1426 Error_NumericConstantTooLong ();
1427 number_builder [number_pos++] = '+';
1429 } else if (c == '-') {
1430 if (number_pos == max_number_size)
1431 Error_NumericConstantTooLong ();
1432 number_builder [number_pos++] = '-';
1435 if (number_pos == max_number_size)
1436 Error_NumericConstantTooLong ();
1437 number_builder [number_pos++] = '+';
1444 type = real_type_suffix (c);
1445 if (type == Token.NONE && !is_real){
1447 return adjust_int (c);
1451 if (type == Token.NONE){
1456 return adjust_real (type);
1458 Console.WriteLine ("This should not be reached");
1459 throw new Exception ("Is Number should never reach this point");
1463 // Accepts exactly count (4 or 8) hex, no more no less
1465 int getHex (int count, out bool error)
1470 int top = count != -1 ? count : 4;
1474 for (i = 0; i < top; i++){
1477 if (c >= '0' && c <= '9')
1478 c = (int) c - (int) '0';
1479 else if (c >= 'A' && c <= 'F')
1480 c = (int) c - (int) 'A' + 10;
1481 else if (c >= 'a' && c <= 'f')
1482 c = (int) c - (int) 'a' + 10;
1488 total = (total * 16) + c;
1490 int p = peek_char ();
1493 if (!is_hex ((char)p))
1534 v = getHex (-1, out error);
1539 v = getHex (4, out error);
1544 v = getHex (8, out error);
1549 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1559 if (putback_char != -1) {
1577 if (putback_char != -1)
1578 return putback_char;
1579 putback_char = reader.Read ();
1580 return putback_char;
1585 if (putback_char != -1)
1586 return putback_char;
1587 return reader.Peek ();
1590 void putback (int c)
1592 if (putback_char != -1){
1593 Console.WriteLine ("Col: " + col);
1594 Console.WriteLine ("Row: " + line);
1595 Console.WriteLine ("Name: " + ref_name.Name);
1596 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
1597 throw new Exception ("This should not happen putback on putback");
1599 if (c == '\n' || col == 0) {
1600 // It won't happen though.
1610 public bool advance ()
1612 return peek_char () != -1;
1615 public Object Value {
1621 public Object value ()
1626 static bool IsCastToken (int token)
1631 case Token.IDENTIFIER:
1632 case Token.LITERAL_INTEGER:
1633 case Token.LITERAL_FLOAT:
1634 case Token.LITERAL_DOUBLE:
1635 case Token.LITERAL_DECIMAL:
1636 case Token.LITERAL_CHARACTER:
1637 case Token.LITERAL_STRING:
1640 case Token.DELEGATE:
1650 case Token.UNCHECKED:
1657 // These can be part of a member access
1677 current_token = xtoken ();
1680 if (current_token != Token.DEFAULT)
1681 return current_token;
1686 current_token = Token.ERROR;
1687 else if (c == Token.OPEN_PARENS)
1688 current_token = Token.DEFAULT_OPEN_PARENS;
1689 else if (c == Token.COLON)
1690 current_token = Token.DEFAULT_COLON;
1694 return current_token;
1697 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
1699 void get_cmd_arg (out string cmd, out string arg)
1703 tokens_seen = false;
1705 static_cmd_arg.Length = 0;
1707 // skip over white space
1708 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1711 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
1712 if (is_identifier_part_character ((char) c)){
1713 static_cmd_arg.Append ((char) c);
1721 cmd = static_cmd_arg.ToString ();
1723 if (c == '\n' || c == '\r'){
1727 // skip over white space
1728 while ((c = get_char ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1733 } else if (c == '\r'){
1735 } else if (c == -1){
1740 static_cmd_arg.Length = 0;
1741 static_cmd_arg.Append ((char) c);
1743 while ((c = get_char ()) != -1 && (c != '\n') && (c != '\r')){
1744 static_cmd_arg.Append ((char) c);
1747 arg = static_cmd_arg.ToString ();
1751 // Handles the #line directive
1753 bool PreProcessLine (string arg)
1755 if (arg.Length == 0)
1758 if (arg == "default"){
1760 ref_name = file_name;
1761 Location.Push (ref_name);
1763 } else if (arg == "hidden"){
1765 // We ignore #line hidden
1773 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
1774 ref_line = System.Int32.Parse (arg.Substring (0, pos));
1777 char [] quotes = { '\"' };
1779 string name = arg.Substring (pos). Trim (quotes);
1780 ref_name = Location.LookupFile (name);
1781 file_name.HasLineDirective = true;
1782 ref_name.HasLineDirective = true;
1783 Location.Push (ref_name);
1785 ref_line = System.Int32.Parse (arg);
1795 // Handles #define and #undef
1797 void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
1799 if (arg.Length == 0 || arg == "true" || arg == "false"){
1800 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
1804 if (arg.IndexOfAny (simple_whitespaces) != -1){
1805 Error_EndLineExpected ();
1809 if (!is_identifier_start_character (arg [0]))
1810 Report.Error (1001, Location, "Identifier expected: " + arg);
1812 foreach (char c in arg.Substring (1)){
1813 if (!is_identifier_part_character (c)){
1814 Report.Error (1001, Location, "Identifier expected: " + arg);
1819 if (!caller_is_taking)
1823 if (defines == null)
1824 defines = new Hashtable ();
1827 if (defines == null)
1829 if (defines.Contains (arg))
1830 defines.Remove (arg);
1835 /// Handles #pragma directive
1837 void PreProcessPragma (string arg)
1839 const string warning = "warning";
1840 const string w_disable = "warning disable";
1841 const string w_restore = "warning restore";
1843 if (arg == w_disable) {
1844 Report.RegisterWarningRegion (Location).WarningDisable (line);
1848 if (arg == w_restore) {
1849 Report.RegisterWarningRegion (Location).WarningEnable (line);
1853 if (arg.StartsWith (w_disable)) {
1854 int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
1855 foreach (int code in codes) {
1857 Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
1862 if (arg.StartsWith (w_restore)) {
1863 int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
1864 Hashtable w_table = Report.warning_ignore_table;
1865 foreach (int code in codes) {
1866 if (w_table != null && w_table.Contains (code))
1867 Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
1868 Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
1873 if (arg.StartsWith (warning)) {
1874 Report.Warning (1634, 1, Location, "Expected disable or restore");
1878 Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
1881 int[] ParseNumbers (string text)
1883 string[] string_array = text.Split (',');
1884 int[] values = new int [string_array.Length];
1886 foreach (string string_code in string_array) {
1888 values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
1890 catch (FormatException) {
1891 Report.Warning (1692, 1, Location, "Invalid number");
1897 bool eval_val (string s)
1904 if (defines == null)
1906 if (defines.Contains (s))
1912 bool pp_primary (ref string s)
1921 s = s.Substring (1);
1922 bool val = pp_expr (ref s, false);
1923 if (s.Length > 0 && s [0] == ')'){
1924 s = s.Substring (1);
1927 Error_InvalidDirective ();
1931 if (is_identifier_start_character (c)){
1937 if (is_identifier_part_character (c)){
1941 bool v = eval_val (s.Substring (0, j));
1942 s = s.Substring (j);
1945 bool vv = eval_val (s);
1950 Error_InvalidDirective ();
1954 bool pp_unary (ref string s)
1961 if (len > 1 && s [1] == '='){
1962 Error_InvalidDirective ();
1965 s = s.Substring (1);
1966 return ! pp_primary (ref s);
1968 return pp_primary (ref s);
1970 Error_InvalidDirective ();
1975 bool pp_eq (ref string s)
1977 bool va = pp_unary (ref s);
1983 if (len > 2 && s [1] == '='){
1984 s = s.Substring (2);
1985 return va == pp_unary (ref s);
1987 Error_InvalidDirective ();
1990 } else if (s [0] == '!' && len > 1 && s [1] == '='){
1991 s = s.Substring (2);
1993 return va != pp_unary (ref s);
2002 bool pp_and (ref string s)
2004 bool va = pp_eq (ref s);
2010 if (len > 2 && s [1] == '&'){
2011 s = s.Substring (2);
2012 return (va & pp_and (ref s));
2014 Error_InvalidDirective ();
2023 // Evaluates an expression for `#if' or `#elif'
2025 bool pp_expr (ref string s, bool isTerm)
2027 bool va = pp_and (ref s);
2034 if (len > 2 && s [1] == '|'){
2035 s = s.Substring (2);
2036 return va | pp_expr (ref s, isTerm);
2038 Error_InvalidDirective ();
2043 Error_EndLineExpected ();
2051 bool eval (string s)
2053 bool v = pp_expr (ref s, true);
2062 void Error_NumericConstantTooLong ()
2064 Report.Error (1021, Location, "Numeric constant too long");
2067 void Error_InvalidDirective ()
2069 Report.Error (1517, Location, "Invalid preprocessor directive");
2072 void Error_UnexpectedDirective (string extra)
2076 "Unexpected processor directive (" + extra + ")");
2079 void Error_TokensSeen ()
2081 Report.Error (1032, Location,
2082 "Cannot define or undefine preprocessor symbols after first token in file");
2085 void Eror_WrongPreprocessorLocation ()
2087 Report.Error (1040, Location,
2088 "Preprocessor directives must appear as the first non-whitespace character on a line");
2091 void Error_EndLineExpected ()
2093 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
2097 // if true, then the code continues processing the code
2098 // if false, the code stays in a loop until another directive is
2100 // When caller_is_taking is false we ignore all directives except the ones
2101 // which can help us to identify where the #if block ends
2102 bool handle_preprocessing_directive (bool caller_is_taking)
2105 bool region_directive = false;
2107 get_cmd_arg (out cmd, out arg);
2109 // Eat any trailing whitespaces and single-line comments
2110 if (arg.IndexOf ("//") != -1)
2111 arg = arg.Substring (0, arg.IndexOf ("//"));
2112 arg = arg.Trim (simple_whitespaces);
2115 // The first group of pre-processing instructions is always processed
2119 region_directive = true;
2124 if (ifstack == null || ifstack.Count == 0){
2125 Error_UnexpectedDirective ("no #region for this #endregion");
2128 int pop = (int) ifstack.Pop ();
2130 if ((pop & REGION) == 0)
2131 Report.Error (1027, Location, "Expected `#endif' directive");
2133 return caller_is_taking;
2136 if (ifstack == null)
2137 ifstack = new Stack (2);
2139 int flags = region_directive ? REGION : 0;
2140 if (ifstack.Count == 0){
2141 flags |= PARENT_TAKING;
2143 int state = (int) ifstack.Peek ();
2144 if ((state & TAKING) != 0) {
2145 flags |= PARENT_TAKING;
2149 if (caller_is_taking && eval (arg)) {
2150 ifstack.Push (flags | TAKING);
2153 ifstack.Push (flags);
2157 if (ifstack == null || ifstack.Count == 0){
2158 Error_UnexpectedDirective ("no #if for this #endif");
2161 pop = (int) ifstack.Pop ();
2163 if ((pop & REGION) != 0)
2164 Report.Error (1038, Location, "#endregion directive expected");
2166 if (arg.Length != 0) {
2167 Error_EndLineExpected ();
2170 if (ifstack.Count == 0)
2173 int state = (int) ifstack.Peek ();
2174 return (state & TAKING) != 0;
2178 if (ifstack == null || ifstack.Count == 0){
2179 Error_UnexpectedDirective ("no #if for this #elif");
2182 int state = (int) ifstack.Pop ();
2184 if ((state & REGION) != 0) {
2185 Report.Error (1038, Location, "#endregion directive expected");
2189 if ((state & ELSE_SEEN) != 0){
2190 Error_UnexpectedDirective ("#elif not valid after #else");
2194 if ((state & TAKING) != 0) {
2199 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
2200 ifstack.Push (state | TAKING);
2204 ifstack.Push (state);
2209 if (ifstack == null || ifstack.Count == 0){
2210 Error_UnexpectedDirective ("no #if for this #else");
2213 int state = (int) ifstack.Peek ();
2215 if ((state & REGION) != 0) {
2216 Report.Error (1038, Location, "#endregion directive expected");
2220 if ((state & ELSE_SEEN) != 0){
2221 Error_UnexpectedDirective ("#else within #else");
2227 if (arg.Length != 0) {
2228 Error_EndLineExpected ();
2233 if ((state & PARENT_TAKING) != 0) {
2234 ret = (state & TAKING) == 0;
2242 ifstack.Push (state | ELSE_SEEN);
2247 if (any_token_seen){
2248 Error_TokensSeen ();
2249 return caller_is_taking;
2251 PreProcessDefinition (true, arg, caller_is_taking);
2252 return caller_is_taking;
2255 if (any_token_seen){
2256 Error_TokensSeen ();
2257 return caller_is_taking;
2259 PreProcessDefinition (false, arg, caller_is_taking);
2260 return caller_is_taking;
2264 // These are only processed if we are in a `taking' block
2266 if (!caller_is_taking)
2271 Report.Error (1029, Location, "#error: '" + arg + "'");
2275 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
2279 if (RootContext.Version == LanguageVersion.ISO_1) {
2280 Report.FeatureIsNotISO1 (Location, "#pragma");
2284 PreProcessPragma (arg);
2288 if (!PreProcessLine (arg))
2291 "The line number specified for #line directive is missing or invalid");
2292 return caller_is_taking;
2295 Report.Error (1024, Location, "Wrong preprocessor directive");
2300 private int consume_string (bool quoted)
2303 string_builder.Length = 0;
2305 while ((c = get_char ()) != -1){
2307 if (quoted && peek_char () == '"'){
2308 string_builder.Append ((char) c);
2312 val = string_builder.ToString ();
2313 return Token.LITERAL_STRING;
2319 Report.Error (1010, Location, "Newline in constant");
2327 string_builder.Append ((char) c);
2330 Report.Error (1039, Location, "Unterminated string literal");
2334 private int consume_identifier (int s)
2336 int res = consume_identifier (s, false);
2338 if (doc_state == XmlCommentState.Allowed)
2339 doc_state = XmlCommentState.NotAllowed;
2342 case Token.NAMESPACE:
2343 check_incorrect_doc_comment ();
2347 if (res == Token.PARTIAL) {
2348 // Save current position and parse next token.
2351 int next_token = token ();
2352 bool ok = (next_token == Token.CLASS) ||
2353 (next_token == Token.STRUCT) ||
2354 (next_token == Token.INTERFACE);
2361 if (next_token < Token.LAST_KEYWORD)
2362 Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
2364 val = new LocatedToken (Location, "partial");
2365 return Token.IDENTIFIER;
2371 private int consume_identifier (int s, bool quoted)
2376 id_builder [0] = (char) s;
2378 current_location = new Location (ref_line, Col);
2380 while ((c = get_char ()) != -1) {
2382 if (is_identifier_part_character ((char) c)){
2383 if (pos == max_id_size){
2384 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
2388 id_builder [pos++] = (char) c;
2389 // putback_char = -1;
2390 } else if (c == '\\') {
2394 // putback_char = c;
2401 // Optimization: avoids doing the keyword lookup
2402 // on uppercase letters and _
2404 if (!quoted && (s >= 'a' || s == '_')){
2405 int keyword = GetKeyword (id_builder, pos);
2406 if (keyword != -1) {
2413 // Keep identifiers in an array of hashtables to avoid needless
2417 if (identifiers [pos] != null) {
2418 val = identifiers [pos][id_builder];
2420 val = new LocatedToken (Location, (string) val);
2422 escaped_identifiers.Add (val);
2423 return Token.IDENTIFIER;
2427 identifiers [pos] = new CharArrayHashtable (pos);
2429 val = new String (id_builder, 0, pos);
2430 if (RootContext.Version == LanguageVersion.ISO_1) {
2431 for (int i = 1; i < id_builder.Length; i += 3) {
2432 if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
2433 Report.Error (1638, Location,
2434 "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
2440 char [] chars = new char [pos];
2441 Array.Copy (id_builder, chars, pos);
2443 identifiers [pos] [chars] = val;
2445 val = new LocatedToken (Location, (string) val);
2447 escaped_identifiers.Add (val);
2448 return Token.IDENTIFIER;
2451 public int xtoken ()
2454 bool doread = false;
2457 // Whether we have seen comments on the current line
2458 bool comments_seen = false;
2460 for (;(c = get_char ()) != -1;) {
2462 col = ((col + 8) / 8) * 8;
2466 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0 || c == 0)
2470 if (peek_char () == '\n')
2473 any_token_seen |= tokens_seen;
2474 tokens_seen = false;
2475 comments_seen = false;
2479 // Handle double-slash comments.
2481 int d = peek_char ();
2485 if (RootContext.Documentation != null && peek_char () == '/') {
2487 // Don't allow ////.
2488 if ((d = peek_char ()) != '/') {
2489 update_comment_location ();
2490 if (doc_state == XmlCommentState.Allowed)
2491 handle_one_line_xml_comment ();
2492 else if (doc_state == XmlCommentState.NotAllowed)
2493 warn_incorrect_doc_comment ();
2496 while ((d = get_char ()) != -1 && (d != '\n') && d != '\r')
2499 any_token_seen |= tokens_seen;
2500 tokens_seen = false;
2501 comments_seen = false;
2503 } else if (d == '*'){
2505 bool docAppend = false;
2506 if (RootContext.Documentation != null && peek_char () == '*') {
2508 update_comment_location ();
2509 // But when it is /**/, just do nothing.
2510 if (peek_char () == '/') {
2514 if (doc_state == XmlCommentState.Allowed)
2516 else if (doc_state == XmlCommentState.NotAllowed)
2517 warn_incorrect_doc_comment ();
2520 int current_comment_start = 0;
2522 current_comment_start = xml_comment_buffer.Length;
2523 xml_comment_buffer.Append (Environment.NewLine);
2526 Location start_location = Location;
2528 while ((d = get_char ()) != -1){
2529 if (d == '*' && peek_char () == '/'){
2531 comments_seen = true;
2535 xml_comment_buffer.Append ((char) d);
2538 any_token_seen |= tokens_seen;
2539 tokens_seen = false;
2541 // Reset 'comments_seen' just to be consistent.
2542 // It doesn't matter either way, here.
2544 comments_seen = false;
2548 Report.Error (1035, start_location, "End-of-file found, '*/' expected");
2551 update_formatted_doc_comment (current_comment_start);
2554 goto is_punct_label;
2558 if (c == '\\' || is_identifier_start_character ((char)c)){
2560 return consume_identifier (c);
2564 current_location = new Location (ref_line, Col);
2565 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
2575 any_token_seen |= tokens_seen;
2576 tokens_seen = false;
2577 comments_seen = false;
2581 if (c >= '0' && c <= '9'){
2583 return is_number (c);
2588 int peek = peek_char ();
2589 if (peek >= '0' && peek <= '9')
2590 return is_number (c);
2595 if (tokens_seen || comments_seen) {
2596 Eror_WrongPreprocessorLocation ();
2600 if (handle_preprocessing_directive (true))
2603 bool directive_expected = false;
2604 while ((c = get_char ()) != -1) {
2606 directive_expected = true;
2607 } else if (!directive_expected) {
2608 // TODO: Implement comment support for disabled code and uncomment this code
2610 // Eror_WrongPreprocessorLocation ();
2611 // return Token.ERROR;
2616 if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
2620 if (handle_preprocessing_directive (false))
2623 directive_expected = false;
2627 tokens_seen = false;
2635 return consume_string (false);
2641 error_details = "Empty character literal";
2642 Report.Error (1011, Location, error_details);
2645 if (c == '\r' || c == '\n') {
2646 Report.Error (1010, Location, "Newline in constant");
2652 val = new System.Char ();
2657 error_details = "Too many characters in character literal";
2658 Report.Error (1012, Location, error_details);
2660 // Try to recover, read until newline or next "'"
2661 while ((c = get_char ()) != -1){
2670 return Token.LITERAL_CHARACTER;
2677 return consume_string (true);
2678 } else if (is_identifier_start_character ((char) c)){
2679 return consume_identifier (c, true);
2681 Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
2685 error_details = ((char)c).ToString ();
2694 // Handles one line xml comment
2696 private void handle_one_line_xml_comment ()
2699 while ((c = peek_char ()) == ' ')
2700 get_char (); // skip heading whitespaces.
2701 while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') {
2702 xml_comment_buffer.Append ((char) get_char ());
2704 if (c == '\r' || c == '\n')
2705 xml_comment_buffer.Append (Environment.NewLine);
2709 // Remove heading "*" in Javadoc-like xml documentation.
2711 private void update_formatted_doc_comment (int current_comment_start)
2713 int length = xml_comment_buffer.Length - current_comment_start;
2714 string [] lines = xml_comment_buffer.ToString (
2715 current_comment_start,
2716 length).Replace ("\r", "").Split ('\n');
2718 // The first line starts with /**, thus it is not target
2719 // for the format check.
2720 for (int i = 1; i < lines.Length; i++) {
2721 string s = lines [i];
2722 int idx = s.IndexOf ('*');
2725 if (i < lines.Length - 1)
2729 head = s.Substring (0, idx);
2730 foreach (char c in head)
2733 lines [i] = s.Substring (idx + 1);
2735 xml_comment_buffer.Remove (current_comment_start, length);
2736 xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
2740 // Updates current comment location.
2742 private void update_comment_location ()
2744 if (current_comment_location.IsNull) {
2745 // "-2" is for heading "//" or "/*"
2746 current_comment_location =
2747 new Location (ref_line, col - 2);
2752 // Checks if there was incorrect doc comments and raise
2755 public void check_incorrect_doc_comment ()
2757 if (xml_comment_buffer.Length > 0)
2758 warn_incorrect_doc_comment ();
2762 // Raises a warning when tokenizer found incorrect doccomment
2765 private void warn_incorrect_doc_comment ()
2767 if (doc_state != XmlCommentState.Error) {
2768 doc_state = XmlCommentState.Error;
2769 // in csc, it is 'XML comment is not placed on
2770 // a valid language element'. But that does not
2772 Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
2777 // Consumes the saved xml comment lines (if any)
2778 // as for current target member or type.
2780 public string consume_doc_comment ()
2782 if (xml_comment_buffer.Length > 0) {
2783 string ret = xml_comment_buffer.ToString ();
2784 reset_doc_comment ();
2790 void reset_doc_comment ()
2792 xml_comment_buffer.Length = 0;
2793 current_comment_location = Location.Null;
2796 public void cleanup ()
2798 if (ifstack != null && ifstack.Count >= 1) {
2799 current_location = new Location (ref_line, Col);
2800 int state = (int) ifstack.Pop ();
2801 if ((state & REGION) != 0)
2802 Report.Error (1038, Location, "#endregion directive expected");
2804 Report.Error (1027, Location, "Expected `#endif' directive");
2810 // Indicates whether it accepts XML documentation or not.
2812 public enum XmlCommentState {
2813 // comment is allowed in this state.
2815 // comment is not allowed in this state.
2817 // once comments appeared when it is NotAllowed, then the
2818 // state is changed to it, until the state is changed to