roc/crates/compiler/parse/src/test_helpers.rs
Joshua Warner 07be8ec000
Refactor Parser trait to pass min_indent
This removes the need to explicitly pass thru min_indent when using the parser combinators.

My ultimate goal here is to evolve the current parser closer toward a purely combinator-based parser,
at which point we can more easily transition smoothly to a formal(ish) grammar, or expand the meanings of combinators
to include things like:
* Incremental (re)parsing
* Unified parsing and formatting code
* Better error recovery
* Using the main parser directly for syntax highlighting
2022-10-31 13:31:47 -07:00

43 lines
1.2 KiB
Rust

use crate::ast;
use crate::ast::Defs;
use crate::module::module_defs;
use crate::parser::Parser;
use crate::parser::SourceError;
use crate::parser::SyntaxError;
use crate::state::State;
use bumpalo::Bump;
use roc_region::all::Loc;
use roc_region::all::Position;
pub fn parse_expr_with<'a>(
arena: &'a Bump,
input: &'a str,
) -> Result<ast::Expr<'a>, SyntaxError<'a>> {
parse_loc_with(arena, input)
.map(|loc_expr| loc_expr.value)
.map_err(|e| e.problem)
}
#[allow(dead_code)]
pub fn parse_loc_with<'a>(
arena: &'a Bump,
input: &'a str,
) -> Result<Loc<ast::Expr<'a>>, SourceError<'a, SyntaxError<'a>>> {
let state = State::new(input.trim().as_bytes());
match crate::expr::test_parse_expr(0, arena, state.clone()) {
Ok(loc_expr) => Ok(loc_expr),
Err(fail) => Err(SyntaxError::Expr(fail, Position::default()).into_source_error(&state)),
}
}
pub fn parse_defs_with<'a>(arena: &'a Bump, input: &'a str) -> Result<Defs<'a>, SyntaxError<'a>> {
let state = State::new(input.trim().as_bytes());
let min_indent = 0;
match module_defs().parse(arena, state, min_indent) {
Ok(tuple) => Ok(tuple.1),
Err(tuple) => Err(tuple.1),
}
}