Bash++
Bash++ compiler internal documentation
BaseListener.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <memory>
9#include <type_traits>
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
17namespace AST {
18
19// Single source of truth for node list (used to generate wrappers + map)
20#define AST_LISTENER_NODE_LIST(X) \
21 X(ArrayAssignment) \
22 X(ArrayIndex) \
23 X(Bash53NativeSupershell) \
24 X(BashArithmeticForCondition) \
25 X(BashArithmeticForStatement) \
26 X(BashArithmeticStatement) \
27 X(BashArithmeticSubstitution) \
28 X(BashCaseInput) \
29 X(BashCasePattern) \
30 X(BashCasePatternHeader) \
31 X(BashCaseStatement) \
32 X(BashCommand) \
33 X(BashCommandSequence) \
34 X(BashForStatement) \
35 X(BashFunction) \
36 X(BashIfCondition) \
37 X(BashIfElseBranch) \
38 X(BashIfRootBranch) \
39 X(BashIfStatement) \
40 X(BashInCondition) \
41 X(BashPipeline) \
42 X(BashRedirection) \
43 X(BashSelectStatement) \
44 X(BashTestConditionCommand) \
45 X(BashUntilStatement) \
46 X(BashVariable) \
47 X(BashWhileOrUntilCondition) \
48 X(BashWhileStatement) \
49 X(Block) \
50 X(ClassDefinition) \
51 X(Connective) \
52 X(ConstructorDefinition) \
53 X(DatamemberDeclaration) \
54 X(DeleteStatement) \
55 X(DestructorDefinition) \
56 X(DoublequotedString) \
57 X(DynamicCast) \
58 X(DynamicCastTarget) \
59 X(HeredocBody) \
60 X(HereString) \
61 X(IncludeStatement) \
62 X(MethodDefinition) \
63 X(NewStatement) \
64 X(ObjectAssignment) \
65 X(ObjectInstantiation) \
66 X(ObjectReference) \
67 X(ParameterExpansion) \
68 X(PointerDeclaration) \
69 X(PrimitiveAssignment) \
70 X(ProcessSubstitution) \
71 X(Program) \
72 X(RawSubshell) \
73 X(RawText) \
74 X(Rvalue) \
75 X(SubshellSubstitution) \
76 X(Supershell) \
77 X(TypeofExpression) \
78 X(ValueAssignment) \
79
109template <class Derived>
111 private:
112 Derived& self() {
113 return static_cast<Derived&>(*this);
114 }
115
116 protected:
117 bool program_has_errors = false;
118
119 public:
120 virtual ~BaseListener() = default;
121
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 inline void set_has_errors(bool has_errors) {
155 this->program_has_errors = has_errors;
156 }
157};
158
159} // namespace AST
#define AST_CASE(Name)
#define AST_LISTENER_NODE_LIST(X)
Definition BaseListener.h:20
CRTP base class for AST listeners. CRTP is a kind of language hack that makes static polymorphism pos...
Definition BaseListener.h:110
bool program_has_errors
Definition BaseListener.h:117
virtual ~BaseListener()=default
void walk(std::shared_ptr< AST::ASTNode > node)
Definition BaseListener.h:122
void set_has_errors(bool has_errors)
Definition BaseListener.h:154
Derived & self()
Definition BaseListener.h:112
void print() const
Definition SyntaxError.h:103
An exception thrown when a syntax error is encountered This exception can be constructed from any lis...
Definition SyntaxError.h:126
Definition AccessModifier.h:8
An exception thrown when an internal error occurs.
Definition InternalError.h:21