Removed StringBuilder + stringconverter code.

Both can easily be implement using STL code.
This commit is contained in:
Mike Lischke 2016-04-06 16:28:05 +02:00
parent 3d17066e0c
commit 8f2e95516b
36 changed files with 214 additions and 537 deletions

View File

@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
272415E41CB54C08007E056A /* UnbufferedTokenStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C666A91C9584050021E494 /* UnbufferedTokenStream.cpp */; };
272415E51CB54C09007E056A /* UnbufferedTokenStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C666A91C9584050021E494 /* UnbufferedTokenStream.cpp */; };
2747A70A1CA691310030247B /* ConfigLookup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2747A7081CA691310030247B /* ConfigLookup.cpp */; };
2747A70B1CA691310030247B /* ConfigLookup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2747A7081CA691310030247B /* ConfigLookup.cpp */; };
2747A70C1CA691310030247B /* ConfigLookup.h in Headers */ = {isa = PBXBuildFile; fileRef = 2747A7091CA691310030247B /* ConfigLookup.h */; };
@ -405,12 +407,6 @@
27C669071C9585230021E494 /* CPPUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668EE1C9585230021E494 /* CPPUtils.h */; };
27C669081C9585230021E494 /* Declarations.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668EF1C9585230021E494 /* Declarations.h */; };
27C669091C9585230021E494 /* Declarations.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668EF1C9585230021E494 /* Declarations.h */; };
27C6690A1C9585230021E494 /* StringBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C668F01C9585230021E494 /* StringBuilder.cpp */; };
27C6690B1C9585230021E494 /* StringBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C668F01C9585230021E494 /* StringBuilder.cpp */; };
27C6690C1C9585230021E494 /* StringBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668F11C9585230021E494 /* StringBuilder.h */; };
27C6690D1C9585230021E494 /* StringBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668F11C9585230021E494 /* StringBuilder.h */; };
27C6690E1C9585230021E494 /* stringconverter.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668F21C9585230021E494 /* stringconverter.h */; };
27C6690F1C9585230021E494 /* stringconverter.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668F21C9585230021E494 /* stringconverter.h */; };
27C669101C9585230021E494 /* Strings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C668F31C9585230021E494 /* Strings.cpp */; };
27C669111C9585230021E494 /* Strings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C668F31C9585230021E494 /* Strings.cpp */; };
27C669121C9585230021E494 /* Strings.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C668F41C9585230021E494 /* Strings.h */; };
@ -753,9 +749,6 @@
27C668ED1C9585230021E494 /* CPPUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CPPUtils.cpp; sourceTree = "<group>"; };
27C668EE1C9585230021E494 /* CPPUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPPUtils.h; sourceTree = "<group>"; };
27C668EF1C9585230021E494 /* Declarations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Declarations.h; sourceTree = "<group>"; };
27C668F01C9585230021E494 /* StringBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringBuilder.cpp; sourceTree = "<group>"; };
27C668F11C9585230021E494 /* StringBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringBuilder.h; sourceTree = "<group>"; };
27C668F21C9585230021E494 /* stringconverter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = stringconverter.h; sourceTree = "<group>"; };
27C668F31C9585230021E494 /* Strings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Strings.cpp; sourceTree = "<group>"; };
27C668F41C9585230021E494 /* Strings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Strings.h; sourceTree = "<group>"; };
27C668F51C9585230021E494 /* guid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = guid.cpp; sourceTree = "<group>"; };
@ -1033,9 +1026,6 @@
27C668ED1C9585230021E494 /* CPPUtils.cpp */,
27C668EE1C9585230021E494 /* CPPUtils.h */,
27C668EF1C9585230021E494 /* Declarations.h */,
27C668F01C9585230021E494 /* StringBuilder.cpp */,
27C668F11C9585230021E494 /* StringBuilder.h */,
27C668F21C9585230021E494 /* stringconverter.h */,
27C668F31C9585230021E494 /* Strings.cpp */,
27C668F41C9585230021E494 /* Strings.h */,
27C668F51C9585230021E494 /* guid.cpp */,
@ -1340,7 +1330,6 @@
27C6680F1C95846E0021E494 /* OrderedATNConfigSet.h in Headers */,
27C666B11C9584050021E494 /* ANTLRErrorStrategy.h in Headers */,
27C667B91C95846E0021E494 /* ATNConfig.h in Headers */,
27C6690F1C9585230021E494 /* stringconverter.h in Headers */,
27C6670D1C9584050021E494 /* NoViableAltException.h in Headers */,
27C6699E1C9585B80021E494 /* ParseTreeListener.h in Headers */,
27C6681B1C95846E0021E494 /* PlusLoopbackState.h in Headers */,
@ -1354,7 +1343,6 @@
27C668571C95846E0021E494 /* StarLoopEntryState.h in Headers */,
27C6683F1C95846E0021E494 /* RuleTransition.h in Headers */,
27C667231C9584050021E494 /* Recognizer.h in Headers */,
27C6690D1C9585230021E494 /* StringBuilder.h in Headers */,
27C667F11C95846E0021E494 /* EmptyPredictionContext.h in Headers */,
27C667331C9584050021E494 /* TokenSource.h in Headers */,
27C6684F1C95846E0021E494 /* StarBlockStartState.h in Headers */,
@ -1447,7 +1435,6 @@
2747A70C1CA691310030247B /* ConfigLookup.h in Headers */,
27C668731C9584B60021E494 /* DFASerializer.h in Headers */,
27C667361C9584050021E494 /* TokenStream.h in Headers */,
27C6690E1C9585230021E494 /* stringconverter.h in Headers */,
27C666E41C9584050021E494 /* Exceptions.h in Headers */,
27C667321C9584050021E494 /* TokenSource.h in Headers */,
27C66A1A1C958AB30021E494 /* TagChunk.h in Headers */,
@ -1491,7 +1478,6 @@
27C668041C95846E0021E494 /* LoopEndState.h in Headers */,
27C6685A1C95846E0021E494 /* TokensStartState.h in Headers */,
27C668561C95846E0021E494 /* StarLoopEntryState.h in Headers */,
27C6690C1C9585230021E494 /* StringBuilder.h in Headers */,
27C667B41C95846E0021E494 /* ATN.h in Headers */,
27C668AC1C9584FA0021E494 /* EqualityComparator.h in Headers */,
27C669AD1C9585B80021E494 /* RuleNode.h in Headers */,
@ -1762,6 +1748,7 @@
27C666FB1C9584050021E494 /* Lexer.cpp in Sources */,
27C668351C95846E0021E494 /* RuleStartState.cpp in Sources */,
27C66A0B1C958AB30021E494 /* TagChunk.cpp in Sources */,
272415E51CB54C09007E056A /* UnbufferedTokenStream.cpp in Sources */,
27C668AB1C9584FA0021E494 /* EqualityComparator.cpp in Sources */,
27C666FF1C9584050021E494 /* LexerInterpreter.cpp in Sources */,
27C667FF1C95846E0021E494 /* LL1Analyzer.cpp in Sources */,
@ -1778,7 +1765,6 @@
27C667C71C95846E0021E494 /* ATNSerializer.cpp in Sources */,
27C6680D1C95846E0021E494 /* OrderedATNConfigSet.cpp in Sources */,
27C666DB1C9584050021E494 /* DefaultErrorStrategy.cpp in Sources */,
27C6690B1C9585230021E494 /* StringBuilder.cpp in Sources */,
27C666F11C9584050021E494 /* InterpreterRuleContext.cpp in Sources */,
27C6672D1C9584050021E494 /* TokenFactory.cpp in Sources */,
27C66A091C958AB30021E494 /* RuleTagToken.cpp in Sources */,
@ -1890,6 +1876,7 @@
27C667A61C95846E0021E494 /* AbstractPredicateTransition.cpp in Sources */,
27C667AE1C95846E0021E494 /* ArrayPredictionContext.cpp in Sources */,
27C669041C9585230021E494 /* CPPUtils.cpp in Sources */,
272415E41CB54C08007E056A /* UnbufferedTokenStream.cpp in Sources */,
27C667021C9584050021E494 /* LexerNoViableAltException.cpp in Sources */,
27C667EE1C95846E0021E494 /* EmptyPredictionContext.cpp in Sources */,
27C666C61C9584050021E494 /* CharStream.cpp in Sources */,
@ -1930,7 +1917,6 @@
27C6670E1C9584050021E494 /* Parser.cpp in Sources */,
27C667F61C95846E0021E494 /* LexerATNConfig.cpp in Sources */,
27C6683C1C95846E0021E494 /* RuleTransition.cpp in Sources */,
27C6690A1C9585230021E494 /* StringBuilder.cpp in Sources */,
27C667341C9584050021E494 /* TokenStream.cpp in Sources */,
27C669141C9585230021E494 /* guid.cpp in Sources */,
27C667281C9584050021E494 /* Token.cpp in Sources */,

