Skip to main content

Engine

Struct Engine 

Source
pub struct Engine<C> { /* private fields */ }
Expand description

The multi-pass fixpoint driver.

An engine owns a Schedule of passes and runs them over a ParseTree in rounds:

  • Round r applies schedule[r] to every node whose depth == r and whose status is Unparsed or Failed.
  • Nodes expanded in round r create children at depth r + 1; nodes that fail are retried at depth r + 1 by the next pass.
  • The run reaches its fixpoint when a round finds no ready nodes, or after max_rounds rounds.

§Examples

use increparse::{Engine, Outcome, ParseTree, Pass, Schedule, SerialExecutor, Span};

struct MarkDone;

impl Pass for MarkDone {
    type Ctx = ();
    fn parse(&self, _source: &str, _span: Span, _ctx: &()) -> Outcome<()> {
        Outcome::Done
    }
}

let engine = Engine::with((MarkDone,));
let mut tree = ParseTree::new(0, Span::new(0, 3, 0), ());
let report = engine.run("abc", &mut tree, &SerialExecutor, &increparse::CancelToken::new());

assert!(report.reached_fixpoint);
assert_eq!(tree.status(tree.root()), increparse::Status::Done);

Implementations§

Source§

impl<C> Engine<C>

Source

pub fn new(schedule: Schedule<C>) -> Engine<C>

Creates an engine with default EngineConfig.

Source

pub fn with(passes: impl Passes<C>) -> Engine<C>

Creates an engine from passes directly — a tuple of mixed pass types (up to 12) or a Vec of boxed passes:

use increparse::{pass_fn, Engine, Outcome, Span};

let a = pass_fn(|_source: &str, _span, _ctx: &()| Outcome::Done);
let b = pass_fn(|_source: &str, _span, _ctx: &()| Outcome::Done);
let engine = Engine::with((a, b)); // round 0 runs `a`, round 1 runs `b`
Source

pub fn with_config(passes: impl Passes<C>, config: EngineConfig) -> Engine<C>

Creates an engine with an explicit configuration.

Source

pub fn schedule(&self) -> &Schedule<C>

The pass schedule this engine runs.

Source

pub fn config(&self) -> &EngineConfig

The engine’s configuration.

Source

pub fn max_rounds(&self) -> usize

The effective round cap: the configured cap, or one round per pass.

Source

pub fn run<E>( &self, source: &str, tree: &mut ParseTree<C>, exec: &E, cancel: &CancelToken, ) -> RunReport
where C: Clone + PartialEq + Send + 'static, E: Executor,

Runs passes over tree until the fixpoint, the round cap, or cancellation.

source must be the same text (at the revision the tree was created with) that passes will slice via their spans. The tree is updated in place; if the run is cancelled partway, the merged prefix of each batch keeps the tree consistent and a later call resumes where this one stopped.

When re-expanding a node that already has children (after ParseTree::edit), produced children are matched against the existing ones by span and context — equal children are reused whole, so only the edited regions are re-parsed. This is why C must implement PartialEq.

A run over an already-settled tree reports reached_fixpoint without doing any work. A run over a non-settled tree with an empty schedule does no work and reports neither fixpoint nor cancellation — the root simply stays Unparsed.

Auto Trait Implementations§

§

impl<C> !RefUnwindSafe for Engine<C>

§

impl<C> !UnwindSafe for Engine<C>

§

impl<C> Freeze for Engine<C>
where Schedule<C>: Freeze,

§

impl<C> Send for Engine<C>
where Schedule<C>: Send,

§

impl<C> Sync for Engine<C>
where Schedule<C>: Sync,

§

impl<C> Unpin for Engine<C>
where Schedule<C>: Unpin,

§

impl<C> UnsafeUnpin for Engine<C>

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