aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/parsing/ParserBase.h
diff options
context:
space:
mode:
Diffstat (limited to 'libsolidity/parsing/ParserBase.h')
-rw-r--r--libsolidity/parsing/ParserBase.h22
1 files changed, 21 insertions, 1 deletions
diff --git a/libsolidity/parsing/ParserBase.h b/libsolidity/parsing/ParserBase.h
index 5b03ab5e..fd0de0d1 100644
--- a/libsolidity/parsing/ParserBase.h
+++ b/libsolidity/parsing/ParserBase.h
@@ -36,11 +36,25 @@ class Scanner;
class ParserBase
{
public:
- ParserBase(ErrorReporter& errorReporter): m_errorReporter(errorReporter) {}
+ explicit ParserBase(ErrorReporter& errorReporter): m_errorReporter(errorReporter) {}
std::shared_ptr<std::string const> const& sourceName() const;
protected:
+ /// Utility class that creates an error and throws an exception if the
+ /// recursion depth is too deep.
+ class RecursionGuard
+ {
+ public:
+ explicit RecursionGuard(ParserBase& _parser): m_parser(_parser)
+ {
+ m_parser.increaseRecursionDepth();
+ }
+ ~RecursionGuard() { m_parser.decreaseRecursionDepth(); }
+ private:
+ ParserBase& m_parser;
+ };
+
/// Start position of the current token
int position() const;
/// End position of the current token
@@ -56,6 +70,10 @@ protected:
Token::Value advance();
///@}
+ /// Increases the recursion depth and throws an exception if it is too deep.
+ void increaseRecursionDepth();
+ void decreaseRecursionDepth();
+
/// Creates a @ref ParserError and annotates it with the current position and the
/// given @a _description.
void parserError(std::string const& _description);
@@ -67,6 +85,8 @@ protected:
std::shared_ptr<Scanner> m_scanner;
/// The reference to the list of errors and warning to add errors/warnings during parsing
ErrorReporter& m_errorReporter;
+ /// Current recursion depth during parsing.
+ size_t m_recursionDepth = 0;
};
}