[clever] r442 committed - - Improved error messages of method/attribute redefinition.

1 view
Skip to first unread message

cle...@googlecode.com

unread,
May 19, 2011, 12:12:03 PM5/19/11
to cleve...@googlegroups.com
Revision: 442
Author: muriloufg
Date: Thu May 19 09:10:58 2011
Log: - Improved error messages of method/attribute redefinition.
http://code.google.com/p/clever/source/detail?r=442

Modified:
/trunk
/trunk/include/ast.h
/trunk/src/astvisitor.cc
/trunk/src/parser.y

=======================================
--- /trunk/include/ast.h Wed May 18 16:09:13 2011
+++ /trunk/include/ast.h Thu May 19 09:10:58 2011
@@ -946,7 +946,28 @@

class ClassDeclaration : public ASTNode {
public:
- ClassDeclaration(ASTNode* name, ASTNode* body)
+
+ class DeclarationError {
+ public:
+ enum error_type { METHOD_DECLARATION, ATTRIBUTE_DECLARATION };
+
+ DeclarationError() {}
+
+ const std::string& get_identifier() { return m_identifier; }
+ const location& get_location() { return m_location; }
+ error_type get_type() { return m_type; }
+
+ void set_identifier(const std::string& ident) { m_identifier = ident; }
+ void set_location(const location& loc) { m_location = loc; }
+ void set_error_type(error_type type) { m_type = type; }
+
+ private:
+ std::string m_identifier;
+ location m_location;
+ error_type m_type;
+ };
+
+ ClassDeclaration(ASTNode* name, ASTNode* body)
: m_name(name), m_body(body) {
m_name->addRef();
m_body->addRef();
@@ -957,19 +978,24 @@
m_body->delRef();
}

- int check() {
+ bool check(DeclarationError& error) {
ClassStmtList* list = static_cast<ClassStmtList*>(m_body);
std::list<AttributeDeclaration*>& attribs = list->get_attrib_decl();
std::list<AttributeDeclaration*>::const_iterator it;
-
+
std::set<std::string> s;
-
+
for (it = attribs.begin(); it != attribs.end(); ++it) {
- if (s.find((*it)->get_variable()->get_value()->get_name()->str()) ==
s.end()) {
- s.insert((*it)->get_variable()->get_value()->get_name()->str());
+ const std::string& str =
(*it)->get_variable()->get_value()->get_name()->str();
+
+ if (s.find(str) == s.end()) {
+ s.insert(str);
}
else {
- return 1;
+ error.set_error_type(DeclarationError::ATTRIBUTE_DECLARATION);
+ error.set_identifier(str);
+ error.set_location((*it)->get_location());
+ return false;
}
}

@@ -979,15 +1005,20 @@
std::list<MethodDeclaration*>::const_iterator it2;

for (it2 = methods.begin(); it2 != methods.end(); ++it2) {
- if (s.find((*it2)->get_name()->get_value()->get_name()->str()) ==
s.end()) {
- s.insert((*it2)->get_name()->get_value()->get_name()->str());
+ const std::string& str =
(*it2)->get_name()->get_value()->get_name()->str();
+
+ if (s.find(str) == s.end()) {
+ s.insert(str);
}
else {
- return 2;
+ error.set_error_type(DeclarationError::METHOD_DECLARATION);
+ error.set_identifier(str);
+ error.set_location((*it2)->get_location());
+ return false;
}
}

- return 0;
+ return true;
}

void accept(ASTVisitor& visitor) throw() {
=======================================
--- /trunk/src/astvisitor.cc Wed May 18 16:09:13 2011
+++ /trunk/src/astvisitor.cc Thu May 19 09:10:58 2011
@@ -29,7 +29,6 @@
#include "astvisitor.h"
#include "typetable.h"
#include "compiler.h"
-#include <iostream> //debug

namespace clever { namespace ast {

@@ -639,17 +638,22 @@
* Generates opcodes for class declaration
*/
AST_VISITOR(CodeGenVisitor, ClassDeclaration) {
- int check = expr->check();
- const CString* class_name = expr->get_class_name();
-
- if (check == 0) {
-
- }
- else if (check == 1) {
- Compiler::errorf(expr->get_location(), "Attribute redefinition in
class %s", class_name->str().c_str());
+ ClassDeclaration::DeclarationError error;
+
+ bool ok = expr->check(error);
+ const CString* class_name = expr->get_class_name();
+
+ if (ok) {
+ //@TODO
}
else {
- Compiler::errorf(expr->get_location(), "Method redefinition in
class %s", class_name->str().c_str());
+ Compiler::errorf(error.get_location(),
+ "Redefinition of %s `%s' in class `%s'",
+ (error.get_type() ==
ClassDeclaration::DeclarationError::METHOD_DECLARATION) ?
+ "method" : "attribute",
+ error.get_identifier().c_str(),
+ class_name->str().c_str()
+ );
}
}

=======================================
--- /trunk/src/parser.y Wed May 18 16:09:13 2011
+++ /trunk/src/parser.y Thu May 19 09:10:58 2011
@@ -200,7 +200,7 @@
;

class_stmt:
- /* empty */ { $$ = new ast::ClassStmtList; }
+ /* empty */ { $$ = new ast::ClassStmtList;
$$->set_location(yyloc); }
| class_stmt_no_empty { $$ = $1; }
;

@@ -210,11 +210,11 @@
;

method_declaration:
- access_modifier TYPE IDENT '(' args_declaration ')' block_stmt { $$ =
new ast::MethodDeclaration($1, $2, $3, $5, $7); }
+ access_modifier TYPE IDENT '(' args_declaration ')' block_stmt { $$ =
new ast::MethodDeclaration($1, $2, $3, $5, $7); $$->set_location(yyloc); }
;

attribute_declaration:
- access_modifier TYPE IDENT ';' { $$ = new
ast::AttributeDeclaration($1, $2, $3); }
+ access_modifier TYPE IDENT ';' { $$ = new
ast::AttributeDeclaration($1, $2, $3); $$->set_location(yyloc); }
;

arg_list:

Reply all
Reply to author
Forward
0 new messages