/src/solidity/libsmtutil/SMTLib2Parser.cpp
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #include <libsmtutil/SMTLib2Parser.h> |
20 | | |
21 | | #include <liblangutil/Common.h> |
22 | | |
23 | | #include <libsolutil/Visitor.h> |
24 | | #include <libsolutil/StringUtils.h> |
25 | | |
26 | | |
27 | | using namespace solidity::langutil; |
28 | | using namespace solidity::smtutil; |
29 | | |
30 | 0 | std::string SMTLib2Expression::toString() const { |
31 | 0 | return std::visit(solidity::util::GenericVisitor{ |
32 | 0 | [](std::string const& _sv) { return _sv; }, |
33 | 0 | [](std::vector<SMTLib2Expression> const& _subExpr) { |
34 | 0 | std::vector<std::string> formatted; |
35 | 0 | for (auto const& item: _subExpr) |
36 | 0 | formatted.emplace_back(item.toString()); |
37 | 0 | return "(" + solidity::util::joinHumanReadable(formatted, " ") + ")"; |
38 | 0 | } |
39 | 0 | }, data); |
40 | 0 | } |
41 | | |
42 | 0 | SMTLib2Expression SMTLib2Parser::parseExpression() { |
43 | 0 | skipWhitespace(); |
44 | 0 | if (token() == '(') |
45 | 0 | { |
46 | 0 | advance(); |
47 | 0 | skipWhitespace(); |
48 | 0 | std::vector<SMTLib2Expression> subExpressions; |
49 | 0 | while (token() != 0 && token() != ')') |
50 | 0 | { |
51 | 0 | subExpressions.emplace_back(parseExpression()); |
52 | 0 | skipWhitespace(); |
53 | 0 | } |
54 | 0 | if (token() != ')') |
55 | 0 | throw ParsingException{}; |
56 | | // Simulate whitespace because we do not want to read the next token since it might block. |
57 | 0 | m_token = ' '; |
58 | 0 | return {std::move(subExpressions)}; |
59 | 0 | } else |
60 | 0 | return {parseToken()}; |
61 | 0 | } |
62 | | |
63 | 0 | std::string SMTLib2Parser::parseToken() { |
64 | 0 | std::string result; |
65 | |
|
66 | 0 | skipWhitespace(); |
67 | 0 | bool isPipe = token() == '|'; |
68 | 0 | if (isPipe) |
69 | 0 | advance(); |
70 | 0 | while (token() != 0) |
71 | 0 | { |
72 | 0 | char c = token(); |
73 | 0 | if (isPipe && c == '|') |
74 | 0 | { |
75 | 0 | advance(); |
76 | 0 | break; |
77 | 0 | } else if (!isPipe && (isWhiteSpace(c) || c == '(' || c == ')')) |
78 | 0 | break; |
79 | 0 | result.push_back(c); |
80 | 0 | advance(); |
81 | 0 | } |
82 | 0 | return result; |
83 | 0 | } |
84 | | |
85 | 0 | void SMTLib2Parser::advance() { |
86 | 0 | if (!m_input.good()) |
87 | 0 | throw ParsingException{}; |
88 | 0 | m_token = static_cast<char>(m_input.get()); |
89 | 0 | if (token() == ';') |
90 | 0 | while (token() != '\n' && token() != 0) |
91 | 0 | m_token = static_cast<char>(m_input.get()); |
92 | 0 | } |
93 | | |
94 | 0 | void SMTLib2Parser::skipWhitespace() { |
95 | 0 | while (isWhiteSpace(token())) |
96 | 0 | advance(); |
97 | 0 | } |