Browse Source

2008-10-09 Marek Safar <[email protected]>

	* cs-tokenizer.cs, cs-parser.jay: Fixed more subtle parser problems


svn path=/trunk/mcs/; revision=115323
Marek Safar 17 years ago
parent
commit
9c51479719
3 changed files with 66 additions and 49 deletions
  1. 4 0
      mcs/mcs/ChangeLog
  2. 53 39
      mcs/mcs/cs-parser.jay
  3. 9 10
      mcs/mcs/cs-tokenizer.cs

+ 4 - 0
mcs/mcs/ChangeLog

@@ -1,3 +1,7 @@
+2008-10-09  Marek Safar  <[email protected]>
+
+	* cs-tokenizer.cs, cs-parser.jay: Fixed more subtle parser problems
+
 2008-10-08  Marek Safar  <[email protected]>
 
 	* cs-tokenizer.cs, eval.cs, anonymous.cs, statement.cs, class.cs

+ 53 - 39
mcs/mcs/cs-parser.jay

@@ -148,7 +148,6 @@ namespace Mono.CSharp
 %token ABSTRACT	
 %token AS
 %token ADD
-%token ASSEMBLY
 %token BASE	
 %token BOOL	
 %token BREAK	
@@ -280,6 +279,7 @@ namespace Mono.CSharp
 %token DIV
 %token CARRET
 %token INTERR
+%token EXTERN_ALIAS
 
 /* C# multi-character operators. */
 %token DOUBLE_COLON
@@ -387,12 +387,12 @@ extern_alias_directives
 	;
 
 extern_alias_directive
-	: EXTERN IDENTIFIER IDENTIFIER SEMICOLON
+	: EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
 	  {
 		LocatedToken lt = (LocatedToken) $2;
 		string s = lt.Value;
 		if (s != "alias"){
-			Report.Error (1003, lt.Location, "'alias' expected");
+			syntax_error (lt.Location, "`alias' expected");
 		} else if (RootContext.Version == LanguageVersion.ISO_1) {
 			Report.FeatureIsNotAvailable (lt.Location, "external alias");
 		} else {
@@ -400,6 +400,10 @@ extern_alias_directive
 			current_namespace.AddUsingExternalAlias (lt.Value, lt.Location);
 		}
 	  }
+	| EXTERN_ALIAS error
+	  {
+	  	syntax_error (GetLocation ($1), "`alias' expected");   // TODO: better
+	  }
 	;
  
 using_directives
@@ -580,8 +584,8 @@ namespace_member_declaration
 
 type_declaration
 	: class_declaration		
-	| struct_declaration		
-	| interface_declaration		
+	| struct_declaration
+	| interface_declaration
 	| enum_declaration		
 	| delegate_declaration
 //
@@ -697,7 +701,7 @@ attribute_section
 	  {
 		$$ = $3;
  	  }
-	  | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
+	| OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
 	  {
 		$$ = $2;
 	  }
@@ -845,12 +849,12 @@ named_argument_list
 
 		$$ = args;
 	  }
-	  | named_argument_list COMMA expression
-	    {
-		  Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
-		  $$ = null;
-		}
-        ;
+	| named_argument_list COMMA expression
+	  {
+		Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
+		$$ = null;
+	  }
+	;
 
 named_argument
 	: IDENTIFIER ASSIGN expression
@@ -916,6 +920,7 @@ struct_declaration
 	  }
 	  struct_body
 	  {
+		--lexer.parsing_declaration;	  
 		if (RootContext.Documentation != null)
 			Lexer.doc_state = XmlCommentState.Allowed;
 	  }
@@ -1151,10 +1156,8 @@ local_variable_declarator
 	  {
 		$$ = new VariableDeclaration ((LocatedToken) $1, null);
 	  }
-	| IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
+	| IDENTIFIER variable_bad_array
 	  {
-		Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
-			"To declare a fixed size buffer field, use the fixed keyword before the field type");
 		$$ = null;
 	  }
 	;
