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()
{
// remember character after '/' to distinguish comment types
const auto comment_char = get();
// expect // or /* to start a comment
if (comment_char != '/' and comment_char != '*')
switch (get())
{
return false;
}
while (true)
{
switch (get())
case '/':
{
// EOF inside a /* comment is an error, in // it is OK
case std::char_traits<char_type>::eof():
case '\0':
while (true)
{
return comment_char == '/';
}
// a newline ends the // comment
case '\n':
case '\r':
{
if (comment_char == '/')
switch (get())
{
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 '/':
{
return true;
}
case std::char_traits<char_type>::eof():
case '\0':
return false;
default:
case '*':
{
switch (get())
{
unget();
break;
case '/':
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()
{
// remember character after '/' to distinguish comment types
const auto comment_char = get();
// expect // or /* to start a comment
if (comment_char != '/' and comment_char != '*')
switch (get())
{
return false;
}
while (true)
{
switch (get())
case '/':
{
// EOF inside a /* comment is an error, in // it is OK
case std::char_traits<char_type>::eof():
case '\0':
while (true)
{
return comment_char == '/';
}
// a newline ends the // comment
case '\n':
case '\r':
{
if (comment_char == '/')
switch (get())
{
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 '/':
{
return true;
}
case std::char_traits<char_type>::eof():
case '\0':
return false;
default:
case '*':
{
switch (get())
{
unget();
break;
case '/':
return true;
default:
{
unget();
break;
}
}
}
}
break;
}
default:
break;
}
default:
return false;
}
}