aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/SyntaxTestParser.cpp
blob: c37caca5329e5505446d50632ed852df4bdeab83 (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
93
94
95
96
97
/*
    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/>.
*/

#include <test/libsolidity/SyntaxTestParser.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/throw_exception.hpp>
#include <cctype>
#include <fstream>
#include <stdexcept>

using namespace dev;
using namespace solidity;
using namespace dev::solidity::test;
using namespace std;

template<typename IteratorType>
void skipWhitespace(IteratorType& it, IteratorType end)
{
    while (it != end && isspace(*it))
        ++it;
}

template<typename IteratorType>
void skipSlashes(IteratorType& it, IteratorType end)
{
    while (it != end && *it == '/')
        ++it;
}

std::string SyntaxTestParser::parseSource(std::istream& _stream)
{
    std::string source;
    string line;
    string const delimiter("// ----");
    while (getline(_stream, line))
        if (boost::algorithm::starts_with(line, delimiter))
            break;
        else
            source += line + "\n";
    return source;
}

std::vector<SyntaxTestExpectation> SyntaxTestParser::parseExpectations(std::istream& _stream)
{
    std::vector<SyntaxTestExpectation> expectations;
    std::string line;
    while (getline(_stream, line))
    {
        auto it = line.begin();

        skipSlashes(it, line.end());
        skipWhitespace(it, line.end());

        if (it == line.end()) continue;

        auto typeBegin = it;
        while (it != line.end() && *it != ':')
            ++it;
        string errorType(typeBegin, it);

        // skip colon
        if (it != line.end()) it++;

        skipWhitespace(it, line.end());

        string errorMessage(it, line.end());
        expectations.emplace_back(SyntaxTestExpectation{move(errorType), move(errorMessage)});
    }
    return expectations;
}

SyntaxTest SyntaxTestParser::parse(string const& _filename)
{
    ifstream file(_filename);
    if (!file)
        BOOST_THROW_EXCEPTION(runtime_error("cannot open test contract: \"" + _filename + "\""));
    file.exceptions(ios::badbit);

    SyntaxTest result;
    result.source = parseSource(file);
    result.expectations = parseExpectations(file);
    return result;
}