/src/solidity/liblangutil/ParserBase.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 | | * @author Christian <c@ethdev.com> |
20 | | * @date 2016 |
21 | | * Solidity parser shared functionality. |
22 | | */ |
23 | | |
24 | | #include <liblangutil/ParserBase.h> |
25 | | #include <liblangutil/Scanner.h> |
26 | | #include <liblangutil/ErrorReporter.h> |
27 | | |
28 | | using namespace solidity; |
29 | | using namespace solidity::langutil; |
30 | | |
31 | | SourceLocation ParserBase::currentLocation() const |
32 | 85.2M | { |
33 | 85.2M | return m_scanner->currentLocation(); |
34 | 85.2M | } |
35 | | |
36 | | Token ParserBase::currentToken() const |
37 | 108M | { |
38 | 108M | return m_scanner->currentToken(); |
39 | 108M | } |
40 | | |
41 | | Token ParserBase::peekNextToken() const |
42 | 0 | { |
43 | 0 | return m_scanner->peekNextToken(); |
44 | 0 | } |
45 | | |
46 | | std::string_view ParserBase::currentLiteral() const |
47 | 40.2M | { |
48 | 40.2M | return m_scanner->currentLiteral(); |
49 | 40.2M | } |
50 | | |
51 | | Token ParserBase::advance() |
52 | 67.9M | { |
53 | 67.9M | return m_scanner->next(); |
54 | 67.9M | } |
55 | | |
56 | | std::string ParserBase::tokenName(Token _token) |
57 | 12.3k | { |
58 | 12.3k | if (_token == Token::Identifier) |
59 | 3.04k | return "identifier"; |
60 | 9.30k | else if (_token == Token::EOS) |
61 | 1.47k | return "end of source"; |
62 | 7.83k | else if (TokenTraits::isReservedKeyword(_token)) |
63 | 24 | return "reserved keyword '" + TokenTraits::friendlyName(_token) + "'"; |
64 | 7.81k | else if (TokenTraits::isElementaryTypeName(_token)) //for the sake of accuracy in reporting |
65 | 47 | { |
66 | 47 | ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); |
67 | 47 | return "'" + elemTypeName.toString() + "'"; |
68 | 47 | } |
69 | 7.76k | else |
70 | 7.76k | return "'" + TokenTraits::friendlyName(_token) + "'"; |
71 | 12.3k | } |
72 | | |
73 | | void ParserBase::expectToken(Token _value, bool _advance) |
74 | 41.3M | { |
75 | 41.3M | Token tok = m_scanner->currentToken(); |
76 | 41.3M | if (tok != _value) |
77 | 6.14k | fatalParserError( |
78 | 6.14k | 2314_error, |
79 | 6.14k | "Expected " + ParserBase::tokenName(_value) + " but got " + tokenName(tok) |
80 | 6.14k | ); |
81 | 41.3M | if (_advance) |
82 | 40.6M | advance(); |
83 | 41.3M | } |
84 | | |
85 | | void ParserBase::increaseRecursionDepth() |
86 | 66.8M | { |
87 | 66.8M | m_recursionDepth++; |
88 | 66.8M | if (m_recursionDepth >= 1200) |
89 | 6 | fatalParserError(7319_error, "Maximum recursion depth reached during parsing."); |
90 | 66.8M | } |
91 | | |
92 | | void ParserBase::decreaseRecursionDepth() |
93 | 66.8M | { |
94 | 66.8M | solAssert(m_recursionDepth > 0, ""); |
95 | 66.8M | m_recursionDepth--; |
96 | 66.8M | } |
97 | | |
98 | | void ParserBase::parserWarning(ErrorId _error, std::string const& _description) |
99 | 25 | { |
100 | 25 | m_errorReporter.warning(_error, currentLocation(), _description); |
101 | 25 | } |
102 | | |
103 | | void ParserBase::parserWarning(ErrorId _error, SourceLocation const& _location, std::string const& _description) |
104 | 28.6k | { |
105 | 28.6k | m_errorReporter.warning(_error, _location, _description); |
106 | 28.6k | } |
107 | | |
108 | | void ParserBase::parserError(ErrorId _error, SourceLocation const& _location, std::string const& _description) |
109 | 30.3k | { |
110 | 30.3k | m_errorReporter.parserError(_error, _location, _description); |
111 | 30.3k | } |
112 | | |
113 | | void ParserBase::parserError(ErrorId _error, std::string const& _description) |
114 | 30.3k | { |
115 | 30.3k | parserError(_error, currentLocation(), _description); |
116 | 30.3k | } |
117 | | |
118 | | void ParserBase::fatalParserError(ErrorId _error, std::string const& _description) |
119 | 20.2k | { |
120 | 20.2k | fatalParserError(_error, currentLocation(), _description); |
121 | 20.2k | } |
122 | | |
123 | | void ParserBase::fatalParserError(ErrorId _error, SourceLocation const& _location, std::string const& _description) |
124 | 20.4k | { |
125 | 20.4k | m_errorReporter.fatalParserError(_error, _location, _description); |
126 | 20.4k | } |