prism_compiler/
parser.rs

1use crate::lang::{TcEnv, UnionIndex};
2use prism_parser::core::cache::Allocs;
3use prism_parser::error::aggregate_error::{AggregatedParseError, ParseResultExt};
4use prism_parser::error::set_error::SetError;
5use prism_parser::grammar::GrammarFile;
6use prism_parser::parse_grammar;
7use prism_parser::parser::parser_instance::run_parser_rule;
8use std::sync::LazyLock;
9
10pub static GRAMMAR: LazyLock<GrammarFile<'static, 'static>> = LazyLock::new(|| {
11    parse_grammar::<SetError>(include_str!("../resources/prism.pg"), Allocs::new_leaking())
12        .unwrap_or_eprint()
13});
14
15pub fn parse_prism_in_env<'p>(
16    program: &'p str,
17    env: &mut TcEnv,
18) -> Result<UnionIndex, AggregatedParseError<'p, SetError<'p>>> {
19    run_parser_rule::<SetError, _>(&GRAMMAR, "expr", program, |r, allocs| {
20        env.insert_from_action_result(r, program, allocs)
21    })
22}
23
24pub fn parse_prism(program: &str) -> Result<(TcEnv, UnionIndex), AggregatedParseError<SetError>> {
25    let mut env = TcEnv::default();
26    parse_prism_in_env(program, &mut env).map(|i| (env, i))
27}