Česky
Kamil Dudka

Flex/Bison based compiler and interpreter written in C++ (using Boost)

File detail

Name:DownloadvypIO.cc [Download]
Location: vyp08 > vyp08-1.0pre1 > src
Size:5.3 KB
Last modification:2009-07-04 19:51

Source code

/*
 * Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
 *
 * This file is part of vyp08 (compiler and interpreter of VYP08 language).
 *
 * vyp08 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * vyp08 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with vyp08.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include "config.h"
#include "vypIO.h"
 
#ifndef BUILDING_DOX
#   include <string>
#   include <iomanip>
#endif
 
using std::string;
 
namespace StreamDecorator {
    // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Color implementation
    bool Color::useColors = false;
    struct Color::Private {
        EColor            color;
    };
    Color::Color(EColor color):
        d(new Private)
    {
        d->color = color;
    }
    Color::Color(const Color &cObj):
        d(new Private)
    {
        d->color = cObj.d->color;
    }
    Color::~Color() {
        delete d;
    }
    void Color::enable(bool b) {
        Color::useColors = b;
    }
    bool Color::isEnabled() {
        return Color::useColors;
    }
    std::ostream& operator<< (std::ostream &stream, const Color &cObj) {
        if (Color::useColors) {
            static const char ESC = '\033';
            stream << ESC;
            switch (cObj.d->color) {
                case C_NO_COLOR:     stream << "[0m";    break;
                case C_BLUE:         stream << "[0;34m"; break;
                case C_GREEN:        stream << "[0;32m"; break;
                case C_CYAN:         stream << "[0;36m"; break;
                case C_RED:          stream << "[0;31m"; break;
                case C_PURPLE:       stream << "[0;35m"; break;
                case C_BROWN:        stream << "[0;33m"; break;
                case C_LIGHT_GRAY:   stream << "[0;37m"; break;
                case C_DARK_GRAY:    stream << "[1;30m"; break;
                case C_LIGHT_BLUE:   stream << "[1;34m"; break;
                case C_LIGHT_GREEN:  stream << "[1;32m"; break;
                case C_LIGHT_CYAN:   stream << "[1;36m"; break;
                case C_LIGHT_RED:    stream << "[1;31m"; break;
                case C_LIGHT_PURPLE: stream << "[1;35m"; break;
                case C_YELLOW:       stream << "[1;33m"; break;
                case C_WHITE:        stream << "[1;37m"; break;
            }
        }
        return stream;
    }
 
    // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Error implementation
    struct Error::Private {
        EError      type;
        string      file;
        string      msg;
        int         lineno;
        string      category;
        Token       token;
    };
    Error::Error(EError type, std::string file, std::string msg, int lineno, std::string category, Token token):
        d(new Private)
    {
        d->type     = type;
        d->file     = file;
        d->msg      = msg;
        d->lineno   = lineno;
        d->category = category;
        d->token    = token;
    }
    Error::Error(EError type, std::string file, std::string msg, Token token, bool internal):
        d(new Private)
    {
        d->type     = type;
        d->file     = file;
        d->msg      = msg;
        d->lineno   = token.lineno;
        if (internal) {
            d->category = "internal";
            d->token    = token;
        } else if (!token.text.empty())
            d->category += string("'") + token.text + "'";
    }
    Error::~Error() {
        delete d;
    }
    std::ostream& operator<< (std::ostream &str, const Error &err) {
        str << err.d->file << ": ";
        if (err.d->lineno)
            // print line number
            str << Color(C_LIGHT_GREEN) << err.d->lineno
                << Color(C_NO_COLOR) << ": ";
 
        switch (err.d->type) {
            case E_ERROR:
                str << Color(C_LIGHT_RED) << "error"
                    << Color(C_NO_COLOR) << ": ";
                break;
            case E_WARNING:
                str << Color(C_YELLOW) << "warrning"
                    << Color(C_NO_COLOR) << ": ";
                break;
            case E_NOTE:
                str << Color(C_LIGHT_BLUE) << "note"
                    << Color(C_NO_COLOR) << ": ";
                break;
            default:
                str << err.d->msg;
                return str;
        }
 
        if (!err.d->category.empty())
            // print category
            str << Color(C_LIGHT_CYAN) << err.d->category 
                << Color(C_NO_COLOR) << ": ";
 
        // print message
        str << err.d->msg;
 
        if (err.d->token.type != ETOKEN_NULL)
            // print related token
            str << " "
                << Color(C_LIGHT_BLUE) << "[" << Color(C_NO_COLOR)
                << "caused by token " << err.d->token
                << Color(C_LIGHT_BLUE) << "]" << Color(C_NO_COLOR);
 
        return str;
    }
} // namespace StreamDecorator