Coverage Report

Created: 2022-08-24 06:52

/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
  /// Set @a _parserErrorRecovery to true for additional error
42
  /// recovery.  This is experimental and intended for use
43
  /// by front-end tools that need partial AST information even
44
  /// when errors occur.
45
  explicit ParserBase(ErrorReporter& errorReporter, bool _parserErrorRecovery = false): m_errorReporter(errorReporter)
46
119k
  {
47
119k
    m_parserErrorRecovery = _parserErrorRecovery;
48
119k
  }
49
50
119k
  virtual ~ParserBase() = default;
51
52
protected:
53
  /// Utility class that creates an error and throws an exception if the
54
  /// recursion depth is too deep.
55
  class RecursionGuard
56
  {
57
  public:
58
    explicit RecursionGuard(ParserBase& _parser): m_parser(_parser)
59
51.3M
    {
60
51.3M
      m_parser.increaseRecursionDepth();
61
51.3M
    }
62
51.3M
    ~RecursionGuard() { m_parser.decreaseRecursionDepth(); }
63
  private:
64
    ParserBase& m_parser;
65
  };
66
67
  /// Location of the current token
68
  virtual SourceLocation currentLocation() const;
69
70
  ///@{
71
  ///@name Helper functions
72
  /// If current token value is not @a _value, throw exception otherwise advance token
73
  //  @a if _advance is true and error recovery is in effect.
74
  void expectToken(Token _value, bool _advance = true);
75
76
  /// Like expectToken but if there is an error ignores tokens until
77
  /// the expected token or EOS is seen. If EOS is encountered, back up to the error point,
78
  /// and throw an exception so that a higher grammar rule has an opportunity to recover.
79
  void expectTokenOrConsumeUntil(Token _value, std::string const& _currentNodeName, bool _advance = true);
80
  Token currentToken() const;
81
  Token peekNextToken() const;
82
  std::string tokenName(Token _token);
83
  std::string currentLiteral() const;
84
  virtual Token advance();
85
  ///@}
86
87
  /// Increases the recursion depth and throws an exception if it is too deep.
88
  void increaseRecursionDepth();
89
  void decreaseRecursionDepth();
90
91
  /// Creates a @ref ParserError and annotates it with the current position and the
92
  /// given @a _description.
93
  void parserError(ErrorId _error, std::string const& _description);
94
  void parserError(ErrorId _error, SourceLocation const& _location, std::string const& _description);
95
96
  /// Creates a @ref ParserWarning and annotates it with the current position and the
97
  /// given @a _description.
98
  void parserWarning(ErrorId _error, std::string const& _description);
99
  void parserWarning(ErrorId _error, SourceLocation const& _location, std::string const& _description);
100
101
  /// Creates a @ref ParserError and annotates it with the current position and the
102
  /// given @a _description. Throws the FatalError.
103
  void fatalParserError(ErrorId _error, std::string const& _description);
104
  void fatalParserError(ErrorId _error, SourceLocation const& _location, std::string const& _description);
105
106
  std::shared_ptr<Scanner> m_scanner;
107
  /// The reference to the list of errors, warnings and infos to add errors/warnings/infos during parsing
108
  ErrorReporter& m_errorReporter;
109
  /// Current recursion depth during parsing.
110
  size_t m_recursionDepth = 0;
111
  /// True if we are in parser error recovery. Usually this means we are scanning for
112
  /// a synchronization token like ';', or '}'. We use this to reduce cascaded error messages.
113
  bool m_inParserRecovery = false;
114
  bool m_parserErrorRecovery = false;
115
};
116
117
}