TokenStream

Struct TokenStream 

Source
pub struct TokenStream<'t, T>
where T: TokenType,
{ /* private fields */ }
Expand description

A generic implementation of a stream of tokens with type T

The TokenStream implementation does not own the Tokens. It returns references to the tokens on demand. The caller can clone them if needed.

The token streams are used internally to generate the AST. They are not meant to be used independently.

Implementations§

Source§

impl<'t, T> TokenStream<'t, T>
where T: TokenType,

Source

pub fn new(tokens: &'t [TokenImpl<T>], max_stack_size: usize) -> Self

Create a new TokenStream with the given tokens

Examples found in repository?
examples/pt_html.rs (line 24)
8fn main() {
9    // This example parses regen.grammar, and prints html code with prismjs classes
10
11    // This example is ran from the `docs` directory
12    let grammar_source = fs::read_to_string("../regen.grammar").unwrap();
13
14    // These is a wrapper regen::parse_language_from_grammar that creates
15    // a Language object from a grammar string. However, it does not store
16    // semantic info, so we can't use that here.
17
18    // Run tokenizer
19    let lex_output = grammar::tokenize(&grammar_source);
20
21    // Create token stream
22    // 200 is the stack size, meaning the AST can have depth <= 200
23    // which is plenty. The default stack size for CLI is 2048
24    let mut ts = TokenStream::new(&lex_output.tokens, 200);
25
26    // Generate AST (need the ASTParser trait)
27    let parser = grammar::Parser;
28    let asts = parser.parse_ast_all(&mut ts).unwrap(); // error if syntax error
29
30    // collect semantic info so far
31    let mut outer_tbs = TokenBlocks::new(&grammar_source);
32    lex_output.apply_semantic(&mut outer_tbs);
33    asts.iter()
34        .for_each(|ast| ast.apply_semantic(&mut outer_tbs, &None));
35
36    // Generate PT, because it fills in additional semantic info
37    // This requires a lang builder as a context, but we won't need it
38    let mut lang_builder: Box<LangBuilder> = Box::default();
39    for ast in &asts {
40        // if you don't need semantic you can use the parse_pt method
41        match ast.parse_pt_with_semantic(outer_tbs, lang_builder) {
42            ParseTreeResultSemantic::Ok { /*pt*/ ctx, tbs, .. } => {
43                outer_tbs = tbs;
44                lang_builder = ctx;
45            },
46            ParseTreeResultSemantic::Err { .. /*pt, ctx, tbs, err*/ } => {
47                // should not happen, but you also get the context and semantic info back here
48                unreachable!();
49            }
50        }
51    }
52
53    // now we have the semantic info in outer_tbs, we can convert it to HTML
54
55    let code = outer_tbs.get_html(to_prismjs);
56
57    println!("{}", code);
58}
Source

pub fn is_exhausted(&self) -> bool

Returns if there is no token left after the current position

Source

pub fn get_guess_err_token(&self) -> Option<&'t TokenImpl<T>>

Get the best guess at which token is causing a syntax error

Source

pub fn set_error(&mut self, force: bool)

Set the error guess at the current index

Source

pub fn consume(&mut self) -> Option<&'t TokenImpl<T>>

Returns the next token if available, and advance the position A reference is returned to avoid copying the token

Source

pub fn push(&mut self) -> bool

Push the current position to stack so it can be restored

Source

pub fn pop(&mut self)

Pop position stack without restoring the position

Source

pub fn restore(&mut self)

Restore the position to be the index on the stack top. This does not pop the stack.

Trait Implementations§

Source§

impl<'t, T> Debug for TokenStream<'t, T>
where T: TokenType + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'t, T> Freeze for TokenStream<'t, T>

§

impl<'t, T> RefUnwindSafe for TokenStream<'t, T>
where T: RefUnwindSafe,

§

impl<'t, T> Send for TokenStream<'t, T>
where T: Sync,

§

impl<'t, T> Sync for TokenStream<'t, T>
where T: Sync,

§

impl<'t, T> Unpin for TokenStream<'t, T>

§

impl<'t, T> UnwindSafe for TokenStream<'t, T>
where T: RefUnwindSafe,

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