pub struct Injection<'a> { /* private fields */ }Expand description
A range of a parsed source covered by an injection (LSF2 §12): another
language’s content, or a token of this language whose text has structure
of its own. See Parse::injections.
§Examples
use lang_forge::Language;
// `$name.field` inside a string is parsed by the `path` rule.
let lang = Language::from_lsf(
"[sketch]\nformat = 2\n[language]\nname = \"s\"\nversion = \"1.0.0\"\n\
[lexer.strings.DQ]\nopen = '\"'\nembedded = [{ token = \"DQ_VAR\", regex = '\\$[a-z]+(\\.[a-z]+)*', parse = \"path\" }]\n\
interpolate = [{ open = \"{\", close = \"}\", rule = \"path\" }]\n\
[lexer.tokens]\nVAR = { regex = '\\$[a-z]+' }\n\
[rules]\nfile = \"DQ*\"\npath = \"VAR ('.' IDENT)*\"\n",
)?;
let src = "\"hello $user.name\"";
let parse = lang.parse(src);
let injection = &parse.injections()[0];
assert_eq!((injection.id(), injection.language(), injection.is_editor()), ("DQ_VAR", "self", false));
assert_eq!(injection.span().start().to_usize(), 7);
let tree = injection.tree().expect("a self-injection is parsed");
assert_eq!(lang.kind_name(*tree.kind()), "path");
assert_eq!(tree.text(src), Some("$user.name"));Implementations§
Source§impl<'a> Injection<'a>
impl<'a> Injection<'a>
Sourcepub fn id(&self) -> &'a str
pub fn id(&self) -> &'a str
The injection’s id from [injections], or the token class’s name for
an embedded token with parse.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Injection<'a>
impl<'a> RefUnwindSafe for Injection<'a>
impl<'a> Send for Injection<'a>
impl<'a> Sync for Injection<'a>
impl<'a> Unpin for Injection<'a>
impl<'a> UnsafeUnpin for Injection<'a>
impl<'a> UnwindSafe for Injection<'a>
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