Skip to main content

Rule

Struct Rule 

Source
pub struct Rule(/* private fields */);
Expand description

One entry of a pattern list: what the tokenizer tries at each position.

A rule is one of:

  • a Match: one regular expression, scoped as a whole and by capture group ("match" in the JSON);
  • a BeginEnd: a region from a begin match to an end match, with its own patterns inside ("begin"/"end");
  • a BeginWhile: a region that starts at a begin match and continues on each following line while a while pattern matches at its start ("begin"/"while"), as Markdown block quotes do;
  • an include of a repository entry, the grammar itself, the base grammar, or another grammar ("include");
  • a list of rules tried together (Rule::patterns).

Match, BeginEnd, and BeginWhile convert into a Rule with into(), and every method that takes a rule accepts them directly.

§Examples

use textmate_lang::{BeginEnd, Grammar, Match, Rule};

let json = Grammar::new("source.demo")
    .pattern(Rule::include("comments"))
    .pattern(Match::new(r"\b(let|fn)\b").scope("keyword.other.demo"))
    .repository(
        "comments",
        Rule::patterns([
            Match::new("//.*$").scope("comment.line.double-slash.demo").into(),
            BeginEnd::new(r"/\*", r"\*/").scope("comment.block.demo").into(),
        ]),
    )
    .to_json()?;
assert!(json.contains(r##""include": "#comments""##));

Implementations§

Source§

impl Rule

Source

pub fn include(name: impl Into<Cow<'static, str>>) -> Rule

Includes the repository entry called name ("include": "#name").

The entry must exist (Error::UndefinedInclude), checked when the grammar is emitted. The entry’s patterns are tried as if they were written in place of the include.

ParameterMeaning
nameA repository entry’s name, without the #.
§Examples
use textmate_lang::{Grammar, Match, Rule};

let json = Grammar::new("source.demo")
    .pattern(Rule::include("numbers"))
    .repository("numbers", Match::new(r"\d+").scope("constant.numeric.demo"))
    .to_json()?;
assert!(json.contains(r##""include": "#numbers""##));
Source

pub fn include_self() -> Rule

Includes this grammar’s top-level patterns ("include": "$self"), typically inside a region whose contents are the language again, such as the body of a block.

§Examples
use textmate_lang::{BeginEnd, Grammar, Rule};

let json = Grammar::new("source.demo")
    .pattern(BeginEnd::new(r"\{", r"\}").scope("meta.block.demo").pattern(Rule::include_self()))
    .to_json()?;
assert!(json.contains(r#""include": "$self""#));
Source

pub fn include_base() -> Rule

Includes the base grammar’s top-level patterns ("include": "$base"): the grammar of the file being highlighted. That is this grammar, unless this one is embedded in another (a SQL grammar inside a host language’s strings, say), in which case it is the host.

§Examples
use textmate_lang::{BeginEnd, Grammar, Rule};

let json = Grammar::new("source.demo")
    .pattern(BeginEnd::new(r"\(", r"\)").pattern(Rule::include_base()))
    .to_json()?;
assert!(json.contains(r#""include": "$base""#));
Source

pub fn include_scope(scope: impl Into<Cow<'static, str>>) -> Rule

Includes another grammar’s top-level patterns by its scope name ("include": "source.js"), to embed one language in another.

The other grammar is not available here, so only the scope name’s syntax is checked. Naming this grammar’s own scope is the same as include_self.

ParameterMeaning
scopeThe other grammar’s scopeName.
§Examples
use textmate_lang::{BeginEnd, Grammar, Rule};

let json = Grammar::new("text.html.demo")
    .pattern(
        BeginEnd::new("<script>", "</script>")
            .content_scope("source.js.embedded.demo")
            .pattern(Rule::include_scope("source.js")),
    )
    .to_json()?;
assert!(json.contains(r#""include": "source.js""#));
Source

pub fn include_scope_entry( scope: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>, ) -> Rule

Includes one repository entry of another grammar ("include": "source.js#expression").

Only the syntax is checked, unless scope is this grammar’s own scope name: then the entry must exist, as for include.

ParameterMeaning
scopeThe other grammar’s scopeName.
nameThe entry in that grammar’s repository.
§Examples
use textmate_lang::{Grammar, Rule};

let json = Grammar::new("source.demo")
    .pattern(Rule::include_scope_entry("source.js", "expression"))
    .to_json()?;
assert!(json.contains(r#""include": "source.js#expression""#));
Source

pub fn patterns(rules: impl IntoIterator<Item = Rule>) -> Rule

A list of rules tried together, as one rule ({"patterns": [...]}).

The usual body of a repository entry that groups related rules, so that one include pulls in all of them. The rules are tried in order; at each position the earliest match wins, and the first rule in the list wins a tie.

ParameterMeaning
rulesThe rules, in priority order.
§Examples
use textmate_lang::{Grammar, Match, Rule};

let literals = Rule::patterns([
    Match::new(r"\b(true|false)\b").scope("constant.language.demo").into(),
    Match::new(r"\b\d+\b").scope("constant.numeric.demo").into(),
]);
let json = Grammar::new("source.demo")
    .pattern(Rule::include("literals"))
    .repository("literals", literals)
    .to_json_compact()?;
assert!(json.contains(r#""literals":{"patterns":[{"name":"constant.language.demo""#));

Trait Implementations§

Source§

impl Clone for Rule

Source§

fn clone(&self) -> Self

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 Rule

Source§

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

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

impl Drop for Rule

Source§

fn drop(&mut self)

Drops nested rules from an explicit stack, so dropping a rule nested arbitrarily deep cannot overflow the call stack.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for Rule

Source§

impl From<BeginEnd> for Rule

Source§

fn from(rule: BeginEnd) -> Rule

Converts to this type from the input type.
Source§

impl From<BeginWhile> for Rule

Source§

fn from(rule: BeginWhile) -> Rule

Converts to this type from the input type.
Source§

impl From<Match> for Rule

Source§

fn from(rule: Match) -> Rule

Converts to this type from the input type.
Source§

impl PartialEq for Rule

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Rule

Auto Trait Implementations§

§

impl Freeze for Rule

§

impl RefUnwindSafe for Rule

§

impl Send for Rule

§

impl Sync for Rule

§

impl Unpin for Rule

§

impl UnsafeUnpin for Rule

§

impl UnwindSafe for Rule

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> 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, !>

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.