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

View File

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

View File

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

View File

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

View File

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

View File

@ -35,7 +35,6 @@
#include "Parser.h" #include "Parser.h"
#include "Interval.h" #include "Interval.h"
#include "DFA.h" #include "DFA.h"
#include "stringconverter.h"
#include "DiagnosticErrorListener.h" #include "DiagnosticErrorListener.h"
@ -83,12 +82,12 @@ std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer,
const std::vector<std::wstring>& ruleNames = recognizer->getRuleNames(); const std::vector<std::wstring>& ruleNames = recognizer->getRuleNames();
if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) { if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) {
return antlrcpp::StringConverterHelper::toString(decision); return std::to_wstring(decision);
} }
std::wstring ruleName = ruleNames[(size_t)ruleIndex]; std::wstring ruleName = ruleNames[(size_t)ruleIndex];
if (ruleName == L"" || ruleName.empty()) { if (ruleName == L"" || ruleName.empty()) {
return antlrcpp::StringConverterHelper::toString(decision); return std::to_wstring(decision);
} }
return std::to_wstring(decision) + L"(" + ruleName + L")"; 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 /// non-null, non-empty string. If such a name is not known, this method
/// returns <seealso cref="#UNKNOWN_SOURCE_NAME"/>. /// returns <seealso cref="#UNKNOWN_SOURCE_NAME"/>.
/// </summary> /// </summary>
virtual std::string getSourceName() = 0; virtual std::string getSourceName() const = 0;
}; };
} // namespace runtime } // namespace runtime

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -113,29 +113,24 @@ namespace atn {
virtual std::wstring toString(); virtual std::wstring toString();
std::wstring toString(bool showAlt) { std::wstring toString(bool showAlt) {
antlrcpp::StringBuilder buf; std::wstringstream ss(L" (");
buf.append(L'('); ss << state;
buf.append(state);
if (showAlt) { if (showAlt) {
buf.append(L","); ss << L"," << alt;
buf.append(alt);
} }
if (context != nullptr) { if (context != nullptr) {
buf.append(L",["); ss << L",[" << context->toString() << L"]";
buf.append(context->toString());
buf.append(L"]");
} }
if (semanticContext != nullptr && semanticContext != SemanticContext::NONE) { if (semanticContext != nullptr && semanticContext != SemanticContext::NONE) {
buf.append(L","); ss << L"," << semanticContext.get();
buf.append(semanticContext.get());
} }
if (reachesIntoOuterContext > 0) { 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() { std::wstring ATNConfigSet::toString() {
antlrcpp::StringBuilder *buf = new antlrcpp::StringBuilder(); std::wstringstream ss;
for (size_t i = 0; i < elements().size(); i++) { for (size_t i = 0; i < elements().size(); i++) {
buf->append(elements().at(i)->toString()); ss << elements().at(i)->toString();
} }
if (hasSemanticContext) { if (hasSemanticContext) {
buf->append(L",hasSemanticContext=").append(hasSemanticContext); ss << L",hasSemanticContext = " << hasSemanticContext;
} }
if (uniqueAlt != ATN::INVALID_ALT_NUMBER) { if (uniqueAlt != ATN::INVALID_ALT_NUMBER) {
buf->append(L",uniqueAlt=").append(uniqueAlt); ss << L",uniqueAlt = " << uniqueAlt;
} }
if (conflictingAlts != nullptr) { if (conflictingAlts != nullptr) {
buf->append(L",conflictingAlts="); ss << L",conflictingAlts = ";
buf->append(conflictingAlts->toString()); ss << conflictingAlts->toString();
} }
if (dipsIntoOuterContext) { if (dipsIntoOuterContext) {
buf->append(L",dipsIntoOuterContext"); ss << L", dipsIntoOuterContext";
} }
return buf->toString(); return ss.str();
} }
bool ATNConfigSet::remove(void *o) { bool ATNConfigSet::remove(void *o) {

View File

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

View File

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

View File

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

View File

@ -29,7 +29,6 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "stringconverter.h"
#include "IntervalSet.h" #include "IntervalSet.h"
#include "Transition.h" #include "Transition.h"
@ -54,5 +53,5 @@ bool AtomTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol)
} }
std::wstring AtomTransition::toString() const { 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 "RuleStopState.h"
#include "ATNConfigSet.h" #include "ATNConfigSet.h"
#include "ATNConfig.h" #include "ATNConfig.h"
#include "stringconverter.h"
#include "Interval.h" #include "Interval.h"
#include "ANTLRErrorListener.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 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) { std::wstring ParserATNSimulator::getLookaheadName(TokenStream *input) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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