Skip comments instead of writing them to output

This simplifies parsing.
This commit is contained in:
Jonathan Müller 2017-04-12 15:09:42 +02:00
commit 4dc771ad6b

View file

@ -268,7 +268,7 @@ namespace
if (starts_with(p, " ")) if (starts_with(p, " "))
// skip one whitespace at most // skip one whitespace at most
p.bump(); p.skip();
while (!starts_with(p, "*/")) while (!starts_with(p, "*/"))
{ {
@ -278,27 +278,27 @@ namespace
while (!result.comment.empty() && result.comment.back() == ' ') while (!result.comment.empty() && result.comment.back() == ' ')
result.comment.pop_back(); result.comment.pop_back();
// skip newline // skip newline
p.bump(); p.skip();
result.comment += '\n'; result.comment += '\n';
// skip indentation // skip indentation
while (starts_with(p, " ")) while (starts_with(p, " "))
p.bump(); p.skip();
// skip continuation star, if any // skip continuation star, if any
if (starts_with(p, "*") && !starts_with(p, "*/")) if (starts_with(p, "*") && !starts_with(p, "*/"))
{ {
p.bump(); p.skip();
if (starts_with(p, " ")) if (starts_with(p, " "))
// skip one whitespace at most // skip one whitespace at most
p.bump(); p.skip();
} }
} }
else else
{ {
result.comment += *p.ptr(); result.comment += *p.ptr();
p.bump(); p.skip();
} }
} }
p.bump(2u); p.skip(2u);
// remove trailing star // remove trailing star
if (!result.comment.empty() && result.comment.back() == '*') if (!result.comment.empty() && result.comment.back() == '*')
@ -311,23 +311,23 @@ namespace
return result; return result;
} }
bool bump_c_comment(position& p, detail::preprocessor_output& output, bool in_main_file) bool skip_c_comment(position& p, detail::preprocessor_output& output, bool in_main_file)
{ {
if (!starts_with(p, "/*")) if (!starts_with(p, "/*"))
return false; return false;
p.bump(2u); p.skip(2u);
if (in_main_file && starts_with(p, "*")) if (in_main_file && starts_with(p, "*"))
{ {
// doc comment // doc comment
p.bump(); p.skip();
output.comments.push_back(parse_c_doc_comment(p)); output.comments.push_back(parse_c_doc_comment(p));
} }
else else
{ {
while (!starts_with(p, "*/")) while (!starts_with(p, "*/"))
p.bump(); p.skip();
p.bump(2u); p.skip(2u);
} }
if (!starts_with(p, "\n")) if (!starts_with(p, "\n"))
@ -347,12 +347,12 @@ namespace
end_of_line ? detail::pp_doc_comment::end_of_line : detail::pp_doc_comment::cpp; end_of_line ? detail::pp_doc_comment::end_of_line : detail::pp_doc_comment::cpp;
if (starts_with(p, " ")) if (starts_with(p, " "))
// skip one whitespace at most // skip one whitespace at most
p.bump(); p.skip();
while (!starts_with(p, "\n")) while (!starts_with(p, "\n"))
{ {
result.comment += *p.ptr(); result.comment += *p.ptr();
p.bump(); p.skip();
} }
// don't skip newline // don't skip newline
@ -383,30 +383,30 @@ namespace
} }
} }
bool bump_cpp_comment(position& p, detail::preprocessor_output& output, bool in_main_file) bool skip_cpp_comment(position& p, detail::preprocessor_output& output, bool in_main_file)
{ {
if (!starts_with(p, "//")) if (!starts_with(p, "//"))
return false; return false;
p.bump(2u); p.skip(2u);
if (in_main_file && (starts_with(p, "/") || starts_with(p, "!"))) if (in_main_file && (starts_with(p, "/") || starts_with(p, "!")))
{ {
// C++ style doc comment // C++ style doc comment
p.bump(); p.skip();
auto comment = parse_cpp_doc_comment(p, false); auto comment = parse_cpp_doc_comment(p, false);
merge_or_add(output, std::move(comment)); merge_or_add(output, std::move(comment));
} }
else if (in_main_file && starts_with(p, "<")) else if (in_main_file && starts_with(p, "<"))
{ {
// end of line doc comment // end of line doc comment
p.bump(); p.skip();
auto comment = parse_cpp_doc_comment(p, true); auto comment = parse_cpp_doc_comment(p, true);
output.comments.push_back(std::move(comment)); output.comments.push_back(std::move(comment));
} }
else else
{ {
while (!starts_with(p, "\n")) while (!starts_with(p, "\n"))
p.bump(); p.skip();
// don't skip newline // don't skip newline
} }
@ -695,9 +695,9 @@ detail::preprocessor_output detail::preprocess(const libclang_compile_config& co
break; break;
} }
} }
else if (bump_c_comment(p, result, file_depth == 0u)) else if (skip_c_comment(p, result, file_depth == 0u))
continue; continue;
else if (bump_cpp_comment(p, result, file_depth == 0u)) else if (skip_cpp_comment(p, result, file_depth == 0u))
continue; continue;
else else
p.bump(); p.bump();