Skip to content

Commit

Permalink
Revert changes to syntax
Browse files Browse the repository at this point in the history
Syntax coloring didn't work for commented code / was intermittent.
Need to revisit this and integrate the better number fixes without
breaking shader coloring.

The syntax coloring code is a weak link; it really needs a rethink on
how to do best.
  • Loading branch information
cmaughan committed Mar 2, 2022
1 parent 4a37e15 commit df2844f
Showing 1 changed file with 63 additions and 189 deletions.
252 changes: 63 additions & 189 deletions src/syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "zep/mcommon/logger.h"
#include "zep/mcommon/string/stringutils.h"

#include <cctype>
#include <string>
#include <vector>

Expand Down Expand Up @@ -104,7 +103,7 @@ void ZepSyntax::QueueUpdateSyntax(GlyphIterator startLocation, GlyphIterator end
// Have the thread update the syntax in the new region
// If the pool has no threads, this will end up serial
//m_syntaxResult = GetEditor().GetThreadPool().enqueue([=]() {
UpdateSyntax();
UpdateSyntax();
//});
}

Expand Down Expand Up @@ -153,29 +152,35 @@ void ZepSyntax::UpdateSyntax()
assert(m_syntax.size() == buffer.size());

std::string delim;
static const std::string lineEnd("\n");
static const std::string whiteSpace(" \t");
std::string lineEnd("\n");

if (m_flags & ZepSyntaxFlags::LispLike)
{
delim = std::string(" \t\n(){}[]");
{
delim = std::string(" \t.\n(){}[]");
}
else
{
delim = std::string(" \t+-*/&|^!=~%#$<>@,\n;(){}[]=:");
delim = std::string(" \t.\n;(){}[]=:");
}

std::string delimWithDot = delim + ".";
// Walk backwards to previous delimiter
while (itrCurrent > buffer.begin())
{
if (std::find(delim.begin(), delim.end(), *itrCurrent) == delim.end())
{
itrCurrent--;
}
else
{
break;
}
}

// Back to the previous line
while (itrCurrent > buffer.begin() && *itrCurrent != '\n')
{
itrCurrent--;
}
if (*itrCurrent == '\n')
{
itrCurrent++;
}
itrEnd = buffer.find_first_of(itrEnd, buffer.end(), lineEnd.begin(), lineEnd.end());

// Mark a region of the syntax buffer with the correct marker
Expand All @@ -199,72 +204,29 @@ void ZepSyntax::UpdateSyntax()
return;
}

if (lineEnd.find_first_of(*itrCurrent) != std::string::npos)
{
itrCurrent++;
continue;
}
// Find a token, skipping delim <itrFirst, itrLast>
auto itrFirst = buffer.find_first_not_of(itrCurrent, buffer.end(), delim.begin(), delim.end());
if (itrFirst == buffer.end())
break;

if (whiteSpace.find_first_of(*itrCurrent) != std::string::npos)
{
mark(itrCurrent, itrCurrent + 1, ThemeColor::Whitespace, ThemeColor::None);
itrCurrent++;
continue;
}
auto itrLast = buffer.find_first_of(itrFirst, buffer.end(), delim.begin(), delim.end());

auto itrFirst = itrCurrent;
auto itrLast = buffer.find_first_of(itrFirst, buffer.end(), lineEnd.begin(), lineEnd.end());
// Ensure we found a token
assert(itrLast >= itrFirst);

if (m_flags & ZepSyntaxFlags::LispLike)
// Mark whitespace
for (auto& itr = itrCurrent; itr < itrFirst; itr++)
{
// Lisp languages use ; or # for comments
std::string commentStr = ";#";
if (commentStr.find_first_of(*itrCurrent) != std::string::npos)
if (*itr == ' ')
{
mark(itrCurrent, itrLast, ThemeColor::Comment, ThemeColor::None);
itrCurrent = itrLast;
continue;
mark(itr, itr + 1, ThemeColor::Whitespace, ThemeColor::None);
}
}
else
{
std::string commentStr = "/#";
if (commentStr.find_first_of(*itrCurrent) != std::string::npos)
else if (*itr == '\t')
{
if (*itrCurrent == '#')
{
mark(itrCurrent, itrLast, ThemeColor::Comment, ThemeColor::None);
itrCurrent = itrLast;
continue;
}
else
{
auto itrCommentStart = itrCurrent++;
if (itrCurrent < buffer.end())
{
if (*itrCurrent == '/')
{
itrLast = buffer.find_first_of(itrCommentStart, buffer.end(), lineEnd.begin(), lineEnd.end());
mark(itrCommentStart, itrLast, ThemeColor::Comment, ThemeColor::None);
itrCurrent = itrLast;
continue;
}
}
}
mark(itr, itr + 1, ThemeColor::Whitespace, ThemeColor::None);
}
}

// Find a token, skipping delim <itrFirst, itrLast>

itrFirst = buffer.find_first_not_of(itrCurrent, buffer.end(), delimWithDot.begin(), delimWithDot.end());
if (itrFirst == buffer.end())
break;

