/src/solidity/liblangutil/ParserBase.h
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 | | #pragma once |
25 | | |
26 | | #include <liblangutil/Token.h> |
27 | | #include <memory> |
28 | | #include <string> |
29 | | |
30 | | namespace solidity::langutil |
31 | | { |
32 | | |
33 | | class ErrorReporter; |
34 | | class Scanner; |
35 | | struct SourceLocation; |
36 | | struct ErrorId; |
37 | | |
38 | | class ParserBase |
39 | | { |
40 | | public: |
41 | | explicit ParserBase(ErrorReporter& errorReporter): |
42 | | m_errorReporter(errorReporter) |
43 | 462k | {} |
44 | | |
45 | 462k | virtual ~ParserBase() = default; |
46 | | |
47 | | protected: |
48 | | /// Utility class that creates an error and throws an exception if the |
49 | | /// recursion depth is too deep. |
50 | | class RecursionGuard |
51 | | { |
52 | | public: |
53 | | explicit RecursionGuard(ParserBase& _parser): m_parser(_parser) |
54 | 54.6M | { |
55 | 54.6M | m_parser.increaseRecursionDepth(); |
56 | 54.6M | } |
57 | 54.6M | ~RecursionGuard() { m_parser.decreaseRecursionDepth(); } |
58 | | private: |
59 | | ParserBase& m_parser; |
60 | | }; |
61 | | |
62 | | /// Location of the current token |
63 | | virtual SourceLocation currentLocation() const; |
64 | | |
65 | | ///@{ |
66 | | ///@name Helper functions |
67 | | /// If current token value is not @a _value, throw exception otherwise advance token |
68 | | // if @a _advance is true |
69 | | void expectToken(Token _value, bool _advance = true); |
70 | | |
71 | | Token currentToken() const; |
72 | | Token peekNextToken() const; |
73 | | std::string tokenName(Token _token); |
74 | | /// Points to the current literal. The string view invalidates when the parser advances. |
75 | | std::string_view currentLiteral() const; |
76 | | virtual Token advance(); |
77 | | ///@} |
78 | | |
79 | | /// Increases the recursion depth and throws an exception if it is too deep. |
80 | | void increaseRecursionDepth(); |
81 | | void decreaseRecursionDepth(); |
82 | | |
83 | | /// Creates a @ref ParserError and annotates it with the current position and the |
84 | | /// given @a _description. |
85 | | void parserError(ErrorId _error, std::string const& _description); |
86 | | void parserError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
87 | | |
88 | | /// Creates a @ref ParserWarning and annotates it with the current position and the |
89 | | /// given @a _description. |
90 | | void parserWarning(ErrorId _error, std::string const& _description); |
91 | | void parserWarning(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
92 | | |
93 | | /// Creates a @ref ParserError and annotates it with the current position and the |
94 | | /// given @a _description. Throws the FatalError. |
95 | | void fatalParserError(ErrorId _error, std::string const& _description); |
96 | | void fatalParserError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
97 | | |
98 | | std::shared_ptr<Scanner> m_scanner; |
99 | | /// The reference to the list of errors, warnings and infos to add errors/warnings/infos during parsing |
100 | | ErrorReporter& m_errorReporter; |
101 | | /// Current recursion depth during parsing. |
102 | | size_t m_recursionDepth = 0; |
103 | | }; |
104 | | |
105 | | } |