Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Lazily deserialize ATN in generated code #3562

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ protected:
public:
virtual std::unique_ptr\<antlr4::Token> nextToken() override {
if (dynamic_cast\<PositionAdjustingLexerATNSimulator *>(_interpreter) == nullptr) {
const auto &atn = _interpreter->atn;
auto &decisionToDFA = dynamic_cast\<antlr4::atn::LexerATNSimulator*>(_interpreter)->_decisionToDFA;
auto &sharedContextCache = dynamic_cast\<antlr4::atn::LexerATNSimulator*>(_interpreter)->getSharedContextCache();
delete _interpreter;
_interpreter = new PositionAdjustingLexerATNSimulator(this, *_atn, *_decisionToDFA, _sharedContextCache);
_interpreter = new PositionAdjustingLexerATNSimulator(this, atn, decisionToDFA, sharedContextCache);
jcking marked this conversation as resolved.
Show resolved Hide resolved
}

return antlr4::Lexer::nextToken();
Expand Down
225 changes: 125 additions & 100 deletions runtime/Cpp/runtime/src/tree/xpath/XPathLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,124 @@

using namespace antlr4;

namespace {

struct XPathLexerStaticData final {
XPathLexerStaticData(std::vector<std::string> ruleNames,
std::vector<std::string> channelNames,
std::vector<std::string> modeNames,
std::vector<std::string> literalNames,
std::vector<std::string> symbolicNames)
: ruleNames(std::move(ruleNames)), channelNames(std::move(channelNames)),
modeNames(std::move(modeNames)), literalNames(std::move(literalNames)),
symbolicNames(std::move(symbolicNames)),
vocabulary(this->literalNames, this->symbolicNames) {}

XPathLexerStaticData(const XPathLexerStaticData&) = delete;
XPathLexerStaticData(XPathLexerStaticData&&) = delete;
XPathLexerStaticData& operator=(const XPathLexerStaticData&) = delete;
XPathLexerStaticData& operator=(XPathLexerStaticData&&) = delete;

std::vector<antlr4::dfa::DFA> decisionToDFA;
antlr4::atn::PredictionContextCache sharedContextCache;
const std::vector<std::string> ruleNames;
const std::vector<std::string> channelNames;
const std::vector<std::string> modeNames;
const std::vector<std::string> literalNames;
const std::vector<std::string> symbolicNames;
const antlr4::dfa::Vocabulary vocabulary;
std::vector<uint16_t> serializedATN;
std::unique_ptr<antlr4::atn::ATN> atn;
};

std::once_flag XPathLexer_onceFlag;
XPathLexerStaticData *XPathLexerstaticData = nullptr;

void XPathLexer_initialize() {
assert(XPathLexerstaticData == nullptr);
auto staticData = std::make_unique<XPathLexerStaticData>(
std::vector<std::string>{
"ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", "NameChar", "NameStartChar",
"STRING"
},
std::vector<std::string>{
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
},
std::vector<std::string>{
"DEFAULT_MODE"
},
std::vector<std::string>{
"", "", "", "'//'", "'/'", "'*'", "'!'"
},
std::vector<std::string>{
"", "TOKEN_REF", "RULE_REF", "ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID",
"STRING"
}
);
static const uint16_t serializedATNSegment0[] = {
0x4, 0x0, 0x8, 0x32, 0x6, 0xffff, 0x2, 0x0, 0x7, 0x0, 0x2, 0x1, 0x7,
0x1, 0x2, 0x2, 0x7, 0x2, 0x2, 0x3, 0x7, 0x3, 0x2, 0x4, 0x7, 0x4,
0x2, 0x5, 0x7, 0x5, 0x2, 0x6, 0x7, 0x6, 0x2, 0x7, 0x7, 0x7, 0x1,
0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2, 0x1, 0x2,
0x1, 0x3, 0x1, 0x3, 0x1, 0x4, 0x1, 0x4, 0x5, 0x4, 0x1d, 0x8, 0x4,
0xa, 0x4, 0xc, 0x4, 0x20, 0x9, 0x4,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0, 0x0, 0x31, 0x10,
0x1, 0x0, 0x0, 0x0, 0x4, 0x0, 0x1e, 0x25, 0x2d, 0x1, 0x1, 0x4, 0x0,
};

size_t serializedATNSize = 0;
serializedATNSize += sizeof(serializedATNSegment0) / sizeof(serializedATNSegment0[0]);
staticData->serializedATN.reserve(serializedATNSize);

staticData->serializedATN.insert(staticData->serializedATN.end(), serializedATNSegment0,
serializedATNSegment0 + sizeof(serializedATNSegment0) / sizeof(serializedATNSegment0[0]));

atn::ATNDeserializer deserializer;
staticData->atn = deserializer.deserialize(staticData->serializedATN);

size_t count = staticData->atn->getNumberOfDecisions();
staticData->decisionToDFA.reserve(count);
for (size_t i = 0; i < count; i++) {
staticData->decisionToDFA.emplace_back(staticData->atn->getDecisionState(i), i);
}
XPathLexerstaticData = staticData.release();
}

}

XPathLexer::XPathLexer(CharStream *input) : Lexer(input) {
_interpreter = new atn::LexerATNSimulator(this, *_atn, *_decisionToDFA, _sharedContextCache);
XPathLexer::initialize();
_interpreter = new atn::LexerATNSimulator(this, *XPathLexerstaticData->atn, XPathLexerstaticData->decisionToDFA, XPathLexerstaticData->sharedContextCache);
}

XPathLexer::~XPathLexer() {
Expand All @@ -21,30 +136,29 @@ std::string XPathLexer::getGrammarFileName() const {
}

const std::vector<std::string>& XPathLexer::getRuleNames() const {
return _ruleNames;
return XPathLexerstaticData->ruleNames;
}

const std::vector<std::string>& XPathLexer::getChannelNames() const {
return _channelNames;
return XPathLexerstaticData->channelNames;
}

const std::vector<std::string>& XPathLexer::getModeNames() const {
return _modeNames;
return XPathLexerstaticData->modeNames;
}

dfa::Vocabulary& XPathLexer::getVocabulary() const {
return _vocabulary;
const dfa::Vocabulary& XPathLexer::getVocabulary() const {
return XPathLexerstaticData->vocabulary;
}

const std::vector<uint16_t>& XPathLexer::getSerializedATN() const {
return _serializedATN;
return XPathLexerstaticData->serializedATN;
}

const atn::ATN& XPathLexer::getATN() const {
return *_atn;
return *XPathLexerstaticData->atn;
}


void XPathLexer::action(RuleContext *context, size_t ruleIndex, size_t actionIndex) {
switch (ruleIndex) {
case 4: IDAction(antlrcpp::downCast<antlr4::RuleContext *>(context), actionIndex); break;
Expand All @@ -68,95 +182,6 @@ void XPathLexer::IDAction(antlr4::RuleContext *context, size_t actionIndex) {
}
}



// Static vars and initialization.
std::vector<dfa::DFA>* XPathLexer::_decisionToDFA = nullptr;
atn::PredictionContextCache XPathLexer::_sharedContextCache;

// We own the ATN which in turn owns the ATN states.
atn::ATN* XPathLexer::_atn = nullptr;
std::vector<uint16_t> XPathLexer::_serializedATN;

std::vector<std::string> XPathLexer::_ruleNames = {
"ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", "NameChar", "NameStartChar",
"STRING"
};

std::vector<std::string> XPathLexer::_channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
};

std::vector<std::string> XPathLexer::_modeNames = {
"DEFAULT_MODE"
};

std::vector<std::string> XPathLexer::_literalNames = {
"", "", "", "'//'", "'/'", "'*'", "'!'"
};

std::vector<std::string> XPathLexer::_symbolicNames = {
"", "TOKEN_REF", "RULE_REF", "ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID",
"STRING"
};

dfa::Vocabulary XPathLexer::_vocabulary(_literalNames, _symbolicNames);

XPathLexer::Initializer::Initializer() {
static const uint16_t serializedATNSegment0[] = {
0x4, 0x0, 0x8, 0x32, 0x6, 0xffff, 0x2, 0x0, 0x7, 0x0, 0x2, 0x1, 0x7,
0x1, 0x2, 0x2, 0x7, 0x2, 0x2, 0x3, 0x7, 0x3, 0x2, 0x4, 0x7, 0x4,
0x2, 0x5, 0x7, 0x5, 0x2, 0x6, 0x7, 0x6, 0x2, 0x7, 0x7, 0x7, 0x1,
0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2, 0x1, 0x2,
0x1, 0x3, 0x1, 0x3, 0x1, 0x4, 0x1, 0x4, 0x5, 0x4, 0x1d, 0x8, 0x4,
0xa, 0x4, 0xc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x30, 0x31, 0x5, 0x27, 0x0, 0x0, 0x31, 0x10,
0x1, 0x0, 0x0, 0x0, 0x4, 0x0, 0x1e, 0x25, 0x2d, 0x1, 0x1, 0x4, 0x0,
};

_serializedATN.insert(_serializedATN.end(), serializedATNSegment0,
serializedATNSegment0 + sizeof(serializedATNSegment0) / sizeof(serializedATNSegment0[0]));


atn::ATNDeserializer deserializer;
_atn = deserializer.deserialize(_serializedATN).release();

size_t count = _atn->getNumberOfDecisions();
_decisionToDFA = new std::vector<dfa::DFA>();
_decisionToDFA->reserve(count);
for (size_t i = 0; i < count; i++) {
_decisionToDFA->emplace_back(_atn->getDecisionState(i), i);
}
void XPathLexer::initialize() {
std::call_once(XPathLexer_onceFlag, XPathLexer_initialize);
}

XPathLexer::Initializer XPathLexer::_init;
34 changes: 12 additions & 22 deletions runtime/Cpp/runtime/src/tree/xpath/XPathLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "antlr4-runtime.h"




class XPathLexer : public antlr4::Lexer {
public:
enum {
Expand All @@ -17,41 +15,33 @@ class XPathLexer : public antlr4::Lexer {
};

explicit XPathLexer(antlr4::CharStream *input);
~XPathLexer();

~XPathLexer() override;

virtual std::string getGrammarFileName() const override;

virtual const std::vector<std::string>& getRuleNames() const override;

virtual const std::vector<std::string>& getChannelNames() const override;

virtual const std::vector<std::string>& getModeNames() const override;
virtual antlr4::dfa::Vocabulary& getVocabulary() const override;

virtual const antlr4::dfa::Vocabulary& getVocabulary() const override;

virtual const std::vector<uint16_t>& getSerializedATN() const override;

virtual const antlr4::atn::ATN& getATN() const override;

virtual void action(antlr4::RuleContext *context, size_t ruleIndex, size_t actionIndex) override;
private:
static std::vector<antlr4::dfa::DFA> *_decisionToDFA;
static antlr4::atn::PredictionContextCache _sharedContextCache;
static std::vector<std::string> _ruleNames;
static std::vector<std::string> _channelNames;
static std::vector<std::string> _modeNames;

static std::vector<std::string> _literalNames;
static std::vector<std::string> _symbolicNames;
static antlr4::dfa::Vocabulary _vocabulary;
static antlr4::atn::ATN *_atn;
static std::vector<uint16_t> _serializedATN;


// By default the static state used to implement the lexer is lazily initialized during the first
// call to the constructor. You can call this function if you wish to initialize the static state
// ahead of time.
static void initialize();
private:
// Individual action functions triggered by action() above.
void IDAction(antlr4::RuleContext *context, size_t actionIndex);

// Individual semantic predicate functions triggered by sempred() above.

struct Initializer {
Initializer();
};
static Initializer _init;
};

Loading