DocumentLoader

Struct DocumentLoader 

Source
pub struct DocumentLoader { /* private fields */ }
Expand description

Document loader with transform shortcuts

DocumentLoader provides a convenient API for loading source text and running transforms on it. It’s used by both production code (CLI, libraries) and tests.

§Example

use lex_parser::lex::loader::DocumentLoader;

// Load from file and parse
let doc = DocumentLoader::from_path("example.lex")
    .unwrap()
    .parse()
    .unwrap();

// Load from string and get tokens
let tokens = DocumentLoader::from_string("Hello world\n")
    .tokenize()
    .unwrap();

Implementations§

Source§

impl DocumentLoader

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, LoaderError>

Load from a file path

§Example
use lex_parser::lex::loader::DocumentLoader;

let loader = DocumentLoader::from_path("example.lex").unwrap();
Source

pub fn from_string<S: Into<String>>(source: S) -> Self

Load from a string

§Example
use lex_parser::lex::loader::DocumentLoader;

let loader = DocumentLoader::from_string("Hello world\n");
Source

pub fn with<O: 'static>( &self, transform: &Transform<String, O>, ) -> Result<O, LoaderError>

Run a custom transform on the source

This is the generic method that all shortcuts use internally.

§Example
use lex_parser::lex::loader::DocumentLoader;
use lex_parser::lex::transforms::standard::LEXING;

let loader = DocumentLoader::from_string("Hello\n");
let tokens = loader.with(&*LEXING).unwrap();
Source

pub fn parse(&self) -> Result<Document, LoaderError>

Parse the source into a Document AST

This is a shortcut for .with(&STRING_TO_AST).

§Example
use lex_parser::lex::loader::DocumentLoader;

let doc = DocumentLoader::from_string("Hello world\n")
    .parse()
    .unwrap();
Source

pub fn tokenize(&self) -> Result<TokenStream, LoaderError>

Tokenize the source with full lexing (including semantic indentation)

This is a shortcut for .with(&LEXING).

§Example
use lex_parser::lex::loader::DocumentLoader;

let tokens = DocumentLoader::from_string("Session:\n    Content\n")
    .tokenize()
    .unwrap();
// tokens include Indent/Dedent
Source

pub fn base_tokens(&self) -> Result<TokenStream, LoaderError>

Get base tokens (core tokenization only, no semantic indentation)

This is a shortcut for .with(&CORE_TOKENIZATION).

§Example
use lex_parser::lex::loader::DocumentLoader;

let tokens = DocumentLoader::from_string("Hello\n")
    .base_tokens()
    .unwrap();
// tokens include raw Indentation tokens, not Indent/Dedent
Source

pub fn source(&self) -> String

Get the raw source string

§Example
use lex_parser::lex::loader::DocumentLoader;

let loader = DocumentLoader::from_string("Hello\n");
assert_eq!(loader.source(), "Hello\n");
Source

pub fn source_ref(&self) -> &str

Get a reference to the raw source string

Use this when you don’t need an owned copy.

Auto Trait Implementations§

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more