Skip to content

Commit

Permalink
Core: CParser nested comments support (thanks cbpporter)
Browse files Browse the repository at this point in the history
git-svn-id: svn://ultimatepp.org/upp/trunk@10707 f0d560ea-af0d-0410-9eb7-867de7ffcac7
  • Loading branch information
cxl committed Jan 15, 2017
1 parent d95383d commit b97af94
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions uppsrc/Core/Parser.h
Expand Up @@ -15,6 +15,7 @@ class CParser {
String fn;
bool skipspaces;
bool skipcomments;
bool nestcomments;
bool uescape;

bool Spaces0();
Expand Down Expand Up @@ -105,6 +106,8 @@ class CParser {
CParser& UnicodeEscape(bool b = true) { uescape = b; return *this; }
CParser& SkipComments(bool b = true);
CParser& NoSkipComments() { return SkipComments(false); }
CParser& NestComments(bool b = true);
CParser& NoNestComments() { return NestComments(false); }

CParser(const char *ptr);
CParser(const char *ptr, const char *fn, int line = 1);
Expand Down
43 changes: 36 additions & 7 deletions uppsrc/Core/parser.cpp
Expand Up @@ -22,6 +22,14 @@ CParser& CParser::SkipComments(bool b)
return *this;
}

CParser& CParser::NestComments(bool b)
{
nestcomments = b;
term = wspc;
Spaces0();
return *this;
}

bool CParser::Spaces0() {
LTIMING("Spaces");
if((byte)*term > ' ' &&
Expand Down Expand Up @@ -60,13 +68,31 @@ bool CParser::Spaces0() {
}
else
if(term[0] == '/' && term[1] == '*' && skipcomments) {
term += 2;
while(*term) {
if(term[0] == '*' && term[1] == '/') {
term += 2;
break;
if(nestcomments) {
int count = 1;
term += 2;
while(*term) {
if(term[0] == '*' && term[1] == '/') {
term += 2;
count--;
if (count == 0)
break;
}
else if(term[0] == '/' && term[1] == '*')
count++;

if(*term++ == '\n') line++;
}
}
else {
term += 2;
while(*term) {
if(term[0] == '*' && term[1] == '/') {
term += 2;
break;
}
if(*term++ == '\n') line++;
}
if(*term++ == '\n') line++;
}
}
if(!*term) break;
Expand Down Expand Up @@ -491,6 +517,7 @@ CParser::CParser(const char *ptr)
{
line = 1;
skipspaces = skipcomments = true;
nestcomments = false;
uescape = false;
Spaces();
}
Expand All @@ -499,6 +526,7 @@ CParser::CParser(const char *ptr, const char *fn, int line)
: term(ptr), wspc(ptr), lineptr(ptr), line(line), fn(fn)
{
skipspaces = skipcomments = true;
nestcomments = false;
uescape = false;
Spaces();
}
Expand All @@ -507,7 +535,8 @@ CParser::CParser()
{
term = lineptr = wspc = NULL;
line = 0;
skipspaces = true;
skipspaces = skipcomments = true;
nestcomments = false;
uescape = false;
}

Expand Down
11 changes: 11 additions & 0 deletions uppsrc/Core/src.tpp/CParser$en-us.tpp
Expand Up @@ -98,6 +98,17 @@ Note that SkipComments has to be called before any parsing happens.&]
[s2;%% Same as SkipComments(false).&]
[s3; &]
[s4; &]
[s5;:Upp`:`:CParser`:`:NestComments`(bool`): [_^Upp`:`:CParser^ CParser][@(0.0.255) `&]_[* N
estComments]([@(0.0.255) bool]_[*@3 b]_`=_[@(0.0.255) true])&]
[s2;%% If active, CParser recognizes nested comments (e.g `"[C /`*
level1 /`* level2 `*/ `*/]`").&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:CParser`:`:NoNestComments`(`): [_^Upp`:`:CParser^ CParser][@(0.0.255) `&]_[* No
NestComments]()&]
[s2;%% Same as NestComments(false).&]
[s3; &]
[s4; &]
[s5;:CParser`:`:Spaces`(`): [@(0.0.255) bool]_[* Spaces]()&]
[s2;%% Skips white`-spaces. Returns [* true] if there were white`-space
to skip, [* false] otherwise. Stores the position before advancing
Expand Down

0 comments on commit b97af94

Please sign in to comment.