View File

@ -85,6 +85,6 @@ void ANTLRFileStream::load(const std::string &fileName, const std::string &encod
}
}
std::string ANTLRFileStream::getSourceName() {
std::string ANTLRFileStream::getSourceName() const {
return _fileName;
}

View File

@ -52,7 +52,7 @@ namespace runtime {
virtual void load(const std::string &fileName, const std::string &encoding);
virtual std::string getSourceName() override;
virtual std::string getSourceName() const override;
};
} // namespace runtime

View File

@ -161,7 +161,7 @@ std::wstring ANTLRInputStream::getText(const Interval &interval) {
return data.substr(start, count);
}
std::string ANTLRInputStream::getSourceName() {
std::string ANTLRInputStream::getSourceName() const {
return name;
}

View File

@ -89,7 +89,7 @@ namespace runtime {
/// </summary>
virtual void seek(size_t index) override;
virtual std::wstring getText(const misc::Interval &interval) override;
virtual std::string getSourceName() override;
virtual std::string getSourceName() const override;
virtual std::wstring toString();
private:

View File

@ -33,7 +33,6 @@
#include "Lexer.h"
#include "RuleContext.h"
#include "Interval.h"
#include "StringBuilder.h"
#include "Exceptions.h"
#include "BufferedTokenStream.h"
@ -345,7 +344,7 @@ std::vector<Token*> BufferedTokenStream::filterForChannel(size_t from, size_t to
/**
* Get the text of all tokens in this buffer.
*/
std::string BufferedTokenStream::getSourceName()
std::string BufferedTokenStream::getSourceName() const
{
return tokenSource->getSourceName();
}
@ -367,15 +366,15 @@ std::wstring BufferedTokenStream::getText(const misc::Interval &interval) {
stop = (int)tokens.size() - 1;
}
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder();
std::wstringstream ss;
for (size_t i = (size_t)start; i <= (size_t)stop; i++) {
Token *t = tokens[i];
if (t->getType() == EOF) {
break;
}
buf->append(t->getText());
ss << t->getText();
}
return buf->toString();
return ss.str();
}
std::wstring BufferedTokenStream::getText(RuleContext *ctx) {

View File

@ -200,7 +200,7 @@ namespace runtime {
virtual std::vector<Token*> filterForChannel(size_t from, size_t to, int channel);
public:
virtual std::string getSourceName() override;
virtual std::string getSourceName() const override;
virtual std::wstring getText() override;
virtual std::wstring getText(const misc::Interval &interval) override;
virtual std::wstring getText(RuleContext *ctx) override;

View File

@ -35,7 +35,6 @@
#include "Parser.h"
#include "Interval.h"
#include "DFA.h"
#include "stringconverter.h"
#include "DiagnosticErrorListener.h"
@ -83,12 +82,12 @@ std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer,
const std::vector<std::wstring>& ruleNames = recognizer->getRuleNames();
if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) {
return antlrcpp::StringConverterHelper::toString(decision);
return std::to_wstring(decision);
}
std::wstring ruleName = ruleNames[(size_t)ruleIndex];
if (ruleName == L"" || ruleName.empty()) {
return antlrcpp::StringConverterHelper::toString(decision);
return std::to_wstring(decision);
}
return std::to_wstring(decision) + L"(" + ruleName + L")";

View File

@ -235,7 +235,7 @@ namespace runtime {
/// non-null, non-empty string. If such a name is not known, this method
/// returns <seealso cref="#UNKNOWN_SOURCE_NAME"/>.
/// </summary>
virtual std::string getSourceName() = 0;
virtual std::string getSourceName() const = 0;
};
} // namespace runtime

View File

@ -34,8 +34,6 @@
#include "Interval.h"
#include "CommonTokenFactory.h"
#include "LexerNoViableAltException.h"
#include "stringconverter.h"
#include "StringBuilder.h"
#include "ANTLRErrorListener.h"
#include "CPPUtils.h"
@ -289,22 +287,16 @@ void Lexer::notifyListeners(LexerNoViableAltException *e) {
}
std::wstring Lexer::getErrorDisplay(const std::wstring &s) {
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder();
std::wstringstream ss;
for (size_t i = 0; i < s.length(); i++) {
char c = ((char*)s.c_str())[i];
buf->append(getErrorDisplay(c));
ss << getErrorDisplay(c);
}
/*
for (auto c : s.toCharArray()) {
buf->append(getErrorDisplay(c));
}*/
return buf->toString();
return ss.str();
}
std::wstring Lexer::getErrorDisplay(int c) {
std::wstring s = antlrcpp::StringConverterHelper::toString(static_cast<wchar_t>(c));
std::wstring s;
switch (c) {
case EOF :
s = L"<EOF>";
@ -318,6 +310,9 @@ std::wstring Lexer::getErrorDisplay(int c) {
case L'\r' :
s = L"\\r";
break;
default:
s = std::to_wstring(c);
break;
}
return s;
}

View File

@ -32,7 +32,6 @@
#include "Trees.h"
#include "Interval.h"
#include "Parser.h"
#include "StringBuilder.h"
#include "RuleContext.h"
@ -92,12 +91,14 @@ std::wstring RuleContext::getText() {
return L"";
}
antlrcpp::StringBuilder builder;
std::wstringstream ss;
for (size_t i = 0; i < getChildCount(); i++) {
builder.append(getChild(i)->getText());
if (i > 0)
ss << L", ";
ss << getChild(i)->getText();
}
return builder.toString();
return ss.str();
}
ssize_t RuleContext::getRuleIndex() const {
@ -159,31 +160,32 @@ std::wstring RuleContext::toString(const std::vector<std::wstring> &ruleNames) {
std::wstring RuleContext::toString(const std::vector<std::wstring> &ruleNames, RuleContext *stop) {
antlrcpp::StringBuilder buffer;
std::wstringstream ss;
RuleContext *p = this;
buffer.append(L"[");
ss << L"[";
while (p != nullptr && p != stop) {
if (ruleNames.empty()) {
if (!p->isEmpty()) {
buffer.append(p->invokingState);
ss << p->invokingState;
}
} else {
ssize_t ruleIndex = p->getRuleIndex();
std::wstring ruleName = (ruleIndex >= 0 && ruleIndex < (ssize_t)ruleNames.size()) ? ruleNames[(size_t)ruleIndex] : std::to_wstring(ruleIndex);
buffer.append(ruleName);
ss << ruleName;
}
if (p->parent != nullptr && (ruleNames.size() > 0 || !p->parent->isEmpty())) {
buffer.append(L" ");
ss << L" ";
}
p = p->parent;
}
buffer.append(L"]");
ss << L"]";
return buffer.toString();
return ss.str();
}
std::wstring RuleContext::toString() {

View File

@ -191,7 +191,7 @@ size_t UnbufferedCharStream::size() {
throw UnsupportedOperationException("Unbuffered stream cannot know its size");
}
std::string UnbufferedCharStream::getSourceName() {
std::string UnbufferedCharStream::getSourceName() const {
return name;
}
@ -216,7 +216,7 @@ std::wstring UnbufferedCharStream::getText(const misc::Interval &interval) {
return std::wstring(data, i, (size_t)interval.length());
}
size_t UnbufferedCharStream::getBufferStartIndex() {
size_t UnbufferedCharStream::getBufferStartIndex() const {
return currentCharIndex - p;
}

View File

@ -167,15 +167,12 @@ namespace runtime {
/// sliding window. Move {@code p} to {@code index-bufferStartIndex}.
/// </summary>
virtual void seek(size_t index) override;
virtual size_t size() override;
virtual std::string getSourceName() override;
virtual std::string getSourceName() const override;
virtual std::wstring getText(const misc::Interval &interval) override;
protected:
size_t getBufferStartIndex();
size_t getBufferStartIndex() const;
private:
void InitializeInstanceFields();

View File

@ -29,8 +29,6 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Token.h"
#include "Exceptions.h"
#include "assert.h"
@ -40,6 +38,8 @@
#include "RuleContext.h"
#include "WritableToken.h"
#include "UnbufferedTokenStream.h"
using namespace org::antlr::v4::runtime;
UnbufferedTokenStream::UnbufferedTokenStream(TokenSource* tokenSource) : UnbufferedTokenStream(tokenSource, 256) {
@ -49,16 +49,15 @@ UnbufferedTokenStream::UnbufferedTokenStream(TokenSource* tokenSource, int buffe
{
InitializeInstanceFields();
this->tokenSource = tokenSource;
tokens = new std::vector<Token*>(); // TODO do we need to actually new this?
n = 0;
fill(1); // prime the pump
}
Token* UnbufferedTokenStream::get(int i)
Token* UnbufferedTokenStream::get(size_t i) const
{ // get absolute index
int bufferStartIndex = getBufferStartIndex();
if (i < bufferStartIndex || i >= bufferStartIndex + n) {
throw IndexOutOfBoundsException(std::wstring(L"get(") + std::to_wstring(i) + std::wstring(L") outside buffer: ") + std::to_wstring(bufferStartIndex) + std::wstring(L"..") + std::to_wstring(bufferStartIndex + n));
size_t bufferStartIndex = getBufferStartIndex();
if (i < bufferStartIndex || i >= bufferStartIndex + tokens.size()) {
throw IndexOutOfBoundsException(std::string("get(") + std::to_string(i) + std::string(") outside buffer: ")
+ std::to_string(bufferStartIndex) + std::string("..") + std::to_string(bufferStartIndex + tokens.size()));
}
return tokens[i - bufferStartIndex];
}
@ -70,25 +69,25 @@ Token* UnbufferedTokenStream::LT(ssize_t i)
}
sync(i);
int index = p + i - 1;
ssize_t index = (ssize_t)p + i - 1;
if (index < 0) {
throw IndexOutOfBoundsException(std::wstring(L"LT(") + std::to_wstring(i) + std::wstring(L") gives negative index"));
throw IndexOutOfBoundsException(std::string("LT(") + std::to_string(i) + std::string(") gives negative index"));
}
if (index >= tokens.size()) {
assert(n > 0 && tokens[n - 1]->getType() == EOF);
return tokens[n - 1];
if (index >= (ssize_t)tokens.size()) {
assert(tokens.size() > 0 && tokens.back()->getType() == EOF);
return tokens.back();
}
return tokens[index];
return tokens[(size_t)index];
}
int UnbufferedTokenStream::LA(ssize_t i)
ssize_t UnbufferedTokenStream::LA(ssize_t i)
{
return LT(i)->getType();
}
TokenSource* UnbufferedTokenStream::getTokenSource()
TokenSource* UnbufferedTokenStream::getTokenSource() const
{
return tokenSource;
}
@ -111,21 +110,22 @@ std::wstring UnbufferedTokenStream::getText(Token* start, Token* stop)
void UnbufferedTokenStream::consume()
{
if (LA(1) == EOF) {
throw IllegalStateException(L"cannot consume EOF");
throw IllegalStateException("cannot consume EOF");
}
// buf always has at least tokens[p==0] in this method due to ctor
lastToken = tokens[p]; // track last token for LT(-1)
// if we're at last token and no markers, opportunity to flush buffer
if (p == n - 1 && numMarkers == 0) {
n = 0;
p = -1; // p++ will leave this at 0
if (p == tokens.size() - 1 && numMarkers == 0) {
tokens.clear();
p = 0;
lastTokenBufferStart = lastToken;
} else {
++p;
}
p++;
currentTokenIndex++;
++currentTokenIndex;
sync(1);
}
@ -134,11 +134,11 @@ void UnbufferedTokenStream::consume()
/// {@code p} index is {@code tokens.length-1}. {@code p+need-1} is the tokens index 'need' elements
/// ahead. If we need 1 element, {@code (p+1-1)==p} must be less than {@code tokens.length}.
/// </summary>
void UnbufferedTokenStream::sync(int want)
void UnbufferedTokenStream::sync(ssize_t want)
{
int need = (p + want - 1) - n + 1; // how many more elements we need?
ssize_t need = ((ssize_t)p + want - 1) - (ssize_t)tokens.size() + 1; // how many more elements we need?
if (need > 0) {
fill(need);
fill((size_t)need);
}
}
@ -147,10 +147,10 @@ void UnbufferedTokenStream::sync(int want)
/// actually added to the buffer. If the return value is less than {@code n},
/// then EOF was reached before {@code n} tokens could be added.
/// </summary>
int UnbufferedTokenStream::fill(int n)
size_t UnbufferedTokenStream::fill(size_t n)
{
for (int i = 0; i < n; i++) {
if (this->n > 0 && tokens[this->n - 1]->getType() == EOF) {
for (size_t i = 0; i < n; i++) {
if (tokens.size() > 0 && tokens.back()->getType() == EOF) {
return i;
}
@ -164,12 +164,11 @@ int UnbufferedTokenStream::fill(int n)
void UnbufferedTokenStream::add(Token* t)
{
WritableToken *writable = dynamic_cast<WritableToken *>(t);
if (t != nullptr) {
t->setTokenIndex(getBufferStartIndex() + n);
if (writable != nullptr) {
writable->setTokenIndex(int(getBufferStartIndex() + tokens.size()));
}
tokens.push_back(t);
n++
}
/// <summary>
@ -179,7 +178,7 @@ void UnbufferedTokenStream::add(Token* t)
/// protection against misuse where {@code seek()} is called on a mark or
/// {@code release()} is called in the wrong order.
/// </summary>
int UnbufferedTokenStream::mark()
ssize_t UnbufferedTokenStream::mark()
{
if (numMarkers == 0) {
lastTokenBufferStart = lastToken;
@ -190,12 +189,11 @@ int UnbufferedTokenStream::mark()
return mark;
}
template <typename T>
void UnbufferedTokenStream::release(int marker)
void UnbufferedTokenStream::release(ssize_t marker)
{
int expectedMark = -numMarkers;
ssize_t expectedMark = -numMarkers;
if (marker != expectedMark) {
throw IllegalStateException(L"release() called with an invalid marker.");
throw IllegalStateException("release() called with an invalid marker.");
}
numMarkers--;
@ -203,8 +201,7 @@ void UnbufferedTokenStream::release(int marker)
if (p > 0) {
// Copy tokens[p]..tokens[n-1] to tokens[0]..tokens[(n-1)-p], reset ptrs
// p is last valid token; move nothing if p==n as we have no valid char
arraycopy(tokens, p, tokens, 0, n - p); // shift n-p tokens from p to 0
n = n - p;
std::vector<Token *>(tokens.begin() + (ssize_t)p, tokens.end()).swap(tokens);
p = 0;
}
@ -212,7 +209,7 @@ void UnbufferedTokenStream::release(int marker)
}
}
int UnbufferedTokenStream::index()
size_t UnbufferedTokenStream::index()
{
return currentTokenIndex;
}
@ -224,70 +221,73 @@ void UnbufferedTokenStream::seek(size_t index)
}
if (index > currentTokenIndex) {
sync(index - currentTokenIndex);
index = std::min(index, getBufferStartIndex() + n - 1);
sync(ssize_t(index - currentTokenIndex));
index = std::min(index, getBufferStartIndex() + tokens.size() - 1);
}
int bufferStartIndex = getBufferStartIndex();
int i = index - bufferStartIndex;
if (i < 0) {
throw IllegalArgumentException(std::wstring(L"cannot seek to negative index ") + std::to_wstring(index));
size_t bufferStartIndex = getBufferStartIndex();
if (bufferStartIndex > index) {
throw IllegalArgumentException(std::string("cannot seek to negative index ") + std::to_string(index));
}
else if (i >= n) {
throw UnsupportedOperationException(std::wstring(L"seek to index outside buffer: ") + std::to_wstring(index) + std::wstring(L" not in ") + std::to_wstring(bufferStartIndex) + std::wstring(L"..") + std::to_wstring(bufferStartIndex + n));
size_t i = index - bufferStartIndex;
if (i >= tokens.size()) {
throw UnsupportedOperationException(std::string("seek to index outside buffer: ") + std::to_string(index) +
" not in " + std::to_string(bufferStartIndex) + ".." + std::to_string(bufferStartIndex + tokens.size()));
}
p = i;
currentTokenIndex = index;
if (p == 0) {
lastToken = lastTokenBufferStart;
}
else {
} else {
lastToken = tokens[p - 1];
}
}
size_t UnbufferedTokenStream::size()
{
throw UnsupportedOperationException(L"Unbuffered stream cannot know its size");
throw UnsupportedOperationException("Unbuffered stream cannot know its size");
}
std::string UnbufferedTokenStream::getSourceName()
std::string UnbufferedTokenStream::getSourceName() const
{
return tokenSource->getSourceName();
}
std::wstring UnbufferedTokenStream::getText(const misc::Interval &interval)
{
int bufferStartIndex = getBufferStartIndex();
int bufferStopIndex = bufferStartIndex + tokens.size() - 1;
size_t bufferStartIndex = getBufferStartIndex();
size_t bufferStopIndex = bufferStartIndex + tokens.size() - 1;
int start = interval->a;
int stop = interval->b;
size_t start = (size_t)interval.a;
size_t stop = (size_t)interval.b;
if (start < bufferStartIndex || stop > bufferStopIndex) {
throw UnsupportedOperationException(std::wstring(L"interval ") + interval->toString() + std::wstring(L" not in token buffer window: ") + std::to_wstring(bufferStartIndex) + std::wstring(L"..") + std::to_wstring(bufferStopIndex));
throw UnsupportedOperationException(std::string("interval ") + interval.toString() +
" not in token buffer window: " + std::to_string(bufferStartIndex) + ".." + std::to_string(bufferStopIndex));
}
int a = start - bufferStartIndex;
int b = stop - bufferStartIndex;
size_t a = start - bufferStartIndex;
size_t b = stop - bufferStartIndex;
StringBuilder* buf = new StringBuilder();
for (int i = a; i <= b; i++) {
Token* t = tokens[i];
buf->append(t->getText());
std::wstringstream ss;
for (size_t i = a; i <= b; i++) {
Token *t = tokens[i];
if (i > 0)
ss << L", ";
ss << t->getText();
}
return buf->toString();
return ss.str();
}
int UnbufferedTokenStream::getBufferStartIndex()
size_t UnbufferedTokenStream::getBufferStartIndex() const
{
return currentTokenIndex - p;
}
void UnbufferedTokenStream::InitializeInstanceFields()
{
n = 0;
p = 0;
numMarkers = 0;
currentTokenIndex = 0;

View File

@ -48,7 +48,7 @@ namespace runtime {
/// we start filling at index 0 again.
/// </summary>
std::vector<Token *>tokens;
std::vector<Token *> tokens;
/// <summary>
/// 0..n-1 index into <seealso cref="#tokens tokens"/> of next token.
@ -56,7 +56,7 @@ namespace runtime {
/// The {@code LT(1)} token is {@code tokens[p]}. If {@code p == n}, we are
/// out of buffered tokens.
/// </summary>
int p;
size_t p;
/// <summary>
/// Count up with <seealso cref="#mark mark()"/> and down with
@ -85,17 +85,14 @@ namespace runtime {
/// This value is used to set the token indexes if the stream provides tokens
/// that implement <seealso cref="WritableToken"/>.
/// </summary>
int currentTokenIndex;
size_t currentTokenIndex;
public:
UnbufferedTokenStream(TokenSource *tokenSource);
UnbufferedTokenStream(TokenSource *tokenSource, int bufferSize);
virtual Token *get(size_t i) const override;
virtual Token *LT(ssize_t i) override;
virtual ssize_t LA(ssize_t i) override;
virtual TokenSource *getTokenSource() const override;
@ -104,6 +101,7 @@ namespace runtime {
virtual std::wstring getText() override;
virtual std::wstring getText(RuleContext *ctx) override;
virtual std::wstring getText(Token *start, Token *stop) override;
virtual void consume() override;
/// <summary>
@ -112,15 +110,14 @@ namespace runtime {
/// ahead. If we need 1 element, {@code (p+1-1)==p} must be less than {@code tokens.length}.
/// </summary>
protected:
virtual void sync(int want);
virtual void sync(ssize_t want);
/// <summary>
/// Add {@code n} elements to the buffer. Returns the number of tokens
/// actually added to the buffer. If the return value is less than {@code n},
/// then EOF was reached before {@code n} tokens could be added.
/// </summary>
virtual int fill(int n);
virtual size_t fill(size_t n);
virtual void add(Token *t);
/// <summary>
@ -132,19 +129,14 @@ namespace runtime {
/// </summary>
public:
virtual ssize_t mark() override;
virtual void release(ssize_t marker) override;
virtual size_t index() override;
virtual void seek(size_t index) override;
virtual size_t size() override;
virtual std::string getSourceName() override;
virtual std::string getSourceName() const override;
protected:
int getBufferStartIndex();
size_t getBufferStartIndex() const;
private:
void InitializeInstanceFields();

View File

@ -30,7 +30,6 @@
*/
#include "MurmurHash.h"
#include "StringBuilder.h"
#include "PredictionContext.h"
#include "ATNConfig.h"

View File

@ -113,29 +113,24 @@ namespace atn {
virtual std::wstring toString();
std::wstring toString(bool showAlt) {
antlrcpp::StringBuilder buf;
std::wstringstream ss(L" (");
buf.append(L'(');
buf.append(state);
ss << state;
if (showAlt) {
buf.append(L",");
buf.append(alt);
ss << L"," << alt;
}
if (context != nullptr) {
buf.append(L",[");
buf.append(context->toString());
buf.append(L"]");
ss << L",[" << context->toString() << L"]";
}
if (semanticContext != nullptr && semanticContext != SemanticContext::NONE) {
buf.append(L",");
buf.append(semanticContext.get());
ss << L"," << semanticContext.get();
}
if (reachesIntoOuterContext > 0) {
buf.append(L",up=").append(reachesIntoOuterContext);
ss << L",up=" << reachesIntoOuterContext;
}
buf.append(L')');
ss << L')';
return buf.toString();
return ss.str();
}
};

View File

@ -227,25 +227,25 @@ void ATNConfigSet::setReadonly(bool readonly) {
}
std::wstring ATNConfigSet::toString() {
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder();
std::wstringstream ss;
for (size_t i = 0; i < elements().size(); i++) {
buf->append(elements().at(i)->toString());
ss << elements().at(i)->toString();
}
if (hasSemanticContext) {
buf->append(L",hasSemanticContext=").append(hasSemanticContext);
ss << L",hasSemanticContext = " << hasSemanticContext;
}
if (uniqueAlt != ATN::INVALID_ALT_NUMBER) {
buf->append(L",uniqueAlt=").append(uniqueAlt);
ss << L",uniqueAlt = " << uniqueAlt;
}
if (conflictingAlts != nullptr) {
buf->append(L",conflictingAlts=");
buf->append(conflictingAlts->toString());
ss << L",conflictingAlts = ";
ss << conflictingAlts->toString();
}
if (dipsIntoOuterContext) {
buf->append(L",dipsIntoOuterContext");
ss << L", dipsIntoOuterContext";
}
return buf->toString();
return ss.str();
}
bool ATNConfigSet::remove(void *o) {

View File

@ -51,7 +51,6 @@
#include "AtomTransition.h"
#include "ActionTransition.h"
#include "ATNDeserializer.h"
#include "stringconverter.h"
#include "TokensStartState.h"
#include "Exceptions.h"
@ -523,7 +522,7 @@ std::wstring ATNSerializer::getTokenName(ssize_t t) {
return tokenNames[(size_t)t];
}
return antlrcpp::StringConverterHelper::toString(t);
return std::to_wstring(t);
}
std::wstring ATNSerializer::getSerializedAsString(ATN *atn) {

View File

@ -32,7 +32,6 @@
#include "ATN.h"
#include "Transition.h"
#include "IntervalSet.h"
#include "stringconverter.h"
#include "ATNState.h"
@ -64,7 +63,7 @@ bool ATNState::isNonGreedyExitState() {
}
std::wstring ATNState::toString() const {
return antlrcpp::StringConverterHelper::toString(stateNumber);
return std::to_wstring(stateNumber);
}
std::vector<Transition*> ATNState::getTransitions() {

View File

@ -81,24 +81,23 @@ std::wstring ArrayPredictionContext::toString() {
if (isEmpty()) {
return L"[]";
}
antlrcpp::StringBuilder buf;
buf.append(L"[");
for (std::vector<int>::size_type i = 0; i < returnStates.size(); i++) {
std::wstringstream ss(L"[");
for (size_t i = 0; i < returnStates.size(); i++) {
if (i > 0) {
buf.append(L", ");
ss << L", ";
}
if (returnStates[i] == EMPTY_RETURN_STATE) {
buf.append(L"$");
ss << L"$";
continue;
}
buf.append(std::to_wstring(returnStates.at(i)));
ss << returnStates[i];
if (!parents[i].expired()) {
buf.append(L" ");
buf.append(parents[i].lock()->toString());
ss << L" " << parents[i].lock()->toString();
} else {
buf.append(L"null");
ss << L"null";
}
}
buf.append(L"]");
return buf.toString();
ss << L"]";
return ss.str();
}

View File

@ -29,7 +29,6 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stringconverter.h"
#include "IntervalSet.h"
#include "Transition.h"
@ -54,5 +53,5 @@ bool AtomTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol)
}
std::wstring AtomTransition::toString() const {
return antlrcpp::StringConverterHelper::toString(_label);
return std::to_wstring(_label);
}

View File

@ -49,7 +49,6 @@
#include "RuleStopState.h"
#include "ATNConfigSet.h"
#include "ATNConfig.h"
#include "stringconverter.h"
#include "Interval.h"
#include "ANTLRErrorListener.h"
@ -969,7 +968,7 @@ std::wstring ParserATNSimulator::getTokenName(ssize_t t) {
return tokensNames[(size_t)t] + L"<" + std::to_wstring(t) + L">";
}
}
return StringConverterHelper::toString(t);
return std::to_wstring(t);
}
std::wstring ParserATNSimulator::getLookaheadName(TokenStream *input) {

View File

@ -30,7 +30,6 @@
*/
#include "PredicateTransition.h"
#include "stringconverter.h"
using namespace org::antlr::v4::runtime::atn;
@ -54,5 +53,5 @@ std::shared_ptr<SemanticContext::Predicate> PredicateTransition::getPredicate()
}
std::wstring PredicateTransition::toString() const {
return std::wstring(L"pred_") + antlrcpp::StringConverterHelper::toString(ruleIndex) + std::wstring(L":") + antlrcpp::StringConverterHelper::toString(predIndex);
return std::wstring(L"pred_") + std::to_wstring(ruleIndex) + std::wstring(L":") + std::to_wstring(predIndex);
}

View File

@ -34,7 +34,6 @@
#include "ArrayPredictionContext.h"
#include "RuleContext.h"
#include "RuleTransition.h"
#include "stringconverter.h"
#include "Arrays.h"
#include "CPPUtils.h"
@ -404,9 +403,9 @@ std::wstring PredictionContext::toDOTString(PredictionContextRef context) {
if (context == nullptr) {
return L"";
}
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder();
buf->append(L"digraph G {\n");
buf->append(L"rankdir=LR;\n");
std::wstringstream ss;
ss << L"digraph G {\n" << L"rankdir=LR;\n";
std::vector<PredictionContextRef> nodes = getAllContextNodes(context);
std::sort(nodes.begin(), nodes.end(), [](PredictionContextRef o1, PredictionContextRef o2) {
@ -415,33 +414,31 @@ std::wstring PredictionContext::toDOTString(PredictionContextRef context) {
for (auto current : nodes) {
if (is<SingletonPredictionContext>(current)) {
std::wstring s = antlrcpp::StringConverterHelper::toString(current->id);
buf->append(L" s").append(s);
std::wstring returnState = antlrcpp::StringConverterHelper::toString(current->getReturnState(0));
std::wstring s = std::to_wstring(current->id);
ss << L" s" << s;
std::wstring returnState = std::to_wstring(current->getReturnState(0));
if (is<EmptyPredictionContext>(current)) {
returnState = L"$";
}
buf->append(L" [label=\"").append(returnState).append(L"\"];\n");
ss << L" [label=\"" << returnState << L"\"];\n";
continue;
}
std::shared_ptr<ArrayPredictionContext> arr = std::static_pointer_cast<ArrayPredictionContext>(current);
buf->append(L" s").append(arr->id);
buf->append(L" [shape=box, label=\"");
buf->append(L"[");
ss << L" s" << arr->id << L" [shape=box, label=\"" << L"[";
bool first = true;
for (auto inv : arr->returnStates) {
if (!first) {
buf->append(L", ");
ss << L", ";
}
if (inv == EMPTY_RETURN_STATE) {
buf->append(L"$");
ss << L"$";
} else {
buf->append(inv);
ss << inv;
}
first = false;
}
buf->append(L"]");
buf->append(L"\"];\n");
ss << L"]";
ss << L"\"];\n";
}
for (auto current : nodes) {
@ -452,21 +449,17 @@ std::wstring PredictionContext::toDOTString(PredictionContextRef context) {
if (current->getParent(i).expired()) {
continue;
}
std::wstring s = antlrcpp::StringConverterHelper::toString(current->id);
buf->append(L" s").append(s);
buf->append(L"->");
buf->append(L"s");
buf->append(current->getParent(i).lock()->id);
ss << L" s" << current->id << L"->" << L"s" << current->getParent(i).lock()->id;
if (current->size() > 1) {
buf->append(std::wstring(L" [label=\"parent[") + antlrcpp::StringConverterHelper::toString(i) + std::wstring(L"]\"];\n"));
ss << L" [label=\"parent[" << i << L"]\"];\n";
} else {
buf->append(L";\n");
ss << L";\n";
}
}
}
buf->append(L"}\n");
return buf->toString();
ss << L"}\n";
return ss.str();
}
// The "visited" map is just a temporary structure to control the retrieval process (which is recursive).
@ -579,9 +572,8 @@ std::vector<std::wstring> PredictionContext::toStrings(Recognizer *recognizer, P
bool last = true;
PredictionContext *p = this;
int stateNumber = currentState;
antlrcpp::StringBuilder localBuffer;
localBuffer.append(L"[");
std::wstringstream ss(L"[");
bool outerContinue = false;
while (!p->isEmpty() && p != stop.get()) {
size_t index = 0;
@ -602,23 +594,23 @@ std::vector<std::wstring> PredictionContext::toStrings(Recognizer *recognizer, P
}
if (recognizer != nullptr) {
if (localBuffer.length() > 1) {
if (ss.tellp() > 1) {
// first char is '[', if more than that this isn't the first rule
localBuffer.append(L' ');
ss << L' ';
}
const ATN &atn = recognizer->getATN();
ATNState *s = atn.states[(size_t)stateNumber];
std::wstring ruleName = recognizer->getRuleNames()[(size_t)s->ruleIndex];
localBuffer.append(ruleName);
ss << ruleName;
} else if (p->getReturnState(index) != EMPTY_RETURN_STATE) {
if (!p->isEmpty()) {
if (localBuffer.length() > 1) {
if (ss.tellp() > 1) {
// first char is '[', if more than that this isn't the first rule
localBuffer.append(L' ');
ss << L' ';
}
localBuffer.append(p->getReturnState(index));
ss << p->getReturnState(index);
}
}
stateNumber = p->getReturnState(index);
@ -628,8 +620,8 @@ std::vector<std::wstring> PredictionContext::toStrings(Recognizer *recognizer, P
if (outerContinue)
continue;
localBuffer.append(L"]");
result.push_back(localBuffer.toString());
ss << L"]";
result.push_back(ss.str());
if (last) {
break;

View File

@ -35,7 +35,6 @@
#include "EqualityComparator.h"
#include "ATN.h"
#include "ATNState.h"
#include "StringBuilder.h"
namespace org {
namespace antlr {

View File

@ -30,7 +30,6 @@
*/
#include "EmptyPredictionContext.h"
#include "stringconverter.h"
#include "SingletonPredictionContext.h"
@ -87,7 +86,7 @@ std::wstring SingletonPredictionContext::toString() const {
if (returnState == EMPTY_RETURN_STATE) {
return L"$";
}
return antlrcpp::StringConverterHelper::toString(returnState);
return std::to_wstring(returnState);
}
return antlrcpp::StringConverterHelper::toString(returnState) + std::wstring(L" ") + up;
return std::to_wstring(returnState) + L" " + up;
}

View File

@ -30,11 +30,8 @@
*/
#include "DFA.h"
#include "StringBuilder.h"
#include "DFAState.h"
#include "stringconverter.h"
#include "DFASerializer.h"
using namespace org::antlr::v4::runtime::dfa;
@ -46,25 +43,21 @@ std::wstring DFASerializer::toString() {
if (dfa->s0 == nullptr) {
return L"";
}
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder();
std::wstringstream ss;
std::vector<DFAState*> states = dfa->getStates();
for (auto s : states) {
for (size_t i = 0; i < s->edges.size(); i++) {
DFAState *t = s->edges[i];
if (t != nullptr && t->stateNumber != INT16_MAX) {
buf->append(getStateString(s));
ss << getStateString(s);
std::wstring label = getEdgeLabel(i);
buf->append(L"-"); buf->append(label); buf->append(L"->"); buf->append(getStateString(t)); buf->append(L"\n");
ss << L"-" << label << L"->" << getStateString(t) << L"\n";
}
}
}
std::wstring output = buf->toString();
if (output.length() == 0) {
return L"";
}
return output;
return ss.str();
}
std::wstring DFASerializer::getEdgeLabel(size_t i) {
@ -75,7 +68,7 @@ std::wstring DFASerializer::getEdgeLabel(size_t i) {
if (!tokenNames.empty()) {
label = tokenNames[i - 1];
} else {
label = antlrcpp::StringConverterHelper::toString(i - 1);
label = std::to_wstring(i - 1);
}
return label;
}

View File

@ -92,21 +92,19 @@ bool DFAState::equals(DFAState *o) {
}
std::wstring DFAState::toString() {
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder();
buf->append(std::to_wstring(stateNumber)); buf->append(L":"); buf->append(configs->toString());
std::wstringstream ss;
ss << stateNumber << L":" << configs->toString();
if (isAcceptState) {
buf->append(L"=>");
ss << L" => ";
if (predicates.size() != 0) {
std::wstring tmp;
for (size_t i = 0; i < predicates.size(); i++) {
tmp.append(predicates[i]->toString());
ss << predicates[i]->toString();
}
buf->append(tmp);
} else {
buf->append(std::to_wstring(prediction));
ss << prediction;
}
}
return buf->toString();
return ss.str();
}
void DFAState::InitializeInstanceFields() {

View File

@ -31,7 +31,6 @@
#include "MurmurHash.h"
#include "Lexer.h"
#include "StringBuilder.h"
#include "Exceptions.h"
#include "IntervalSet.h"
@ -347,82 +346,82 @@ std::wstring IntervalSet::toString() const {
}
std::wstring IntervalSet::toString(bool elemAreChar) const {
antlrcpp::StringBuilder buffer;
if (_intervals.empty()) {
return L"{}";
}
std::wstringstream ss;
size_t effectiveSize = size();
if (effectiveSize > 1) {
buffer.append(L"{");
ss << L"{";
}
bool firstEntry = true;
for (auto &interval : _intervals) {
if (!firstEntry)
buffer.append(L", ");
ss << L", ";
firstEntry = false;
int a = interval.a;
int b = interval.b;
if (a == b) {
if (a == -1) {
buffer.append(L"<EOF>");
ss << L"<EOF>";
} else if (elemAreChar) {
buffer.append(L"'").append(static_cast<wchar_t>(a)).append(L"'");
ss << L"'" << static_cast<wchar_t>(a) << L"'";
} else {
buffer.append(a);
ss << a;
}
} else {
if (elemAreChar) {
buffer.append(L"'").append(static_cast<wchar_t>(a)).append(L"'..'").append(static_cast<wchar_t>(b)).append(L"'");
ss << L"'" << static_cast<wchar_t>(a) << L"'..'" << static_cast<wchar_t>(b) << L"'";
} else {
buffer.append(a).append(L"..").append(b);
ss << a << L".." << b;
}
}
}
if (effectiveSize > 1) {
buffer.append(L"}");
ss << L"}";
}
return buffer.toString();
return ss.str();
}
std::wstring IntervalSet::toString(const std::vector<std::wstring> &tokenNames) const {
antlrcpp::StringBuilder buffer;
if (_intervals.empty()) {
return L"{}";
}
std::wstringstream ss;
size_t effectiveSize = size();
if (effectiveSize > 1) {
buffer.append(L"{");
ss << L"{";
}
bool firstEntry = true;
for (auto &interval : _intervals) {
if (!firstEntry)
buffer.append(L", ");
ss << L", ";
firstEntry = false;
ssize_t a = (ssize_t)interval.a;
ssize_t b = (ssize_t)interval.b;
if (a == b) {
buffer.append(elementName(tokenNames, a));
ss << elementName(tokenNames, a);
} else {
for (ssize_t i = a; i <= b; i++) {
if (i > a) {
buffer.append(L", ");
ss << L", ";
}
buffer.append(elementName(tokenNames, i));
ss << elementName(tokenNames, i);
}
}
}
if (effectiveSize > 1) {
buffer.append(L"}");
ss << L"}";
}
return buffer.toString();
return ss.str();
}
std::wstring IntervalSet::elementName(const std::vector<std::wstring> &tokenNames, ssize_t a) const {

View File

@ -35,17 +35,17 @@ using namespace antlrcpp;
std::wstring Arrays::listToString(const std::vector<std::wstring> &list, const std::wstring &separator)
{
StringBuilder sb;
std::wstringstream ss;
bool firstEntry = true;
for (auto &entry : list) {
sb.append(entry);
ss << entry;
if (!firstEntry)
sb.append(separator);
ss << separator;
firstEntry = false;
}
return sb.toString();
return ss.str();
}
template <>

View File

@ -30,7 +30,6 @@
#pragma once
#include "StringBuilder.h"
#include "Tree.h"
namespace antlrcpp {

View File

@ -1,152 +0,0 @@
#include <iostream>
#include <memory>
#include <cstdarg>
#include "StringBuilder.h"
#include "ATNState.h"
#include "SemanticContext.h"
#include "Strings.h"
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using namespace antlrcpp;
namespace antlrcpp {
StringBuilder & StringBuilder::append(const std::wstring & str) {
scratch.append(str);
if (scratch.size() > ScratchSize) {
main.append(scratch);
scratch.resize(0);
}
return *this;
}
StringBuilder & StringBuilder::append(const org::antlr::v4::runtime::atn::ATNState* const state) {
main = main + state->toString();
return *this;
}
StringBuilder & StringBuilder::append(const org::antlr::v4::runtime::atn::SemanticContext* const state) {
main = main + state->toString();
return *this;
}
StringBuilder & StringBuilder::append(const wchar_t & str) {
scratch.append(wchar2wstring(str));
if (scratch.size() > ScratchSize) {
main.append(scratch);
scratch.resize(0);
}
return *this;
}
const std::wstring & StringBuilder::str() {
if (scratch.size() > 0) {
main.append(scratch);
scratch.resize(0);
}
return main;
}
std::wstring StringBuilder::format(const std::string fmt_str, ...) {
int final_n;
int n = (int)fmt_str.size() * 2; // reserve 2 times as much as the length of the fmt_str
std::string str;
std::unique_ptr<char[]> formatted;
va_list ap;
while(1) {
formatted.reset(new char[n]); // wrap the plain char array into the unique_ptr
strcpy(&formatted[0], fmt_str.c_str());
va_start(ap, fmt_str);
final_n = vsnprintf(&formatted[0], (size_t)n, fmt_str.c_str(), ap);
va_end(ap);
if (final_n < 0 || final_n >= n)
n += abs(final_n - n + 1);
else
break;
}
return std::wstring(s2ws(formatted.get()));
}
const std::wstring & StringBuilder::toString() {
return str();
}
std::wstring::size_type StringBuilder::length() {
return str().size();
}
std::wstring StringBuilder::stringToWstring( std::string str )
{
std::wstring wStr( str.length(), L' ' );
std::copy( str.begin(), str.end(), wStr.begin() );
return wStr;
}
std::string StringBuilder::WstringTostring( std::wstring wStr )
{
std::string str( wStr.length(), ' ' );
std::copy( wStr.begin(), wStr.end(), str.begin() );
return str;
}
std::string StringBuilder::string_replace(std::string src, std::string const& target, std::string const& repl)
{
// handle error situations/trivial cases
if (target.length() == 0) {
// searching for a match to the empty string will result in
// an infinite loop
// it might make sense to throw an exception for this case
return src;
}
if (src.length() == 0) {
return src; // nothing to match against
}
size_t idx = 0;
for (;;) {
idx = src.find( target, idx);
if (idx == std::string::npos) break;
src.replace( idx, target.length(), repl);
idx += repl.length();
}
return src;
}
}

View File

@ -1,71 +0,0 @@
/*
* [The "BSD license"]
* Copyright (c) 2016 Mike Lischke
* Copyright (c) 2013 Dan McLaughlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace antlrcpp {
class StringBuilder {
private:
std::wstring main;
std::wstring scratch;
const std::wstring::size_type ScratchSize = 1024; // or some other arbitrary number
public:
StringBuilder & append(const std::wstring & str);
StringBuilder & append(const org::antlr::v4::runtime::atn::ATNState* const state);
StringBuilder & append(const org::antlr::v4::runtime::atn::SemanticContext* const state);
StringBuilder & append(const wchar_t & str);
const std::wstring & str();
const std::wstring & toString();
std::wstring::size_type length();
// Utility function to provide "format" capability
static std::wstring format(const std::string fmt_str, ...);
static std::wstring stringToWstring( std::string str );
static std::string WstringTostring( std::wstring wStr );
static std::string string_replace(std::string src,
std::string const& target,
std::string const& repl);
};
}

View File

@ -1,34 +0,0 @@
#pragma once
//----------------------------------------------------------------------------------------
// Copyright © 2007 - 2013 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class is used to replace some conversions to or from strings.
//----------------------------------------------------------------------------------------
#include <sstream>
namespace antlrcpp {
class StringConverterHelper
{
public:
template<typename T>
static std::wstring toString(const T &subject)
{
std::wostringstream ss;
ss << subject;
return ss.str();
}
template<typename T>
static T fromString(const std::wstring &subject)
{
std::wistringstream ss(subject);
T target;
ss >> target;
return target;
}
};
}

View File

@ -31,7 +31,6 @@
#include "ErrorNode.h"
#include "Parser.h"
#include "StringBuilder.h"
#include "CPPUtils.h"
#include "Trees.h"
@ -52,19 +51,17 @@ std::wstring Trees::toStringTree(Tree *t, const std::vector<std::wstring> &ruleN
if (t->getChildCount() == 0) {
return s;
}
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder();
buf->append(L"(");
s = antlrcpp::escapeWhitespace(getNodeText(t, ruleNames), false);
buf->append(s);
buf->append(L' ');
std::wstringstream ss;
ss << L"(" << antlrcpp::escapeWhitespace(getNodeText(t, ruleNames), false) << L' ';
for (size_t i = 0; i < t->getChildCount(); i++) {
if (i > 0) {
buf->append(L' ');
ss << L' ';
}
buf->append(toStringTree(t->getChild(i), ruleNames));
ss << toStringTree(t->getChild(i), ruleNames);
}
buf->append(L")");
return buf->toString();
ss << L")";
return ss.str();
}
std::wstring Trees::getNodeText(Tree *t, Parser *recog) {