Diff of /trunk/Cm/Cm.Core/Error.cpp [000000] .. [r1]  Maximize  Restore

Switch to side-by-side view

--- a
+++ b/trunk/Cm/Cm.Core/Error.cpp
@@ -0,0 +1,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

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks