mod cstree_impl;
mod primitive;
mod state;
mod syntax;
mod syntax_kind;
pub mod cst;
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;
pub struct SourceSyntaxTree {
node: SyntaxNode,
}
#[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 }
}
#[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))
}
}