@@ -1162,7 +1165,7 @@ local_variable_declarator
 local_variable_initializer
 	: expression
 	| array_initializer
-	| STACKALLOC type_expression OPEN_BRACKET expression CLOSE_BRACKET
+	| STACKALLOC simple_type OPEN_BRACKET expression CLOSE_BRACKET
 	  {
 		$$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
 	  }
@@ -1170,14 +1173,13 @@ local_variable_initializer
 	  {
 		$$ = new ArglistAccess ((Location) $1);
 	  }
-	| STACKALLOC type
+	| STACKALLOC simple_type
 	  {
 		Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
-                $$ = null;
+		$$ = new StackAlloc ((Expression) $2, null, (Location) $1);		
 	  }
 	;
 
-
 variable_declarators
 	: variable_declarator 
 	  {
@@ -1210,14 +1212,20 @@ variable_declarator
 	  	lexer.parsing_generic_declaration = false;
 		$$ = new VariableMemberDeclaration ((MemberName) $1, null);
 	  }
-	| member_declaration_name OPEN_BRACKET opt_expression CLOSE_BRACKET
+	| member_declaration_name variable_bad_array
 	  {
 		lexer.parsing_generic_declaration = false;	  
-		Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
-			"To declare a fixed size buffer field, use the fixed keyword before the field type");
 		$$ = null;
 	  }
 	;
+	
+variable_bad_array
+	: OPEN_BRACKET opt_expression CLOSE_BRACKET
+	  {
+		Report.Error (650, GetLocation ($1), "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
+			"To declare a fixed size buffer field, use the fixed keyword before the field type");
+	  }
+	;
 
 variable_initializer
 	: expression
@@ -1802,6 +1810,7 @@ interface_declaration
 	  }
 	  interface_body
 	  {
+		--lexer.parsing_declaration;	  
 		if (RootContext.Documentation != null)
 			Lexer.doc_state = XmlCommentState.Allowed;
 	  }
@@ -2758,10 +2767,23 @@ type
 		$$ = TypeManager.system_void_expr;
 	  }	
 	;
+	
+simple_type
+	: type_expression
+	| VOID
+	  {
+	  	Expression.Error_VoidInvalidInTheContext (lexer.Location);
+		$$ = TypeManager.system_void_expr;
+	  }	
+	;		
 
 type_expression_or_array
 	: type_expression
-	| array_type
+	| type_expression rank_specifiers
+	  {
+		string rank_specifiers = (string) $2;
+		$$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
+	  }
 	;
 	
 type_expression
@@ -2891,14 +2913,6 @@ integral_type
 	| CHAR		{ $$ = TypeManager.system_char_expr; }
 	;
 
-array_type
-	: type_expression rank_specifiers
-	  {
-		string rank_specifiers = (string) $2;
-		$$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
-	  }
-	;
-
 predefined_type
 	: builtin_types
 	| VOID
@@ -3293,7 +3307,7 @@ post_decrement_expression
 	;
 
 object_or_delegate_creation_expression
