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 ();
51 // XML documentation buffer. The save point is used to divide
52 // comments on types and comments on members.
54 StringBuilder xml_comment_buffer
;
57 // See comment on XmlCommentState enumeration.
59 XmlCommentState xmlDocState
= XmlCommentState
.Allowed
;
62 // Whether tokens have been seen on this line
64 bool tokens_seen
= false;
67 // Whether a token has been seen on the file
68 // This is needed because `define' is not allowed to be used
69 // after a token has been seen.
71 bool any_token_seen
= false;
73 static Hashtable tokenValues
;
74 static readonly char[] simple_whitespaces
= new char[] { ' ', '\t' }
;
76 private static Hashtable TokenValueName
79 if (tokenValues
== null)
80 tokenValues
= GetTokenValueNameHash ();
86 private static Hashtable
GetTokenValueNameHash ()
88 Type t
= typeof (Token
);
89 FieldInfo
[] fields
= t
.GetFields ();
90 Hashtable hash
= new Hashtable ();
91 foreach (FieldInfo field
in fields
) {
92 if (field
.IsLiteral
&& field
.IsStatic
&& field
.FieldType
== typeof (int))
93 hash
.Add (field
.GetValue (null), field
.Name
);
99 // Returns a verbose representation of the current location
101 public string location
{
105 if (current_token
== Token
.ERROR
)
106 det
= "detail: " + error_details
;
110 // return "Line: "+line+" Col: "+col + "\n" +
111 // "VirtLine: "+ref_line +
112 // " Token: "+current_token + " " + det;
113 string current_token_name
= TokenValueName
[current_token
] as string;
114 if (current_token_name
== null)
115 current_token_name
= current_token
.ToString ();
117 return String
.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name
.Name
,
125 public bool PropertyParsing
{
127 return handle_get_set
;
131 handle_get_set
= value;
135 public bool AssemblyTargetParsing
{
137 return handle_assembly
;
141 handle_assembly
= value;
145 public bool EventParsing
{
147 return handle_remove_add
;
151 handle_remove_add
= value;
155 public bool ConstraintsParsing
{
157 return handle_constraints
;
161 handle_constraints
= value;
165 public static bool LinqEnabled
{
171 public bool TypeOfParsing
{
173 return handle_typeof
;
177 handle_typeof
= value;
181 public bool QueryParsing
{
183 query_parsing
= value;
187 public XmlCommentState doc_state
{
188 get { return xmlDocState; }
190 if (value == XmlCommentState
.Allowed
) {
191 check_incorrect_doc_comment ();
192 reset_doc_comment ();
198 public bool IsEscapedIdentifier (Location loc
)
200 foreach (LocatedToken lt
in escapedIdentifiers
)
201 if (lt
.Location
.Equals (loc
))
209 static CharArrayHashtable
[] keywords
;
210 static Hashtable keywordStrings
;
211 static NumberStyles styles
;
212 static NumberFormatInfo csharp_format_info
;
215 // Values for the associated token returned
225 const int TAKING
= 1;
226 const int ELSE_SEEN
= 4;
227 const int PARENT_TAKING
= 8;
228 const int REGION
= 16;
231 // pre-processor if stack state:
235 static System
.Text
.StringBuilder string_builder
;
237 const int max_id_size
= 512;
238 static char [] id_builder
= new char [max_id_size
];
240 static CharArrayHashtable
[] identifiers
= new CharArrayHashtable
[max_id_size
+ 1];
242 const int max_number_size
= 512;
243 static char [] number_builder
= new char [max_number_size
];
244 static int number_pos
;
247 // Details about the error encoutered by the tokenizer
249 string error_details
;
251 public string error
{
253 return error_details
;
270 // This is used when the tokenizer needs to save
271 // the current position as it needs to do some parsing
272 // on its own to deamiguate a token in behalf of the
275 Stack position_stack
= new Stack (2);
280 public int putback_char
;
281 public int previous_col
;
282 public Stack ifstack
;
283 public int parsing_generic_less_than
;
284 public int current_token
;
286 public Position (Tokenizer t
)
288 position
= t
.reader
.Position
;
289 ref_line
= t
.ref_line
;
291 putback_char
= t
.putback_char
;
292 previous_col
= t
.previous_col
;
293 if (t
.ifstack
!= null && t
.ifstack
.Count
!= 0)
294 ifstack
= (Stack
)t
.ifstack
.Clone ();
295 parsing_generic_less_than
= t
.parsing_generic_less_than
;
296 current_token
= t
.current_token
;
300 public void PushPosition ()
302 position_stack
.Push (new Position (this));
305 public void PopPosition ()
307 Position p
= (Position
) position_stack
.Pop ();
309 reader
.Position
= p
.position
;
310 ref_line
= p
.ref_line
;
312 putback_char
= p
.putback_char
;
313 previous_col
= p
.previous_col
;
315 parsing_generic_less_than
= p
.parsing_generic_less_than
;
316 current_token
= p
.current_token
;
319 // Do not reset the position, ignore it.
320 public void DiscardPosition ()
322 position_stack
.Pop ();
325 static void AddKeyword (string kw
, int token
)
327 keywordStrings
.Add (kw
, kw
);
328 if (keywords
[kw
.Length
] == null) {
329 keywords
[kw
.Length
] = new CharArrayHashtable (kw
.Length
);
331 keywords
[kw
.Length
] [kw
.ToCharArray ()] = token
;
334 static void InitTokens ()
336 keywordStrings
= new Hashtable ();
337 keywords
= new CharArrayHashtable
[64];
339 AddKeyword ("__arglist", Token
.ARGLIST
);
340 AddKeyword ("abstract", Token
.ABSTRACT
);
341 AddKeyword ("as", Token
.AS
);
342 AddKeyword ("add", Token
.ADD
);
343 AddKeyword ("assembly", Token
.ASSEMBLY
);
344 AddKeyword ("base", Token
.BASE
);
345 AddKeyword ("bool", Token
.BOOL
);
346 AddKeyword ("break", Token
.BREAK
);
347 AddKeyword ("byte", Token
.BYTE
);
348 AddKeyword ("case", Token
.CASE
);
349 AddKeyword ("catch", Token
.CATCH
);
350 AddKeyword ("char", Token
.CHAR
);
351 AddKeyword ("checked", Token
.CHECKED
);
352 AddKeyword ("class", Token
.CLASS
);
353 AddKeyword ("const", Token
.CONST
);
354 AddKeyword ("continue", Token
.CONTINUE
);
355 AddKeyword ("decimal", Token
.DECIMAL
);
356 AddKeyword ("default", Token
.DEFAULT
);
357 AddKeyword ("delegate", Token
.DELEGATE
);
358 AddKeyword ("do", Token
.DO
);
359 AddKeyword ("double", Token
.DOUBLE
);
360 AddKeyword ("else", Token
.ELSE
);
361 AddKeyword ("enum", Token
.ENUM
);
362 AddKeyword ("event", Token
.EVENT
);
363 AddKeyword ("explicit", Token
.EXPLICIT
);
364 AddKeyword ("extern", Token
.EXTERN
);
365 AddKeyword ("false", Token
.FALSE
);
366 AddKeyword ("finally", Token
.FINALLY
);
367 AddKeyword ("fixed", Token
.FIXED
);
368 AddKeyword ("float", Token
.FLOAT
);
369 AddKeyword ("for", Token
.FOR
);
370 AddKeyword ("foreach", Token
.FOREACH
);
371 AddKeyword ("goto", Token
.GOTO
);
372 AddKeyword ("get", Token
.GET
);
373 AddKeyword ("if", Token
.IF
);
374 AddKeyword ("implicit", Token
.IMPLICIT
);
375 AddKeyword ("in", Token
.IN
);
376 AddKeyword ("int", Token
.INT
);
377 AddKeyword ("interface", Token
.INTERFACE
);
378 AddKeyword ("internal", Token
.INTERNAL
);
379 AddKeyword ("is", Token
.IS
);
380 AddKeyword ("lock", Token
.LOCK
);
381 AddKeyword ("long", Token
.LONG
);
382 AddKeyword ("namespace", Token
.NAMESPACE
);
383 AddKeyword ("new", Token
.NEW
);
384 AddKeyword ("null", Token
.NULL
);
385 AddKeyword ("object", Token
.OBJECT
);
386 AddKeyword ("operator", Token
.OPERATOR
);
387 AddKeyword ("out", Token
.OUT
);
388 AddKeyword ("override", Token
.OVERRIDE
);
389 AddKeyword ("params", Token
.PARAMS
);
390 AddKeyword ("private", Token
.PRIVATE
);
391 AddKeyword ("protected", Token
.PROTECTED
);
392 AddKeyword ("public", Token
.PUBLIC
);
393 AddKeyword ("readonly", Token
.READONLY
);
394 AddKeyword ("ref", Token
.REF
);
395 AddKeyword ("remove", Token
.REMOVE
);
396 AddKeyword ("return", Token
.RETURN
);
397 AddKeyword ("sbyte", Token
.SBYTE
);
398 AddKeyword ("sealed", Token
.SEALED
);
399 AddKeyword ("set", Token
.SET
);
400 AddKeyword ("short", Token
.SHORT
);
401 AddKeyword ("sizeof", Token
.SIZEOF
);
402 AddKeyword ("stackalloc", Token
.STACKALLOC
);
403 AddKeyword ("static", Token
.STATIC
);
404 AddKeyword ("string", Token
.STRING
);
405 AddKeyword ("struct", Token
.STRUCT
);
406 AddKeyword ("switch", Token
.SWITCH
);
407 AddKeyword ("this", Token
.THIS
);
408 AddKeyword ("throw", Token
.THROW
);
409 AddKeyword ("true", Token
.TRUE
);
410 AddKeyword ("try", Token
.TRY
);
411 AddKeyword ("typeof", Token
.TYPEOF
);
412 AddKeyword ("uint", Token
.UINT
);
413 AddKeyword ("ulong", Token
.ULONG
);
414 AddKeyword ("unchecked", Token
.UNCHECKED
);
415 AddKeyword ("unsafe", Token
.UNSAFE
);
416 AddKeyword ("ushort", Token
.USHORT
);
417 AddKeyword ("using", Token
.USING
);
418 AddKeyword ("virtual", Token
.VIRTUAL
);
419 AddKeyword ("void", Token
.VOID
);
420 AddKeyword ("volatile", Token
.VOLATILE
);
421 AddKeyword ("while", Token
.WHILE
);
422 AddKeyword ("partial", Token
.PARTIAL
);
424 AddKeyword ("where", Token
.WHERE
);
427 AddKeyword ("from", Token
.FROM
);
428 AddKeyword ("join", Token
.JOIN
);
429 AddKeyword ("on", Token
.ON
);
430 AddKeyword ("equals", Token
.EQUALS
);
431 AddKeyword ("select", Token
.SELECT
);
432 AddKeyword ("group", Token
.GROUP
);
433 AddKeyword ("by", Token
.BY
);
434 AddKeyword ("let", Token
.LET
);
435 AddKeyword ("orderby", Token
.ORDERBY
);
436 AddKeyword ("ascending", Token
.ASCENDING
);
437 AddKeyword ("descending", Token
.DESCENDING
);
438 AddKeyword ("into", Token
.INTO
);
451 public static void Reset ()
454 csharp_format_info
= NumberFormatInfo
.InvariantInfo
;
455 styles
= NumberStyles
.Float
;
457 string_builder
= new System
.Text
.StringBuilder ();
460 int GetKeyword (char[] id
, int id_len
)
463 * Keywords are stored in an array of hashtables grouped by their
467 if ((id_len
>= keywords
.Length
) || (keywords
[id_len
] == null))
469 object o
= keywords
[id_len
] [id
];
476 if (handle_get_set
== false && (res
== Token
.GET
|| res
== Token
.SET
))
478 if (handle_remove_add
== false && (res
== Token
.REMOVE
|| res
== Token
.ADD
))
480 if (handle_assembly
== false && res
== Token
.ASSEMBLY
)
484 if (res
== Token
.FROM
&&
485 (current_token
== Token
.ASSIGN
|| current_token
== Token
.OPEN_BRACKET
||
486 current_token
== Token
.RETURN
|| current_token
== Token
.IN
)) {
487 query_parsing
= true;
491 if (!query_parsing
&& res
> Token
.QUERY_FIRST_TOKEN
&& res
< Token
.QUERY_LAST_TOKEN
)
497 if (!handle_constraints
&& res
== Token
.WHERE
)
504 public Location Location
{
505 get { return current_location; }
508 void define (string def
)
510 if (!RootContext
.AllDefines
.Contains (def
)){
511 RootContext
.AllDefines
[def
] = true;
513 if (defines
.Contains (def
))
515 defines
[def
] = true;
518 public Tokenizer (SeekableStreamReader input
, SourceFile file
, ArrayList defs
)
520 this.ref_name
= file
;
521 this.file_name
= file
;
527 defines
= new Hashtable ();
528 foreach (string def
in defs
)
532 xml_comment_buffer
= new StringBuilder ();
535 // FIXME: This could be `Location.Push' but we have to
536 // find out why the MS compiler allows this
538 Mono
.CSharp
.Location
.Push (file
);
541 static bool is_identifier_start_character (char c
)
543 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') || c
== '_' || Char
.IsLetter (c
);
546 static bool is_identifier_part_character (char c
)
548 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') || c
== '_' || (c
>= '0' && c
<= '9') ||
549 Char
.IsLetter (c
) || Char
.GetUnicodeCategory (c
) == UnicodeCategory
.ConnectorPunctuation
;
552 public static bool IsKeyword (string s
)
554 return keywordStrings
[s
] != null;
557 public static bool IsValidIdentifier (string s
)
559 if (s
== null || s
.Length
== 0)
562 if (!is_identifier_start_character (s
[0]))
565 for (int i
= 1; i
< s
.Length
; i
++)
566 if (! is_identifier_part_character (s
[i
]))
572 bool parse_generic_dimension (out int dimension
)
577 int the_token
= token ();
578 if (the_token
== Token
.OP_GENERICS_GT
)
580 else if (the_token
== Token
.COMMA
) {
588 bool parse_less_than ()
591 int the_token
= token ();
592 if (the_token
== Token
.OPEN_BRACKET
) {
594 the_token
= token ();
595 } while (the_token
!= Token
.CLOSE_BRACKET
);
596 the_token
= token ();
599 case Token
.IDENTIFIER
:
622 the_token
= token ();
624 if (the_token
== Token
.OP_GENERICS_GT
)
626 else if (the_token
== Token
.COMMA
|| the_token
== Token
.DOT
|| the_token
== Token
.DOUBLE_COLON
)
628 else if (the_token
== Token
.INTERR
|| the_token
== Token
.STAR
)
630 else if (the_token
== Token
.OP_GENERICS_LT
) {
631 if (!parse_less_than ())
634 } else if (the_token
== Token
.OPEN_BRACKET
) {
636 the_token
= token ();
637 if (the_token
== Token
.CLOSE_BRACKET
)
639 else if (the_token
== Token
.COMMA
)
640 goto rank_specifiers
;
648 public void PutbackNullable ()
650 if (nullable_pos
< 0)
651 throw new Exception ();
655 reader
.Position
= nullable_pos
;
660 public void PutbackCloseParens ()
666 int nullable_pos
= -1;
668 public void CheckNullable (bool is_nullable
)
671 nullable_pos
= reader
.Position
;
677 public int peek_token ()
682 the_token
= token ();
688 bool parse_namespace_or_typename (int next
)
691 next
= peek_token ();
692 while (next
== Token
.IDENTIFIER
){
695 next
= peek_token ();
696 if (next
== Token
.DOT
|| next
== Token
.DOUBLE_COLON
){
698 next
= peek_token ();
701 if (next
== Token
.OP_GENERICS_LT
){
703 if (!parse_less_than ())
713 bool is_simple_type (int token
)
715 return (token
== Token
.BOOL
||
716 token
== Token
.DECIMAL
||
717 token
== Token
.SBYTE
||
718 token
== Token
.BYTE
||
719 token
== Token
.SHORT
||
720 token
== Token
.USHORT
||
721 token
== Token
.INT
||
722 token
== Token
.UINT
||
723 token
== Token
.LONG
||
724 token
== Token
.ULONG
||
725 token
== Token
.CHAR
||
726 token
== Token
.FLOAT
||
727 token
== Token
.DOUBLE
);
730 bool is_builtin_reference_type (int token
)
732 return (token
== Token
.OBJECT
|| token
== Token
.STRING
);
735 bool parse_opt_rank (int next
)
738 if (next
!= Token
.OPEN_BRACKET
)
744 if (next
== Token
.CLOSE_BRACKET
){
745 next
= peek_token ();
748 if (next
== Token
.COMMA
)
758 int next
= peek_token ();
760 if (is_simple_type (next
)){
762 next
= peek_token ();
763 if (next
== Token
.INTERR
)
765 return parse_opt_rank (peek_token ());
767 if (parse_namespace_or_typename (next
)){
768 next
= peek_token ();
769 if (next
== Token
.INTERR
)
771 return parse_opt_rank (peek_token ());
772 } else if (is_builtin_reference_type (next
)){
774 return parse_opt_rank (peek_token ());
781 // Invoked after '(' has been seen and tries to parse:
782 // type identifier [, type identifier]*
784 // if this is the case, instead of returning an
785 // OPEN_PARENS token we return a special token that
786 // triggers lambda parsing.
788 // This is needed because we can not introduce the
789 // explicitly_typed_lambda_parameter_list after a '(' in the
790 // grammar without introducing reduce/reduce conflicts.
792 // We need to parse a type and if it is followed by an
793 // identifier, we know it has to be parsed as a lambda
796 // the type expression can be prefixed with `ref' or `out'
798 public bool parse_lambda_parameters ()
801 int next
= peek_token ();
803 if (next
== Token
.REF
|| next
== Token
.OUT
)
807 next
= peek_token ();
808 if (next
== Token
.IDENTIFIER
){
810 next
= peek_token ();
811 if (next
== Token
.COMMA
){
815 if (next
== Token
.CLOSE_PARENS
)
823 int parsing_generic_less_than
= 0;
825 int is_punct (char c
, ref bool doread
)
835 return Token
.OPEN_BRACE
;
838 return Token
.CLOSE_BRACE
;
840 // To block doccomment inside attribute declaration.
841 if (doc_state
== XmlCommentState
.Allowed
)
842 doc_state
= XmlCommentState
.NotAllowed
;
843 return Token
.OPEN_BRACKET
;
845 return Token
.CLOSE_BRACKET
;
849 bool have_lambda_parameter
= parse_lambda_parameters ();
852 if (have_lambda_parameter
)
853 return Token
.OPEN_PARENS_LAMBDA
;
855 return Token
.OPEN_PARENS
;
857 return Token
.OPEN_PARENS
;
859 if (deambiguate_close_parens
== 0)
860 return Token
.CLOSE_PARENS
;
862 --deambiguate_close_parens
;
866 int new_token
= xtoken ();
870 if (new_token
== Token
.OPEN_PARENS
)
871 return Token
.CLOSE_PARENS_OPEN_PARENS
;
872 else if (new_token
== Token
.MINUS
)
873 return Token
.CLOSE_PARENS_MINUS
;
874 else if (IsCastToken (new_token
))
875 return Token
.CLOSE_PARENS_CAST
;
877 return Token
.CLOSE_PARENS_NO_CAST
;
884 return Token
.SEMICOLON
;
893 if (parsing_generic_less_than
++ > 0)
894 return Token
.OP_GENERICS_LT
;
899 if (parse_generic_dimension (out dimension
)) {
902 return Token
.GENERIC_DIMENSION
;
907 // Save current position and parse next token.
909 bool is_generic_lt
= parse_less_than ();
913 return Token
.OP_GENERICS_LT
;
915 parsing_generic_less_than
= 0;
924 return Token
.OP_SHIFT_LEFT_ASSIGN
;
926 return Token
.OP_SHIFT_LEFT
;
927 } else if (d
== '='){
932 } else if (c
== '>') {
933 if (parsing_generic_less_than
> 0) {
934 parsing_generic_less_than
--;
935 return Token
.OP_GENERICS_GT
;
945 return Token
.OP_SHIFT_RIGHT_ASSIGN
;
947 return Token
.OP_SHIFT_RIGHT
;
948 } else if (d
== '='){
963 t
= Token
.OP_ADD_ASSIGN
;
977 t
= Token
.OP_SUB_ASSIGN
;
1007 return Token
.ASSIGN
;
1013 return Token
.OP_AND
;
1014 } else if (d
== '='){
1016 return Token
.OP_AND_ASSIGN
;
1019 return Token
.BITWISE_AND
;
1026 } else if (d
== '='){
1028 return Token
.OP_OR_ASSIGN
;
1030 return Token
.BITWISE_OR
;
1036 return Token
.OP_MULT_ASSIGN
;
1045 return Token
.OP_DIV_ASSIGN
;
1053 return Token
.OP_MOD_ASSIGN
;
1055 return Token
.PERCENT
;
1061 return Token
.OP_XOR_ASSIGN
;
1063 return Token
.CARRET
;
1074 return Token
.OP_SHIFT_LEFT_ASSIGN
;
1076 return Token
.OP_SHIFT_LEFT
;
1077 } else if (d
== '='){
1091 return Token
.OP_SHIFT_RIGHT_ASSIGN
;
1093 return Token
.OP_SHIFT_RIGHT
;
1094 } else if (d
== '='){
1104 return Token
.DOUBLE_COLON
;
1113 int deambiguate_close_parens
= 0;
1115 public void Deambiguate_CloseParens (object expression
)
1119 // When any binary operation is used we are sure it is not a cast
1120 if (expression
is Binary
)
1123 deambiguate_close_parens
++;
1126 bool decimal_digits (int c
)
1129 bool seen_digits
= false;
1132 if (number_pos
== max_number_size
)
1133 Error_NumericConstantTooLong ();
1134 number_builder
[number_pos
++] = (char) c
;
1138 // We use peekChar2, because decimal_digits needs to do a
1139 // 2-character look-ahead (5.ToString for example).
1141 while ((d
= peekChar2 ()) != -1){
1142 if (d
>= '0' && d
<= '9'){
1143 if (number_pos
== max_number_size
)
1144 Error_NumericConstantTooLong ();
1145 number_builder
[number_pos
++] = (char) d
;
1155 static bool is_hex (int e
)
1157 return (e
>= '0' && e
<= '9') || (e
>= 'A' && e
<= 'F') || (e
>= 'a' && e
<= 'f');
1160 static int real_type_suffix (int c
)
1166 t
= Token
.LITERAL_FLOAT
;
1169 t
= Token
.LITERAL_DOUBLE
;
1172 t
= Token
.LITERAL_DECIMAL
;
1180 int integer_type_suffix (ulong ul
, int c
)
1182 bool is_unsigned
= false;
1183 bool is_long
= false;
1186 bool scanning
= true;
1197 if (!is_unsigned
&& (RootContext
.WarningLevel
>= 4)){
1199 // if we have not seen anything in between
1200 // report this error
1202 Report
.Warning (78, 4, Location
, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
1205 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
1206 // work around that.
1230 if (is_long
&& is_unsigned
){
1232 return Token
.LITERAL_INTEGER
;
1233 } else if (is_unsigned
){
1234 // uint if possible, or ulong else.
1236 if ((ul
& 0xffffffff00000000) == 0)
1240 } else if (is_long
){
1241 // long if possible, ulong otherwise
1242 if ((ul
& 0x8000000000000000) != 0)
1247 // int, uint, long or ulong in that order
1248 if ((ul
& 0xffffffff00000000) == 0){
1249 uint ui
= (uint) ul
;
1251 if ((ui
& 0x80000000) != 0)
1256 if ((ul
& 0x8000000000000000) != 0)
1262 return Token
.LITERAL_INTEGER
;
1266 // given `c' as the next char in the input decide whether
1267 // we need to convert to a special type, and then choose
1268 // the best representation for the integer
1270 int adjust_int (int c
)
1273 if (number_pos
> 9){
1274 ulong ul
= (uint) (number_builder
[0] - '0');
1276 for (int i
= 1; i
< number_pos
; i
++){
1277 ul
= checked ((ul
* 10) + ((uint)(number_builder
[i
] - '0')));
1279 return integer_type_suffix (ul
, c
);
1281 uint ui
= (uint) (number_builder
[0] - '0');
1283 for (int i
= 1; i
< number_pos
; i
++){
1284 ui
= checked ((ui
* 10) + ((uint)(number_builder
[i
] - '0')));
1286 return integer_type_suffix (ui
, c
);
1288 } catch (OverflowException
) {
1289 error_details
= "Integral constant is too large";
1290 Report
.Error (1021, Location
, error_details
);
1292 return Token
.LITERAL_INTEGER
;
1294 catch (FormatException
) {
1295 Report
.Error (1013, Location
, "Invalid number");
1297 return Token
.LITERAL_INTEGER
;
1301 int adjust_real (int t
)
1303 string s
= new String (number_builder
, 0, number_pos
);
1304 const string error_details
= "Floating-point constant is outside the range of type `{0}'";
1307 case Token
.LITERAL_DECIMAL
:
1309 val
= System
.Decimal
.Parse (s
, styles
, csharp_format_info
);
1310 } catch (OverflowException
) {
1312 Report
.Error (594, Location
, error_details
, "decimal");
1315 case Token
.LITERAL_FLOAT
:
1317 val
= float.Parse (s
, styles
, csharp_format_info
);
1318 } catch (OverflowException
) {
1320 Report
.Error (594, Location
, error_details
, "float");
1324 case Token
.LITERAL_DOUBLE
:
1326 t
= Token
.LITERAL_DOUBLE
;
1328 val
= System
.Double
.Parse (s
, styles
, csharp_format_info
);
1329 } catch (OverflowException
) {
1331 Report
.Error (594, Location
, error_details
, "double");
1344 while ((d
= peekChar ()) != -1){
1346 number_builder
[number_pos
++] = (char) d
;
1352 string s
= new String (number_builder
, 0, number_pos
);
1354 if (number_pos
<= 8)
1355 ul
= System
.UInt32
.Parse (s
, NumberStyles
.HexNumber
);
1357 ul
= System
.UInt64
.Parse (s
, NumberStyles
.HexNumber
);
1358 } catch (OverflowException
){
1359 error_details
= "Integral constant is too large";
1360 Report
.Error (1021, Location
, error_details
);
1362 return Token
.LITERAL_INTEGER
;
1364 catch (FormatException
) {
1365 Report
.Error (1013, Location
, "Invalid number");
1367 return Token
.LITERAL_INTEGER
;
1370 return integer_type_suffix (ul
, peekChar ());
1374 // Invoked if we know we have .digits or digits
1376 int is_number (int c
)
1378 bool is_real
= false;
1383 if (c
>= '0' && c
<= '9'){
1385 int peek
= peekChar ();
1387 if (peek
== 'x' || peek
== 'X')
1388 return handle_hex ();
1395 // We need to handle the case of
1396 // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1399 if (decimal_digits ('.')){
1405 return adjust_int (-1);
1409 if (c
== 'e' || c
== 'E'){
1411 if (number_pos
== max_number_size
)
1412 Error_NumericConstantTooLong ();
1413 number_builder
[number_pos
++] = 'e';
1417 if (number_pos
== max_number_size
)
1418 Error_NumericConstantTooLong ();
1419 number_builder
[number_pos
++] = '+';
1421 } else if (c
== '-') {
1422 if (number_pos
== max_number_size
)
1423 Error_NumericConstantTooLong ();
1424 number_builder
[number_pos
++] = '-';
1427 if (number_pos
== max_number_size
)
1428 Error_NumericConstantTooLong ();
1429 number_builder
[number_pos
++] = '+';
1436 type
= real_type_suffix (c
);
1437 if (type
== Token
.NONE
&& !is_real
){
1439 return adjust_int (c
);
1443 if (type
== Token
.NONE
){
1448 return adjust_real (type
);
1450 Console
.WriteLine ("This should not be reached");
1451 throw new Exception ("Is Number should never reach this point");
1455 // Accepts exactly count (4 or 8) hex, no more no less
1457 int getHex (int count
, out bool error
)
1462 int top
= count
!= -1 ? count
: 4;
1466 for (i
= 0; i
< top
; i
++){
1469 if (c
>= '0' && c
<= '9')
1470 c
= (int) c
- (int) '0';
1471 else if (c
>= 'A' && c
<= 'F')
1472 c
= (int) c
- (int) 'A' + 10;
1473 else if (c
>= 'a' && c
<= 'f')
1474 c
= (int) c
- (int) 'a' + 10;
1480 total
= (total
* 16) + c
;
1482 int p
= peekChar ();
1485 if (!is_hex ((char)p
))
1526 v
= getHex (-1, out error
);
1531 v
= getHex (4, out error
);
1536 v
= getHex (8, out error
);
1541 Report
.Error (1009, Location
, "Unrecognized escape sequence `\\{0}'", ((char)d
).ToString ());
1551 if (putback_char
!= -1) {
1569 if (putback_char
!= -1)
1570 return putback_char
;
1571 putback_char
= reader
.Read ();
1572 return putback_char
;
1577 if (putback_char
!= -1)
1578 return putback_char
;
1579 return reader
.Peek ();
1582 void putback (int c
)
1584 if (putback_char
!= -1){
1585 Console
.WriteLine ("Col: " + col
);
1586 Console
.WriteLine ("Row: " + line
);
1587 Console
.WriteLine ("Name: " + ref_name
.Name
);
1588 Console
.WriteLine ("Current [{0}] putting back [{1}] ", putback_char
, c
);
1589 throw new Exception ("This should not happen putback on putback");
1591 if (c
== '\n' || col
== 0) {
1592 // It won't happen though.
1602 public bool advance ()
1604 return peekChar () != -1;
1607 public Object Value
{
1613 public Object
value ()
1618 static bool IsCastToken (int token
)
1623 case Token
.IDENTIFIER
:
1624 case Token
.LITERAL_INTEGER
:
1625 case Token
.LITERAL_FLOAT
:
1626 case Token
.LITERAL_DOUBLE
:
1627 case Token
.LITERAL_DECIMAL
:
1628 case Token
.LITERAL_CHARACTER
:
1629 case Token
.LITERAL_STRING
:
1632 case Token
.DELEGATE
:
1642 case Token
.UNCHECKED
:
1649 // These can be part of a member access
1669 current_token
= xtoken ();
1672 if (current_token
!= Token
.DEFAULT
)
1673 return current_token
;
1678 current_token
= Token
.ERROR
;
1679 else if (c
== Token
.OPEN_PARENS
)
1680 current_token
= Token
.DEFAULT_OPEN_PARENS
;
1681 else if (c
== Token
.COLON
)
1682 current_token
= Token
.DEFAULT_COLON
;
1686 return current_token
;
1689 static StringBuilder static_cmd_arg
= new System
.Text
.StringBuilder ();
1691 void get_cmd_arg (out string cmd
, out string arg
)
1695 tokens_seen
= false;
1697 static_cmd_arg
.Length
= 0;
1699 // skip over white space
1700 while ((c
= getChar ()) != -1 && (c
!= '\n') && ((c
== '\r') || (c
== ' ') || (c
== '\t')))
1703 while ((c
!= -1) && (c
!= '\n') && (c
!= ' ') && (c
!= '\t') && (c
!= '\r')){
1704 if (is_identifier_part_character ((char) c
)){
1705 static_cmd_arg
.Append ((char) c
);
1713 cmd
= static_cmd_arg
.ToString ();
1715 if (c
== '\n' || c
== '\r'){
1719 // skip over white space
1720 while ((c
= getChar ()) != -1 && (c
!= '\n') && ((c
== '\r') || (c
== ' ') || (c
== '\t')))
1725 } else if (c
== '\r'){
1727 } else if (c
== -1){
1732 static_cmd_arg
.Length
= 0;
1733 static_cmd_arg
.Append ((char) c
);
1735 while ((c
= getChar ()) != -1 && (c
!= '\n') && (c
!= '\r')){
1736 static_cmd_arg
.Append ((char) c
);
1739 arg
= static_cmd_arg
.ToString ();
1743 // Handles the #line directive
1745 bool PreProcessLine (string arg
)
1747 if (arg
.Length
== 0)
1750 if (arg
== "default"){
1752 ref_name
= file_name
;
1753 Location
.Push (ref_name
);
1755 } else if (arg
== "hidden"){
1757 // We ignore #line hidden
1765 if ((pos
= arg
.IndexOf (' ')) != -1 && pos
!= 0){
1766 ref_line
= System
.Int32
.Parse (arg
.Substring (0, pos
));
1769 char [] quotes
= { '\"' }
;
1771 string name
= arg
.Substring (pos
). Trim (quotes
);
1772 ref_name
= Location
.LookupFile (name
);
1773 file_name
.HasLineDirective
= true;
1774 ref_name
.HasLineDirective
= true;
1775 Location
.Push (ref_name
);
1777 ref_line
= System
.Int32
.Parse (arg
);
1787 // Handles #define and #undef
1789 void PreProcessDefinition (bool is_define
, string arg
, bool caller_is_taking
)
1791 if (arg
.Length
== 0 || arg
== "true" || arg
== "false"){
1792 Report
.Error (1001, Location
, "Missing identifer to pre-processor directive");
1796 if (arg
.IndexOfAny (simple_whitespaces
) != -1){
1797 Error_EndLineExpected ();
1801 if (!is_identifier_start_character (arg
[0]))
1802 Report
.Error (1001, Location
, "Identifier expected: " + arg
);
1804 foreach (char c
in arg
.Substring (1)){
1805 if (!is_identifier_part_character (c
)){
1806 Report
.Error (1001, Location
, "Identifier expected: " + arg
);
1811 if (!caller_is_taking
)
1815 if (defines
== null)
1816 defines
= new Hashtable ();
1819 if (defines
== null)
1821 if (defines
.Contains (arg
))
1822 defines
.Remove (arg
);
1827 /// Handles #pragma directive
1829 void PreProcessPragma (string arg
)
1831 const string warning
= "warning";
1832 const string w_disable
= "warning disable";
1833 const string w_restore
= "warning restore";
1835 if (arg
== w_disable
) {
1836 Report
.RegisterWarningRegion (Location
).WarningDisable (line
);
1840 if (arg
== w_restore
) {
1841 Report
.RegisterWarningRegion (Location
).WarningEnable (line
);
1845 if (arg
.StartsWith (w_disable
)) {
1846 int[] codes
= ParseNumbers (arg
.Substring (w_disable
.Length
));
1847 foreach (int code
in codes
) {
1849 Report
.RegisterWarningRegion (Location
).WarningDisable (Location
, code
);
1854 if (arg
.StartsWith (w_restore
)) {
1855 int[] codes
= ParseNumbers (arg
.Substring (w_restore
.Length
));
1856 Hashtable w_table
= Report
.warning_ignore_table
;
1857 foreach (int code
in codes
) {
1858 if (w_table
!= null && w_table
.Contains (code
))
1859 Report
.Warning (1635, 1, Location
, String
.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code
));
1860 Report
.RegisterWarningRegion (Location
).WarningEnable (Location
, code
);
1865 if (arg
.StartsWith (warning
)) {
1866 Report
.Warning (1634, 1, Location
, "Expected disable or restore");
1870 Report
.Warning (1633, 1, Location
, "Unrecognized #pragma directive");
1873 int[] ParseNumbers (string text
)
1875 string[] string_array
= text
.Split (',');
1876 int[] values
= new int [string_array
.Length
];
1878 foreach (string string_code
in string_array
) {
1880 values
[index
++] = int.Parse (string_code
, System
.Globalization
.CultureInfo
.InvariantCulture
);
1882 catch (FormatException
) {
1883 Report
.Warning (1692, 1, Location
, "Invalid number");
1889 bool eval_val (string s
)
1896 if (defines
== null)
1898 if (defines
.Contains (s
))
1904 bool pp_primary (ref string s
)
1913 s
= s
.Substring (1);
1914 bool val
= pp_expr (ref s
, false);
1915 if (s
.Length
> 0 && s
[0] == ')'){
1916 s
= s
.Substring (1);
1919 Error_InvalidDirective ();
1923 if (is_identifier_start_character (c
)){
1929 if (is_identifier_part_character (c
)){
1933 bool v
= eval_val (s
.Substring (0, j
));
1934 s
= s
.Substring (j
);
1937 bool vv
= eval_val (s
);
1942 Error_InvalidDirective ();
1946 bool pp_unary (ref string s
)
1953 if (len
> 1 && s
[1] == '='){
1954 Error_InvalidDirective ();
1957 s
= s
.Substring (1);
1958 return ! pp_primary (ref s
);
1960 return pp_primary (ref s
);
1962 Error_InvalidDirective ();
1967 bool pp_eq (ref string s
)
1969 bool va
= pp_unary (ref s
);
1975 if (len
> 2 && s
[1] == '='){
1976 s
= s
.Substring (2);
1977 return va
== pp_unary (ref s
);
1979 Error_InvalidDirective ();
1982 } else if (s
[0] == '!' && len
> 1 && s
[1] == '='){
1983 s
= s
.Substring (2);
1985 return va
!= pp_unary (ref s
);
1994 bool pp_and (ref string s
)
1996 bool va
= pp_eq (ref s
);
2002 if (len
> 2 && s
[1] == '&'){
2003 s
= s
.Substring (2);
2004 return (va
& pp_and (ref s
));
2006 Error_InvalidDirective ();
2015 // Evaluates an expression for `#if' or `#elif'
2017 bool pp_expr (ref string s
, bool isTerm
)
2019 bool va
= pp_and (ref s
);
2026 if (len
> 2 && s
[1] == '|'){
2027 s
= s
.Substring (2);
2028 return va
| pp_expr (ref s
, isTerm
);
2030 Error_InvalidDirective ();
2035 Error_EndLineExpected ();
2043 bool eval (string s
)
2045 bool v
= pp_expr (ref s
, true);
2054 void Error_NumericConstantTooLong ()
2056 Report
.Error (1021, Location
, "Numeric constant too long");
2059 void Error_InvalidDirective ()
2061 Report
.Error (1517, Location
, "Invalid preprocessor directive");
2064 void Error_UnexpectedDirective (string extra
)
2068 "Unexpected processor directive (" + extra
+ ")");
2071 void Error_TokensSeen ()
2073 Report
.Error (1032, Location
,
2074 "Cannot define or undefine preprocessor symbols after first token in file");
2077 void Eror_WrongPreprocessorLocation ()
2079 Report
.Error (1040, Location
,
2080 "Preprocessor directives must appear as the first non-whitespace character on a line");
2083 void Error_EndLineExpected ()
2085 Report
.Error (1025, Location
, "Single-line comment or end-of-line expected");
2089 // if true, then the code continues processing the code
2090 // if false, the code stays in a loop until another directive is
2092 // When caller_is_taking is false we ignore all directives except the ones
2093 // which can help us to identify where the #if block ends
2094 bool handle_preprocessing_directive (bool caller_is_taking
)
2097 bool region_directive
= false;
2099 get_cmd_arg (out cmd
, out arg
);
2101 // Eat any trailing whitespaces and single-line comments
2102 if (arg
.IndexOf ("//") != -1)
2103 arg
= arg
.Substring (0, arg
.IndexOf ("//"));
2104 arg
= arg
.Trim (simple_whitespaces
);
2107 // The first group of pre-processing instructions is always processed
2111 region_directive
= true;
2116 if (ifstack
== null || ifstack
.Count
== 0){
2117 Error_UnexpectedDirective ("no #region for this #endregion");
2120 int pop
= (int) ifstack
.Pop ();
2122 if ((pop
& REGION
) == 0)
2123 Report
.Error (1027, Location
, "Expected `#endif' directive");
2125 return caller_is_taking
;
2128 if (ifstack
== null)
2129 ifstack
= new Stack (2);
2131 int flags
= region_directive
? REGION
: 0;
2132 if (ifstack
.Count
== 0){
2133 flags
|= PARENT_TAKING
;
2135 int state
= (int) ifstack
.Peek ();
2136 if ((state
& TAKING
) != 0) {
2137 flags
|= PARENT_TAKING
;
2141 if (caller_is_taking
&& eval (arg
)) {
2142 ifstack
.Push (flags
| TAKING
);
2145 ifstack
.Push (flags
);
2149 if (ifstack
== null || ifstack
.Count
== 0){
2150 Error_UnexpectedDirective ("no #if for this #endif");
2153 pop
= (int) ifstack
.Pop ();
2155 if ((pop
& REGION
) != 0)
2156 Report
.Error (1038, Location
, "#endregion directive expected");
2158 if (arg
.Length
!= 0) {
2159 Error_EndLineExpected ();
2162 if (ifstack
.Count
== 0)
2165 int state
= (int) ifstack
.Peek ();
2166 return (state
& TAKING
) != 0;
2170 if (ifstack
== null || ifstack
.Count
== 0){
2171 Error_UnexpectedDirective ("no #if for this #elif");
2174 int state
= (int) ifstack
.Pop ();
2176 if ((state
& REGION
) != 0) {
2177 Report
.Error (1038, Location
, "#endregion directive expected");
2181 if ((state
& ELSE_SEEN
) != 0){
2182 Error_UnexpectedDirective ("#elif not valid after #else");
2186 if ((state
& TAKING
) != 0) {
2191 if (eval (arg
) && ((state
& PARENT_TAKING
) != 0)){
2192 ifstack
.Push (state
| TAKING
);
2196 ifstack
.Push (state
);
2201 if (ifstack
== null || ifstack
.Count
== 0){
2202 Error_UnexpectedDirective ("no #if for this #else");
2205 int state
= (int) ifstack
.Peek ();
2207 if ((state
& REGION
) != 0) {
2208 Report
.Error (1038, Location
, "#endregion directive expected");
2212 if ((state
& ELSE_SEEN
) != 0){
2213 Error_UnexpectedDirective ("#else within #else");
2219 if (arg
.Length
!= 0) {
2220 Error_EndLineExpected ();
2225 if ((state
& PARENT_TAKING
) != 0) {
2226 ret
= (state
& TAKING
) == 0;
2234 ifstack
.Push (state
| ELSE_SEEN
);
2239 if (any_token_seen
){
2240 Error_TokensSeen ();
2241 return caller_is_taking
;
2243 PreProcessDefinition (true, arg
, caller_is_taking
);
2244 return caller_is_taking
;
2247 if (any_token_seen
){
2248 Error_TokensSeen ();
2249 return caller_is_taking
;
2251 PreProcessDefinition (false, arg
, caller_is_taking
);
2252 return caller_is_taking
;
2256 // These are only processed if we are in a `taking' block
2258 if (!caller_is_taking
)
2263 Report
.Error (1029, Location
, "#error: '" + arg
+ "'");
2267 Report
.Warning (1030, 1, Location
, "#warning: `{0}'", arg
);
2271 if (RootContext
.Version
== LanguageVersion
.ISO_1
) {
2272 Report
.FeatureIsNotISO1 (Location
, "#pragma");
2276 PreProcessPragma (arg
);
2280 if (!PreProcessLine (arg
))
2283 "The line number specified for #line directive is missing or invalid");
2284 return caller_is_taking
;
2287 Report
.Error (1024, Location
, "Wrong preprocessor directive");
2292 private int consume_string (bool quoted
)
2295 string_builder
.Length
= 0;
2297 while ((c
= getChar ()) != -1){
2299 if (quoted
&& peekChar () == '"'){
2300 string_builder
.Append ((char) c
);
2304 val
= string_builder
.ToString ();
2305 return Token
.LITERAL_STRING
;
2311 Report
.Error (1010, Location
, "Newline in constant");
2319 string_builder
.Append ((char) c
);
2322 Report
.Error (1039, Location
, "Unterminated string literal");
2326 private int consume_identifier (int s
)
2328 int res
= consume_identifier (s
, false);
2330 if (doc_state
== XmlCommentState
.Allowed
)
2331 doc_state
= XmlCommentState
.NotAllowed
;
2334 case Token
.NAMESPACE
:
2335 check_incorrect_doc_comment ();
2339 if (res
== Token
.PARTIAL
) {
2340 // Save current position and parse next token.
2343 int next_token
= token ();
2344 bool ok
= (next_token
== Token
.CLASS
) ||
2345 (next_token
== Token
.STRUCT
) ||
2346 (next_token
== Token
.INTERFACE
);
2353 if (next_token
< Token
.LAST_KEYWORD
)
2354 Report
.Error (267, Location
, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
2356 val
= new LocatedToken (Location
, "partial");
2357 return Token
.IDENTIFIER
;
2363 private int consume_identifier (int s
, bool quoted
)
2368 id_builder
[0] = (char) s
;
2370 current_location
= new Location (ref_line
, Col
);
2372 while ((c
= getChar ()) != -1) {
2374 if (is_identifier_part_character ((char) c
)){
2375 if (pos
== max_id_size
){
2376 Report
.Error (645, Location
, "Identifier too long (limit is 512 chars)");
2380 id_builder
[pos
++] = (char) c
;
2381 // putback_char = -1;
2382 } else if (c
== '\\') {
2386 // putback_char = c;
2393 // Optimization: avoids doing the keyword lookup
2394 // on uppercase letters and _
2396 if (!quoted
&& (s
>= 'a' || s
== '_')){
2397 int keyword
= GetKeyword (id_builder
, pos
);
2398 if (keyword
!= -1) {
2405 // Keep identifiers in an array of hashtables to avoid needless
2409 if (identifiers
[pos
] != null) {
2410 val
= identifiers
[pos
][id_builder
];
2412 val
= new LocatedToken (Location
, (string) val
);
2414 escapedIdentifiers
.Add (val
);
2415 return Token
.IDENTIFIER
;
2419 identifiers
[pos
] = new CharArrayHashtable (pos
);
2421 val
= new String (id_builder
, 0, pos
);
2422 if (RootContext
.Version
== LanguageVersion
.ISO_1
) {
2423 for (int i
= 1; i
< id_builder
.Length
; i
+= 3) {
2424 if (id_builder
[i
] == '_' && (id_builder
[i
- 1] == '_' || id_builder
[i
+ 1] == '_')) {
2425 Report
.Error (1638, Location
,
2426 "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val
.ToString ());
2432 char [] chars
= new char [pos
];
2433 Array
.Copy (id_builder
, chars
, pos
);
2435 identifiers
[pos
] [chars
] = val
;
2437 val
= new LocatedToken (Location
, (string) val
);
2439 escapedIdentifiers
.Add (val
);
2440 return Token
.IDENTIFIER
;
2443 public int xtoken ()
2446 bool doread
= false;
2449 // Whether we have seen comments on the current line
2450 bool comments_seen
= false;
2452 for (;(c
= getChar ()) != -1;) {
2454 col
= ((col
+ 8) / 8) * 8;
2458 if (c
== ' ' || c
== '\f' || c
== '\v' || c
== 0xa0 || c
== 0)
2462 if (peekChar () == '\n')
2465 any_token_seen
|= tokens_seen
;
2466 tokens_seen
= false;
2467 comments_seen
= false;
2471 // Handle double-slash comments.
2473 int d
= peekChar ();
2477 if (RootContext
.Documentation
!= null && peekChar () == '/') {
2479 // Don't allow ////.
2480 if ((d
= peekChar ()) != '/') {
2481 update_comment_location ();
2482 if (doc_state
== XmlCommentState
.Allowed
)
2483 handle_one_line_xml_comment ();
2484 else if (doc_state
== XmlCommentState
.NotAllowed
)
2485 warn_incorrect_doc_comment ();
2488 while ((d
= getChar ()) != -1 && (d
!= '\n') && d
!= '\r')
2491 any_token_seen
|= tokens_seen
;
2492 tokens_seen
= false;
2493 comments_seen
= false;
2495 } else if (d
== '*'){
2497 bool docAppend
= false;
2498 if (RootContext
.Documentation
!= null && peekChar () == '*') {
2500 update_comment_location ();
2501 // But when it is /**/, just do nothing.
2502 if (peekChar () == '/') {
2506 if (doc_state
== XmlCommentState
.Allowed
)
2508 else if (doc_state
== XmlCommentState
.NotAllowed
)
2509 warn_incorrect_doc_comment ();
2512 int current_comment_start
= 0;
2514 current_comment_start
= xml_comment_buffer
.Length
;
2515 xml_comment_buffer
.Append (Environment
.NewLine
);
2518 Location start_location
= Location
;
2520 while ((d
= getChar ()) != -1){
2521 if (d
== '*' && peekChar () == '/'){
2523 comments_seen
= true;
2527 xml_comment_buffer
.Append ((char) d
);
2530 any_token_seen
|= tokens_seen
;
2531 tokens_seen
= false;
2533 // Reset 'comments_seen' just to be consistent.
2534 // It doesn't matter either way, here.
2536 comments_seen
= false;
2540 Report
.Error (1035, start_location
, "End-of-file found, '*/' expected");
2543 update_formatted_doc_comment (current_comment_start
);
2546 goto is_punct_label
;
2550 if (c
== '\\' || is_identifier_start_character ((char)c
)){
2552 return consume_identifier (c
);
2556 current_location
= new Location (ref_line
, Col
);
2557 if ((t
= is_punct ((char)c
, ref doread
)) != Token
.ERROR
){
2567 any_token_seen
|= tokens_seen
;
2568 tokens_seen
= false;
2569 comments_seen
= false;
2573 if (c
>= '0' && c
<= '9'){
2575 return is_number (c
);
2580 int peek
= peekChar ();
2581 if (peek
>= '0' && peek
<= '9')
2582 return is_number (c
);
2587 if (tokens_seen
|| comments_seen
) {
2588 Eror_WrongPreprocessorLocation ();
2592 if (handle_preprocessing_directive (true))
2595 bool directive_expected
= false;
2596 while ((c
= getChar ()) != -1) {
2598 directive_expected
= true;
2599 } else if (!directive_expected
) {
2600 // TODO: Implement comment support for disabled code and uncomment this code
2602 // Eror_WrongPreprocessorLocation ();
2603 // return Token.ERROR;
2608 if (c
== ' ' || c
== '\t' || c
== '\r' || c
== '\n' || c
== '\f' || c
== '\v' )
2612 if (handle_preprocessing_directive (false))
2615 directive_expected
= false;
2619 tokens_seen
= false;
2627 return consume_string (false);
2633 error_details
= "Empty character literal";
2634 Report
.Error (1011, Location
, error_details
);
2637 if (c
== '\r' || c
== '\n') {
2638 Report
.Error (1010, Location
, "Newline in constant");
2644 val
= new System
.Char ();
2649 error_details
= "Too many characters in character literal";
2650 Report
.Error (1012, Location
, error_details
);
2652 // Try to recover, read until newline or next "'"
2653 while ((c
= getChar ()) != -1){
2662 return Token
.LITERAL_CHARACTER
;
2669 return consume_string (true);
2670 } else if (is_identifier_start_character ((char) c
)){
2671 return consume_identifier (c
, true);
2673 Report
.Error (1646, Location
, "Keyword, identifier, or string expected after verbatim specifier: @");
2677 error_details
= ((char)c
).ToString ();
2686 // Handles one line xml comment
2688 private void handle_one_line_xml_comment ()
2691 while ((c
= peekChar ()) == ' ')
2692 getChar (); // skip heading whitespaces.
2693 while ((c
= peekChar ()) != -1 && c
!= '\n' && c
!= '\r') {
2694 xml_comment_buffer
.Append ((char) getChar ());
2696 if (c
== '\r' || c
== '\n')
2697 xml_comment_buffer
.Append (Environment
.NewLine
);
2701 // Remove heading "*" in Javadoc-like xml documentation.
2703 private void update_formatted_doc_comment (int current_comment_start
)
2705 int length
= xml_comment_buffer
.Length
- current_comment_start
;
2706 string [] lines
= xml_comment_buffer
.ToString (
2707 current_comment_start
,
2708 length
).Replace ("\r", "").Split ('\n');
2710 // The first line starts with /**, thus it is not target
2711 // for the format check.
2712 for (int i
= 1; i
< lines
.Length
; i
++) {
2713 string s
= lines
[i
];
2714 int idx
= s
.IndexOf ('*');
2717 if (i
< lines
.Length
- 1)
2721 head
= s
.Substring (0, idx
);
2722 foreach (char c
in head
)
2725 lines
[i
] = s
.Substring (idx
+ 1);
2727 xml_comment_buffer
.Remove (current_comment_start
, length
);
2728 xml_comment_buffer
.Insert (current_comment_start
, String
.Join (Environment
.NewLine
, lines
));
2732 // Updates current comment location.
2734 private void update_comment_location ()
2736 if (current_comment_location
.IsNull
) {
2737 // "-2" is for heading "//" or "/*"
2738 current_comment_location
=
2739 new Location (ref_line
, col
- 2);
2744 // Checks if there was incorrect doc comments and raise
2747 public void check_incorrect_doc_comment ()
2749 if (xml_comment_buffer
.Length
> 0)
2750 warn_incorrect_doc_comment ();
2754 // Raises a warning when tokenizer found incorrect doccomment
2757 private void warn_incorrect_doc_comment ()
2759 if (doc_state
!= XmlCommentState
.Error
) {
2760 doc_state
= XmlCommentState
.Error
;
2761 // in csc, it is 'XML comment is not placed on
2762 // a valid language element'. But that does not
2764 Report
.Warning (1587, 2, Location
, "XML comment is not placed on a valid language element");
2769 // Consumes the saved xml comment lines (if any)
2770 // as for current target member or type.
2772 public string consume_doc_comment ()
2774 if (xml_comment_buffer
.Length
> 0) {
2775 string ret
= xml_comment_buffer
.ToString ();
2776 reset_doc_comment ();
2782 void reset_doc_comment ()
2784 xml_comment_buffer
.Length
= 0;
2785 current_comment_location
= Location
.Null
;
2788 public void cleanup ()
2790 if (ifstack
!= null && ifstack
.Count
>= 1) {
2791 current_location
= new Location (ref_line
, Col
);
2792 int state
= (int) ifstack
.Pop ();
2793 if ((state
& REGION
) != 0)
2794 Report
.Error (1038, Location
, "#endregion directive expected");
2796 Report
.Error (1027, Location
, "Expected `#endif' directive");
2802 // Indicates whether it accepts XML documentation or not.
2804 public enum XmlCommentState
{
2805 // comment is allowed in this state.
2807 // comment is not allowed in this state.
2809 // once comments appeared when it is NotAllowed, then the
2810 // state is changed to it, until the state is changed to