pub struct DeclarativeProfile {
pub name: String,
pub extensions: Vec<String>,
pub line_comments: Vec<LineDelimiter>,
pub block_comments: Vec<BlockDelimiter>,
pub strings: Vec<StringDelimiter>,
pub protected_patterns: Vec<ProtectedPattern>,
}Expand description
A deliberately limited scanner profile for unambiguous comment syntaxes.
A profile describes a syntax whose comments and strings are literal delimiters and nothing more, so that one byte-oriented pass can find them with no grammar and no backtracking. That is the whole of what it can express, and the limits are enforced rather than assumed:
- Every delimiter is a literal token. It must not be empty and must not contain a line terminator.
- No comment delimiter may be a prefix of another comment delimiter, no string delimiter of another string delimiter, and no comment delimiter of a string delimiter or the reverse. One position therefore never has two readings, which is what makes the single pass correct.
- A nested block needs a start and an end that are distinct and neither contained in the other, so the depth count cannot be fooled.
- A comment’s
CommentKindis whatever the delimiter declares. There is no classification by content the way a built-in scanner does it: a profile finds no shebang, no encoding line, and no license notice unless aProtectedPatternsays so.
A syntax that needs more than this — a regex literal, a heredoc, an indentation rule — needs a scanner plugin instead.
§Examples
use ocomment_core::{
CommentKind, DeclarativeProfile, LineDelimiter, StringDelimiter, TransformOptions,
transform_profile,
};
let profile = DeclarativeProfile {
name: "lisp".into(),
extensions: vec!["lisp".into()],
line_comments: vec![LineDelimiter {
start: ";;".into(),
requires_boundary: false,
kind: CommentKind::Line,
}],
strings: vec![StringDelimiter {
start: "\"".into(),
end: "\"".into(),
escape: Some("\\".into()),
multiline: false,
}],
..Default::default()
};
let source = b"(print \";; not a comment\") ;; a comment\n";
let result = transform_profile(source, &profile, TransformOptions::default()).unwrap();
assert_eq!(result.output, b"(print \";; not a comment\") \n");Fields§
§name: StringWhat to call the profile in a diagnostic. It must not be blank.
extensions: Vec<String>The file extensions this profile claims, with or without the leading dot and matched case-insensitively. The scanner never reads this; it is for whoever picks a profile for a path.
line_comments: Vec<LineDelimiter>Tokens that open a comment running to the end of the line.
block_comments: Vec<BlockDelimiter>Tokens that open a comment running to a closing token.
strings: Vec<StringDelimiter>String forms to skip, so a comment token inside one is only text.
protected_patterns: Vec<ProtectedPattern>Substrings that turn a comment into a kept directive.
Trait Implementations§
Source§impl Clone for DeclarativeProfile
impl Clone for DeclarativeProfile
Source§fn clone(&self) -> DeclarativeProfile
fn clone(&self) -> DeclarativeProfile
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more