Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r1]: trunk / Cm / Cm.Core / Error.cpp Maximize Restore History

Download this file

Error.cpp    120 lines (103 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <Cm.Core/Error.hpp>
#include <iostream>
namespace Cm { namespace Core {
using Soul::Util::ReadFile;
using Soul::Util::IntToString;
String Category(int code)
{
if (code < T0)
{
return "P" + IntToString(code);
}
else if (code < C0)
{
return "T" + IntToString(code - T0);
}
else if (code < A0)
{
return "C" + IntToString(code - C0);
}
else
{
return "A" + IntToString(code - A0);
}
}
Exception::Exception(int number_): number(number_)
{
}
Exception::Exception(int number_, const String& message_): number(number_), message(message_), errorPos()
{
}
Exception::Exception(int number_, const String& message_, const Position& errorPos_): number(number_), message(message_), errorPos(errorPos_)
{
}
TypeCheckException::TypeCheckException(int number_): Exception(number_)
{
}
TypeCheckException::TypeCheckException(int number_, const String& message_): Exception(number_, message_)
{
}
TypeCheckException::TypeCheckException(int number_, const String& message_, const Position& errorPos_): Exception(number_, message_, errorPos_)
{
}
CodeGenException::CodeGenException(int number_): Exception(number_)
{
}
CodeGenException::CodeGenException(int number_, const String& message_): Exception(number_, message_)
{
}
CodeGenException::CodeGenException(int number_, const String& message_, const Position& errorPos_): Exception(number_, message_, errorPos_)
{
}
void Print(const String& title, const String& errorMessage, const Position& errorPos, const std::vector<String>& filenames)
{
if (errorPos.Valid())
{
std::cout << title << " in file " << filenames[errorPos.file] << " at line " << errorPos.line << ":\n" << errorMessage << ":" << std::endl;
String content = ReadFile(filenames[errorPos.file]);
int length = content.length();
int index = errorPos.index;
int lineStart = index;
if (lineStart < length)
{
while (lineStart >= 0 && content[lineStart] != '\n' && content[lineStart] != '\r')
{
--lineStart;
}
++lineStart;
}
int lineEnd = index;
while (lineEnd < length && content[lineEnd] != '\n' && content[lineEnd] != '\r')
{
++lineEnd;
}
std::string errorLine = content.substr(lineStart, lineEnd - lineStart);
std::cout << errorLine << "\n" << std::string(index - lineStart, ' ') << "^" << std::endl;
}
else
{
std::cout << title + ": " << errorMessage << std::endl;
}
}
String GetErrorCodeClass(int code)
{
String cls;
if (code < T0)
{
cls = "P" + IntToString(code - P0);
}
else if (code < C0)
{
cls = "T" + IntToString(code - T0);
}
else if (code < A0)
{
cls = "A" + IntToString(code - C0);
}
return cls;
}
void PrintError(int code, const std::vector<String>& filenames, const Exception& ex)
{
Print("Error " + GetErrorCodeClass(code), ex.Message(), ex.ErrorPos(), filenames);
}
} } // namespoace Cm::Core