Skip to main content

DeclarativeProfile

Struct DeclarativeProfile 

Source
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 CommentKind is 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 a ProtectedPattern says 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: String

What 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

Source§

fn clone(&self) -> DeclarativeProfile

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DeclarativeProfile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DeclarativeProfile

Source§

fn default() -> DeclarativeProfile

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for DeclarativeProfile

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for DeclarativeProfile

Source§

impl PartialEq for DeclarativeProfile

Source§

fn eq(&self, other: &DeclarativeProfile) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for DeclarativeProfile

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for DeclarativeProfile

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.