Bash++
Bash++ compiler internal documentation
BaseListener.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <memory>
10
11#include <AST/ASTNode.h>
12#include <AST/NodeTypes.h>
13#include <AST/Nodes/Nodes.h>
14#include <error/InternalError.h>
15#include <error/SyntaxError.h>
16#include <error/ParserError.h>
17
18namespace AST {
19
20// Single source of truth for node list (used to generate wrappers + map)
21#define AST_LISTENER_NODE_LIST(X) \
22 X(ArrayAssignment) \
23 X(ArrayIndex) \
24 X(Bash53NativeSupershell) \
25 X(BashArithmeticForCondition) \
26 X(BashArithmeticForStatement) \
27 X(BashArithmeticStatement) \
28 X(BashArithmeticSubstitution) \
29 X(BashCaseInput) \
30 X(BashCasePattern) \
31 X(BashCasePatternHeader) \
32 X(BashCaseStatement) \
33 X(BashCommand) \
34 X(BashCommandSequence) \
35 X(BashForStatement) \
36 X(BashFunction) \
37 X(BashIfCondition) \
38 X(BashIfElseBranch) \
39 X(BashIfRootBranch) \
40 X(BashIfStatement) \
41 X(BashInCondition) \
42 X(BashPipeline) \
43 X(BashRedirection) \
44 X(BashSelectStatement) \
45 X(BashTestConditionCommand) \
46 X(BashUntilStatement) \
47 X(BashVariable) \
48 X(BashWhileOrUntilCondition) \
49 X(BashWhileStatement) \
50 X(Block) \
51 X(ClassDefinition) \
52 X(Connective) \
53 X(ConstructorDefinition) \
54 X(DatamemberDeclaration) \
55 X(DeleteStatement) \
56 X(DestructorDefinition) \
57 X(DoublequotedString) \
58 X(DynamicCast) \
59 X(DynamicCastTarget) \
60 X(HeredocBody) \
61 X(HereString) \
62 X(IncludeStatement) \
63 X(MethodDefinition) \
64 X(NewStatement) \
65 X(ObjectAssignment) \
66 X(ObjectInstantiation) \
67 X(ObjectReference) \
68 X(ParameterExpansion) \
69 X(PointerDeclaration) \
70 X(PrimitiveAssignment) \
71 X(ProcessSubstitution) \
72 X(Program) \
73 X(RawSubshell) \
74 X(RawText) \
75 X(Rvalue) \
76 X(SubshellSubstitution) \
77 X(Supershell) \
78 X(TypeofExpression) \
79 X(ValueAssignment) \
80
110template <class Derived>
112 private:
113 Derived& self() {
114 return static_cast<Derived&>(*this);
115 }
116
117 protected:
118 bool program_has_errors = false;
119 std::vector<AST::ParserError> parser_errors;
120
121 public:
122 void walk(std::shared_ptr<AST::ASTNode> node) {
123 try {
124 switch (node->getType()) {
125 #define AST_CASE(Name) \
126 case AST::NodeType::Name: \
127 if constexpr (requires(Derived& d, std::shared_ptr<AST::Name> x) { d.enter##Name(x); }) { \
128 self().enter##Name(std::static_pointer_cast<AST::Name>(node)); \
129 } \
130 for (const auto& child : node->getChildren()) { \
131 walk(child); \
132 } \
133 if constexpr (requires(Derived& d, std::shared_ptr<AST::Name> x) { d.exit##Name(x); }) { \
134 self().exit##Name(std::static_pointer_cast<AST::Name>(node)); \
135 } \
136 break;
137
139
140 #undef AST_CASE
141
142 default:
143 throw bpp::ErrorHandling::InternalError("Unknown AST node type encountered in listener walk");
144 }
145 } catch (const bpp::ErrorHandling::SyntaxError& e) {
146 // Cancel traversal of this node and its children, but continue to traverse the rest of the tree
147 this->program_has_errors = true;
148 e.print();
149 return;
150 }
151 }
152 #undef AST_LISTENER_NODE_LIST
153
154 void set_has_errors(bool has_errors) {
155 this->program_has_errors = has_errors;
156 }
157
158 void set_parser_errors(const std::vector<AST::ParserError>& errors) {
159 this->parser_errors = errors;
160 if (!errors.empty()) this->program_has_errors = true;
161 }
162};
163
164} // namespace AST
#define AST_CASE(Name)
#define AST_LISTENER_NODE_LIST(X)
Definition BaseListener.h:21
CRTP base class for AST listeners. CRTP is a kind of language hack that makes static polymorphism pos...
Definition BaseListener.h:111
void set_parser_errors(const std::vector< AST::ParserError > &errors)
Definition BaseListener.h:158
bool program_has_errors
Definition BaseListener.h:118
void walk(std::shared_ptr< AST::ASTNode > node)
Definition BaseListener.h:122
std::vector< AST::ParserError > parser_errors
Definition BaseListener.h:119
void set_has_errors(bool has_errors)
Definition BaseListener.h:154
Derived & self()
Definition BaseListener.h:113
void print() const
Definition SyntaxError.h:102
An exception thrown when a syntax error is encountered This exception can be constructed from any lis...
Definition SyntaxError.h:125
Definition AccessModifier.h:10
An exception thrown when an internal error occurs.
Definition InternalError.h:21