aboutsummaryrefslogtreecommitdiffstats
path: root/liblangutil/SourceReferenceFormatter.cpp
blob: 4bc47a656779eb4263cca34214346ac13e310e1a (plain) (blame)
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
/*
    This file is part of solidity.

    solidity 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
    (at your option) any later version.

    solidity 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 solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * Formatting functions for errors referencing positions and locations in the source.
 */

#include <liblangutil/SourceReferenceFormatter.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/Exceptions.h>

using namespace std;
using namespace dev;
using namespace langutil;

void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location)
{
    printSourceLocation(SourceReferenceExtractor::extract(_location));
}

void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
{
    if (_ref.position.line < 0)
        return; // Nothing we can print here

    if (!_ref.multiline)
    {
        m_stream << _ref.text << endl;

        // mark the text-range like this: ^-----^
        for_each(
            _ref.text.cbegin(),
            _ref.text.cbegin() + _ref.startColumn,
            [this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
        );
        m_stream << "^";
        if (_ref.endColumn > _ref.startColumn + 2)
            m_stream << string(_ref.endColumn - _ref.startColumn - 2, '-');
        if (_ref.endColumn > _ref.startColumn + 1)
            m_stream << "^";
        m_stream << endl;
    }
    else
        m_stream <<
            _ref.text <<
            endl <<
            string(_ref.startColumn, ' ') <<
            "^ (Relevant source part starts here and spans across multiple lines)." <<
            endl;
}

void SourceReferenceFormatter::printSourceName(SourceReference const& _ref)
{
    if (_ref.position.line != -1)
        m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": ";
}

void SourceReferenceFormatter::printExceptionInformation(dev::Exception const& _error, std::string const& _category)
{
    printExceptionInformation(SourceReferenceExtractor::extract(_error, _category));
}

void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
{
    printSourceName(_msg.primary);

    m_stream << _msg.category << ": " << _msg.primary.message << endl;

    printSourceLocation(_msg.primary);

    for (auto const& ref: _msg.secondary)
    {
        printSourceName(ref);
        m_stream << ref.message << endl;
        printSourceLocation(ref);
    }
}