Commit 464f5fbb authored by Johnny Jazeix's avatar Johnny Jazeix

Ajout des erreurs dans la fenetre ainsi que dans un fichier. Debut de la

partie raccourcis dans le manuel user
parent 313b4780
#include "Errors.hpp"
using namespace std;
using namespace message_ns;
std::queue<std::string> Error::_errors;
std::queue<std::string> Error::_warnings;
const string Error::_EXPECT_END_DEF = "expected %EndEventDef";
const string Error::_EXPECT_EVENT_DEF = "expected %EventDef";
const string Error::_EXPECT_NAME_DEF = "the definition is not named";
const string Error::_EXPECT_ID_DEF = "the definition is not identified";
const string Error::_EXTRA_TOKEN_IN_DEF = "there are too much token";
const string Error::_UNKNOWN_EVENT = "unknown fields on event ";
const string Error::_FIELD_TYPE_MISSING = "a field type is missing ";
const string Error::_FIELD_TYPE_UNKNOWN = "a field type is unknown ";
const string Error::_EMPTY_DEF = "a definition line is empty";
const string Error::_INCOMPATIBLE_VALUE_IN_EVENT = "incompatible value : ";
const string Error::_UNKNOWN_TYPE_IN_EVENT = "unknown type : ";
const string Error::_UNKNOWN_EVENT_IN_PARSER_DEF_NOT_FOUND = "unknown field for the event. Should have been found on the definition parser : ";
string Error::_content = "";
void Error::set_error(string kind_of_error, unsigned int line_number){
//_number_of_errors ++;
char line[10];
sprintf(line, "%d", line_number);
Error::_content = kind_of_error + " on line " + line;
Error::_errors.push(Error::_content);
}
void Error::set_warning(string kind_of_error, unsigned int line_number){
//_number_of_warnings ++;
char line[10];
sprintf(line, "%d", line_number);
Error::_content = kind_of_error + " on line " + line;
Error::_warnings.push(Error::_content);
}
void Error::set_and_print_error(string kind_of_error, unsigned int line_number){
set_error(kind_of_error, line_number);
print_error();
}
void Error::set_and_print_warning(string kind_of_error, unsigned int line_number){
set_warning(kind_of_error, line_number);
print_warning();
}
bool Error::set_error_if(bool condition, string kind_of_error, unsigned int line_number){
if(condition){
set_error(kind_of_error, line_number);
return true;
}
return false;
}
bool Error::set_warning_if(bool condition, string kind_of_error, unsigned int line_number){
if(condition){
set_warning(kind_of_error, line_number);
return true;
}
return false;
}
void Error::print_error(){
message << _content << ende;
}
void Error::print_warning(){
message << _content << endw;
}
bool Error::set_and_print_error_if(bool condition, string kind_of_error, unsigned int line_number){
if(condition){
set_error(kind_of_error, line_number);
print_error();
return true;
}
return false;
}
bool Error::set_and_print_warning_if(bool condition, string kind_of_error, unsigned int line_number){
if(condition){
set_warning(kind_of_error, line_number);
print_warning();
return true;
}
return false;
}
void Error::print_numbers_of_warning_and_errors(){
message << "Your trace has " << Error::_errors.size() << " errors and " << Error::_warnings.size() << " warnings." << endi;
}
void Error::flush_in_file(const string &filename){
if(_errors.empty() && _warnings.empty()){
return;
}
else{
ofstream outfile(filename.c_str(), ios::out | ios::trunc);
const int number_of_errors = Error::_errors.size();
const int number_of_warnings = Error::_warnings.size();
if(!outfile.is_open()){
cerr << "unable to open " << filename << " to print the errors encountered in the file opening" << endl;
return;
}
else{
outfile << "File " << filename << endl << endl;
outfile << "Errors :" << endl;
// Print the errors
while(!_errors.empty()){
outfile << _errors.front() << endl;
_errors.pop();
}
// Print the warnings
outfile << endl << "Warnings :" << endl;
while(!_warnings.empty()){
outfile << _warnings.front() << endl;
_warnings.pop();
}
outfile << endl << "Your trace has " << number_of_errors << " errors and " << number_of_warnings << " warnings." << endl;
outfile.close();
}
}
}
#ifndef ERRORS_HPP
#define ERRORS_HPP
/**
* This file contains all the significations of the errors or warnings that can be thrown by the parser
*
**/
#include <string>
#include <queue>
#include <iostream>
#include <fstream>
#include "../message/message_ns.hpp"
class Error{
private:
Error();
static int _line_error;
static std::queue<std::string> _errors;
static std::queue<std::string> _warnings;
static unsigned int _number_of_warnings;
static unsigned int _number_of_errors;
static std::string _content;
public:
const static std::string _EXPECT_END_DEF;
const static std::string _EXPECT_EVENT_DEF;
const static std::string _EXPECT_NAME_DEF;
const static std::string _EXPECT_ID_DEF;
const static std::string _EXTRA_TOKEN_IN_DEF;
const static std::string _UNKNOWN_EVENT;
const static std::string _FIELD_TYPE_MISSING;
const static std::string _FIELD_TYPE_UNKNOWN;
const static std::string _EMPTY_DEF;
const static std::string _INCOMPATIBLE_VALUE_IN_EVENT;
const static std::string _UNKNOWN_TYPE_IN_EVENT;
const static std::string _UNKNOWN_EVENT_IN_PARSER_DEF_NOT_FOUND;
static void set_error(std::string kind_of_error, unsigned int line_number);
static void set_warning(std::string kind_of_error, unsigned int line_number);
static void set_and_print_error(std::string kind_of_error, unsigned int line_number);
static void set_and_print_warning(std::string kind_of_error, unsigned int line_number);
static void print_error();
static void print_warning();
static bool set_error_if(bool condition, std::string kind_of_error, unsigned int line_number);
static bool set_warning_if(bool condition, std::string kind_of_error, unsigned int line_number);
static bool set_and_print_error_if(bool condition, std::string kind_of_error, unsigned int line_number);
static bool set_and_print_warning_if(bool condition, std::string kind_of_error, unsigned int line_number);
static void print_numbers_of_warning_and_errors();
static void flush_in_file(const std::string &filename);
};
#endif // ERRORS_HPP
......@@ -2,14 +2,8 @@
#define PARSER_HPP
#include <string>
#include "../message/message_ns.hpp"
#define WARN_IF(condition, message2) if (condition) \
message << message2 << endw;
#define DIE_IF(condition, message2) if (condition){ \
message << message2 << ende; \
return;}
#include "Errors.hpp"
/*!
* \class Parser Parser.hpp "../parser/src/Parser.hpp"
......
#include "ParserDefinitionDecoder.hpp"
using namespace std;
using namespace message_ns;
ParserDefinitionDecoder::ParserDefinitionDecoder(){
_state = _OUT_A_DEFINITION;
......@@ -12,20 +11,36 @@ int ParserDefinitionDecoder::definitions_number(){
}
void ParserDefinitionDecoder::enter_definition(Line &line){
DIE_IF(_state == _IN_A_DEFINITION, "Error : expected %EndEventDef");
Error::set_and_print_error_if(_state == _IN_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count());
string definition_name;
DIE_IF(!line.item(2, definition_name), "Error : a definition need a name");
Error::set_and_print_error_if(!line.item(2, definition_name), Error::_EXPECT_NAME_DEF, line.get_line_count());
//DIE_IF(!line.item(2, definition_name), "Error : a definition need a name");
unsigned int definition_identity;
string definition_identity_string;
DIE_IF(!line.item(3, definition_identity_string), "Error : definition has no identifier");
DIE_IF(sscanf(definition_identity_string.c_str(), "%d", &definition_identity) != 1, "Error : expected identifier for a definition");
if(Error::set_and_print_error_if(!line.item(3, definition_identity_string), Error::_EXPECT_ID_DEF, line.get_line_count())){
return;
}
//DIE_IF(!line.item(3, definition_identity_string), "Error : definition has no identifier");
if(Error::set_and_print_error_if(sscanf(definition_identity_string.c_str(), "%d", &definition_identity) != 1,
Error::_EXPECT_ID_DEF, line.get_line_count())){
return;
}
//DIE_IF(sscanf(definition_identity_string.c_str(), "%d", &definition_identity) != 1, "Error : expected identifier for a definition");
Definition current_definition = Definition(definition_name);
_definitions.insert(pair<unsigned int,Definition>(definition_identity, current_definition));
_current_definition = definition_identity;
WARN_IF((line.length() > 4), "Warning : extra token in %EventDef");
Error::set_and_print_warning_if(line.length() > 4, Error::_EXTRA_TOKEN_IN_DEF, line.get_line_count());
//WARN_IF((line.length() > 4), Error::_EXPECT_EVENT_DEF + line_number);
//"Warning : extra token in %EventDef on line << line_number << endl");
_state = _IN_A_DEFINITION;
return;
......@@ -33,24 +48,44 @@ void ParserDefinitionDecoder::enter_definition(Line &line){
void ParserDefinitionDecoder::leave_definition(Line &line){
DIE_IF(_state != _IN_A_DEFINITION, "Error : expected %EventDef");
if(Error::set_and_print_error_if(_state != _IN_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count())){
return;
}
// DIE_IF(_state != _IN_A_DEFINITION, "Error : expected %EventDef");
_state = _OUT_A_DEFINITION;
DIE_IF(!_definitions[_current_definition].check_definition(), "Error : unknown event : " + _definitions[_current_definition].get_event_name());
WARN_IF(line.length() > 2, "Warning : extra token in %EventDef");
if(Error::set_and_print_error_if(!_definitions[_current_definition].check_definition(), Error::_UNKNOWN_EVENT + _definitions[_current_definition].get_event_name(), line.get_line_count())){
return;
}
//DIE_IF(!_definitions[_current_definition].check_definition(), "Error : unknown event : " + _definitions[_current_definition].get_event_name());
Error::set_and_print_warning_if(line.length() > 2, Error::_EXTRA_TOKEN_IN_DEF, line.get_line_count());
//WARN_IF(line.length() > 2, "Warning : extra token in %EventDef");
}
void ParserDefinitionDecoder::add_field_to_definition(std::string& first_token,Line& line){
DIE_IF(_state == _OUT_A_DEFINITION, "Error : expected %EventDef2");
if(Error::set_and_print_error_if(_state == _OUT_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count())){
return;
}
//DIE_IF(_state == _OUT_A_DEFINITION, "Error : expected %EventDef");
string field_type;
DIE_IF(!line.item(2, field_type), "Error : a field type is missing");
DIE_IF(field_type != "string" && field_type != "int" &&
field_type != "hex" && field_type != "date" &&
field_type != "double" && field_type != "color"
, "Error : unknown type");
if(Error::set_and_print_error_if(!line.item(2, field_type), Error::_FIELD_TYPE_MISSING, line.get_line_count())){
return;
}
// DIE_IF(!line.item(2, field_type), "Error : a field type is missing");
if(Error::set_and_print_error_if(field_type != "string" && field_type != "int" &&
field_type != "hex" && field_type != "date" &&
field_type != "double" && field_type != "color",
Error::_FIELD_TYPE_UNKNOWN, line.get_line_count())){
return;
}
// DIE_IF(field_type != "string" && field_type != "int" &&
// field_type != "hex" && field_type != "date" &&
// field_type != "double" && field_type != "color"
// , "Error : unknown type");
_definitions[_current_definition].store(first_token, field_type);
}
......@@ -58,7 +93,10 @@ void ParserDefinitionDecoder::add_field_to_definition(std::string& first_token,L
void ParserDefinitionDecoder::store_definition(Line &line){
string first_token;
DIE_IF(!line.item(1,first_token), "Warning : a definition line is empty");
if(Error::set_and_print_warning_if(!line.item(1, first_token), Error::_EMPTY_DEF, line.get_line_count())){
return;
}
//DIE_IF(!line.item(1, first_token), "Warning : a definition line is empty");
if (first_token == "EventDef"){
enter_definition(line);
......
......@@ -7,14 +7,8 @@
#include "Definition.hpp"
#include "Line.hpp"
#include "../message/message_ns.hpp"
#define WARN_IF(condition, message2) if (condition) \
message << message2 << endw;
#define DIE_IF(condition, message2) if (condition){ \
message << message2 << ende; \
return;}
#include "Errors.hpp"
//#include "Macros.hpp"
/*!
*
......
......@@ -35,7 +35,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
string current_value;
if (!line.item(i, current_value)) {
message << "warning : missing values" << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
//message << "warning : missing values" << endl;
return;
}
......@@ -67,7 +68,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
else if (fields[i-1]._name == "Time") {
if(!Date::instantiate(current_value, time)) {
cerr << "warning : incompatible value : " << current_value << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
//cerr << "warning : incompatible value : " << current_value << endl;
return;
}
}
......@@ -83,7 +85,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
else if (fields[i-1]._name == "Value") {
if(fields[i-1]._type == "double") {
if(!Double::instantiate(current_value, value_double)) {
cerr << "warning : incompatible value : " << current_value << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
// cerr << "warning : incompatible value : " << current_value << endl;
return;
}
}
......@@ -112,7 +115,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
else if(fields[i-1]._type == "double") {
Double value;
if(!Double::instantiate(current_value, value)) {
cerr << "warning : incompatible value : " << current_value << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
//cerr << "warning : incompatible value : " << current_value << endl;
return;
}
extra_fields.push_back(&value);
......@@ -120,7 +124,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
else if(fields[i-1]._type == "hex") {
Hex value;
if(!Hex::instantiate(current_value, value)) {
cerr << "warning : incompatible value : " << current_value << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
//cerr << "warning : incompatible value : " << current_value << endl;
return;
}
extra_fields.push_back(&value);
......@@ -128,7 +133,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
else if(fields[i-1]._type == "date") {
Date value;
if(!Date::instantiate(current_value, value)) {
cerr << "warning : incompatible value : " << current_value << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
//cerr << "warning : incompatible value : " << current_value << endl;
return;
}
extra_fields.push_back(&value);
......@@ -136,7 +142,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
else if(fields[i-1]._type == "int") {
Integer value;
if(!Integer::instantiate(current_value, value)) {
cerr << "warning : incompatible value : " << current_value << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
//cerr << "warning : incompatible value : " << current_value << endl;
return;
}
extra_fields.push_back(&value);
......@@ -144,13 +151,15 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
else if(fields[i-1]._type == "color") {
Color value;
if(!Color::instantiate(current_value, value)) {
cerr << "warning : incompatible value : " << current_value << endl;
Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count());
//cerr << "warning : incompatible value : " << current_value << endl;
return;
}
extra_fields.push_back(&value);
}
else {
cerr << "warning : unknown type" << endl;
Error::set_and_print_warning(Error::_UNKNOWN_TYPE_IN_EVENT + current_value, line.get_line_count());
//cerr << "warning : unknown type" << endl;
return;
}
}
......@@ -243,7 +252,8 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T
}
else {
cerr << "BUG PARSER BIS" << endl;
Error::set_and_print_warning(Error::_UNKNOWN_EVENT_IN_PARSER_DEF_NOT_FOUND + event_name, line.get_line_count());
//cerr << "BUG PARSER BIS" << endl;
return;
}
}
......
......@@ -7,7 +7,7 @@
#include "Definition.hpp"
#include "Line.hpp"
#include "../trace/values/Values.hpp"
#include "../message/message_ns.hpp"
#include "Errors.hpp"
//#ifdef DEBUG_PRINT_TRACE
//#include "../../tests/parser/stub/trace.hpp"
......
#include "ParserPaje.hpp"
using namespace std;
using namespace message_ns;
void ParserPaje::parse(string filename, Trace &trace){
......@@ -25,7 +24,11 @@ void ParserPaje::parse(string filename, Trace &trace){
continue; // We have \n
}
else{
DIE_IF(sscanf(event_identity_string.c_str(), "%d", &event_identity) != 1, "expected identifier for a definition");
if(Error::set_and_print_error_if (sscanf(event_identity_string.c_str(), "%d", &event_identity) != 1,
Error::_EXPECT_ID_DEF, line.get_line_count())){
return;
}
// DIE_IF(sscanf(event_identity_string.c_str(), "%d", &event_identity) != 1, "expected identifier for a definition");
parserevent->store_event(
parserdefinition->get_definition(event_identity),
......@@ -34,6 +37,10 @@ void ParserPaje::parse(string filename, Trace &trace){
}
}
// We print the warnings
Error::print_numbers_of_warning_and_errors();
Error::flush_in_file("log.txt");
delete parserdefinition;
delete parserevent;
}
......@@ -9,7 +9,7 @@
#include "ParserEventDecoder.hpp"
#include "Parser.hpp"
#include "Line.hpp"
#include "../message/message_ns.hpp"
#include "Errors.hpp"
//#ifdef DEBUG_PRINT_TRACE
//#include "../../tests/parser/stub/trace.hpp"
......
......@@ -69,6 +69,7 @@ SOURCES += main.cpp \
message/message.cpp \
message/message_ns.cpp \
parser/Definition.cpp \
parser/Errors.cpp \
parser/Line.cpp \
parser/ParserDefinitionDecoder.cpp \
parser/ParserEventDecoder.cpp \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment