skullbrain-parser 0.1.0

Parses skullbrain tokens into a concrete syntax tree (CST).
Documentation
//! # skullbrain-parser
//!
//! The skullbrain crate is a library that provides a parser,, and a concrete syntax tree.

mod cstree_impl;
mod primitive;
mod state;
mod syntax;
mod syntax_kind;

/// A typed-cst layer
pub mod cst;

/// A typed-ast layer
pub mod ast;

#[cfg(test)]
mod test;
use std::path::Path;

pub(crate) use syntax_kind::SyntaxKind;

use cstree::build::NodeCache;
use cstree_impl::CstreeBuilder;
use state::{Diagnostics, ParserState};

use crate::cst::SyntaxNode;
use crate::state::ConcreteSyntaxTreeBuilder;

/// Represents the complete Concrete Syntax Tree (CST) for a source file.
pub struct SourceSyntaxTree {
    node: SyntaxNode,
}

/// Parses the given source code
#[allow(private_bounds)]
pub fn parse_source(
    file_name: &Path,
    input: &str,
    cache: &mut NodeCache,
    diagnostic: impl Diagnostics,
) -> SourceSyntaxTree {
    let builder = CstreeBuilder::new(cache, input);
    let mut state = ParserState::new(file_name, input, diagnostic, builder);
    while let Some(_) = state.peek() {
        state.parse_expression();
    }

    let green = state.finish();
    let node = SyntaxNode::new_root(green);

    SourceSyntaxTree { node }
}

/// Parses the given source code
#[allow(private_bounds)]
pub fn parse_file(
    file_name: &Path,
    cache: &mut NodeCache,
    diagnostic: impl Diagnostics,
) -> std::io::Result<SourceSyntaxTree> {
    let input = std::fs::read_to_string(file_name)?;
    Ok(parse_source(file_name, input.as_str(), cache, diagnostic))
}

impl SourceSyntaxTree {
    ///
    pub fn expressions(&self) -> impl Iterator<Item = crate::cst::Expr> + use<'_> {
        self.node
            .children_with_tokens()
            .map(|syn| crate::cst::Expr::cast_element(syn))
    }
}