view misc/Error.d @ 88:eb5b2c719a39 new_gen

Major change to locations, tokens and expressions. A location (now SourceLocation or SLoc) is only 32 bit in size - disadvantage is that it can't find its own text. You have to go through the new SourceManager to do that. This has caused changes to a lot of stuff and removal of DataSource and the old Location Additionally Exp has gotten some location stuff, so we can give proper error messages. Not in Decl and Stmt yet, but thats coming too.
author Anders Halager <halager@gmail.com>
date Sun, 04 May 2008 18:13:46 +0200
parents 4ae365eff712
children 621cedba53ea
line wrap: on
line source

module misc.Error;

import tango.core.Exception,
       Array = tango.core.Array,
       tango.text.Util;

import llvm.type;

import lexer.Token,
       sema.DType,
       sema.Symbol;

class Error : Exception
{

    this(char[] message)
    {
        super(message);
        args ~= message;
    }

    char[] toString()
    {
        char[256] tmp = void;
        char[] msg = layout(tmp, args);
        /*
        if (location.source.name.length > 0)
            msg = location.toString ~ ": " ~ msg;
        else
            msg = msg.dup;

        if (toks.length > 0)
        {
            Array.sort(toks,
                    (Token a, Token b)
                    {
                        return a.location.position - b.location.position;
                    });
            char[] data = toks[0].location.source.data;
            size_t low    = toks[0].location.position;
            size_t high   = toks[$ - 1].location.position;

            size_t line_start = Array.rfind(data[0 .. low], '\n');
            size_t line_end = high + Array.find(data[high .. $], '\n');
            char[] line = trim(data[line_start + 1 .. line_end]);
            char[] marks = line.dup;
            marks[] = ' ';
            foreach (tok; toks[0 .. $])
            {
                size_t p = tok.location.position - (line.ptr - data.ptr);
                marks[p .. p + tok.length] = '~';
            }
            size_t p = main_tok.location.position - (line.ptr - data.ptr);
            marks[p .. p + main_tok.length] = '^';

            msg ~= "\n    ";
            msg ~= line;
            msg ~= "\n    ";
            msg ~= marks;
        }
*/
        return msg;
    }

    Error arg(char[] s)
    {
        if (args.length == 11)
            throw new Exception("Sorry, errors only support up to 10 args");
        args ~= s;
        return this;
    }

    Error arg(char[][] s)
    {
        char[] res = s[0 .. $ - 1].join(", ");
        if (s.length > 1)
            res ~= " and ";
        res ~= s[$ - 1];
        return arg(res);
    }

    Error arg(char c)
    {
        return arg([c]);
    }

    Error arg(DType[] types)
    {
        char[][] res;
        foreach (type; types)
            res ~= type.name();
        return arg(res);
    }

    Error arg(Tok[] toks...)
    {
        char[][] res;
        foreach (t; toks)
            res ~= typeToString[t];
        return arg(res);
    }

    Error arg(Symbol sym)
    {
        return arg(sym.type.name ~ " " ~ sym.id.get);
    }

    /*
    Error loc(Location loc)
    {
        location = loc;
        return this;
    }
    */

    Error tok(Token tok)
    {
        /*
        if (toks.length > 0)
            assert(tok.location.source == toks[0].location.source,
                    "Tokens must come from the same source");
        else
        {
            main_tok = tok;
            loc = tok.location;
        }
        toks ~= tok;
    */
        return this;
    }

private:
    char[][] args;
    //Location location;
    Token[] toks;
    Token main_tok;
}