I am trying to implement an include feature in the lexer so that when it hits '#include "filename"' it will switch to a stream of that file. I got it working using a lexer action shown below. When I run it it seg faults.
antlr4::ANTLRInputStream new_source(new_file); // new file is an open ifstream
int pos = _input->index();
filestack.push(std::make_pair(_input,pos)); //my stack to keep track of previous files
reset();
_input= static_cast<antlr4::CharStream*>(&new_source);
I checked that static_cast<> works and returns a non null pointer, and the assignment is successful. However, when it continues on it segfaults after it goes into the recompiled ANLTR runtime. Is there something I'm missing?
UPDATE: I just recompiled the c++ runtime with debug flags on, and now I see it's failing at LexerATNSimulator::failOrAccept when it returns _prevAccept.dfaState->prediction.
Also, this is what happens before the segfault:
It exits out of the custom lexer action and the LexerActionExecutor.
It enters LexerATNSimulator::accept.
exits LexerATNSimulator::accept.
Enters LexerATNSimulator::failOrAccept
Segfault
I am resetting the lexer when switching over, could that have some to do with the failure?