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

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

View file

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