6 #ifndef SRC_LISTENER_BASHPPLISTENER_H_
7 #define SRC_LISTENER_BASHPPLISTENER_H_
15 #include <antlr4-runtime.h>
17 #include "../antlr/BashppParserBaseListener.h"
21 #include "../bpp_include/bpp_entity.cpp"
22 #include "../bpp_include/bpp_code_entity.cpp"
23 #include "../bpp_include/bpp_string.cpp"
24 #include "../bpp_include/bash_while_loop.cpp"
25 #include "../bpp_include/bash_while_condition.cpp"
26 #include "../bpp_include/bash_if.cpp"
27 #include "../bpp_include/bash_if_branch.cpp"
28 #include "../bpp_include/bash_case.cpp"
29 #include "../bpp_include/bash_case_pattern.cpp"
30 #include "../bpp_include/bash_for.cpp"
31 #include "../bpp_include/bpp_delete_statement.cpp"
32 #include "../bpp_include/bpp_dynamic_cast_statement.cpp"
33 #include "../bpp_include/bpp_value_assignment.cpp"
34 #include "../bpp_include/bpp_object_reference.cpp"
35 #include "../bpp_include/bpp_object_assignment.cpp"
36 #include "../bpp_include/bpp_pointer_dereference.cpp"
37 #include "../bpp_include/bpp_program.cpp"
38 #include "../bpp_include/bpp_class.cpp"
39 #include "../bpp_include/bpp_constructor.cpp"
40 #include "../bpp_include/bpp_destructor.cpp"
41 #include "../bpp_include/bpp_datamember.cpp"
42 #include "../bpp_include/bpp_method.cpp"
43 #include "../bpp_include/bpp_method_parameter.cpp"
44 #include "../bpp_include/bpp_object.cpp"
46 #include "../syntax_error.cpp"
47 #include "../internal_error.cpp"
49 #define skip_singlequote_string if (in_singlequote_string) return;
50 #define skip_syntax_errors if (error_thrown) { \
51 if (error_context == ctx) { \
52 error_thrown = false; \
53 error_context = nullptr; \
78 class BashppListener :
public BashppParserBaseListener, std::enable_shared_from_this<BashppListener> {
124 std::shared_ptr<bpp::bpp_program>
program = std::make_shared<bpp::bpp_program>();
145 "class",
"constructor",
"delete",
"destructor",
146 "dynamic_cast",
"include",
"include_once",
"method",
147 "new",
"nullptr",
"primitive",
"private",
148 "protected",
"public",
"this",
"virtual"
153 if (keyword == protected_keyword) {
167 #define set_error_context error_thrown = true; error_context = ctx;
169 #define output_syntax_error(symbol, msg) \
170 int line = symbol->getLine(); \
171 int column = symbol->getCharPositionInLine(); \
172 std::string text = symbol->getText(); \
173 print_syntax_error_or_warning(source_file, line, column, text, msg, get_include_stack()); \
174 program_has_errors = true;
176 #define throw_syntax_error_sym(symbol, msg) \
177 output_syntax_error(symbol, msg) \
181 #define throw_syntax_error(token, msg) antlr4::Token* symbol = token->getSymbol(); \
182 throw_syntax_error_sym(symbol, msg) \
186 #define throw_syntax_error_from_exitRule(token, msg) antlr4::Token* symbol = token->getSymbol(); \
187 output_syntax_error(symbol, msg) \
190 #define show_warning_sym(symbol, msg) \
191 if (!suppress_warnings) { \
192 int line = symbol->getLine(); \
193 int column = symbol->getCharPositionInLine(); \
194 std::string text = symbol->getText(); \
195 print_syntax_error_or_warning(source_file, line, column, text, msg, get_include_stack(), true); \
198 #define show_warning(token, msg) antlr4::Token* symbol = token->getSymbol(); \
199 show_warning_sym(symbol, msg)
243 void enterProgram(BashppParser::ProgramContext *ctx)
override;
244 void exitProgram(BashppParser::ProgramContext *ctx)
override;
279 void enterStatement(BashppParser::StatementContext *ctx)
override;
280 void exitStatement(BashppParser::StatementContext *ctx)
override;
317 void exitSupershell(BashppParser::SupershellContext *ctx)
override;
319 void enterSubshell(BashppParser::SubshellContext *ctx)
override;
320 void exitSubshell(BashppParser::SubshellContext *ctx)
override;
328 void enterString(BashppParser::StringContext *ctx)
override;
329 void exitString(BashppParser::StringContext *ctx)
override;
334 void enterParameter(BashppParser::ParameterContext *ctx)
override;
335 void exitParameter(BashppParser::ParameterContext *ctx)
override;
341 void exitRaw_rvalue(BashppParser::Raw_rvalueContext *ctx)
override;
382 void enterHeredoc(BashppParser::HeredocContext *ctx)
override;
383 void exitHeredoc(BashppParser::HeredocContext *ctx)
override;
The main listener class for the Bash++ compiler.
Definition: BashppListener.h:78
void enterValue_assignment(BashppParser::Value_assignmentContext *ctx) override
Definition: value_assignment.cpp:11
code_segment generate_method_call_code(const std::string &reference_code, const std::string &method_name, std::shared_ptr< bpp::bpp_class > assumed_class)
Generates a code segment for calling a method.
Definition: BashppListener.cpp:104
void enterBash_while_loop(BashppParser::Bash_while_loopContext *ctx) override
Definition: bash_while_loop.cpp:17
void enterArray_index(BashppParser::Array_indexContext *ctx) override
Definition: array_index.cpp:11
void exitBash_for_header(BashppParser::Bash_for_headerContext *ctx) override
Definition: bash_for_loop.cpp:72
std::shared_ptr< bpp::bash_while_condition > current_while_condition
Definition: BashppListener.h:129
void exitObject_assignment(BashppParser::Object_assignmentContext *ctx) override
Definition: object_assignment.cpp:26
void exitBash_if_root_branch(BashppParser::Bash_if_root_branchContext *ctx) override
Definition: bash_if_statement.cpp:145
void exitDelete_statement(BashppParser::Delete_statementContext *ctx) override
Definition: delete_statement.cpp:38
void enterBash_case_statement(BashppParser::Bash_case_statementContext *ctx) override
Definition: bash_case_statement.cpp:18
void enterBash_if_root_branch(BashppParser::Bash_if_root_branchContext *ctx) override
Definition: bash_if_statement.cpp:124
void enterMember_declaration(BashppParser::Member_declarationContext *ctx) override
Definition: member_declaration.cpp:11
bool run_on_exit
Definition: BashppListener.h:116
void exitArray_value(BashppParser::Array_valueContext *ctx) override
Definition: array_value.cpp:38
std::stack< std::string > include_stack
A chain of included files, from the main file to the current file (used for error reporting)
Definition: BashppListener.h:108
bool error_thrown
Definition: BashppListener.h:162
void set_include_paths(std::shared_ptr< std::vector< std::string >> include_paths)
Definition: BashppListener.cpp:161
void set_output_file(std::string output_file)
Definition: BashppListener.cpp:195
void exitRaw_rvalue(BashppParser::Raw_rvalueContext *ctx) override
Definition: raw_rvalue.cpp:22
bool program_has_errors
Definition: BashppListener.h:165
void exitBash_case_statement(BashppParser::Bash_case_statementContext *ctx) override
Definition: bash_case_statement.cpp:51
void exitSelf_reference(BashppParser::Self_referenceContext *ctx) override
Definition: self_reference.cpp:43
code_segment generate_dynamic_cast_code(const std::string &reference_code, const std::string &class_name)
Generates a code segment for performing a dynamic cast.
Definition: BashppListener.cpp:145
void enterHeredoc_header(BashppParser::Heredoc_headerContext *ctx) override
Definition: heredoc.cpp:54
void enterBash_while_condition(BashppParser::Bash_while_conditionContext *ctx) override
Definition: bash_while_loop.cpp:68
void set_run_on_exit(bool run_on_exit)
Definition: BashppListener.cpp:199
void enterInclude_statement(BashppParser::Include_statementContext *ctx) override
Definition: include_statement.cpp:11
void enterSubshell(BashppParser::SubshellContext *ctx) override
Definition: subshell.cpp:11
void exitSinglequote_string(BashppParser::Singlequote_stringContext *ctx) override
Definition: singlequote_string.cpp:24
void exitTerminal_token(BashppParser::Terminal_tokenContext *ctx) override
Definition: terminal_token.cpp:23
void enterPointer_dereference(BashppParser::Pointer_dereferenceContext *ctx) override
Definition: pointer_dereference.cpp:11
std::vector< char * > arguments
Command-line arguments to pass to the compiled program if run_on_exit is true.
Definition: BashppListener.h:122
bool in_singlequote_string
Definition: BashppListener.h:127
void enterClass_definition(BashppParser::Class_definitionContext *ctx) override
Definition: class_definition.cpp:11
void enterArray_value(BashppParser::Array_valueContext *ctx) override
Definition: array_value.cpp:11
void exitNullptr_ref(BashppParser::Nullptr_refContext *ctx) override
Definition: nullptr_ref.cpp:30
void enterSelf_reference(BashppParser::Self_referenceContext *ctx) override
Definition: self_reference.cpp:11
void exitClass_definition(BashppParser::Class_definitionContext *ctx) override
Definition: class_definition.cpp:51
code_segment generate_supershell_code(const std::string &code_to_run_in_supershell)
Generates a supershell code segment for executing a bash command.
Definition: BashppListener.cpp:28
void exitConstructor_definition(BashppParser::Constructor_definitionContext *ctx) override
Definition: constructor_definition.cpp:38
void set_source_file(std::string source_file)
Definition: BashppListener.cpp:157
void set_output_stream(std::shared_ptr< std::ostream > output_stream)
Definition: BashppListener.cpp:191
bool in_comment
Definition: BashppListener.h:126
void exitObject_reference_as_lvalue(BashppParser::Object_reference_as_lvalueContext *ctx) override
Definition: object_reference_as_lvalue.cpp:274
void exitBash_if_statement(BashppParser::Bash_if_statementContext *ctx) override
Definition: bash_if_statement.cpp:94
void enterSelf_reference_as_lvalue(BashppParser::Self_reference_as_lvalueContext *ctx) override
Definition: self_reference_as_lvalue.cpp:11
void set_errors()
Sets the program_has_errors flag to true.
Definition: BashppListener.cpp:187
bool in_while_condition
Definition: BashppListener.h:128
void enterDelete_statement(BashppParser::Delete_statementContext *ctx) override
Definition: delete_statement.cpp:11
void enterBash_for_loop(BashppParser::Bash_for_loopContext *ctx) override
Definition: bash_for_loop.cpp:17
void enterOther_statement(BashppParser::Other_statementContext *ctx) override
Definition: other_statement.cpp:11
void set_included(bool included)
Definition: BashppListener.cpp:169
std::shared_ptr< std::ostream > output_stream
Pointer to the output stream to write the compiled code to.
Definition: BashppListener.h:114
std::set< std::string > get_included_files()
Definition: BashppListener.cpp:215
void exitBash_arithmetic(BashppParser::Bash_arithmeticContext *ctx) override
Definition: bash_arithmetic.cpp:38
void set_included_from(BashppListener *included_from)
Sets the included_from pointer to the given listener.
Definition: BashppListener.cpp:176
void enterMethod_definition(BashppParser::Method_definitionContext *ctx) override
Definition: method_definition.cpp:11
void exitHeredoc_header(BashppParser::Heredoc_headerContext *ctx) override
Definition: heredoc.cpp:70
std::string output_file
Definition: BashppListener.h:115
void exitDynamic_cast_statement(BashppParser::Dynamic_cast_statementContext *ctx) override
Definition: dynamic_cast.cpp:46
void enterExtra_statement(BashppParser::Extra_statementContext *ctx) override
Definition: extra_statement.cpp:11
void enterStatement(BashppParser::StatementContext *ctx) override
Definition: statement.cpp:11
void exitBash_for_loop(BashppParser::Bash_for_loopContext *ctx) override
Definition: bash_for_loop.cpp:33
void enterObject_instantiation(BashppParser::Object_instantiationContext *ctx) override
Definition: object_instantiation.cpp:11
void exitObject_address(BashppParser::Object_addressContext *ctx) override
Definition: object_address.cpp:35
void exitMember_declaration(BashppParser::Member_declarationContext *ctx) override
Definition: member_declaration.cpp:64
bool included
Definition: BashppListener.h:86
code_segment generate_delete_code(std::shared_ptr< bpp::bpp_object > object, const std::string &object_reference_string, bool force_pointer=false)
Generates a code segment for deleting an object.
Definition: BashppListener.cpp:72
void exitExtra_statement(BashppParser::Extra_statementContext *ctx) override
Definition: extra_statement.cpp:23
void exitParameter(BashppParser::ParameterContext *ctx) override
Definition: parameter.cpp:51
bool suppress_warnings
Definition: BashppListener.h:95
void set_arguments(std::vector< char * > arguments)
Definition: BashppListener.cpp:207
antlr4::ParserRuleContext * error_context
Definition: BashppListener.h:163
void enterSinglequote_string(BashppParser::Singlequote_stringContext *ctx) override
Definition: singlequote_string.cpp:11
void enterParameter(BashppParser::ParameterContext *ctx) override
Definition: parameter.cpp:11
void enterObject_reference(BashppParser::Object_referenceContext *ctx) override
Definition: object_reference.cpp:11
std::stack< std::shared_ptr< bpp::bpp_entity > > entity_stack
A stack to keep track of the current entity being processed.
Definition: BashppListener.h:142
std::set< std::string > included_files
A set of (unique) included files (used for '@include_once' directives)
Definition: BashppListener.h:101
void enterNullptr_ref(BashppParser::Nullptr_refContext *ctx) override
Definition: nullptr_ref.cpp:11
void exitBash_case_pattern(BashppParser::Bash_case_patternContext *ctx) override
Definition: bash_case_statement.cpp:93
void enterNew_statement(BashppParser::New_statementContext *ctx) override
Definition: new_statement.cpp:11
void enterBash_if_condition(BashppParser::Bash_if_conditionContext *ctx) override
Definition: bash_if_statement.cpp:213
void enterBash_if_else_branch(BashppParser::Bash_if_else_branchContext *ctx) override
Definition: bash_if_statement.cpp:166
void exitObject_instantiation(BashppParser::Object_instantiationContext *ctx) override
Definition: object_instantiation.cpp:82
void enterBash_if_statement(BashppParser::Bash_if_statementContext *ctx) override
Definition: bash_if_statement.cpp:19
void exitHeredoc(BashppParser::HeredocContext *ctx) override
Definition: heredoc.cpp:33
bool is_protected_keyword(const std::string &keyword)
Definition: BashppListener.h:151
void enterTerminal_token(BashppParser::Terminal_tokenContext *ctx) override
Definition: terminal_token.cpp:11
void enterObject_assignment(BashppParser::Object_assignmentContext *ctx) override
Definition: object_assignment.cpp:11
BashppListener * included_from
Definition: BashppListener.h:102
void enterBash_arithmetic(BashppParser::Bash_arithmeticContext *ctx) override
Definition: bash_arithmetic.cpp:11
void enterHeredoc(BashppParser::HeredocContext *ctx) override
Definition: heredoc.cpp:17
std::shared_ptr< std::vector< std::string > > include_paths
A list of paths to search for included files.
Definition: BashppListener.h:92
void exitBash_while_loop(BashppParser::Bash_while_loopContext *ctx) override
Definition: bash_while_loop.cpp:33
void enterDestructor_definition(BashppParser::Destructor_definitionContext *ctx) override
Definition: destructor_definition.cpp:11
void set_suppress_warnings(bool suppress_warnings)
Definition: BashppListener.cpp:203
void enterBash_case_pattern(BashppParser::Bash_case_patternContext *ctx) override
Definition: bash_case_statement.cpp:74
void enterGeneral_statement(BashppParser::General_statementContext *ctx) override
Definition: general_statement.cpp:11
void enterString(BashppParser::StringContext *ctx) override
Definition: string.cpp:11
void enterDynamic_cast_statement(BashppParser::Dynamic_cast_statementContext *ctx) override
Definition: dynamic_cast.cpp:11
void exitSupershell(BashppParser::SupershellContext *ctx) override
Definition: supershell.cpp:34
void exitOther_statement(BashppParser::Other_statementContext *ctx) override
Definition: other_statement.cpp:30
void exitNew_statement(BashppParser::New_statementContext *ctx) override
Definition: new_statement.cpp:46
void enterSupershell(BashppParser::SupershellContext *ctx) override
Definition: supershell.cpp:11
std::shared_ptr< bpp::bpp_class > primitive
Definition: BashppListener.h:160
void enterBash_case_pattern_header(BashppParser::Bash_case_pattern_headerContext *ctx) override
Definition: bash_case_statement.cpp:118
void exitBash_while_condition(BashppParser::Bash_while_conditionContext *ctx) override
Definition: bash_while_loop.cpp:89
void exitGeneral_statement(BashppParser::General_statementContext *ctx) override
Definition: general_statement.cpp:24
void exitPointer_dereference(BashppParser::Pointer_dereferenceContext *ctx) override
Definition: pointer_dereference.cpp:48
void exitProgram(BashppParser::ProgramContext *ctx) override
Definition: program.cpp:31
void exitSubshell(BashppParser::SubshellContext *ctx) override
Definition: subshell.cpp:31
void enterPointer_declaration(BashppParser::Pointer_declarationContext *ctx) override
Definition: pointer_declaration.cpp:11
void exitArray_index(BashppParser::Array_indexContext *ctx) override
Definition: array_index.cpp:39
void exitBash_case_pattern_header(BashppParser::Bash_case_pattern_headerContext *ctx) override
Definition: bash_case_statement.cpp:135
void exitClass_body_statement(BashppParser::Class_body_statementContext *ctx) override
Definition: class_body_statement.cpp:44
void exitString(BashppParser::StringContext *ctx) override
Definition: string.cpp:35
void enterRaw_rvalue(BashppParser::Raw_rvalueContext *ctx) override
Definition: raw_rvalue.cpp:11
void enterProgram(BashppParser::ProgramContext *ctx) override
Definition: program.cpp:14
void exitDestructor_definition(BashppParser::Destructor_definitionContext *ctx) override
Definition: destructor_definition.cpp:38
void enterDeprecated_subshell(BashppParser::Deprecated_subshellContext *ctx) override
Definition: deprecated_subshell.cpp:11
void exitObject_reference(BashppParser::Object_referenceContext *ctx) override
Definition: object_reference.cpp:40
void exitInclude_statement(BashppParser::Include_statementContext *ctx) override
Definition: include_statement.cpp:172
std::shared_ptr< bpp::bpp_program > program
Definition: BashppListener.h:124
void enterObject_reference_as_lvalue(BashppParser::Object_reference_as_lvalueContext *ctx) override
Definition: object_reference_as_lvalue.cpp:11
std::string source_file
Path to the source file being compiled (used for error reporting)
Definition: BashppListener.h:84
void enterConstructor_definition(BashppParser::Constructor_definitionContext *ctx) override
Definition: constructor_definition.cpp:11
void enterClass_body_statement(BashppParser::Class_body_statementContext *ctx) override
Definition: class_body_statement.cpp:11
std::shared_ptr< bpp::bpp_program > get_program()
Definition: BashppListener.cpp:211
void exitValue_assignment(BashppParser::Value_assignmentContext *ctx) override
Definition: value_assignment.cpp:35
void enterBash_for_header(BashppParser::Bash_for_headerContext *ctx) override
Definition: bash_for_loop.cpp:56
void exitStatement(BashppParser::StatementContext *ctx) override
Definition: statement.cpp:16
void exitBash_if_else_branch(BashppParser::Bash_if_else_branchContext *ctx) override
Definition: bash_if_statement.cpp:192
void exitPointer_declaration(BashppParser::Pointer_declarationContext *ctx) override
Definition: pointer_declaration.cpp:92
void exitSelf_reference_as_lvalue(BashppParser::Self_reference_as_lvalueContext *ctx) override
Definition: self_reference_as_lvalue.cpp:212
void set_dynamic_linking(bool dynamic_linking)
Definition: BashppListener.cpp:165
void enterObject_address(BashppParser::Object_addressContext *ctx) override
Definition: object_address.cpp:11
static constexpr const char * protected_keywords[]
Definition: BashppListener.h:144
void exitBash_if_condition(BashppParser::Bash_if_conditionContext *ctx) override
Definition: bash_if_statement.cpp:232
bool dynamic_linking
Definition: BashppListener.h:94
std::stack< std::string > get_include_stack()
Definition: BashppListener.cpp:219
void exitMethod_definition(BashppParser::Method_definitionContext *ctx) override
Definition: method_definition.cpp:49
void exitDeprecated_subshell(BashppParser::Deprecated_subshellContext *ctx) override
Definition: deprecated_subshell.cpp:31
A struct to hold (compiled) code segments.
Definition: BashppListener.h:232
std::string post_code
Definition: BashppListener.h:235
std::string code
Definition: BashppListener.h:234
std::string pre_code
Definition: BashppListener.h:233