Gate doc-comment tokenizing.

This commit is contained in:
Stephen Chung 2021-04-09 23:13:33 +08:00
parent 29d186b361
commit 4aaf957d73
4 changed files with 37 additions and 4 deletions

View File

@ -758,6 +758,8 @@ pub struct Engine {
pub(crate) limits: Limits,
/// Disable doc-comments?
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
pub(crate) disable_doc_comments: bool,
}
@ -874,6 +876,8 @@ impl Engine {
max_map_size: None,
},
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
disable_doc_comments: false,
};
@ -930,6 +934,8 @@ impl Engine {
max_map_size: None,
},
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
disable_doc_comments: false,
}
}

View File

@ -33,7 +33,11 @@ impl Engine {
pub fn optimization_level(&self) -> crate::OptimizationLevel {
self.optimization_level
}
/// Enable/disable doc-comments.
/// _(METADATA)_ Enable/disable doc-comments for functions.
/// Exported under the `metadata` feature only.
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
#[inline(always)]
pub fn enable_doc_comments(&mut self, enable: bool) -> &mut Self {
self.disable_doc_comments = !enable;

View File

@ -838,6 +838,8 @@ pub struct TokenizeState {
/// Include comments?
pub include_comments: bool,
/// Disable doc-comments?
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
pub disable_doc_comments: bool,
/// Is the current tokenizer position within the text stream of an interpolated string?
pub is_within_text_terminated_by: Option<char>,
@ -1155,6 +1157,8 @@ fn is_numeric_digit(c: char) -> bool {
}
/// Test if the comment block is a doc-comment.
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
#[inline(always)]
pub fn is_doc_comment(comment: &str) -> bool {
(comment.starts_with("///") && !comment.starts_with("////"))
@ -1178,10 +1182,22 @@ fn get_next_token_inner(
state.comment_level = scan_block_comment(stream, state.comment_level, pos, &mut comment);
if state.include_comments
|| (!state.disable_doc_comments && is_doc_comment(comment.as_ref().unwrap()))
{
let include_comments = state.include_comments;
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
let include_comments =
if !state.disable_doc_comments && is_doc_comment(comment.as_ref().unwrap()) {
true
} else {
include_comments
};
if include_comments {
return Some((Token::Comment(comment.unwrap()), start_pos));
} else if state.comment_level > 0 {
// Reached EOF without ending comment block
return None;
}
}
@ -1496,6 +1512,8 @@ fn get_next_token_inner(
eat_next(stream, pos);
let mut comment = match stream.peek_next() {
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
Some('/') if !state.disable_doc_comments => {
eat_next(stream, pos);
@ -1529,6 +1547,8 @@ fn get_next_token_inner(
eat_next(stream, pos);
let mut comment = match stream.peek_next() {
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
Some('*') if !state.disable_doc_comments => {
eat_next(stream, pos);
@ -2024,6 +2044,8 @@ impl Engine {
comment_level: 0,
end_with_none: false,
include_comments: false,
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
disable_doc_comments: self.disable_doc_comments,
is_within_text_terminated_by: None,
},

View File

@ -27,6 +27,7 @@ fn test_comments() -> Result<(), Box<EvalAltResult>> {
}
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
#[test]
fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();