Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start work on checkpoint #1

Merged
merged 1 commit into from
Jan 3, 2022
Merged

Start work on checkpoint #1

merged 1 commit into from
Jan 3, 2022

Conversation

udoprog
Copy link
Owner

@udoprog udoprog commented Jan 3, 2022

This introduces basic checkpoints which ensures a well-balanced tree.

Checkpointing is used to decide late in a parsing process what node to emit.

use anyhow::Result;
use syntree::{print, Span, TreeBuilder};

#[derive(Debug, Clone, Copy)]
enum Syntax {
    Root,
    Number,
    Lit,
    Whitespace,
}

fn main() -> Result<()> {
    let mut b = TreeBuilder::new();

    let c = b.checkpoint();

    b.start_node(Syntax::Number);
    b.token(Syntax::Lit, Span::new(1, 2));
    b.end_node()?;

    b.token(Syntax::Whitespace, Span::new(2, 5));

    b.start_node(Syntax::Number);
    b.token(Syntax::Lit, Span::new(5, 7));
    b.token(Syntax::Lit, Span::new(7, 9));
    b.end_node()?;

    b.insert_node_at(c, Syntax::Root);

    let tree = b.build()?;

    print::print(&mut std::io::stdout(), &tree)?;
    Ok(())
}

@udoprog udoprog merged commit e0b3663 into main Jan 3, 2022
@udoprog udoprog deleted the checkpoint branch January 3, 2022 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant