// This also implements the preprocessor
//
// Author: Miguel de Icaza (miguel@gnu.org)
+// Marek Safar (marek.safar@seznam.cz)
//
// Licensed under the terms of the GNU GPL
//
public class Tokenizer : yyParser.yyInput
{
SeekableStreamReader reader;
- public SourceFile ref_name;
- public SourceFile file_name;
- public int ref_line = 1;
- public int line = 1;
- public int col = 1;
- public int current_token;
+ SourceFile ref_name;
+ SourceFile file_name;
+ int ref_line = 1;
+ int line = 1;
+ int col = 0;
+ int previous_col;
+ int current_token;
bool handle_get_set = false;
bool handle_remove_add = false;
bool handle_assembly = false;
+ bool handle_constraints = false;
+ bool handle_typeof = false;
+ Location current_location;
+ Location current_comment_location = Location.Null;
+ ArrayList escapedIdentifiers = new ArrayList ();
//
// XML documentation buffer. The save point is used to divide
bool any_token_seen = false;
static Hashtable tokenValues;
+ static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
private static Hashtable TokenValueName
{
}
}
+ public bool ConstraintsParsing {
+ get {
+ return handle_constraints;
+ }
+
+ set {
+ handle_constraints = value;
+ }
+ }
+
+ public bool TypeOfParsing {
+ get {
+ return handle_typeof;
+ }
+
+ set {
+ handle_typeof = value;
+ }
+ }
+
public XmlCommentState doc_state {
get { return xmlDocState; }
set {
if (value == XmlCommentState.Allowed) {
check_incorrect_doc_comment ();
- consume_doc_comment ();
+ reset_doc_comment ();
}
xmlDocState = value;
}
}
-
+ public bool IsEscapedIdentifier (Location loc)
+ {
+ foreach (LocatedToken lt in escapedIdentifiers)
+ if (lt.Location.Equals (loc))
+ return true;
+ return false;
+ }
+
//
// Class variables
//
static CharArrayHashtable[] keywords;
+ static Hashtable keywordStrings = new Hashtable ();
static NumberStyles styles;
static NumberFormatInfo csharp_format_info;
Hashtable defines;
const int TAKING = 1;
- const int TAKEN_BEFORE = 2;
const int ELSE_SEEN = 4;
const int PARENT_TAKING = 8;
const int REGION = 16;
static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
- const int max_number_size = 128;
+ const int max_number_size = 512;
static char [] number_builder = new char [max_number_size];
static int number_pos;
}
}
+ //
+ // This is used when the tokenizer needs to save
+ // the current position as it needs to do some parsing
+ // on its own to deamiguate a token in behalf of the
+ // parser.
+ //
+ Stack position_stack = new Stack (2);
+ class Position {
+ public int position;
+ public int ref_line;
+ public int col;
+ public int putback_char;
+ public int previous_col;
+ public Stack ifstack;
+#if GMCS_SOURCES
+ public int parsing_generic_less_than;
+#endif
+ public Position (Tokenizer t)
+ {
+ position = t.reader.Position;
+ ref_line = t.ref_line;
+ col = t.col;
+ putback_char = t.putback_char;
+ previous_col = t.previous_col;
+ if (t.ifstack != null && t.ifstack.Count != 0)
+ ifstack = (Stack)t.ifstack.Clone ();
+#if GMCS_SOURCES
+ parsing_generic_less_than = t.parsing_generic_less_than;
+#endif
+ }
+ }
+
+ public void PushPosition ()
+ {
+ position_stack.Push (new Position (this));
+ }
+
+ public void PopPosition ()
+ {
+ Position p = (Position) position_stack.Pop ();
+
+ reader.Position = p.position;
+ ref_line = p.ref_line;
+ col = p.col;
+ putback_char = p.putback_char;
+ previous_col = p.previous_col;
+ ifstack = p.ifstack;
+ }
+
+ // Do not reset the position, ignore it.
+ public void DiscardPosition ()
+ {
+ position_stack.Pop ();
+ }
+
static void AddKeyword (string kw, int token) {
+ keywordStrings.Add (kw, kw);
if (keywords [kw.Length] == null) {
keywords [kw.Length] = new CharArrayHashtable (kw.Length);
}
AddKeyword ("volatile", Token.VOLATILE);
AddKeyword ("while", Token.WHILE);
AddKeyword ("partial", Token.PARTIAL);
+#if GMCS_SOURCE
+ AddKeyword ("where", Token.WHERE);
+#endif
}
//
return -1;
if (handle_assembly == false && res == Token.ASSEMBLY)
return -1;
-
+#if GMCS_SOURCE
+ if (handle_constraints == false && res == Token.WHERE)
+ return -1;
+#endif
return res;
}
public Location Location {
- get {
- return new Location (ref_line);
- }
+ get { return current_location; }
}
void define (string def)
Mono.CSharp.Location.Push (file);
}
- public static void Cleanup () {
- identifiers = null;
- }
-
static bool is_identifier_start_character (char c)
{
- return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter (c);
}
static bool is_identifier_part_character (char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
}
-
+
+ public static bool IsKeyword (string s)
+ {
+ return keywordStrings [s] != null;
+ }
+
public static bool IsValidIdentifier (string s)
{
if (s == null || s.Length == 0)
return false;
-
+
if (!is_identifier_start_character (s [0]))
return false;
return true;
}
+#if GMCS_SOURCE
+ bool parse_generic_dimension (out int dimension)
+ {
+ dimension = 1;
+
+ again:
+ int the_token = token ();
+ if (the_token == Token.OP_GENERICS_GT)
+ return true;
+ else if (the_token == Token.COMMA) {
+ dimension++;
+ goto again;
+ }
+
+ return false;
+ }
+
+ bool parse_less_than ()
+ {
+ start:
+ int the_token = token ();
+ if (the_token == Token.OPEN_BRACKET) {
+ do {
+ the_token = token ();
+ } while (the_token != Token.CLOSE_BRACKET);
+ the_token = token ();
+ }
+ switch (the_token) {
+ case Token.IDENTIFIER:
+ case Token.OBJECT:
+ case Token.STRING:
+ case Token.BOOL:
+ case Token.DECIMAL:
+ case Token.FLOAT:
+ case Token.DOUBLE:
+ case Token.SBYTE:
+ case Token.BYTE:
+ case Token.SHORT:
+ case Token.USHORT:
+ case Token.INT:
+ case Token.UINT:
+ case Token.LONG:
+ case Token.ULONG:
+ case Token.CHAR:
+ case Token.VOID:
+ break;
+
+ default:
+ return false;
+ }
+ again:
+ the_token = token ();
+
+ if (the_token == Token.OP_GENERICS_GT)
+ return true;
+ else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
+ goto start;
+ else if (the_token == Token.INTERR || the_token == Token.STAR)
+ goto again;
+ else if (the_token == Token.OP_GENERICS_LT) {
+ if (!parse_less_than ())
+ return false;
+ goto again;
+ } else if (the_token == Token.OPEN_BRACKET) {
+ rank_specifiers:
+ the_token = token ();
+ if (the_token == Token.CLOSE_BRACKET)
+ goto again;
+ else if (the_token == Token.COMMA)
+ goto rank_specifiers;
+ return false;
+ }
+
+ return false;
+ }
+
+ int parsing_generic_less_than = 0;
+
+ public void PutbackNullable ()
+ {
+ if (nullable_pos < 0)
+ throw new Exception ();
+
+ current_token = -1;
+ val = null;
+ reader.Position = nullable_pos;
+
+ putback_char = '?';
+ }
+
+ public void PutbackCloseParens ()
+ {
+ putback_char = ')';
+ }
+
+
+ int nullable_pos = -1;
+
+ public void CheckNullable (bool is_nullable)
+ {
+ if (is_nullable)
+ nullable_pos = reader.Position;
+ else
+ nullable_pos = -1;
+ }
+#endif
int is_punct (char c, ref bool doread)
{
int d;
switch (c){
case '{':
+ val = Location;
return Token.OPEN_BRACE;
case '}':
+ val = Location;
return Token.CLOSE_BRACE;
case '[':
// To block doccomment inside attribute declaration.
--deambiguate_close_parens;
- // Save current position and parse next token.
- int old = reader.Position;
- int new_token = token ();
- reader.Position = old;
- putback_char = -1;
+ PushPosition ();
+
+ int new_token = xtoken ();
+
+ PopPosition ();
if (new_token == Token.OPEN_PARENS)
return Token.CLOSE_PARENS_OPEN_PARENS;
case ',':
return Token.COMMA;
- case ':':
- return Token.COLON;
case ';':
+ val = Location;
return Token.SEMICOLON;
case '~':
+ val = Location;
return Token.TILDE;
case '?':
return Token.INTERR;
}
+#if GMCS_SOURCE
+ if (c == '<') {
+ if (parsing_generic_less_than++ > 0)
+ return Token.OP_GENERICS_LT;
+
+ if (handle_typeof) {
+ int dimension;
+ PushPosition ();
+ if (parse_generic_dimension (out dimension)) {
+ val = dimension;
+ DiscardPosition ();
+ return Token.GENERIC_DIMENSION;
+ }
+ PopPosition ();
+ }
+
+ // Save current position and parse next token.
+ PushPosition ();
+ bool is_generic_lt = parse_less_than ();
+ PopPosition ();
+
+ if (is_generic_lt) {
+ parsing_generic_less_than++;
+ return Token.OP_GENERICS_LT;
+ } else
+ parsing_generic_less_than = 0;
+
+ d = peekChar ();
+ if (d == '<'){
+ getChar ();
+ d = peekChar ();
+
+ if (d == '='){
+ doread = true;
+ return Token.OP_SHIFT_LEFT_ASSIGN;
+ }
+ return Token.OP_SHIFT_LEFT;
+ } else if (d == '='){
+ doread = true;
+ return Token.OP_LE;
+ }
+ return Token.OP_LT;
+ } else if (c == '>') {
+ if (parsing_generic_less_than > 0) {
+ parsing_generic_less_than--;
+ return Token.OP_GENERICS_GT;
+ }
+ d = peekChar ();
+ if (d == '>'){
+ getChar ();
+ d = peekChar ();
+
+ if (d == '='){
+ doread = true;
+ return Token.OP_SHIFT_RIGHT_ASSIGN;
+ }
+ return Token.OP_SHIFT_RIGHT;
+ } else if (d == '='){
+ doread = true;
+ return Token.OP_GE;
+ }
+ return Token.OP_GT;
+ }
+#endif
d = peekChar ();
if (c == '+'){
- if (d == '+')
+ if (d == '+') {
+ val = Location;
t = Token.OP_INC;
+ }
else if (d == '=')
t = Token.OP_ADD_ASSIGN;
- else
+ else {
+ val = Location;
return Token.PLUS;
+ }
doread = true;
return t;
}
if (c == '-'){
- if (d == '-')
+ if (d == '-') {
+ val = Location;
t = Token.OP_DEC;
+ }
else if (d == '=')
t = Token.OP_SUB_ASSIGN;
else if (d == '>')
t = Token.OP_PTR;
- else
+ else {
+ val = Location;
return Token.MINUS;
+ }
doread = true;
return t;
}
doread = true;
return Token.OP_NE;
}
+ val = Location;
return Token.BANG;
}
doread = true;
return Token.OP_AND_ASSIGN;
}
+ val = Location;
return Token.BITWISE_AND;
}
doread = true;
return Token.OP_MULT_ASSIGN;
}
+ val = Location;
return Token.STAR;
}
return Token.CARRET;
}
+#if !GMCS_SOURCE
if (c == '<'){
if (d == '<'){
getChar ();
}
return Token.OP_GT;
}
+#endif
+ if (c == ':'){
+ if (d == ':'){
+ doread = true;
+ return Token.DOUBLE_COLON;
+ }
+ val = Location;
+ return Token.COLON;
+ }
+
return Token.ERROR;
}
int deambiguate_close_parens = 0;
- public void Deambiguate_CloseParens ()
+ public void Deambiguate_CloseParens (object expression)
{
putback (')');
+
+ // When any binary operation is used we are sure it is not a cast
+ if (expression is Binary)
+ return;
+
deambiguate_close_parens++;
}
- void Error_NumericConstantTooLong ()
- {
- Report.Error (1021, Location, "Numeric constant too long");
- }
-
bool decimal_digits (int c)
{
int d;
return seen_digits;
}
- bool is_hex (int e)
+ static bool is_hex (int e)
{
return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
}
-
- void hex_digits (int c)
- {
- if (c != -1)
- number_builder [number_pos++] = (char) c;
-
- }
-
- int real_type_suffix (int c)
+
+ static int real_type_suffix (int c)
{
int t;
// if we have not seen anything in between
// report this error
//
- Report.Warning (78, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
+ Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
}
//
// This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
val = 0ul;
return Token.LITERAL_INTEGER;
}
+ catch (FormatException) {
+ Report.Error (1013, Location, "Invalid number");
+ val = 0ul;
+ return Token.LITERAL_INTEGER;
+ }
}
int adjust_real (int t)
{
string s = new String (number_builder, 0, number_pos);
+ const string error_details = "Floating-point constant is outside the range of type `{0}'";
switch (t){
case Token.LITERAL_DECIMAL:
val = System.Decimal.Parse (s, styles, csharp_format_info);
} catch (OverflowException) {
val = 0m;
- error_details = "Floating-point constant is outside the range of the type 'decimal'";
- Report.Error (594, Location, error_details);
+ Report.Error (594, Location, error_details, "decimal");
}
break;
case Token.LITERAL_FLOAT:
try {
- val = (float) System.Double.Parse (s, styles, csharp_format_info);
+ val = float.Parse (s, styles, csharp_format_info);
} catch (OverflowException) {
val = 0.0f;
- error_details = "Floating-point constant is outside the range of the type 'float'";
- Report.Error (594, Location, error_details);
+ Report.Error (594, Location, error_details, "float");
}
break;
val = System.Double.Parse (s, styles, csharp_format_info);
} catch (OverflowException) {
val = 0.0;
- error_details = "Floating-point constant is outside the range of the type 'double'";
- Report.Error (594, Location, error_details);
+ Report.Error (594, Location, error_details, "double");
}
break;
}
goto default;
return v;
default:
- Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);
+ Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
return d;
}
getChar ();
int getChar ()
{
- if (putback_char != -1){
- int x = putback_char;
+ int x;
+ if (putback_char != -1) {
+ x = putback_char;
putback_char = -1;
-
- return x;
+ } else
+ x = reader.Read ();
+ if (x == '\n') {
+ line++;
+ ref_line++;
+ previous_col = col;
+ col = 0;
}
- return reader.Read ();
+ else
+ col++;
+ return x;
}
int peekChar ()
Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
throw new Exception ("This should not happen putback on putback");
}
+ if (c == '\n' || col == 0) {
+ // It won't happen though.
+ line--;
+ ref_line--;
+ col = previous_col;
+ }
+ else
+ col--;
putback_char = c;
}
return val;
}
- bool IsCastToken (int token)
+ static bool IsCastToken (int token)
{
switch (token) {
case Token.BANG:
case Token.LITERAL_STRING:
case Token.BASE:
case Token.CHECKED:
+ case Token.DELEGATE:
case Token.FALSE:
case Token.FIXED:
case Token.NEW:
case Token.TYPEOF:
case Token.UNCHECKED:
case Token.UNSAFE:
+#if GMCS_SOURCE
+ case Token.DEFAULT:
+#endif
//
// These can be part of a member access
}
public int token ()
- {
+ {
current_token = xtoken ();
- return current_token;
- }
+
+#if GMCS_SOURCE
+ if (current_token != Token.DEFAULT)
+ return current_token;
+
+ PushPosition();
+ int c = xtoken();
+ if (c == -1)
+ current_token = Token.ERROR;
+ else if (c == Token.OPEN_PARENS)
+ current_token = Token.DEFAULT_OPEN_PARENS;
+ else if (c == Token.COLON)
+ current_token = Token.DEFAULT_COLON;
+ else
+ PopPosition();
+#endif
+ return current_token;
+ }
static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
// skip over white space
while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
;
-
-
+
while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
if (is_identifier_part_character ((char) c)){
static_cmd_arg.Append ((char) c);
cmd = static_cmd_arg.ToString ();
- if (c == '\n'){
- line++;
- ref_line++;
+ if (c == '\n' || c == '\r'){
return;
- } else if (c == '\r')
- col = 0;
+ }
// skip over white space
while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
;
if (c == '\n'){
- line++;
- ref_line++;
return;
} else if (c == '\r'){
- col = 0;
+ return;
+ } else if (c == -1){
+ arg = "";
return;
}
static_cmd_arg.Append ((char) c);
}
- if (c == '\n'){
- line++;
- ref_line++;
- } else if (c == '\r')
- col = 0;
- arg = static_cmd_arg.ToString ().Trim ();
+ arg = static_cmd_arg.ToString ();
}
//
//
bool PreProcessLine (string arg)
{
- if (arg == "")
+ if (arg.Length == 0)
return false;
if (arg == "default"){
//
return true;
}
-
+
try {
int pos;
//
// Handles #define and #undef
//
- void PreProcessDefinition (bool is_define, string arg)
+ void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
{
- if (arg == "" || arg == "true" || arg == "false"){
+ if (arg.Length == 0 || arg == "true" || arg == "false"){
Report.Error (1001, Location, "Missing identifer to pre-processor directive");
return;
}
- char[] whitespace = { ' ', '\t' };
- if (arg.IndexOfAny (whitespace) != -1){
- Report.Error (1025, Location, "Single-line comment or end-of-line expected");
+ if (arg.IndexOfAny (simple_whitespaces) != -1){
+ Error_EndLineExpected ();
return;
}
}
}
+ if (!caller_is_taking)
+ return;
+
if (is_define){
if (defines == null)
defines = new Hashtable ();
Hashtable w_table = Report.warning_ignore_table;
foreach (int code in codes) {
if (w_table != null && w_table.Contains (code))
- Report.Warning (1635, 1, Location, "Cannot restore warning 'CS{0:0000}' because it was disabled globally", code);
+ Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
}
return;
values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
}
catch (FormatException) {
- Report.Warning (1692, Location, "Invalid number");
+ Report.Warning (1692, 1, Location, "Invalid number");
}
}
return values;
if (c == '('){
s = s.Substring (1);
- bool val = pp_expr (ref s);
+ bool val = pp_expr (ref s, false);
if (s.Length > 0 && s [0] == ')'){
s = s.Substring (1);
return val;
//
// Evaluates an expression for `#if' or `#elif'
//
- bool pp_expr (ref string s)
+ bool pp_expr (ref string s, bool isTerm)
{
bool va = pp_and (ref s);
s = s.Trim ();
if (c == '|'){
if (len > 2 && s [1] == '|'){
s = s.Substring (2);
- return va | pp_expr (ref s);
+ return va | pp_expr (ref s, isTerm);
} else {
Error_InvalidDirective ();
return false;
}
- }
+ }
+ if (isTerm) {
+ Error_EndLineExpected ();
+ return false;
+ }
}
return va;
bool eval (string s)
{
- bool v = pp_expr (ref s);
+ bool v = pp_expr (ref s, true);
s = s.Trim ();
if (s.Length != 0){
- Error_InvalidDirective ();
return false;
}
return v;
}
+
+ void Error_NumericConstantTooLong ()
+ {
+ Report.Error (1021, Location, "Numeric constant too long");
+ }
void Error_InvalidDirective ()
{
- Report.Error (1517, Location, "Invalid pre-processor directive");
+ Report.Error (1517, Location, "Invalid preprocessor directive");
}
void Error_UnexpectedDirective (string extra)
void Error_TokensSeen ()
{
- Report.Error (
- 1032, Location,
- "Cannot define or undefine pre-processor symbols after a token in the file");
+ Report.Error (1032, Location,
+ "Cannot define or undefine preprocessor symbols after first token in file");
+ }
+
+ void Eror_WrongPreprocessorLocation ()
+ {
+ Report.Error (1040, Location,
+ "Preprocessor directives must appear as the first non-whitespace character on a line");
+ }
+
+ void Error_EndLineExpected ()
+ {
+ Report.Error (1025, Location, "Single-line comment or end-of-line expected");
}
//
// if true, then the code continues processing the code
// if false, the code stays in a loop until another directive is
// reached.
- //
+ // When caller_is_taking is false we ignore all directives except the ones
+ // which can help us to identify where the #if block ends
bool handle_preprocessing_directive (bool caller_is_taking)
{
string cmd, arg;
// Eat any trailing whitespaces and single-line comments
if (arg.IndexOf ("//") != -1)
arg = arg.Substring (0, arg.IndexOf ("//"));
- arg = arg.TrimEnd (' ', '\t');
+ arg = arg.Trim (simple_whitespaces);
//
// The first group of pre-processing instructions is always processed
//
switch (cmd){
- case "pragma":
- if (RootContext.Version == LanguageVersion.ISO_1) {
- Report.FeatureIsNotStandardized (Location, "#pragma");
- return caller_is_taking;
- }
-
- PreProcessPragma (arg);
- return caller_is_taking;
-
- case "line":
- if (!PreProcessLine (arg))
- Report.Error (
- 1576, Location,
- "Argument to #line directive is missing or invalid");
- return caller_is_taking;
-
case "region":
region_directive = true;
arg = "true";
goto case "if";
case "endregion":
- region_directive = true;
- goto case "endif";
-
- case "if":
- if (arg == ""){
- Error_InvalidDirective ();
+ if (ifstack == null || ifstack.Count == 0){
+ Error_UnexpectedDirective ("no #region for this #endregion");
return true;
}
- bool taking = false;
+ int pop = (int) ifstack.Pop ();
+
+ if ((pop & REGION) == 0)
+ Report.Error (1027, Location, "Expected `#endif' directive");
+
+ return caller_is_taking;
+
+ case "if":
if (ifstack == null)
- ifstack = new Stack ();
+ ifstack = new Stack (2);
+ int flags = region_directive ? REGION : 0;
if (ifstack.Count == 0){
- taking = true;
+ flags |= PARENT_TAKING;
} else {
int state = (int) ifstack.Peek ();
- if ((state & TAKING) != 0)
- taking = true;
+ if ((state & TAKING) != 0) {
+ flags |= PARENT_TAKING;
+ }
}
- if (eval (arg) && taking){
- int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
- if (region_directive)
- push |= REGION;
- ifstack.Push (push);
+ if (caller_is_taking && eval (arg)) {
+ ifstack.Push (flags | TAKING);
return true;
- } else {
- int push = (taking ? PARENT_TAKING : 0);
- if (region_directive)
- push |= REGION;
- ifstack.Push (push);
- return false;
}
+ ifstack.Push (flags);
+ return false;
case "endif":
if (ifstack == null || ifstack.Count == 0){
Error_UnexpectedDirective ("no #if for this #endif");
return true;
} else {
- int pop = (int) ifstack.Pop ();
+ pop = (int) ifstack.Pop ();
- if (region_directive && ((pop & REGION) == 0))
- Report.Error (1027, Location, "#endif directive expected");
- else if (!region_directive && ((pop & REGION) != 0))
+ if ((pop & REGION) != 0)
Report.Error (1038, Location, "#endregion directive expected");
+ if (arg.Length != 0) {
+ Error_EndLineExpected ();
+ }
+
if (ifstack.Count == 0)
return true;
- else {
- int state = (int) ifstack.Peek ();
- if ((state & TAKING) != 0)
- return true;
- else
- return false;
- }
+ int state = (int) ifstack.Peek ();
+ return (state & TAKING) != 0;
}
case "elif":
Error_UnexpectedDirective ("no #if for this #elif");
return true;
} else {
- int state = (int) ifstack.Peek ();
+ int state = (int) ifstack.Pop ();
if ((state & REGION) != 0) {
Report.Error (1038, Location, "#endregion directive expected");
return true;
}
- if ((state & (TAKEN_BEFORE | TAKING)) != 0)
+ if ((state & TAKING) != 0) {
+ ifstack.Push (0);
return false;
+ }
if (eval (arg) && ((state & PARENT_TAKING) != 0)){
- state = (int) ifstack.Pop ();
- ifstack.Push (state | TAKING | TAKEN_BEFORE);
+ ifstack.Push (state | TAKING);
return true;
- } else
- return false;
+ }
+
+ ifstack.Push (state);
+ return false;
}
case "else":
if (ifstack == null || ifstack.Count == 0){
- Report.Error (
- 1028, Location,
- "Unexpected processor directive (no #if for this #else)");
+ Error_UnexpectedDirective ("no #if for this #else");
return true;
} else {
int state = (int) ifstack.Peek ();
ifstack.Pop ();
- bool ret;
- if ((state & TAKEN_BEFORE) == 0){
- ret = ((state & PARENT_TAKING) != 0);
- } else
- ret = false;
-
- if (ret)
- state |= TAKING;
- else
- state &= ~TAKING;
+ if (arg.Length != 0) {
+ Error_EndLineExpected ();
+ return true;
+ }
+
+ bool ret = false;
+ if ((state & PARENT_TAKING) != 0) {
+ ret = (state & TAKING) == 0;
+ if (ret)
+ state |= TAKING;
+ else
+ state &= ~TAKING;
+ }
+
ifstack.Push (state | ELSE_SEEN);
return ret;
}
+ case "define":
+ if (any_token_seen){
+ Error_TokensSeen ();
+ return caller_is_taking;
+ }
+ PreProcessDefinition (true, arg, caller_is_taking);
+ return caller_is_taking;
+
+ case "undef":
+ if (any_token_seen){
+ Error_TokensSeen ();
+ return caller_is_taking;
+ }
+ PreProcessDefinition (false, arg, caller_is_taking);
+ return caller_is_taking;
}
//
return false;
switch (cmd){
- case "define":
- if (any_token_seen){
- Error_TokensSeen ();
- return true;
- }
- PreProcessDefinition (true, arg);
+ case "error":
+ Report.Error (1029, Location, "#error: '" + arg + "'");
+ return true;
+
+ case "warning":
+ Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
return true;
- case "undef":
- if (any_token_seen){
- Error_TokensSeen ();
+ case "pragma":
+ if (RootContext.Version == LanguageVersion.ISO_1) {
+ Report.FeatureIsNotStandardized (Location, "#pragma");
return true;
}
- PreProcessDefinition (false, arg);
- return true;
- case "error":
- Report.Error (1029, Location, "#error: '" + arg + "'");
+ PreProcessPragma (arg);
return true;
- case "warning":
- Report.Warning (1030, Location, "#warning: '{0}'", arg);
- return true;
+ case "line":
+ if (!PreProcessLine (arg))
+ Report.Error (
+ 1576, Location,
+ "The line number specified for #line directive is missing or invalid");
+ return caller_is_taking;
}
- Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
+ Report.Error (1024, Location, "Wrong preprocessor directive");
return true;
}
- private int consume_string (bool quoted)
+ private int consume_string (bool quoted)
{
int c;
string_builder.Length = 0;
if (c == '\n'){
if (!quoted)
Report.Error (1010, Location, "Newline in constant");
- line++;
- ref_line++;
- col = 0;
- } else
- col++;
+ }
if (!quoted){
c = escape (c);
if (res == Token.PARTIAL) {
// Save current position and parse next token.
- int old = reader.Position;
- int old_putback = putback_char;
-
- putback_char = -1;
+ PushPosition ();
int next_token = token ();
bool ok = (next_token == Token.CLASS) ||
(next_token == Token.STRUCT) ||
(next_token == Token.INTERFACE);
- reader.Position = old;
- putback_char = old_putback;
+ PopPosition ();
if (ok)
return res;
- else {
- val = "partial";
- return Token.IDENTIFIER;
- }
+
+ if (next_token < Token.LAST_KEYWORD)
+ Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
+
+ val = new LocatedToken (Location, "partial");
+ return Token.IDENTIFIER;
}
return res;
private int consume_identifier (int s, bool quoted)
{
int pos = 1;
- int c;
+ int c = -1;
id_builder [0] = (char) s;
- while ((c = reader.Read ()) != -1) {
+ current_location = new Location (ref_line, Col);
+
+ while ((c = getChar ()) != -1) {
+ loop:
if (is_identifier_part_character ((char) c)){
if (pos == max_id_size){
Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
}
id_builder [pos++] = (char) c;
- putback_char = -1;
- col++;
+// putback_char = -1;
+ } else if (c == '\\') {
+ c = escape (c);
+ goto loop;
} else {
- putback_char = c;
+// putback_char = c;
+ putback (c);
break;
}
}
//
if (!quoted && (s >= 'a' || s == '_')){
int keyword = GetKeyword (id_builder, pos);
- if (keyword != -1)
+ if (keyword != -1) {
+ val = Location;
return keyword;
+ }
}
//
if (identifiers [pos] != null) {
val = identifiers [pos][id_builder];
if (val != null) {
+ val = new LocatedToken (Location, (string) val);
+ if (quoted)
+ escapedIdentifiers.Add (val);
return Token.IDENTIFIER;
}
}
identifiers [pos] = new CharArrayHashtable (pos);
val = new String (id_builder, 0, pos);
+ if (RootContext.Version == LanguageVersion.ISO_1) {
+ for (int i = 1; i < id_builder.Length; i += 3) {
+ if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
+ Report.Error (1638, Location,
+ "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
+ break;
+ }
+ }
+ }
char [] chars = new char [pos];
Array.Copy (id_builder, chars, pos);
identifiers [pos] [chars] = val;
+ val = new LocatedToken (Location, (string) val);
+ if (quoted)
+ escapedIdentifiers.Add (val);
return Token.IDENTIFIER;
}
bool comments_seen = false;
val = null;
- // optimization: eliminate col and implement #directive semantic correctly.
- for (;(c = getChar ()) != -1; col++) {
- if (c == ' ')
- continue;
-
- if (c == '\t') {
- col = (((col + 8) / 8) * 8) - 1;
- continue;
- }
-
- if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
+ for (;(c = getChar ()) != -1;) {
+ if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == 0xa0)
continue;
if (c == '\r') {
if (peekChar () == '\n')
getChar ();
- line++;
- ref_line++;
- col = 0;
any_token_seen |= tokens_seen;
tokens_seen = false;
comments_seen = false;
getChar ();
// Don't allow ////.
if ((d = peekChar ()) != '/') {
+ update_comment_location ();
if (doc_state == XmlCommentState.Allowed)
handle_one_line_xml_comment ();
else if (doc_state == XmlCommentState.NotAllowed)
}
}
while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
- col++;
if (d == '\n'){
- line++;
- ref_line++;
- col = 0;
}
any_token_seen |= tokens_seen;
tokens_seen = false;
bool docAppend = false;
if (RootContext.Documentation != null && peekChar () == '*') {
getChar ();
+ update_comment_location ();
// But when it is /**/, just do nothing.
if (peekChar () == '/') {
getChar ();
xml_comment_buffer.Append (Environment.NewLine);
}
+ Location start_location = Location;
+
while ((d = getChar ()) != -1){
if (d == '*' && peekChar () == '/'){
getChar ();
- col++;
comments_seen = true;
break;
}
xml_comment_buffer.Append ((char) d);
if (d == '\n'){
- line++;
- ref_line++;
- col = 0;
any_token_seen |= tokens_seen;
tokens_seen = false;
//
comments_seen = false;
}
}
+ if (!comments_seen)
+ Report.Error (1035, start_location, "End-of-file found, '*/' expected");
+
if (docAppend)
update_formatted_doc_comment (current_comment_start);
continue;
}
- if (is_identifier_start_character ((char)c)){
+ if (c == '\\' || is_identifier_start_character ((char)c)){
tokens_seen = true;
return consume_identifier (c);
}
is_punct_label:
+ current_location = new Location (ref_line, Col);
if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
tokens_seen = true;
if (doread){
getChar ();
- col++;
}
return t;
}
// white space
if (c == '\n'){
- line++;
- ref_line++;
- col = 0;
any_token_seen |= tokens_seen;
tokens_seen = false;
comments_seen = false;
return Token.DOT;
}
- /* For now, ignore pre-processor commands */
- // FIXME: In C# the '#' is not limited to appear
- // on the first column.
if (c == '#') {
- bool cont = true;
-
if (tokens_seen || comments_seen) {
- error_details = "Preprocessor directives must appear as the first non-whitespace " +
- "character on a line.";
+ Eror_WrongPreprocessorLocation ();
+ return Token.ERROR;
+ }
+
+ if (handle_preprocessing_directive (true))
+ continue;
- Report.Error (1040, Location, error_details);
+ bool directive_expected = false;
+ while ((c = getChar ()) != -1) {
+ if (col == 1) {
+ directive_expected = true;
+ } else if (!directive_expected) {
+ // TODO: Implement comment support for disabled code and uncomment this code
+// if (c == '#') {
+// Eror_WrongPreprocessorLocation ();
+// return Token.ERROR;
+// }
+ continue;
+ }
- return Token.ERROR;
- }
-
- start_again:
-
- cont = handle_preprocessing_directive (cont);
+ if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
+ continue;
- if (cont){
- col = 0;
- continue;
+ if (c == '#') {
+ if (handle_preprocessing_directive (false))
+ break;
+ }
+ directive_expected = false;
}
- col = 1;
-
- bool skipping = false;
- for (;(c = getChar ()) != -1; col++){
- if (c == '\n'){
- col = 0;
- line++;
- ref_line++;
- skipping = false;
- } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
- continue;
- else if (c != '#')
- skipping = true;
- if (c == '#' && !skipping)
- goto start_again;
+
+ if (c != -1) {
+ tokens_seen = false;
+ continue;
}
- any_token_seen |= tokens_seen;
- tokens_seen = false;
- if (c == -1)
- Report.Error (1027, Location, "#endif/#endregion expected");
- continue;
+
+ return Token.EOF;
}
if (c == '"')
Report.Error (1011, Location, error_details);
return Token.ERROR;
}
+ if (c == '\r' || c == '\n') {
+ Report.Error (1010, Location, "Newline in constant");
+ return Token.ERROR;
+ }
c = escape (c);
if (c == -1)
return Token.ERROR;
// Try to recover, read until newline or next "'"
while ((c = getChar ()) != -1){
- if (c == '\n' || c == '\''){
- line++;
- ref_line++;
- col = 0;
+ if (c == '\n'){
+ break;
+ }
+ else if (c == '\'')
break;
- } else
- col++;
-
}
return Token.ERROR;
}
while ((c = peekChar ()) == ' ')
getChar (); // skip heading whitespaces.
while ((c = peekChar ()) != -1 && c != '\n' && c != '\r') {
- col++;
xml_comment_buffer.Append ((char) getChar ());
}
if (c == '\r' || c == '\n')
xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
}
+ //
+ // Updates current comment location.
+ //
+ private void update_comment_location ()
+ {
+ if (current_comment_location.IsNull) {
+ // "-2" is for heading "//" or "/*"
+ current_comment_location =
+ new Location (ref_line, col - 2);
+ }
+ }
+
//
// Checks if there was incorrect doc comments and raise
// warnings.
//
private void warn_incorrect_doc_comment ()
{
- doc_state = XmlCommentState.Error;
- // in csc, it is 'XML comment is not placed on a valid
- // language element'. But that does not make sense.
- Report.Warning (1587, 2, Location, "XML comment is placed on an invalid language element which can not accept it.");
+ if (doc_state != XmlCommentState.Error) {
+ doc_state = XmlCommentState.Error;
+ // in csc, it is 'XML comment is not placed on
+ // a valid language element'. But that does not
+ // make sense.
+ Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
+ }
}
//
{
if (xml_comment_buffer.Length > 0) {
string ret = xml_comment_buffer.ToString ();
- xml_comment_buffer.Length = 0;
+ reset_doc_comment ();
return ret;
}
return null;
}
+ void reset_doc_comment ()
+ {
+ xml_comment_buffer.Length = 0;
+ current_comment_location = Location.Null;
+ }
+
public void cleanup ()
{
if (ifstack != null && ifstack.Count >= 1) {
+ current_location = new Location (ref_line, Col);
int state = (int) ifstack.Pop ();
if ((state & REGION) != 0)
Report.Error (1038, Location, "#endregion directive expected");
else
- Report.Error (1027, "#endif directive expected");
+ Report.Error (1027, Location, "Expected `#endif' directive");
}
-
}
}
Error
}
}
+