Skip to main content

Kind

Struct Kind 

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

The kind of a token or node in a forged language’s syntax tree.

A schematic declares its vocabulary as text — rule names, keywords, punctuation — so a forged language cannot have a Rust enum of its own. Kind stands in for one: a small Copy value, assigned when the language is forged, that compares as cheaply as an enum discriminant. Tokens and nodes share the type, the model syntax_lang is built on.

Kinds are looked up by name with Language::kind and named with Language::kind_name. Look a kind up once, keep it, and compare against it while walking trees: the comparison is a single integer compare.

NameKind of
a rule name, such as let_stmtthe node the rule builds
a literal’s text, such as let or +=the keyword or symbol token
a Pratt level’s node, or binary, prefix, postfixan operator node
IDENT, NUMBER, STRING, NEWLINEthe built-in token classes
WHITESPACE, COMMENTtrivia tokens
UNKNOWNa character the lexer did not recognize (trivia)
ERRORa node wrapping tokens the parser skipped

A kind belongs to the language that produced it. Comparing kinds from two different languages is meaningless, and naming one language’s kind with another language returns the wrong name or "<unknown>".

§Trivia

TokenKind::is_trivia answers without the language: whitespace, comments, unrecognized characters, and — unless the schematic sets newlines = true — line breaks are trivia. Trivia is kept in the tree, so it stays lossless, but the parser never sees it.

§Examples

use lang_forge::Language;
use lang_forge::syntax_lang::TokenKind;

let lang = Language::from_lsf(
    r#"
    [language]
    name = "sum"

    [rules]
    sum = "NUMBER ('+' NUMBER)*"
    "#,
)?;

let plus = lang.kind("+").expect("the grammar uses '+'");
let space = lang.kind("WHITESPACE").expect("built in");
assert_eq!(lang.kind_name(plus), "+");
assert!(space.is_trivia());
assert!(!plus.is_trivia());

let parse = lang.parse("1 + 2");
let pluses = parse.tree().tokens().filter(|t| *t.kind() == plus).count();
assert_eq!(pluses, 1);

Trait Implementations§

Source§

impl Clone for Kind

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 Copy for Kind

Source§

impl Debug for Kind

Source§

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

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

impl Eq for Kind

Source§

impl Hash for Kind

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Kind

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Kind

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 PartialOrd for Kind

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Kind

Source§

impl TokenKind for Kind

Source§

fn is_trivia(&self) -> bool

Whether this kind is trivia: whitespace, comments, and anything else a parser skips because it is not part of the grammar. Read more
Source§

fn is_eof(&self) -> bool

Whether this kind is the end-of-input marker the lexer emits once the source is exhausted. Read more
Source§

fn symbol(&self) -> Option<Symbol>

The interned lexeme this token carries, if any. Read more

Auto Trait Implementations§

§

impl Freeze for Kind

§

impl RefUnwindSafe for Kind

§

impl Send for Kind

§

impl Sync for Kind

§

impl Unpin for Kind

§

impl UnsafeUnpin for Kind

§

impl UnwindSafe for Kind

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.