itrLast = buffer.find_first_of(itrFirst, buffer.end(), delimWithDot.begin(), delimWithDot.end());

// Ensure we found a token
assert(itrLast >= itrFirst);

// Do I need to make a string here?
auto token = std::string(itrFirst, itrLast);
if (m_flags & ZepSyntaxFlags::CaseInsensitive)
Expand All @@ -280,6 +242,10 @@ void ZepSyntax::UpdateSyntax()
{
mark(itrFirst, itrLast, ThemeColor::Identifier, ThemeColor::None);
}
else if (token.find_first_not_of("0123456789") == std::string::npos)
{
mark(itrFirst, itrLast, ThemeColor::Number, ThemeColor::None);
}
else if (token.find_first_not_of("{}()[]") == std::string::npos)
{
mark(itrFirst, itrLast, ThemeColor::Parenthesis, ThemeColor::None);
Expand All @@ -293,127 +259,6 @@ void ZepSyntax::UpdateSyntax()
mark(itrFirst, itrLast, ThemeColor::Normal, ThemeColor::None);
}

// Find numbers - very crude, but better than nothing
// won't handle exponent numbers, nor C++17's hexadecimal floating points
auto maybeParseHexa = [&](decltype(itrFirst) itr, decltype(itrFirst)& last) -> bool {
auto first = itr;
static const std::string hexa("0123456789abcdefABCDEF");
while (itr < buffer.end())
{
auto ch = *itr;
if (delim.find_first_of(ch) != std::string::npos)
{
break;
}
if (hexa.find_first_of(ch) == std::string::npos)
{
return false;
}
itr++;
}

last = itr;
return itr != first;
};
auto maybeParseOctal = [&](decltype(itrFirst) itr, decltype(itrFirst)& last) -> bool {
auto first = itr;
static const std::string octal("01234567");
while (itr < buffer.end())
{
auto ch = *itr;
if (delim.find_first_of(ch) != std::string::npos)
{
break;
}
if (octal.find_first_of(ch) == std::string::npos)
{
return false;
}
itr++;
}

last = itr;
return itr != first;
};
auto maybeParseFloat = [&](decltype(itrFirst) itr, decltype(itrFirst)& last) -> bool {
auto first = itr;
static const std::string numbers("0123456789");
bool gotDot = false;
bool gotF = false;
bool gotSomething = false;
while (itr < buffer.end())
{
auto ch = *itr;
if (delim.find_first_of(ch) != std::string::npos)
{
break;
}
else if (gotF)
{
return false;
}
if (ch == '.')
{
if (!gotDot)
{
gotDot = true;
}
else
{
return false;
}
}
else if (ch == 'f' && !gotF)
{
gotF = true;
}
else if (numbers.find_first_of(ch) != std::string::npos)
{
gotSomething = true;
}
else
{
return false;
}
itr++;
}

last = itr;
return itr != first && gotSomething;
};
if (std::isdigit(*itrFirst) || *itrFirst == '.')
{
auto itrNum = itrFirst;
auto last = itrFirst;
bool parsed = false;
if (*itrFirst == '0')
{
itrNum++;
switch (*itrNum)
{
case 'x':
parsed = maybeParseHexa(itrFirst + 2, last);
break;
case '.':
parsed = maybeParseFloat(itrFirst, last);
break;
default:
parsed = maybeParseOctal(itrFirst, last);
break;
}
}
else
{
parsed = maybeParseFloat(itrFirst, last);
}

if (parsed)
{
itrLast = last;
mark(itrFirst, itrLast, ThemeColor::Number, ThemeColor::None);
}
}

// Find String
auto findString = [&](uint8_t ch) {
auto itrString = itrFirst;
Expand Down Expand Up @@ -449,6 +294,35 @@ void ZepSyntax::UpdateSyntax()
findString('\"');
findString('\'');

if (m_flags & ZepSyntaxFlags::LispLike)
{
// Lisp languages use ; or # for comments
std::string commentStr = ";#";
auto itrComment = buffer.find_first_of(itrFirst, itrLast, commentStr.begin(), commentStr.end());
if (itrComment != buffer.end())
{
itrLast = buffer.find_first_of(itrComment, buffer.end(), lineEnd.begin(), lineEnd.end());
mark(itrComment, itrLast, ThemeColor::Comment, ThemeColor::None);
}
}
else
{
std::string commentStr = "/";
auto itrComment = buffer.find_first_of(itrFirst, itrLast, commentStr.begin(), commentStr.end());
if (itrComment != buffer.end())
{
auto itrCommentStart = itrComment++;
if (itrComment < buffer.end())
{
if (*itrComment == '/')
{
itrLast = buffer.find_first_of(itrCommentStart, buffer.end(), lineEnd.begin(), lineEnd.end());
mark(itrCommentStart, itrLast, ThemeColor::Comment, ThemeColor::None);
}
}
}
}

itrCurrent = itrLast;
}

Expand Down

0 comments on commit df2844f

Please sign in to comment.