From 999db12a3a74f5b0985e94b8d4bac661b8dc7fc3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 13 Nov 2020 14:35:52 +0100 Subject: [PATCH] Added // and /* */ style comments to scripting language --- source/parser/lexer.cpp | 23 +++++++++++++++++++++ source/views/view_pattern.cpp | 38 ++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/source/parser/lexer.cpp b/source/parser/lexer.cpp index 8a538c190..4faeffd3f 100644 --- a/source/parser/lexer.cpp +++ b/source/parser/lexer.cpp @@ -70,6 +70,29 @@ namespace hex::lang { u32 offset = 0; while (offset < code.length()) { + // Handle comments + if (code[offset] == '/') { + offset++; + + if (offset < code.length() && code[offset] == '/') { + offset++; + while (offset < code.length()) { + if (code[offset] == '\n' || code[offset] == '\r') + break; + offset++; + } + } else if (offset < code.length() && code[offset] == '*') { + offset++; + while (offset < (code.length() - 1)) { + if (code[offset] == '*' && code[offset + 1] == '/') + break; + offset++; + } + + offset += 2; + } else offset--; + } + const char& c = code[offset]; if (std::isblank(c) || std::isspace(c)) { diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index 15fbcd5e7..d2cc93993 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -7,6 +7,9 @@ namespace hex { ViewPattern::ViewPattern(prv::Provider* &dataProvider, std::vector &highlights) : View(), m_dataProvider(dataProvider), m_highlights(highlights) { + + this->m_buffer = new char[0xFF'FFFF]; + std::memset(this->m_buffer, 0x00, 0xFF'FFFF); } ViewPattern::~ViewPattern() { if (this->m_buffer != nullptr) @@ -34,7 +37,7 @@ namespace hex { return; if (ImGui::Begin("Pattern", &this->m_windowOpen, ImGuiWindowFlags_None)) { - if (this->m_dataProvider != nullptr && this->m_dataProvider->isReadable()) { + if (this->m_buffer != nullptr && this->m_dataProvider != nullptr && this->m_dataProvider->isReadable()) { ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); @@ -59,23 +62,26 @@ namespace hex { this->m_fileBrowser.Display(); if (this->m_fileBrowser.HasSelected()) { + FILE *file = fopen(this->m_fileBrowser.GetSelected().string().c_str(), "rb"); - - fseek(file, 0, SEEK_END); - size_t size = ftell(file); - rewind(file); - - if (this->m_buffer != nullptr) - delete[] this->m_buffer; - this->m_buffer = new char[size + 1]; - std::memset(this->m_buffer, 0x00, size + 1); - - fread(this->m_buffer, size, 1, file); - - fclose(file); - - this->parsePattern(this->m_buffer); this->m_fileBrowser.ClearSelected(); + + if (file != nullptr) { + fseek(file, 0, SEEK_END); + size_t size = ftell(file); + rewind(file); + + if (size >= 0xFF'FFFF) { + fclose(file); + return; + } + + fread(this->m_buffer, size, 1, file); + + fclose(file); + + this->parsePattern(this->m_buffer); + } } }