Skip to content

Commit

Permalink
ETH: Parser fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nopjne committed Apr 12, 2021
1 parent 92581ca commit f20c73c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 27 deletions.
6 changes: 6 additions & 0 deletions rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,12 @@ class GenericValue {
DownFlags = 0x20000;
}

if (MemberCount() == 0) {
if (RAPIDJSON_UNLIKELY(!handler.StartObject(0 | DownFlags)))
return false;
return handler.EndObject(DownFlags, data_.o.size);
}

if (RAPIDJSON_UNLIKELY(!handler.StartObject(member->name.data_.f.flags | DownFlags)))
return false;

Expand Down
54 changes: 44 additions & 10 deletions rapidjson/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -893,15 +893,15 @@ class GenericReader {
if (RAPIDJSON_UNLIKELY(!handler.StartObject()))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());

SkipWhitespaceAndComments<parseFlags>(is);
SkipWhitespaceNewLineAndComments<parseFlags>(is);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;

if (Consume(is, '}')) {
if (RAPIDJSON_UNLIKELY(!handler.EndObject(0))) // empty object
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());

void* NewLocation = handler.stack_.End<void>();
assert(Location == NewLocation);
//assert(Location == NewLocation);
return;
}

Expand All @@ -912,7 +912,7 @@ class GenericReader {

Ch PeekCh = is.Peek();
bool ValueExpected = false;
if (PeekCh != '{') {
if ((PeekCh != '{') && (PeekCh != '}')) {
ParseValueString<parseFlags>(is, handler, true, &IsArray);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;

Expand All @@ -938,24 +938,26 @@ class GenericReader {
SkipWhitespaceAndComments<parseFlags>(is);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;

bool objectstart = is.Peek() != '{';
char peekit = is.Peek();
bool objectstart = (is.Peek() != '{');
if (objectstart == false) {
// No equals sign check or object open, parse the extra name.
ParseValue<parseFlags>(is, handler);
if (is.Peek() != '}') {
ParseValue<parseFlags>(is, handler);
}
}
}

SkipWhitespaceAndComments<parseFlags>(is);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;

if ((ValueExpected != false) && (equals == false) && (is.Peek() == '\n')) {
if ((ValueExpected != false) && (equals == false) && (is.Peek() == '\n') && (PeekCh != '}')) {
char str[] = "\0";
handler.String(str, sizeof(str), true);
handler.SetFlags(true, 0x8000);
}

++memberCount;

char peeeek = is.Peek();
switch (is.Peek()) {
case ';':
Expand Down Expand Up @@ -1343,21 +1345,37 @@ class GenericReader {
Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16
};
#undef Z16
#if DEBUG
char entitydefbuffertrack[4096];
entitydefbuffertrack[0] = 0;
int indtrack = 0;
#endif

//!@endcond
//! properties
char entitydefbuffer[10] = " ";
int index = 0;
size_t currentlen = 0;
for (;;) {
// Scan and copy string before "\\\"" or < 0x20. This is an optional optimzation.
if (!(parseFlags & kParseValidateEncodingFlag))
ScanCopyUnescapedString(is, os);

Ch c = is.Peek();
//if (index < sizeof(entitydefbuffer)) {
if (index < (sizeof(entitydefbuffer))) {
entitydefbuffer[index] = c;
index += 1;
index %= 10;
//}
//index %= 10;
}
currentlen += 1;
#if DEBUG
entitydefbuffertrack[indtrack] = c;
entitydefbuffertrack[indtrack + 1] = 0;
indtrack += 1;
if (memcmp(entitydefbuffertrack, "entityDef =", sizeof("entityDef ")) == 0) {
int a = 0;
}
#endif

if (RAPIDJSON_UNLIKELY(c == '\\')) { // Escape
size_t escapeOffset = is.Tell(); // For invalid escaping, report the initial '\\' as error offset
Expand Down Expand Up @@ -1403,10 +1421,18 @@ class GenericReader {
}

if (!entitydef) {
EntityDefOut = entitydef;
os.Put('\0'); // null-terminate the string
return;
}

// When this is an entityDef definition prevent allowing "=" signs.
if (RAPIDJSON_UNLIKELY(c == '=')) {
os.Put('\0'); // null-terminate the string
return;
}

// entityDef needs to have a "=" or be on level 2.
EntityDefOut = true;

size_t offset = is.Tell();
Expand All @@ -1415,8 +1441,16 @@ class GenericReader {
!Transcoder<SEncoding, TEncoding>::Transcode(is, os))))
RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, offset);
}
else if (((RAPIDJSON_UNLIKELY(c == '{') || (RAPIDJSON_UNLIKELY(c == '='))) && (EntityDefOut != false))) {
os.Put('\0'); // null-terminate the string
return;
}
else if (RAPIDJSON_UNLIKELY(static_cast<unsigned>(c) < 0x20)) { // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
if (c == '\n') {
if (EntityDefOut != false) {
RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, is.Tell());
}

return;
} else if (c == '\0') {
RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell());
Expand Down
10 changes: 10 additions & 0 deletions rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ class Writer {
hideStatementEnd = true;
if (memcmp(stringBuffer, "entityDef ", sizeof("entityDef")) == 0) {
hideStatementEnd = false;
if ((level_stack_.GetSize() > (2 * sizeof(void*)))) {
specialKeyword = false;
}
}

if (memcmp(stringBuffer, "entity\0", sizeof("entity\0")) == 0) {
Expand All @@ -530,6 +533,13 @@ class Writer {
specialKeyword = false;
}
}

if ((memcmp(stringBuffer, "properties\0", sizeof("properties\0")) == 0)) {
if ((level_stack_.GetSize() > (2 * sizeof(void*)))) {
hideStatementEnd = false;
specialKeyword = false;
}
}
}

if (stringBuffer[0] == 0) {
Expand Down
22 changes: 16 additions & 6 deletions wxGUI/dataview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
using namespace rapidjson;
using namespace std;

wxString gVersion = "0.8";
wxString gVersion = "0.8.1";
std::map<std::string, std::set<std::string>> ValueMap;
int DecompressEntities(std::istream* input, char** OutDecompressedData, size_t& OutSize, size_t InSize);
int CompressEntities(const char* destFilename, byte* uncompressedData, size_t size);
Expand Down Expand Up @@ -1005,9 +1005,10 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int
firstPanelSz->Add(navigationSizer, 0, wxGROW | wxALL, 5);

//firstPanelSz->Add(m_ctrl[Page_EntityView], 1, wxGROW | wxALL, 5);
sizerCurrent->RecalcSizes();
firstPanelSz->Add(m_Splitter, 1, wxGROW | wxALL, 5);
firstPanelSz->Add(m_MHInterfaceStatus, 0, wxGROW | wxALL, 5);
firstPanelSz->Add(sizerCurrent);
firstPanelSz->Add(sizerCurrent, 0, wxGROW | wxALL, 5);
firstPanel->SetSizerAndFit(firstPanelSz);
m_notebook->AddPage(firstPanel, "EntityView");

Expand Down Expand Up @@ -2766,20 +2767,29 @@ bool MyFrame::OpenFileInternal(wxString FilePath)

} else {
ifstream InputStream(FilePath.c_str().AsChar());
rapidjson::ParseResult Result;
if (InputStream.good() != false) {
rapidjson::IStreamWrapper InStream(InputStream);
m_Document.ParseStream<rapidjson::kParseCommentsFlag |
rapidjson::kParseTrailingCommasFlag |
rapidjson::kParseNanAndInfFlag>(InStream);
Result = m_Document.ParseStream<rapidjson::kParseCommentsFlag |
rapidjson::kParseTrailingCommasFlag |
rapidjson::kParseNanAndInfFlag>(InStream);
}

if (m_Document.HasParseError() != false) {
wxMessageBox(wxString::Format("Error parsing the entities definition file. (Could not decompress or parse).\n%s\n (Syntax error at offset: %llu)", FilePath, (long long)m_Document.GetErrorOffset()), _("Error"), wxOK, this);
Error = 1;

// Show the stream in the edit box and go to the parse error.
m_edit->SetTextAndHighlight(wxString(DecompressedData), nullptr);
ifstream InputStreamError(FilePath.c_str().AsChar());
std::vector<char> TempData;
assert(InputStreamError.good() != false);
TempData.resize(Size);
char *Output = &(TempData[0]);

InputStreamError.read(Output, Size);
m_edit->SetTextAndHighlight(wxString(Output), nullptr);
m_edit->SetViewWhiteSpace(wxSTC_WS_VISIBLEALWAYS);
m_edit->SetParseErrorHelper(m_edit->GetText(), Result.Offset());

} else {
m_CurrentlyLoadedFileCompressed = false;
Expand Down
26 changes: 16 additions & 10 deletions wxGUI/edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ void Edit::MySetFocus(wxFocusEvent& Event)
Event.Skip();
}

void Edit::SetParseErrorHelper(wxString String, size_t Offset)
{
size_t CharNr = 0;
size_t LineNumber = FreqWithLimit(String, Offset, '\n', CharNr);

// Position some text under the parsing error.
wxString ErrorString;
ErrorString.resize(CharNr + 6, L' ');
ErrorString += "^ Syntax Error";
AnnotationSetStyle(LineNumber, ANNOTATION_STYLE);
AnnotationSetText(LineNumber, ErrorString);
AnnotationSetVisible(wxSTC_ANNOTATION_STANDARD);
}


void Edit::MyHandleKillFocus(wxFocusEvent& Event)
{
if (Modified() != false) {
Expand All @@ -190,16 +205,7 @@ void Edit::MyHandleKillFocus(wxFocusEvent& Event)
wxString error = wxString::Format("Syntax error at offset: %llu Line:\n Do you want to discard your changes?", (long long)TextData.GetErrorOffset());
int Discard = wxMessageBox(error, _("Error"), wxYES_NO | wxICON_ERROR, this);

size_t CharNr = 0;
size_t LineNumber = FreqWithLimit(LineEndNormalization, Result.Offset(), '\n', CharNr);

// Position some text under the parsing error.
wxString ErrorString;
ErrorString.resize(CharNr + 6, L' ');
ErrorString += "^ Syntax Error";
AnnotationSetStyle(LineNumber, ANNOTATION_STYLE);
AnnotationSetText(LineNumber, ErrorString);
AnnotationSetVisible(wxSTC_ANNOTATION_STANDARD);
SetParseErrorHelper(LineEndNormalization, Result.Offset());
if (Discard == wxNO) {
SetFocus();
} else {
Expand Down
1 change: 1 addition & 0 deletions wxGUI/edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class Edit: public wxStyledTextCtrl {
void MySetFocus(wxFocusEvent& Event);
void MyHandleKillFocus(wxFocusEvent& Event);
EntityTreeModelNode* GetCurrentNode() { return m_CurrentNode; }
void SetParseErrorHelper(wxString String, size_t Offset);

private:
// file
Expand Down
1 change: 0 additions & 1 deletion wxGUI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ int main(void)
sprintf_s(string, "bad input file %zu\n", document.GetErrorOffset());
OutputDebugStringA(string);
printf(string);

}

ofstream OfStream("../chrispy_out.txt", std::ofstream::binary);
Expand Down

0 comments on commit f20c73c

Please sign in to comment.