Skip to main content

Node

Enum Node 

Source
pub enum Node<'ast> {
Show 19 variants Null, Bool(bool), Number(&'ast Number), String(&'ast str), StringChunks(&'ast [StringChunk<Ast<'ast>>]), Fun { args: &'ast [Pattern<'ast>], body: &'ast Ast<'ast>, }, Let { bindings: &'ast [LetBinding<'ast>], body: &'ast Ast<'ast>, rec: bool, }, App { head: &'ast Ast<'ast>, args: &'ast [Ast<'ast>], }, Var(LocIdent), EnumVariant { tag: LocIdent, arg: Option<&'ast Ast<'ast>>, }, Record(&'ast Record<'ast>), IfThenElse { cond: &'ast Ast<'ast>, then_branch: &'ast Ast<'ast>, else_branch: &'ast Ast<'ast>, }, Match(Match<'ast>), Array(&'ast [Ast<'ast>]), PrimOpApp { op: &'ast PrimOp, args: &'ast [Ast<'ast>], }, Annotated { annot: &'ast Annotation<'ast>, inner: &'ast Ast<'ast>, }, Import(Import<'ast>), Type(&'ast Type<'ast>), ParseError(&'ast ParseError),
}
Expand description

A node of the Nickel AST.

Nodes are built by the parser and then mostly traversed immutably. Such nodes are optimized for sharing (hence immutability) and for size, as the size of an enum can grow quite quickly in Rust. In particular, any data that is bigger than a few words isn’t usually owned but rather a reference to some arena-allocated data

Using an arena has another advantage: the data is allocated in the same order as the AST is built. This means that even if there are reference indirections, the children of a node are most likely close to the node itself in memory, which should be good for cache locality.

Variants§

§

Null

The null value.

§

Bool(bool)

A boolean value.

§

Number(&'ast Number)

A number.

A number is an arbitrary-precision rational in Nickel. It’s not small and thus we put it behind a reference to avoid size bloat.

§

String(&'ast str)

A string literal.

§

StringChunks(&'ast [StringChunk<Ast<'ast>>])

A string containing interpolated expressions, represented as a list of either literals or expressions.

As opposed to nickel_lang_core::term::Term::StrChunks, the chunks are stored in the original order: "hello%{var}" will give ["hello", var].

§

Fun

A function.

Fields

§args: &'ast [Pattern<'ast>]
§body: &'ast Ast<'ast>
§

Let

A let block.

Fields

§bindings: &'ast [LetBinding<'ast>]
§body: &'ast Ast<'ast>
§rec: bool
§

App

An application to one or more arguments.

Fields

§head: &'ast Ast<'ast>
§args: &'ast [Ast<'ast>]
§

Var(LocIdent)

A variable.

§

EnumVariant

An enum variant (an algebraic datatype).

Variants have at most one argument: variants with no arguments are often called simply enum tags. Note that one can just use a record as an argument to emulate variants with multiple arguments.

Fields

§arg: Option<&'ast Ast<'ast>>
§

Record(&'ast Record<'ast>)

A record.

§

IfThenElse

An if-then-else expression.

Fields

§cond: &'ast Ast<'ast>
§then_branch: &'ast Ast<'ast>
§else_branch: &'ast Ast<'ast>
§

Match(Match<'ast>)

A match expression. This expression is still to be applied to an argument to match on.

§

Array(&'ast [Ast<'ast>])

An array.

§

PrimOpApp

An n-ary primitive operation application. As opposed to a traditional function application:

  1. The function part is necessarily a primitive operation.
  2. The arguments are forced before entering the

Fields

§op: &'ast PrimOp
§args: &'ast [Ast<'ast>]
§

Annotated

A term with a type and/or contract annotation.

Fields

§annot: &'ast Annotation<'ast>
§inner: &'ast Ast<'ast>
§

Import(Import<'ast>)

An import.

§

Type(&'ast Type<'ast>)

A type in term position, such as in let my_contract = Number -> Number in ....

During evaluation, this will get turned into a contract.

§

ParseError(&'ast ParseError)

A term that couldn’t be parsed properly. Used by the LSP to handle partially valid programs.

Implementations§

Source§

impl<'ast> Node<'ast>

Source

pub fn try_str_chunk_as_static_str(&self) -> Option<String>

Tries to extract a static literal from string chunks.

This methods returns a Some(..) when the term is a Node::StringChunks and all the chunks are StringChunk::Literal

Source

pub fn spanned(self, pos: TermPos) -> Ast<'ast>

Attaches a position to this node turning it into an Ast.

Trait Implementations§

Source§

impl<'ast> Clone for Node<'ast>

Source§

fn clone(&self) -> Node<'ast>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl CloneTo for Node<'_>

Source§

type Data<'a> = Node<'a>

This is always Self, be we need associated types to make Rust understand that Self is always parametric over the 'ast lifetime. We’re using GATs to emulate higher-kinded types.
Source§

fn clone_to<'to>(data: Node<'_>, dest: &'to AstAlloc) -> Node<'to>

Clones owned data from the current allocator to dest.
Source§

impl<'ast> Debug for Node<'ast>

Source§

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

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

impl<'ast> Default for Node<'ast>

Source§

fn default() -> Node<'ast>

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

impl Display for Node<'_>

Source§

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

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

impl<'ast> From<Node<'ast>> for Ast<'ast>

Source§

fn from(node: Node<'ast>) -> Self

Converts to this type from the input type.
Source§

impl IsAtom for Node<'_>

Source§

fn is_atom(&self) -> bool

Determine if an expression is an atom of the surface syntax. Atoms are basic elements of the syntax that can freely substituted without being parenthesized.
Source§

impl<'ast> PartialEq for Node<'ast>

Source§

fn eq(&self, other: &Node<'ast>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Pretty<'a, Allocator> for &Node<'_>

Source§

fn pretty(self, allocator: &'a Allocator) -> DocBuilder<'a, Allocator>

Converts self into a document
Source§

impl Allocable for Node<'_>

Source§

impl<'ast> Eq for Node<'ast>

Source§

impl<'ast> StructuralPartialEq for Node<'ast>

Auto Trait Implementations§

§

impl<'ast> Freeze for Node<'ast>

§

impl<'ast> RefUnwindSafe for Node<'ast>

§

impl<'ast> Send for Node<'ast>

§

impl<'ast> Sync for Node<'ast>

§

impl<'ast> Unpin for Node<'ast>

§

impl<'ast> UnsafeUnpin for Node<'ast>

§

impl<'ast> UnwindSafe for Node<'ast>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

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

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

Source§

impl<T> ToDebugString for T
where T: Debug,

Source§

fn to_debug_string(&self) -> String

Returns the String produced by Ts Debug implementation.

§Examples
use malachite_base::strings::ToDebugString;

assert_eq!([1, 2, 3].to_debug_string(), "[1, 2, 3]");
assert_eq!(
    [vec![2, 3], vec![], vec![4]].to_debug_string(),
    "[[2, 3], [], [4]]"
);
assert_eq!(Some(5).to_debug_string(), "Some(5)");
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryConvert<'_, T> for U
where U: TryFrom<T>,

Source§

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

Source§

fn try_convert( _: &AstAlloc, from: T, ) -> Result<U, <U as TryConvert<'_, T>>::Error>

Source§

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

Source§

type Error = Infallible

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.
Source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U