-	: NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
+	: NEW simple_type OPEN_PARENS opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
 	  {
 		if ($6 != null) {
 			if (RootContext.Version <= LanguageVersion.ISO_2)
@@ -3304,7 +3318,7 @@ object_or_delegate_creation_expression
 		else
 			$$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
 	  }
-	| NEW type object_or_collection_initializer
+	| NEW simple_type object_or_collection_initializer
 	  {
 		if (RootContext.Version <= LanguageVersion.ISO_2)
 			Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
@@ -3314,13 +3328,13 @@ object_or_delegate_creation_expression
 	;
 
 array_creation_expression
-	: NEW type_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
+	: NEW simple_type OPEN_BRACKET expression_list CLOSE_BRACKET 
 	  opt_rank_specifier
 	  opt_array_initializer
 	  {
 		$$ = new ArrayCreation ((FullNamedExpression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
 	  }
-	| NEW type_expression rank_specifiers array_initializer
+	| NEW simple_type rank_specifiers array_initializer
 	  {
 		$$ = new ArrayCreation ((FullNamedExpression) $2, (string) $3, (ArrayList) $4, (Location) $1);
 	  }
@@ -3333,7 +3347,7 @@ array_creation_expression
 		Report.Error (1031, (Location) $1, "Type expected");
 		$$ = null;
 	  }          
-	| NEW type_expression error
+	| NEW simple_type error
 	  {
 		Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
 		$$ = null;
@@ -4091,6 +4105,7 @@ class_declaration
 	  }
 	  class_body
 	  {
+		--lexer.parsing_declaration;	  
 		if (RootContext.Documentation != null)
 			Lexer.doc_state = XmlCommentState.Allowed;
 	  }
@@ -5533,6 +5548,7 @@ interactive_parsing
 		method.Block = (ToplevelBlock) end_block(lexer.Location);
 		current_container.AddMethod (method);
 
+		--lexer.parsing_declaration;
 		InteractiveResult = pop_current_class ();
 		current_local_parameters = null;
 	  } 
@@ -5649,7 +5665,7 @@ void push_current_class (TypeContainer tc, object partial_token)
 	else
 		current_container = current_container.AddTypeContainer (tc);
 
-
+	++lexer.parsing_declaration;
 	current_class = tc;
 }
 
@@ -6026,8 +6042,6 @@ static public string GetTokenName (int token)
 		return "as";
 	case Token.ADD:
 		return "add";
-	case Token.ASSEMBLY:
-		return "assembly";
 	case Token.BASE:
 		return "base";
 	case Token.BREAK:

+ 9 - 10
mcs/mcs/cs-tokenizer.cs

@@ -38,7 +38,6 @@ namespace Mono.CSharp
 		int current_token;
 		bool handle_get_set = false;
 		bool handle_remove_add = false;
-		bool handle_assembly = false;
 		bool handle_where = false;
 		bool handle_typeof = false;
 		bool lambda_arguments_parsing;
@@ -58,6 +57,12 @@ namespace Mono.CSharp
 		// Set when parsing generic declaration (type or method header)
 		//
 		public bool parsing_generic_declaration;
+		
+		//
+		// The value indicates that we have not reach any declaration or
+		// namespace yet
+		//
+		public int parsing_declaration;
 
 		//
 		// The special character to inject on streams to trigger the EXPRESSION_PARSE
@@ -153,11 +158,6 @@ namespace Mono.CSharp
 			set { handle_get_set = value; }
                 }
 
-		public bool AssemblyTargetParsing {
-			get { return handle_assembly; }
-			set { handle_assembly = value; }
-		}
-
 		public bool EventParsing {
 			get { return handle_remove_add; }
 			set { handle_remove_add = value; }
@@ -333,7 +333,6 @@ namespace Mono.CSharp
 			AddKeyword ("abstract", Token.ABSTRACT);
 			AddKeyword ("as", Token.AS);
 			AddKeyword ("add", Token.ADD);
-			AddKeyword ("assembly", Token.ASSEMBLY);
 			AddKeyword ("base", Token.BASE);
 			AddKeyword ("bool", Token.BOOL);
 			AddKeyword ("break", Token.BREAK);
@@ -467,9 +466,9 @@ namespace Mono.CSharp
 				return -1;
 			if (!handle_remove_add && (res == Token.REMOVE || res == Token.ADD))
 				return -1;
-			if (!handle_assembly && res == Token.ASSEMBLY)
-				return -1;
-			
+			if (parsing_declaration == 0 && res == Token.EXTERN)
+				return Token.EXTERN_ALIAS;
+
 			//
 			// A query expression is any expression that starts with `from identifier'
 			// followed by any token except ; , =