Implemented enhanced CommonToken::toString method. Fixes #1483.

This commit is contained in:
Mike Lischke 2016-12-14 17:04:05 +01:00
parent 50df59fea8
commit 3b5c2a34d2
4 changed files with 19 additions and 4 deletions

View File

@ -3,10 +3,14 @@
* can be found in the LICENSE.txt file in the project root.
*/
#include "misc/Interval.h"
#include "TokenSource.h"
#include "support/StringUtils.h"
#include "CharStream.h"
#include "Recognizer.h"
#include "Vocabulary.h"
#include "misc/Interval.h"
#include "support/StringUtils.h"
#include "support/CPPUtils.h"
#include "CommonToken.h"
@ -149,6 +153,10 @@ antlr4::CharStream *CommonToken::getInputStream() const {
}
std::string CommonToken::toString() const {
return toString(nullptr);
}
std::string CommonToken::toString(Recognizer *r) const {
std::stringstream ss;
std::string channelStr;
@ -164,8 +172,12 @@ std::string CommonToken::toString() const {
txt = "<no text>";
}
std::string typeString = std::to_string(symbolToNumeric(_type));
if (r != nullptr)
typeString = r->getVocabulary().getDisplayName(_type);
ss << "[@" << symbolToNumeric(getTokenIndex()) << "," << symbolToNumeric(_start) << ":" << symbolToNumeric(_stop)
<< "='" << txt << "',<" << symbolToNumeric(_type) << ">" << channelStr << "," << _line << ":"
<< "='" << txt << "',<" << typeString << ">" << channelStr << "," << _line << ":"
<< getCharPositionInLine() << "]";
return ss.str();

View File

@ -150,6 +150,7 @@ namespace antlr4 {
virtual std::string toString() const override;
virtual std::string toString(Recognizer *r) const;
private:
void InitializeInstanceFields();
};

View File

@ -5,6 +5,8 @@
#pragma once
#include "antlr4-common.h"
namespace antlr4 {
/// The default mechanism for creating tokens. It's used by default in Lexer and

View File

@ -11,7 +11,7 @@ namespace antlr4 {
namespace misc {
// Helpers to convert certain unsigned symbols (e.g. Token::EOF) to their original numeric value (e.g. -1)
// and vice version. This is needed mostly for intervals to keep their original order and for toString()
// and vice versa. This is needed mostly for intervals to keep their original order and for toString()
// methods to print the original numeric value (e.g. for tests).
size_t numericToSymbol(ssize_t v);
ssize_t symbolToNumeric(size_t v);