forked from jasder/antlr
Merge branch 'master' into feature/addtests
This commit is contained in:
commit
89c01af247
|
@ -35,10 +35,10 @@ using namespace org::antlr::v4::runtime;
|
|||
|
||||
ANTLRFileStream::ANTLRFileStream(const std::string &fileName) {
|
||||
_fileName = fileName;
|
||||
load(fileName);
|
||||
loadFromFile(fileName);
|
||||
}
|
||||
|
||||
void ANTLRFileStream::load(const std::string &fileName) {
|
||||
void ANTLRFileStream::loadFromFile(const std::string &fileName) {
|
||||
_fileName = fileName;
|
||||
if (_fileName.empty()) {
|
||||
return;
|
||||
|
|
|
@ -49,7 +49,7 @@ namespace runtime {
|
|||
// Assumes a file name encoded in UTF-8 and file content in the same encoding (with or w/o BOM).
|
||||
ANTLRFileStream(const std::string &fileName);
|
||||
|
||||
virtual void load(const std::string &fileName);
|
||||
virtual void loadFromFile(const std::string &fileName);
|
||||
virtual std::string getSourceName() const override;
|
||||
};
|
||||
|
||||
|
|
|
@ -45,8 +45,7 @@ using misc::Interval;
|
|||
|
||||
ANTLRInputStream::ANTLRInputStream(const std::string &input) {
|
||||
InitializeInstanceFields();
|
||||
|
||||
data = utfConverter.from_bytes(input);
|
||||
load(input);
|
||||
}
|
||||
|
||||
ANTLRInputStream::ANTLRInputStream(const char data[], size_t numberOfActualCharsInArray)
|
||||
|
@ -57,6 +56,11 @@ ANTLRInputStream::ANTLRInputStream(std::wistream &stream) {
|
|||
load(stream);
|
||||
}
|
||||
|
||||
void ANTLRInputStream::load(const std::string &input) {
|
||||
data = utfConverter.from_bytes(input);
|
||||
p = 0;
|
||||
}
|
||||
|
||||
void ANTLRInputStream::load(std::wistream &stream) {
|
||||
if (!stream.good() || stream.eof()) // No fail, bad or EOF.
|
||||
return;
|
||||
|
|
|
@ -57,6 +57,7 @@ namespace runtime {
|
|||
ANTLRInputStream(const char data[], size_t numberOfActualCharsInArray);
|
||||
ANTLRInputStream(std::wistream &stream);
|
||||
|
||||
virtual void load(const std::string &input);
|
||||
virtual void load(std::wistream &stream);
|
||||
|
||||
/// Reset the stream so that it's in the same state it was
|
||||
|
|
|
@ -213,6 +213,7 @@ void BufferedTokenStream::setup() {
|
|||
void BufferedTokenStream::setTokenSource(TokenSource *tokenSource) {
|
||||
_tokenSource = tokenSource;
|
||||
_tokens.clear();
|
||||
_fetchedEOF = false;
|
||||
_needSetup = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,12 @@
|
|||
using namespace antlrcpp;
|
||||
using namespace org::antlr::v4::runtime;
|
||||
|
||||
Lexer::Lexer(CharStream *input) : _input(input) {
|
||||
Lexer::Lexer() : Recognizer() {
|
||||
InitializeInstanceFields();
|
||||
_input = nullptr;
|
||||
}
|
||||
|
||||
Lexer::Lexer(CharStream *input) : Recognizer(), _input(input) {
|
||||
InitializeInstanceFields();
|
||||
}
|
||||
|
||||
|
|
|
@ -109,6 +109,7 @@ namespace runtime {
|
|||
/// the input char buffer. Use setText() or can set this instance var.
|
||||
std::string text;
|
||||
|
||||
Lexer();
|
||||
Lexer(CharStream *input);
|
||||
|
||||
virtual void reset();
|
||||
|
|
|
@ -63,17 +63,17 @@ namespace antlrcpp {
|
|||
|
||||
// Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places.
|
||||
template <typename T1, typename T2>
|
||||
bool is(T2 &obj) { // For value types.
|
||||
inline bool is(T2 &obj) { // For value types.
|
||||
return dynamic_cast<typename std::add_const<T1>::type *>(&obj) != nullptr;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
bool is(T2 *obj) { // For pointer types.
|
||||
inline bool is(T2 *obj) { // For pointer types.
|
||||
return dynamic_cast<T1>(obj) != nullptr;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
bool is(Ref<T2> obj) { // For shared pointers.
|
||||
inline bool is(Ref<T2> obj) { // For shared pointers.
|
||||
return dynamic_cast<T1*>(obj.get()) != nullptr;
|
||||
}
|
||||
|
||||
|
|
|
@ -69,12 +69,43 @@ std::string Trees::toStringTree(Ref<Tree> t, const std::vector<std::string> &rul
|
|||
|
||||
std::stringstream ss;
|
||||
ss << "(" << temp << ' ';
|
||||
/*
|
||||
for (size_t i = 0; i < t->getChildCount(); i++) {
|
||||
if (i > 0) {
|
||||
ss << ' ';
|
||||
}
|
||||
ss << toStringTree(t->getChild(i), ruleNames);
|
||||
}
|
||||
*/
|
||||
|
||||
// Implement the recursive walk as iteration to avoid trouble we deep nesting.
|
||||
std::stack<size_t> stack;
|
||||
size_t childIndex = 0;
|
||||
Ref<Tree> run = t;
|
||||
while (childIndex < run->getChildCount()) {
|
||||
if (childIndex > 0) {
|
||||
ss << ' ';
|
||||
}
|
||||
Ref<Tree> child = run->getChild(childIndex);
|
||||
std::string temp = antlrcpp::escapeWhitespace(Trees::getNodeText(child, ruleNames), false);
|
||||
if (child->getChildCount() > 0) {
|
||||
// Go deeper one level.
|
||||
stack.push(childIndex + 1);
|
||||
run = child;
|
||||
childIndex = 0;
|
||||
ss << "(" << temp << " ";
|
||||
} else {
|
||||
ss << temp;
|
||||
if (++childIndex == run->getChildCount() && stack.size() > 0) {
|
||||
// Reached the end of the current level. See if we can step up from here.
|
||||
childIndex = stack.top();
|
||||
stack.pop();
|
||||
run = run->getParent().lock();
|
||||
ss << ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ss << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ public:
|
|||
virtual void action(Ref\<RuleContext> context, int ruleIndex, int actionIndex) override;
|
||||
<endif>
|
||||
<if (sempredFuncs)>
|
||||
virtual bool sempred(Ref\<RuleContext> context, int ruleIndex, int predicateIndex) override;
|
||||
virtual bool sempred(Ref\<RuleContext> _localctx, int ruleIndex, int predicateIndex) override;
|
||||
<endif>
|
||||
|
||||
private:
|
||||
|
@ -237,11 +237,11 @@ void <r.factory.grammar.name>::<r.name>Action(Ref\<<r.ctxType>\> context, int ac
|
|||
>>
|
||||
|
||||
RuleSempredFunctionHeader(r, actions) ::= <<
|
||||
bool <r.name>Sempred(Ref\<<r.ctxType>\> context, int predicateIndex);
|
||||
bool <r.name>Sempred(Ref\<<r.ctxType>\> _localctx, int predicateIndex);
|
||||
>>
|
||||
|
||||
RuleSempredFunction(r, actions) ::= <<
|
||||
bool <r.factory.grammar.name>::<r.name>Sempred(Ref\<<r.ctxType>\> context, int predicateIndex) {
|
||||
bool <r.factory.grammar.name>::<r.name>Sempred(Ref\<<r.ctxType>\> _localctx, int predicateIndex) {
|
||||
switch (predicateIndex) {
|
||||
<actions: {index | case <index>: return <actions.(index)>;}; separator="\n">
|
||||
|
||||
|
@ -288,7 +288,7 @@ public:
|
|||
<funcs; separator = "\n">
|
||||
|
||||
<if (sempredFuncs)>
|
||||
virtual bool sempred(Ref\<RuleContext> context, int ruleIndex, int predicateIndex) override;
|
||||
virtual bool sempred(Ref\<RuleContext> _localctx, int ruleIndex, int predicateIndex) override;
|
||||
<sempredFuncs.values; separator = "\n">
|
||||
<endif>
|
||||
|
||||
|
|
Loading…
Reference in New Issue