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 escapedIdentifiers = 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 xmlDocState = 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 tokenValues;
78 static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
80 private static Hashtable TokenValueName
83 if (tokenValues == null)
84 tokenValues = 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 xmlDocState; }
188 if (value == XmlCommentState.Allowed) {
189 check_incorrect_doc_comment ();
190 reset_doc_comment ();
196 public bool IsEscapedIdentifier (Location loc)
198 foreach (LocatedToken lt in escapedIdentifiers)
199 if (lt.Location.Equals (loc))
207 static CharArrayHashtable[] keywords;
208 static Hashtable keywordStrings;
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 keywordStrings.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 keywordStrings = 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 keywordStrings [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 if (parsing_generic_less_than++ > 0)
895 return Token.OP_GENERICS_LT;
900 if (parse_generic_dimension (out dimension)) {
903 return Token.GENERIC_DIMENSION;
908 // Save current position and parse next token.
910 bool is_generic_lt = parse_less_than ();
914 return Token.OP_GENERICS_LT;
916 parsing_generic_less_than = 0;
925 return Token.OP_SHIFT_LEFT_ASSIGN;
927 return Token.OP_SHIFT_LEFT;
928 } else if (d == '='){
933 } else if (c == '>') {
934 if (parsing_generic_less_than > 0) {
935 parsing_generic_less_than--;
936 return Token.OP_GENERICS_GT;
946 return Token.OP_SHIFT_RIGHT_ASSIGN;
948 return Token.OP_SHIFT_RIGHT;
949 } else if (d == '='){
964 t = Token.OP_ADD_ASSIGN;
978 t = Token.OP_SUB_ASSIGN;
1008 return Token.ASSIGN;
1014 return Token.OP_AND;
1015 } else if (d == '='){
1017 return Token.OP_AND_ASSIGN;
1020 return Token.BITWISE_AND;
1027 } else if (d == '='){
1029 return Token.OP_OR_ASSIGN;
1031 return Token.BITWISE_OR;
1037 return Token.OP_MULT_ASSIGN;
1046 return Token.OP_DIV_ASSIGN;
1054 return Token.OP_MOD_ASSIGN;
1056 return Token.PERCENT;
1062 return Token.OP_XOR_ASSIGN;
1064 return Token.CARRET;
1075 return Token.OP_SHIFT_LEFT_ASSIGN;
1077 return Token.OP_SHIFT_LEFT;
1078 } else if (d == '='){
1092 return Token.OP_SHIFT_RIGHT_ASSIGN;
1094 return Token.OP_SHIFT_RIGHT;
1095 } else if (d == '='){
1105 return Token.DOUBLE_COLON;
1114 int deambiguate_close_parens = 0;
1116 public void Deambiguate_CloseParens (object expression)
1120 // When any binary operation is used we are sure it is not a cast
1121 if (expression is Binary)
1124 deambiguate_close_parens++;
1127 bool decimal_digits (int c)
1130 bool seen_digits = false;
1133 if (number_pos == max_number_size)
1134 Error_NumericConstantTooLong ();
1135 number_builder [number_pos++] = (char) c;
1139 // We use peekChar2, because decimal_digits needs to do a
1140 // 2-character look-ahead (5.ToString for example).
1142 while ((d = peekChar2 ()) != -1){
1143 if (d >= '0' && d <= '9'){
1144 if (number_pos == max_number_size)
1145 Error_NumericConstantTooLong ();
1146 number_builder [number_pos++] = (char) d;
1156 static bool is_hex (int e)
1158 return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1161 static int real_type_suffix (int c)
1167 t = Token.LITERAL_FLOAT;
1170 t = Token.LITERAL_DOUBLE;
1173 t= Token.LITERAL_DECIMAL;
1181 int integer_type_suffix (ulong ul, int c)
1183 bool is_unsigned = false;
1184 bool is_long = false;
1187 bool scanning = true;
1198 if (!is_unsigned && (RootContext.WarningLevel >= 4)){
1200 // if we have not seen anything in between
1201 // report this error
1203 Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
1206 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
1207 // work around that.
1231 if (is_long && is_unsigned){
1233 return Token.LITERAL_INTEGER;
1234 } else if (is_unsigned){
1235 // uint if possible, or ulong else.
1237 if ((ul & 0xffffffff00000000) == 0)
1241 } else if (is_long){
1242 // long if possible, ulong otherwise
1243 if ((ul & 0x8000000000000000) != 0)
1248 // int, uint, long or ulong in that order
1249 if ((ul & 0xffffffff00000000) == 0){
1250 uint ui = (uint) ul;
1252 if ((ui & 0x80000000) != 0)
1257 if ((ul & 0x8000000000000000) != 0)
1263 return Token.LITERAL_INTEGER;
1267 // given `c' as the next char in the input decide whether
1268 // we need to convert to a special type, and then choose
1269 // the best representation for the integer
1271 int adjust_int (int c)
1274 if (number_pos > 9){
1275 ulong ul = (uint) (number_builder [0] - '0');
1277 for (int i = 1; i < number_pos; i++){
1278 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
1280 return integer_type_suffix (ul, c);
1282 uint ui = (uint) (number_builder [0] - '0');
1284 for (int i = 1; i < number_pos; i++){
1285 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
1287 return integer_type_suffix (ui, c);
1289 } catch (OverflowException) {
1290 error_details = "Integral constant is too large";
1291 Report.Error (1021, Location, error_details);
1293 return Token.LITERAL_INTEGER;
1295 catch (FormatException) {
1296 Report.Error (1013, Location, "Invalid number");
1298 return Token.LITERAL_INTEGER;
1302 int adjust_real (int t)
1304 string s = new String (number_builder, 0, number_pos);
1305 const string error_details = "Floating-point constant is outside the range of type `{0}'";
1308 case Token.LITERAL_DECIMAL:
1310 val = System.Decimal.Parse (s, styles, csharp_format_info);
1311 } catch (OverflowException) {
1313 Report.Error (594, Location, error_details, "decimal");
1316 case Token.LITERAL_FLOAT:
1318 val = float.Parse (s, styles, csharp_format_info);
1319 } catch (OverflowException) {
1321 Report.Error (594, Location, error_details, "float");
1325 case Token.LITERAL_DOUBLE:
1327 t = Token.LITERAL_DOUBLE;
1329 val = System.Double.Parse (s, styles, csharp_format_info);
1330 } catch (OverflowException) {
1332 Report.Error (594, Location, error_details, "double");
1345 while ((d = peekChar ()) != -1){
1347 number_builder [number_pos++] = (char) d;
1353 string s = new String (number_builder, 0, number_pos);
1355 if (number_pos <= 8)
1356 ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
1358 ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
1359 } catch (OverflowException){
1360 error_details = "Integral constant is too large";
1361 Report.Error (1021, Location, error_details);
1363 return Token.LITERAL_INTEGER;
1365 catch (FormatException) {
1366 Report.Error (1013, Location, "Invalid number");
1368 return Token.LITERAL_INTEGER;
1371 return integer_type_suffix (ul, peekChar ());
1375 // Invoked if we know we have .digits or digits
1377 int is_number (int c)
1379 bool is_real = false;
1384 if (c >= '0' && c <= '9'){
1386 int peek = peekChar ();
1388 if (peek == 'x' || peek == 'X')
1389 return handle_hex ();
1396 // We need to handle the case of
1397 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1400 if (decimal_digits ('.')){
1406 return adjust_int (-1);
1410 if (c == 'e' || c == 'E'){
1412 if (number_pos == max_number_size)
1413 Error_NumericConstantTooLong ();
1414 number_builder [number_pos++] = 'e';
1418 if (number_pos == max_number_size)
1419 Error_NumericConstantTooLong ();
1420 number_builder [number_pos++] = '+';
1422 } else if (c == '-') {
1423 if (number_pos == max_number_size)
1424 Error_NumericConstantTooLong ();
1425 number_builder [number_pos++] = '-';
1428 if (number_pos == max_number_size)
1429 Error_NumericConstantTooLong ();
1430 number_builder [number_pos++] = '+';
1437 type = real_type_suffix (c);
1438 if (type == Token.NONE && !is_real){
1440 return adjust_int (c);
1444 if (type == Token.NONE){
1449 return adjust_real (type);
1451 Console.WriteLine ("This should not be reached");
1452 throw new Exception ("Is Number should never reach this point");
1456 // Accepts exactly count (4 or 8) hex, no more no less
1458 int getHex (int count, out bool error)
1463 int top = count != -1 ? count : 4;
1467 for (i = 0; i < top; i++){
1470 if (c >= '0' && c <= '9')
1471 c = (int) c - (int) '0';
1472 else if (c >= 'A' && c <= 'F')
1473 c = (int) c - (int) 'A' + 10;
1474 else if (c >= 'a' && c <= 'f')
1475 c = (int) c - (int) 'a' + 10;
1481 total = (total * 16) + c;
1483 int p = peekChar ();
1486 if (!is_hex ((char)p))
1527 v = getHex (-1, out error);
1532 v = getHex (4, out error);
1537 v = getHex (8, out error);
1542 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1552 if (putback_char != -1) {
1570 if (putback_char != -1)
1571 return putback_char;
1572 putback_char = reader.Read ();
1573 return putback_char;
1578 if (putback_char != -1)
1579 return putback_char;
1580 return reader.Peek ();
1583 void putback (int c)
1585 if (putback_char != -1){
1586 Console.WriteLine ("Col: " + col);
1587 Console.WriteLine ("Row: " + line);
1588 Console.WriteLine ("Name: " + ref_name.Name);
1589 Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
1590 throw new Exception ("This should not happen putback on putback");
1592 if (c == '\n' || col == 0) {
1593 // It won't happen though.
1603 public bool advance ()
1605 return peekChar () != -1;
1608 public Object Value {
1614 public Object value ()
1619 static bool IsCastToken (int token)
1624 case Token.IDENTIFIER:
1625 case Token.LITERAL_INTEGER:
1626 case Token.LITERAL_FLOAT:
1627 case Token.LITERAL_DOUBLE:
1628 case Token.LITERAL_DECIMAL:
1629 case Token.LITERAL_CHARACTER:
1630 case Token.LITERAL_STRING:
1633 case Token.DELEGATE:
1643 case Token.UNCHECKED:
1650 // These can be part of a member access
1670 current_token = xtoken ();
1673 if (current_token != Token.DEFAULT)
1674 return current_token;
1679 current_token = Token.ERROR;
1680 else if (c == Token.OPEN_PARENS)
1681 current_token = Token.DEFAULT_OPEN_PARENS;
1682 else if (c == Token.COLON)
1683 current_token = Token.DEFAULT_COLON;
1687 return current_token;
1690 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
1692 void get_cmd_arg (out string cmd, out string arg)
1696 tokens_seen = false;
1698 static_cmd_arg.Length = 0;
1700 // skip over white space
1701 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1704 while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
1705 if (is_identifier_part_character ((char) c)){
1706 static_cmd_arg.Append ((char) c);
1714 cmd = static_cmd_arg.ToString ();
1716 if (c == '\n' || c == '\r'){
1720 // skip over white space
1721 while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1726 } else if (c == '\r'){
1728 } else if (c == -1){
1733 static_cmd_arg.Length = 0;
1734 static_cmd_arg.Append ((char) c);
1736 while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
1737 static_cmd_arg.Append ((char) c);
1740 arg = static_cmd_arg.ToString ();
1744 // Handles the #line directive
1746 bool PreProcessLine (string arg)
1748 if (arg.Length == 0)
1751 if (arg == "default"){
1753 ref_name = file_name;
1754 Location.Push (ref_name);
1756 } else if (arg == "hidden"){
1758 // We ignore #line hidden
1766 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
1767 ref_line = System.Int32.Parse (arg.Substring (0, pos));
1770 char [] quotes = { '\"' };
1772 string name = arg.Substring (pos). Trim (quotes);
1773 ref_name = Location.LookupFile (name);
1774 file_name.HasLineDirective = true;
1775 ref_name.HasLineDirective = true;
1776 Location.Push (ref_name);
1778 ref_line = System.Int32.Parse (arg);
1788 // Handles #define and #undef
1790 void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
1792 if (arg.Length == 0 || arg == "true" || arg == "false"){
1793 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
1797 if (arg.IndexOfAny (simple_whitespaces) != -1){
1798 Error_EndLineExpected ();
1802 if (!is_identifier_start_character (arg [0]))
1803 Report.Error (1001, Location, "Identifier expected: " + arg);
1805 foreach (char c in arg.Substring (1)){
1806 if (!is_identifier_part_character (c)){
1807 Report.Error (1001, Location, "Identifier expected: " + arg);
1812 if (!caller_is_taking)
1816 if (defines == null)
1817 defines = new Hashtable ();
1820 if (defines == null)
1822 if (defines.Contains (arg))
1823 defines.Remove (arg);
1828 /// Handles #pragma directive
1830 void PreProcessPragma (string arg)
1832 const string warning = "warning";
1833 const string w_disable = "warning disable";
1834 const string w_restore = "warning restore";
1836 if (arg == w_disable) {
1837 Report.RegisterWarningRegion (Location).WarningDisable (line);
1841 if (arg == w_restore) {
1842 Report.RegisterWarningRegion (Location).WarningEnable (line);
1846 if (arg.StartsWith (w_disable)) {
1847 int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
1848 foreach (int code in codes) {
1850 Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
1855 if (arg.StartsWith (w_restore)) {
1856 int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
1857 Hashtable w_table = Report.warning_ignore_table;
1858 foreach (int code in codes) {
1859 if (w_table != null && w_table.Contains (code))
1860 Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
1861 Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
1866 if (arg.StartsWith (warning)) {
1867 Report.Warning (1634, 1, Location, "Expected disable or restore");
1871 Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
1874 int[] ParseNumbers (string text)
1876 string[] string_array = text.Split (',');
1877 int[] values = new int [string_array.Length];
1879 foreach (string string_code in string_array) {
1881 values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
1883 catch (FormatException) {
1884 Report.Warning (1692, 1, Location, "Invalid number");
1890 bool eval_val (string s)
1897 if (defines == null)
1899 if (defines.Contains (s))
1905 bool pp_primary (ref string s)
1914 s = s.Substring (1);
1915 bool val = pp_expr (ref s, false);
1916 if (s.Length > 0 && s [0] == ')'){
1917 s = s.Substring (1);
1920 Error_InvalidDirective ();
1924 if (is_identifier_start_character (c)){
1930 if (is_identifier_part_character (c)){
1934 bool v = eval_val (s.Substring (0, j));
1935 s = s.Substring (j);
1938 bool vv = eval_val (s);
1943 Error_InvalidDirective ();
1947 bool pp_unary (ref string s)
1954 if (len > 1 && s [1] == '='){
1955 Error_InvalidDirective ();
1958 s = s.Substring (1);
1959 return ! pp_primary (ref s);
1961 return pp_primary (ref s);
1963 Error_InvalidDirective ();
1968 bool pp_eq (ref string s)
1970 bool va = pp_unary (ref s);
1976 if (len > 2 && s [1] == '='){
1977 s = s.Substring (2);
1978 return va == pp_unary (ref s);
1980 Error_InvalidDirective ();
1983 } else if (s [0] == '!' && len > 1 && s [1] == '='){
1984 s = s.Substring (2);
1986 return va != pp_unary (ref s);
1995 bool pp_and (ref string s)
1997 bool va = pp_eq (ref s);
2003 if (len > 2 && s [1] == '&'){
2004 s = s.Substring (2);
2005 return (va & pp_and (ref s));
2007 Error_InvalidDirective ();
2016 // Evaluates an expression for `#if' or `#elif'
2018 bool pp_expr (ref string s, bool isTerm)
2020 bool va = pp_and (ref s);
2027 if (len > 2 && s [1] == '|'){
2028 s = s.Substring (2);
2029 return va | pp_expr (ref s, isTerm);
2031 Error_InvalidDirective ();
2036 Error_EndLineExpected ();
2044 bool eval (string s)
2046 bool v = pp_expr (ref s, true);
2055 void Error_NumericConstantTooLong ()
2057 Report.Error (1021, Location, "Numeric constant too long");
2060 void Error_InvalidDirective ()
2062 Report.Error (1517, Location, "Invalid preprocessor directive");
2065 void Error_UnexpectedDirective (string extra)
2069 "Unexpected processor directive (" + extra + ")");
2072 void Error_TokensSeen ()
2074 Report.Error (1032, Location,
2075 "Cannot define or undefine preprocessor symbols after first token in file");
2078 void Eror_WrongPreprocessorLocation ()
2080 Report.Error (1040, Location,
2081 "Preprocessor directives must appear as the first non-whitespace character on a line");
2084 void Error_EndLineExpected ()
2086 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
2090 // if true, then the code continues processing the code
2091 // if false, the code stays in a loop until another directive is
2093 // When caller_is_taking is false we ignore all directives except the ones
2094 // which can help us to identify where the #if block ends
2095 bool handle_preprocessing_directive (bool caller_is_taking)
2098 bool region_directive = false;
2100 get_cmd_arg (out cmd, out arg);
2102 // Eat any trailing whitespaces and single-line comments
2103 if (arg.IndexOf ("//") != -1)
2104 arg = arg.Substring (0, arg.IndexOf ("//"));
2105 arg = arg.Trim (simple_whitespaces);
2108 // The first group of pre-processing instructions is always processed
2112 region_directive = true;
2117 if (ifstack == null || ifstack.Count == 0){
2118 Error_UnexpectedDirective ("no #region for this #endregion");
2121 int pop = (int) ifstack.Pop ();
2123 if ((pop & REGION) == 0)
2124 Report.Error (1027, Location, "Expected `#endif' directive");
2126 return caller_is_taking;
2129 if (ifstack == null)
2130 ifstack = new Stack (2);
2132 int flags = region_directive ? REGION : 0;
2133 if (ifstack.Count == 0){
2134 flags |= PARENT_TAKING;
2136 int state = (int) ifstack.Peek ();
2137 if ((state & TAKING) != 0) {
2138 flags |= PARENT_TAKING;
2142 if (caller_is_taking && eval (arg)) {
2143 ifstack.Push (flags | TAKING);
2146 ifstack.Push (flags);
2150 if (ifstack == null || ifstack.Count == 0){
2151 Error_UnexpectedDirective ("no #if for this #endif");
2154 pop = (int) ifstack.Pop ();
2156 if ((pop & REGION) != 0)
2157 Report.Error (1038, Location, "#endregion directive expected");
2159 if (arg.Length != 0) {
2160 Error_EndLineExpected ();
2163 if (ifstack.Count == 0)
2166 int state = (int) ifstack.Peek ();
2167 return (state & TAKING) != 0;
2171 if (ifstack == null || ifstack.Count == 0){
2172 Error_UnexpectedDirective ("no #if for this #elif");
2175 int state = (int) ifstack.Pop ();
2177 if ((state & REGION) != 0) {
2178 Report.Error (1038, Location, "#endregion directive expected");
2182 if ((state & ELSE_SEEN) != 0){
2183 Error_UnexpectedDirective ("#elif not valid after #else");
2187 if ((state & TAKING) != 0) {
2192 if (eval (arg) && ((state & PARENT_TAKING) != 0)){
2193 ifstack.Push (state | TAKING);
2197 ifstack.Push (state);
2202 if (ifstack == null || ifstack.Count == 0){
2203 Error_UnexpectedDirective ("no #if for this #else");
2206 int state = (int) ifstack.Peek ();
2208 if ((state & REGION) != 0) {
2209 Report.Error (1038, Location, "#endregion directive expected");
2213 if ((state & ELSE_SEEN) != 0){
2214 Error_UnexpectedDirective ("#else within #else");
2220 if (arg.Length != 0) {
2221 Error_EndLineExpected ();
2226 if ((state & PARENT_TAKING) != 0) {
2227 ret = (state & TAKING) == 0;
2235 ifstack.Push (state | ELSE_SEEN);
2240 if (any_token_seen){
2241 Error_TokensSeen ();
2242 return caller_is_taking;
2244 PreProcessDefinition (true, arg, caller_is_taking);
2245 return caller_is_taking;
2248 if (any_token_seen){
2249 Error_TokensSeen ();
2250 return caller_is_taking;
2252 PreProcessDefinition (false, arg, caller_is_taking);
2253 return caller_is_taking;
2257 // These are only processed if we are in a `taking' block
2259 if (!caller_is_taking)
2264 Report.Error (1029, Location, "#error: '" + arg + "'");
2268 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
2272 if (RootContext.Version == LanguageVersion.ISO_1) {
2273 Report.FeatureIsNotISO1 (Location, "#pragma");
2277 PreProcessPragma (arg);
2281 if (!PreProcessLine (arg))
2284 "The line number specified for #line directive is missing or invalid");
2285 return caller_is_taking;
2288 Report.Error (1024, Location, "Wrong preprocessor directive");
2293 private int consume_string (bool quoted)
2296 string_builder.Length = 0;
2298 while ((c = getChar ()) != -1){
2300 if (quoted && peekChar () == '"'){
2301 string_builder.Append ((char) c);
2305 val = string_builder.ToString ();
2306 return Token.LITERAL_STRING;
2312 Report.Error (1010, Location, "Newline in constant");
2320 string_builder.Append ((char) c);
2323 Report.Error (1039, Location, "Unterminated string literal");
2327 private int consume_identifier (int s)
2329 int res = consume_identifier (s, false);
2331 if (doc_state == XmlCommentState.Allowed)
2332 doc_state = XmlCommentState.NotAllowed;
2335 case Token.NAMESPACE:
2336 check_incorrect_doc_comment ();
2340 if (res == Token.PARTIAL) {
2341 // Save current position and parse next token.
2344 int next_token = token ();
2345 bool ok = (next_token == Token.CLASS) ||
2346 (next_token == Token.STRUCT) ||
2347 (next_token == Token.INTERFACE);
2354 if (next_token < Token.LAST_KEYWORD)
2355 Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
2357 val = new LocatedToken (Location, "partial");
2358 return Token.IDENTIFIER;
2364 private int consume_identifier (int s, bool quoted)
2369 id_builder [0] = (char) s;
2371 current_location = new Location (ref_line, Col);
2373 while ((c = getChar ()) != -1) {
2375 if (is_identifier_part_character ((char) c)){
2376 if (pos == max_id_size){
2377 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
2381 id_builder [pos++] = (char) c;
2382 // putback_char = -1;
2383 } else if (c == '\\') {
2387 // putback_char = c;
2394 // Optimization: avoids doing the keyword lookup
2395 // on uppercase letters and _
2397 if (!quoted && (s >= 'a' || s == '_')){
2398 int keyword = GetKeyword (id_builder, pos);
2399 if (keyword != -1) {
2406 // Keep identifiers in an array of hashtables to avoid needless
2410 if (identifiers [pos] != null) {
2411 val = identifiers [pos][id_builder];
2413 val = new LocatedToken (Location, (string) val);
2415 escapedIdentifiers.Add (val);
2416 return Token.IDENTIFIER;
2420 identifiers [pos] = new CharArrayHashtable (pos);
2422 val = new String (id_builder, 0, pos);
2423 if (RootContext.Version == LanguageVersion.ISO_1) {
2424 for (int i = 1; i < id_builder.Length; i += 3) {
2425 if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
2426 Report.Error (1638, Location,
2427 "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
2433 char [] chars = new char [pos];
2434 Array.Copy (id_builder, chars, pos);
2436 identifiers [pos] [chars] = val;
2438 val = new LocatedToken (Location, (string) val);
2440 escapedIdentifiers.Add (val);
2441 return Token.IDENTIFIER;
2444 public int xtoken ()
2447 bool doread = false;
2450 // Whether we have seen comments on the current line
2451 bool comments_seen = false;
2453 for (;(c = getChar ()) != -1;) {
2455 col = ((col + 8) / 8) * 8;
2459 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0 || c == 0)
2463 if (peekChar () == '\n')
2466 any_token_seen |= tokens_seen;
2467 tokens_seen = false;
2468 comments_seen = false;
2472 // Handle double-slash comments.
2474 int d = peekChar ();
2478 if (RootContext.Documentation != null && peekChar () == '/') {
2480 // Don't allow ////.
2481 if ((d = peekChar ()) != '/') {
2482 update_comment_location ();
2483 if (doc_state == XmlCommentState.Allowed)
2484 handle_one_line_xml_comment ();
2485 else if (doc_state == XmlCommentState.NotAllowed)
2486 warn_incorrect_doc_comment ();
2489 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
2492 any_token_seen |= tokens_seen;
2493 tokens_seen = false;
2494 comments_seen = false;
2496 } else if (d == '*'){
2498 bool docAppend = false;
2499 if (RootContext.Documentation != null && peekChar () == '*') {
2501 update_comment_location ();
2502 // But when it is /**/, just do nothing.
2503 if (peekChar () == '/') {
2507 if (doc_state == XmlCommentState.Allowed)
2509 else if (doc_state == XmlCommentState.NotAllowed)
2510 warn_incorrect_doc_comment ();
2513 int current_comment_start = 0;
2515 current_comment_start = xml_comment_buffer.Length;
2516 xml_comment_buffer.Append (Environment.NewLine);
2519 Location start_location = Location;
2521 while ((d = getChar ()) != -1){
2522 if (d == '*' && peekChar () == '/'){
2524 comments_seen = true;
2528 xml_comment_buffer.Append ((char) d);
2531 any_token_seen |= tokens_seen;
2532 tokens_seen = false;
2534 // Reset 'comments_seen' just to be consistent.
2535 // It doesn't matter either way, here.
2537 comments_seen = false;
2541 Report.Error (1035, start_location, "End-of-file found, '*/' expected");
2544 update_formatted_doc_comment (current_comment_start);
2547 goto is_punct_label;
2551 if (c == '\\' || is_identifier_start_character ((char)c)){
2553 return consume_identifier (c);
2557 current_location = new Location (ref_line, Col);
2558 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
2568 any_token_seen |= tokens_seen;
2569 tokens_seen = false;
2570 comments_seen = false;
2574 if (c >= '0' && c <= '9'){
2576 return is_number (c);
2581 int peek = peekChar ();
2582 if (peek >= '0' && peek <= '9')
2583 return is_number (c);
2588 if (tokens_seen || comments_seen) {
2589 Eror_WrongPreprocessorLocation ();
2593 if (handle_preprocessing_directive (true))
2596 bool directive_expected = false;
2597 while ((c = getChar ()) != -1) {
2599 directive_expected = true;
2600 } else if (!directive_expected) {
2601 // TODO: Implement comment support for disabled code and uncomment this code
2603 // Eror_WrongPreprocessorLocation ();
2604 // return Token.ERROR;
2609 if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
2613 if (handle_preprocessing_directive (false))
2616 directive_expected = false;
2620 tokens_seen = false;
2628 return consume_string (false);
2634 error_details = "Empty character literal";
2635 Report.Error (1011, Location, error_details);
2638 if (c == '\r' || c == '\n') {
2639 Report.Error (1010, Location, "Newline in constant");
2645 val = new System.Char ();
2650 error_details = "Too many characters in character literal";
2651 Report.Error (1012, Location, error_details);
2653 // Try to recover, read until newline or next "'"
2654 while ((c = getChar ()) != -1){
2663 return Token.LITERAL_CHARACTER;
2670 return consume_string (true);
2671 } else if (is_identifier_start_character ((char) c)){
2672 return consume_identifier (c, true);
2674 Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
2678 error_details = ((char)c).ToString ();
2687 // Handles one line xml comment
2689 private void handle_one_line_xml_comment ()
2692 while ((c = peekChar ()) == ' ')
2693 getChar (); // skip heading whitespaces.
2694 while ((c = peekChar ()) != -1 && c != '\n' && c != '\r') {
2695 xml_comment_buffer.Append ((char) getChar ());
2697 if (c == '\r' || c == '\n')
2698 xml_comment_buffer.Append (Environment.NewLine);
2702 // Remove heading "*" in Javadoc-like xml documentation.
2704 private void update_formatted_doc_comment (int current_comment_start)
2706 int length = xml_comment_buffer.Length - current_comment_start;
2707 string [] lines = xml_comment_buffer.ToString (
2708 current_comment_start,
2709 length).Replace ("\r", "").Split ('\n');
2711 // The first line starts with /**, thus it is not target
2712 // for the format check.
2713 for (int i = 1; i < lines.Length; i++) {
2714 string s = lines [i];
2715 int idx = s.IndexOf ('*');
2718 if (i < lines.Length - 1)
2722 head = s.Substring (0, idx);
2723 foreach (char c in head)
2726 lines [i] = s.Substring (idx + 1);
2728 xml_comment_buffer.Remove (current_comment_start, length);
2729 xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
2733 // Updates current comment location.
2735 private void update_comment_location ()
2737 if (current_comment_location.IsNull) {
2738 // "-2" is for heading "//" or "/*"
2739 current_comment_location =
2740 new Location (ref_line, col - 2);
2745 // Checks if there was incorrect doc comments and raise
2748 public void check_incorrect_doc_comment ()
2750 if (xml_comment_buffer.Length > 0)
2751 warn_incorrect_doc_comment ();
2755 // Raises a warning when tokenizer found incorrect doccomment
2758 private void warn_incorrect_doc_comment ()
2760 if (doc_state != XmlCommentState.Error) {
2761 doc_state = XmlCommentState.Error;
2762 // in csc, it is 'XML comment is not placed on
2763 // a valid language element'. But that does not
2765 Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
2770 // Consumes the saved xml comment lines (if any)
2771 // as for current target member or type.
2773 public string consume_doc_comment ()
2775 if (xml_comment_buffer.Length > 0) {
2776 string ret = xml_comment_buffer.ToString ();
2777 reset_doc_comment ();
2783 void reset_doc_comment ()
2785 xml_comment_buffer.Length = 0;
2786 current_comment_location = Location.Null;
2789 public void cleanup ()
2791 if (ifstack != null && ifstack.Count >= 1) {
2792 current_location = new Location (ref_line, Col);
2793 int state = (int) ifstack.Pop ();
2794 if ((state & REGION) != 0)
2795 Report.Error (1038, Location, "#endregion directive expected");
2797 Report.Error (1027, Location, "Expected `#endif' directive");
2803 // Indicates whether it accepts XML documentation or not.
2805 public enum XmlCommentState {
2806 // comment is allowed in this state.
2808 // comment is not allowed in this state.
2810 // once comments appeared when it is NotAllowed, then the
2811 // state is changed to it, until the state is changed to