pub struct Match { /* private fields */ }Expand description
A rule that matches one regular expression ("match").
The whole match takes the rule’s scope; capture groups
take their own with capture. Group 0 is the whole
match.
§Examples
use textmate_lang::{Grammar, Match};
let json = Grammar::new("source.demo")
.pattern(
Match::new(r"\b(fn)\s+([a-z_]\w*)")
.capture(1, "storage.type.function.demo")
.capture(2, "entity.name.function.demo"),
)
.to_json()?;
assert!(json.contains(r#""match": "\\b(fn)\\s+([a-z_]\\w*)""#));Implementations§
Source§impl Match
impl Match
Sourcepub fn new(regex: impl Into<Cow<'static, str>>) -> Match
pub fn new(regex: impl Into<Cow<'static, str>>) -> Match
A rule matching regex, an Oniguruma regular expression in the
supported subset.
| Parameter | Meaning |
|---|---|
regex | The pattern. Must be valid and non-empty, checked on emission. |
§Examples
use textmate_lang::{Error, Grammar, Match, RegexErrorKind};
let err = Grammar::new("source.demo").pattern(Match::new("(unclosed")).to_json().unwrap_err();
match err {
Error::InvalidRegex { path, error } => {
assert_eq!(path, "/patterns/0/match");
assert_eq!(error.kind, RegexErrorKind::UnclosedGroup);
}
other => panic!("{other}"),
}Sourcepub fn scope(self, scope: impl Into<Cow<'static, str>>) -> Match
pub fn scope(self, scope: impl Into<Cow<'static, str>>) -> Match
Scopes the whole match ("name"). May substitute captured text with
$n, ${n:/downcase}, or ${n:/upcase}.
| Parameter | Meaning |
|---|---|
scope | One scope name, or several separated by single spaces. |
§Examples
use textmate_lang::{Grammar, Match};
let json = Grammar::new("source.demo")
.pattern(Match::new(r"\b(if|else)\b").scope("keyword.control.$1.demo"))
.to_json()?;
assert!(json.contains(r#""name": "keyword.control.$1.demo""#));Sourcepub fn capture(self, group: u32, capture: impl Into<Capture>) -> Match
pub fn capture(self, group: u32, capture: impl Into<Capture>) -> Match
Sets what to do with capture group group ("captures").
The group must exist in the pattern
(Error::CaptureOutOfRange), and
may be set once (Error::DuplicateCapture).
| Parameter | Meaning |
|---|---|
group | The group number; 0 is the whole match. |
capture | A scope name, or a Capture. |
§Examples
use textmate_lang::{Grammar, Match};
let json = Grammar::new("source.demo")
.pattern(Match::new(r"(\w+)\s*(=)").capture(2, "keyword.operator.demo").capture(1, "variable.demo"))
.to_json_compact()?;
// Captures are emitted in group order.
assert!(json.contains(r#""captures":{"1":{"name":"variable.demo"},"2":"#));Trait Implementations§
impl Eq for Match
impl StructuralPartialEq for Match
Auto Trait Implementations§
impl Freeze for Match
impl RefUnwindSafe for Match
impl Send for Match
impl Sync for Match
impl Unpin for Match
impl UnsafeUnpin for Match
impl UnwindSafe for Match
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more