improve comment parsing

This commit is contained in:
Niels Lohmann 2020-06-17 14:59:47 +02:00
parent b53c6e2f81
commit e9bfcf7255
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 64 additions and 80 deletions

View file

@ -835,62 +835,54 @@ class lexer : public lexer_base<BasicJsonType>
*/ */
bool scan_comment() bool scan_comment()
{ {
// remember character after '/' to distinguish comment types switch (get())
const auto comment_char = get();
// expect // or /* to start a comment
if (comment_char != '/' and comment_char != '*')
{ {
return false; case '/':
}
while (true)
{
switch (get())
{ {
// EOF inside a /* comment is an error, in // it is OK while (true)
case std::char_traits<char_type>::eof():
case '\0':
{ {
return comment_char == '/'; switch (get())
}
// a newline ends the // comment
case '\n':
case '\r':
{
if (comment_char == '/')
{ {
return true; case '\n':
case '\r':
return true;
default:
break;
} }
break;
} }
}
// */ ends the /* comment case '*':
case '*': {
while (true)
{ {
if (comment_char == '*') switch (get())
{ {
switch (get()) case std::char_traits<char_type>::eof():
{ case '\0':
case '/': return false;
{
return true;
}
default: case '*':
{
switch (get())
{ {
unget(); case '/':
break; return true;
default:
{
unget();
break;
}
} }
} }
} }
break;
} }
default:
break;
} }
default:
return false;
} }
} }

View file

@ -8902,62 +8902,54 @@ class lexer : public lexer_base<BasicJsonType>
*/ */
bool scan_comment() bool scan_comment()
{ {
// remember character after '/' to distinguish comment types switch (get())
const auto comment_char = get();
// expect // or /* to start a comment
if (comment_char != '/' and comment_char != '*')
{ {
return false; case '/':
}
while (true)
{
switch (get())
{ {
// EOF inside a /* comment is an error, in // it is OK while (true)
case std::char_traits<char_type>::eof():
case '\0':
{ {
return comment_char == '/'; switch (get())
}
// a newline ends the // comment
case '\n':
case '\r':
{
if (comment_char == '/')
{ {
return true; case '\n':
case '\r':
return true;
default:
break;
} }
break;
} }
}
// */ ends the /* comment case '*':
case '*': {
while (true)
{ {
if (comment_char == '*') switch (get())
{ {
switch (get()) case std::char_traits<char_type>::eof():
{ case '\0':
case '/': return false;
{
return true;
}
default: case '*':
{
switch (get())
{ {
unget(); case '/':
break; return true;
default:
{
unget();
break;
}
} }
} }
} }
break;
} }
default:
break;
} }
default:
return false;
} }
} }