// cs-parser.jay: The Parser for the C# compiler
//
// Author: Miguel de Icaza (miguel@gnu.org)
+// Ravi Pratap (ravi@ximian.com)
//
// Licensed under the terms of the GNU GPL
//
%nonassoc HIGHPREC
%start compilation_unit
-/*%start namespace_declaration */
%%
compilation_unit
- : opt_using_directives opt_namespace_member_declarations opt_attributes opt_EOF
- {
- // Check that using comes only before namespace elements
- }
- ;
-
+ : outer_declarations opt_EOF
+ | outer_declarations attribute_sections opt_EOF
+ | attribute_sections opt_EOF
+ ;
+
opt_EOF
: /* empty */
| EOF
;
+outer_declarations
+ : outer_declaration
+ | outer_declarations outer_declaration
+ ;
+
+outer_declaration
+ : using_directive
+ | namespace_member_declaration
+ ;
+
using_directives
: using_directive
| using_directives using_directive
}
;
-// namespace_declarations
-// : namespace_declaration
-// | namespace_declarations namespace_declaration
-
+//
+// Strictly speaking, namespaces don't have attributes but
+// we parse global attributes along with namespace declarations and then
+// detach them
+//
namespace_declaration
- : NAMESPACE qualified_identifier
- {
- current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $2);
- }
+ : opt_attributes NAMESPACE qualified_identifier
+ {
+ Attributes attrs = (Attributes) $1;
+
+ if (attrs != null) {
+ foreach (AttributeSection asec in attrs.AttributeSections)
+ if (asec.Target == "assembly")
+ RootContext.AddGlobalAttribute (asec, lexer.Location);
+ }
+
+ current_namespace = RootContext.Tree.RecordNamespace (current_namespace, name, (string) $3);
+ }
namespace_body opt_semicolon
{
current_namespace = current_namespace.Parent;
//
// Attributes 17.2
//
+
opt_attributes
- : /* empty */ { $$ = null; }
- | attribute_section opt_attributes
+ : /* empty */
+ | attribute_sections { $$ = $1; }
+ ;
+
+attribute_sections
+ : attribute_section
+ {
+ AttributeSection sect = (AttributeSection) $1;
+
+ if (sect.Target == "assembly")
+ RootContext.AddGlobalAttribute (sect, lexer.Location);
+
+ $$ = new Attributes ((AttributeSection) $1, lexer.Location);
+ }
+ | attribute_sections attribute_section
{
Attributes attrs = null;
- AttributeSection sect = (AttributeSection) $1;
+ AttributeSection sect = (AttributeSection) $2;
- if (sect.Target == "assembly"){
- RootContext.AddGlobalAttributes (sect, lexer.Location);
- sect = null;
- }
+ if (sect.Target == "assembly")
+ RootContext.AddGlobalAttribute (sect, lexer.Location);
- if ($2 != null) {
- if (sect != null){
- attrs = (Attributes) $2;
- attrs.AddAttribute (sect);
- }
- } else {
- if (sect != null)
- attrs = new Attributes (sect, lexer.Location);
+ if ($1 != null) {
+ attrs = (Attributes) $1;
+ attrs.AddAttribute (sect);
}
$$ = attrs;
(Attributes) $1, lexer.Location);
current_container = new_struct;
current_container.Namespace = current_namespace;
- RootContext.Tree.RecordStruct (full_struct_name, new_struct);
+ RootContext.Tree.RecordDecl (full_struct_name, new_struct);
}
opt_struct_interfaces
struct_body
Struct new_struct = (Struct) current_container;
current_container = current_container.Parent;
- CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
+ CheckDef (current_container.AddStruct (new_struct), new_struct.Name, new_struct.Location);
$$ = new_struct;
}
;
SEMICOLON
{
foreach (VariableDeclaration constant in (ArrayList) $5){
+ Location l = constant.Location;
+
Const c = new Const (
(string) $4, (string) constant.identifier,
- (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
- constant.Location);
+ (Expression) constant.expression_or_array_initializer, (int) $2,
+ (Attributes) $1, l);
- CheckDef (current_container.AddConstant (c), c.Name);
+ CheckDef (current_container.AddConstant (c), c.Name, l);
}
}
;
int mod = (int) $2;
foreach (VariableDeclaration var in (ArrayList) $4){
+ Location l = var.Location;
+
Field field = new Field (type, mod, var.identifier,
- var.expression_or_array_initializer, (Attributes) $1, var.Location);
+ var.expression_or_array_initializer,
+ (Attributes) $1, l);
- CheckDef (current_container.AddField (field), field.Name);
+ CheckDef (current_container.AddField (field), field.Name, l);
}
}
;
}
method.Block = (Block) $2;
- CheckDef (current_container.AddMethod (method), method.Name);
+ CheckDef (current_container.AddMethod (method), method.Name, method.Location);
current_local_parameters = null;
}
if (pair.Second != null)
set_block = (Block) pair.Second;
+ Location loc = (Location) $6;
prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
- (Attributes) $1, (Location) $6);
+ (Attributes) $1, loc);
- CheckDef (current_container.AddProperty (prop), prop.Name);
+ CheckDef (current_container.AddProperty (prop), prop.Name, loc);
implicit_value_parameter_type = null;
}
;
accessor_body
: block
- | SEMICOLON { $$ = new Block (null); }
+ | SEMICOLON { $$ = null; }
;
interface_declaration
}
current_interface = new_interface;
new_interface.Namespace = current_namespace;
- RootContext.Tree.RecordInterface (full_interface_name, new_interface);
+ RootContext.Tree.RecordDecl (full_interface_name, new_interface);
}
opt_interface_base
interface_body opt_semicolon
new_interface.Bases = (ArrayList) $6;
current_interface = null;
- CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
+ CheckDef (current_container.AddInterface (new_interface),
+ new_interface.Name, new_interface.Location);
}
;
{
InterfaceMethod m = (InterfaceMethod) $1;
- CheckDef (current_interface.AddMethod (m), m.Name);
+ CheckDef (current_interface.AddMethod (m), m.Name, m.Location);
}
| interface_property_declaration
{
InterfaceProperty p = (InterfaceProperty) $1;
- CheckDef (current_interface.AddProperty (p), p.Name);
+ CheckDef (current_interface.AddProperty (p), p.Name, p.Location);
}
| interface_event_declaration
{
InterfaceEvent e = (InterfaceEvent) $1;
- CheckDef (current_interface.AddEvent (e), e.Name);
+ CheckDef (current_interface.AddEvent (e), e.Name, lexer.Location);
}
| interface_indexer_declaration
{
InterfaceIndexer i = (InterfaceIndexer) $1;
- CheckDef (current_interface.AddIndexer (i), "indexer");
+ CheckDef (current_interface.AddIndexer (i), "indexer", i.Location);
}
;
: opt_attributes
opt_modifiers
constructor_declarator
- block
+ constructor_body
{
Constructor c = (Constructor) $3;
c.Block = (Block) $4;
if ((c.ModFlags & Modifiers.STATIC) != 0){
if ((c.ModFlags & Modifiers.Accessibility) != 0) {
- Location l = lexer.Location;
- Report.Error (515, l, "Access modifiers are not allowed on static constructors");
+ Report.Error (
+ 515, c.Location,
+ "Access modifiers are not allowed on static constructors");
}
if (c.Initializer != null){
- Location l = lexer.Location;
- Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
+ Report.Error (
+ 514, c.Location,
+ "Static constructors can not have an explicit this or base " +
+ "constructor invocations");
}
if (!c.Parameters.Empty){
- Location l = lexer.Location;
- Report.Error (132, l, "Static constructors should not have parameters");
+ Report.Error (
+ 132, c.Location, "Static constructors should not have parameters");
}
}
- CheckDef (current_container.AddConstructor (c), c.Name);
+ CheckDef (current_container.AddConstructor (c), c.Name, c.Location);
current_local_parameters = null;
}
}
;
+constructor_body
+ : block
+ | SEMICOLON { $$ = null; }
+ ;
+
opt_constructor_initializer
: /* empty */ { $$ = null; }
| constructor_initializer
Location l = lexer.Location;
Method d = new Method (
- "System.Void", 0, "Finalize",
+ "System.Void",
+ Modifiers.PROTECTED | Modifiers.OVERRIDE,
+ "Finalize",
new Parameters (null, null, l), (Attributes) $1, l);
d.Block = (Block) $6;
- CheckDef (current_container.AddMethod (d), d.Name);
+ CheckDef (current_container.AddMethod (d), d.Name, d.Location);
}
}
;
Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
(int) $2, null, null, (Attributes) $1, lexer.Location);
- CheckDef (current_container.AddEvent (e), e.Name);
+ CheckDef (current_container.AddEvent (e), e.Name, e.Location);
}
}
{
implicit_value_parameter_type = (string) $4;
lexer.EventParsing = true;
+ oob_stack.Push (lexer.Location);
}
event_accessor_declarations
{
}
CLOSE_BRACE
{
+ Location loc = (Location) oob_stack.Pop ();
+
Pair pair = (Pair) $8;
Block add_block = null;
Block rem_block = null;
rem_block = (Block) pair.Second;
Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
- (Attributes) $1, lexer.Location);
+ (Attributes) $1, loc);
- CheckDef (current_container.AddEvent (e), e.Name);
+ CheckDef (current_container.AddEvent (e), e.Name, loc);
implicit_value_parameter_type = null;
}
;
enum_declaration
: opt_attributes
opt_modifiers
- ENUM IDENTIFIER
+ ENUM IDENTIFIER
opt_enum_base
enum_body
opt_semicolon
{
+ Location enum_location = lexer.Location;
+
string full_name = MakeName ((string) $4);
- Enum e = new Enum (current_container, (string) $5, (int) $2, full_name, (Attributes) $1, lexer.Location);
+ Enum e = new Enum (
+ current_container, (string) $5, (int) $2, full_name,
+ (Attributes) $1, enum_location);
foreach (VariableDeclaration ev in (ArrayList) $6){
+ Location loc = (Location) ev.Location;
+
CheckDef (e.AddEnumMember (ev.identifier,
(Expression) ev.expression_or_array_initializer,
- (Location) ev.Location),
- ev.identifier);
+ loc),
+ ev.identifier, loc);
}
e.Namespace = current_namespace;
- CheckDef (current_container.AddEnum (e), full_name);
- RootContext.Tree.RecordEnum (full_name, e);
+ CheckDef (current_container.AddEnum (e), full_name, enum_location);
+ RootContext.Tree.RecordDecl (full_name, e);
}
;
CLOSE_PARENS
SEMICOLON
{
- Delegate del = new Delegate ((string) $4, (int) $2,
+ Location l = lexer.Location;
+ Delegate del = new Delegate (current_container, (string) $4, (int) $2,
MakeName ((string) $5), (Parameters) $7,
- (Attributes) $1, lexer.Location);
+ (Attributes) $1, l);
- CheckDef (current_container.AddDelegate (del), del.Name);
+ del.Namespace = current_namespace;
+ CheckDef (current_container.AddDelegate (del), del.Name, l);
}
| opt_attributes
opt_modifiers
CLOSE_PARENS
SEMICOLON
{
- Delegate del = new Delegate ("System.Void", (int) $2, (string) $5, (Parameters) $7,
- (Attributes) $1, lexer.Location);
+ Location l = lexer.Location;
+ Delegate del = new Delegate (
+ current_container,
+ "System.Void", (int) $2, MakeName ((string) $5), (Parameters) $7,
+ (Attributes) $1, l);
- CheckDef (current_container.AddDelegate (del), del.Name);
+ del.Namespace = current_namespace;
+ CheckDef (current_container.AddDelegate (del), del.Name, l);
}
;
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.Multiply,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.Multiply,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_DIV_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.Division,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.Division,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_MOD_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.Modulus,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.Modulus,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_ADD_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.Addition,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.Addition,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_SUB_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.Subtraction,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.Subtraction,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.LeftShift,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.LeftShift,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.RightShift,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.RightShift,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_AND_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.BitwiseAnd,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.BitwiseAnd,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_OR_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.BitwiseOr,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.BitwiseOr,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
| prefixed_unary_expression OP_XOR_ASSIGN expression
{
Location l = lexer.Location;
- $$ = new Assign ((Expression) $1,
- new Binary (Binary.Operator.ExclusiveOr,
- (Expression) $1,
- (Expression) $3, l), l);
+ $$ = new CompoundAssign (
+ (Expression) $1,
+ new Binary (Binary.Operator.ExclusiveOr,
+ (Expression) $1,
+ (Expression) $3, l),
+ (Expression) $3, l);
}
;
(Attributes) $1, lexer.Location);
current_container = new_class;
current_container.Namespace = current_namespace;
- RootContext.Tree.RecordClass (name, new_class);
+ RootContext.Tree.RecordDecl (name, new_class);
}
opt_class_base
class_body
new_class.Bases = (ArrayList) $6;
current_container = current_container.Parent;
- CheckDef (current_container.AddClass (new_class), new_class.Name);
+ CheckDef (current_container.AddClass (new_class), new_class.Name, new_class.Location);
$$ = new_class;
}
block
: OPEN_BRACE
{
- current_block = new Block (current_block);
+ current_block = new Block (current_block, lexer.Location, Location.Null);
}
opt_statement_list CLOSE_BRACE
{
while (current_block.Implicit)
current_block = current_block.Parent;
$$ = current_block;
+ current_block.SetEndLocation (lexer.Location);
current_block = current_block.Parent;
}
;
// because statement_expression is used for example in for_statement
//
statement_expression
- : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
- | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
- | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
- | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
- | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
- | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
- | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
+ : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+ | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+ | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+ | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+ | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+ | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
+ | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
| error {
Report.Error (1002, lexer.Location, "Expecting `;'");
}
Assign a = new Assign (var, expr, decl.Location);
- assign_block.AddStatement (new StatementExpression (a));
+ assign_block.AddStatement (new StatementExpression (a, lexer.Location));
}
}
vars.Add (new DictionaryEntry (var, expr));
// Assign a = new Assign (var, expr, decl.Location);
- // assign_block.AddStatement (new StatementExpression (a));
+ // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
}
$3 = new DictionaryEntry (type, vars);
}
// in the current declaration space
// </summary>
void
-CheckDef (AdditionResult result, string name)
+CheckDef (AdditionResult result, string name, Location l)
{
if (result == AdditionResult.Success)
return;
- Location l = lexer.Location;
-
switch (result){
case AdditionResult.NameExists:
- Report.Error (102, l, "The namespace `" + current_container.Name +
+ Report.Error (102, l, "The container `" + current_container.Name +
"' already contains a definition for `"+
name + "'");
break;
}
void
-CheckDef (bool result, string name)
+CheckDef (bool result, string name, Location l)
{
if (result)
return;
- CheckDef (AdditionResult.NameExists, name);
+ CheckDef (AdditionResult.NameExists, name, l);
}
//
// as `expression' is allowed in argument_lists, which
// do not exist inside a block.
//
-#if BLAH
- if (current_block != null){
- if (current_block.IsVariableDefined (name)){
- return new LocalVariableReference (current_block, name, loc);
- }
- }
-#endif
if (current_local_parameters != null){
int idx;
// int j = 1; int k = j + 1;
//
if (current_block.Used)
- implicit_block = new Block (current_block, true);
+ implicit_block = new Block (current_block, true, loc, Location.Null);
else
implicit_block = current_block;
assign = new Assign (var, expr, decl.Location);
- implicit_block.AddStatement (new StatementExpression (assign));
+ implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
}
return implicit_block;
public override int parse ()
{
- StringBuilder value = new StringBuilder ();
-
global_errors = 0;
try {
if (yacc_verbose_flag)