clean up helpers

This commit is contained in:
Folkert 2021-03-12 03:41:01 +01:00
parent b349ae7ab5
commit cba55734cb
8 changed files with 13 additions and 82 deletions

View file

@ -8,8 +8,6 @@ use roc_can::operator;
use roc_can::scope::Scope; use roc_can::scope::Scope;
use roc_collections::all::MutMap; use roc_collections::all::MutMap;
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds}; use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
use roc_parse::ast;
use roc_parse::parser::{State, SyntaxError};
use roc_problem::can::Problem; use roc_problem::can::Problem;
use roc_region::all::{Located, Region}; use roc_region::all::{Located, Region};
use roc_types::subs::{VarStore, Variable}; use roc_types::subs::{VarStore, Variable};
@ -19,24 +17,6 @@ pub fn test_home() -> ModuleId {
ModuleIds::default().get_or_insert(&"Test".into()) ModuleIds::default().get_or_insert(&"Test".into())
} }
#[allow(dead_code)]
pub fn parse_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)
}
#[allow(dead_code)]
pub fn parse_loc_with<'a>(
arena: &'a Bump,
input: &'a str,
) -> Result<Located<ast::Expr<'a>>, SyntaxError<'a>> {
let state = State::new(input.trim().as_bytes());
match roc_parse::expr::test_parse_expr(0, arena, state) {
Ok((loc_expr, _state)) => Ok(loc_expr),
Err(fail) => Err(SyntaxError::Expr(fail)),
}
}
#[allow(dead_code)] #[allow(dead_code)]
pub fn can_expr(expr_str: &str) -> CanExprOut { pub fn can_expr(expr_str: &str) -> CanExprOut {
can_expr_with(&Bump::new(), test_home(), expr_str) can_expr_with(&Bump::new(), test_home(), expr_str)
@ -54,7 +34,7 @@ pub struct CanExprOut {
#[allow(dead_code)] #[allow(dead_code)]
pub fn can_expr_with(arena: &Bump, home: ModuleId, expr_str: &str) -> CanExprOut { pub fn can_expr_with(arena: &Bump, home: ModuleId, expr_str: &str) -> CanExprOut {
let loc_expr = parse_loc_with(&arena, expr_str).unwrap_or_else(|e| { let loc_expr = roc_parse::test_helpers::parse_loc_with(&arena, expr_str).unwrap_or_else(|e| {
panic!( panic!(
"can_expr_with() got a parse error when attempting to canonicalize:\n\n{:?} {:?}", "can_expr_with() got a parse error when attempting to canonicalize:\n\n{:?} {:?}",
expr_str, e expr_str, e

View file

@ -12,25 +12,15 @@ mod test_fmt {
use roc_fmt::annotation::{Formattable, Newlines, Parens}; use roc_fmt::annotation::{Formattable, Newlines, Parens};
use roc_fmt::def::fmt_def; use roc_fmt::def::fmt_def;
use roc_fmt::module::fmt_module; use roc_fmt::module::fmt_module;
use roc_parse::ast::Expr;
use roc_parse::module::{self, module_defs}; use roc_parse::module::{self, module_defs};
use roc_parse::parser::{Parser, State, SyntaxError}; use roc_parse::parser::{Parser, State};
fn parse_with<'a>(arena: &'a Bump, input: &'a str) -> Result<Expr<'a>, SyntaxError<'a>> {
let state = State::new(input.trim().as_bytes());
match roc_parse::expr::test_parse_expr(0, arena, state) {
Ok((loc_expr, _state)) => Ok(loc_expr.value),
Err(fail) => Err(SyntaxError::Expr(fail)),
}
}
fn expr_formats_to(input: &str, expected: &str) { fn expr_formats_to(input: &str, expected: &str) {
let arena = Bump::new(); let arena = Bump::new();
let input = input.trim_end(); let input = input.trim_end();
let expected = expected.trim_end(); let expected = expected.trim_end();
match parse_with(&arena, input) { match roc_parse::test_helpers::parse_expr_with(&arena, input.trim()) {
Ok(actual) => { Ok(actual) => {
let mut buf = String::new_in(&arena); let mut buf = String::new_in(&arena);

View file

@ -23,7 +23,7 @@ pub fn test_parse_expr<'a>(
min_indent: u16, min_indent: u16,
arena: &'a bumpalo::Bump, arena: &'a bumpalo::Bump,
state: State<'a>, state: State<'a>,
) -> Result<(Located<Expr<'a>>, State<'a>), EExpr<'a>> { ) -> Result<Located<Expr<'a>>, EExpr<'a>> {
let parser = space0_before_e( let parser = space0_before_e(
loc!(|a, s| parse_expr_help(min_indent, a, s)), loc!(|a, s| parse_expr_help(min_indent, a, s)),
min_indent, min_indent,
@ -32,7 +32,7 @@ pub fn test_parse_expr<'a>(
); );
match parser.parse(arena, state) { match parser.parse(arena, state) {
Ok((_, expression, state)) => Ok((expression, state)), Ok((_, expression, _)) => Ok(expression),
Err((_, fail, _)) => Err(fail), Err((_, fail, _)) => Err(fail),
} }
} }

View file

@ -4,7 +4,7 @@ use crate::ident::{lowercase_ident, parse_ident_help, Ident};
use crate::parser::Progress::{self, *}; use crate::parser::Progress::{self, *};
use crate::parser::{ use crate::parser::{
backtrackable, optional, specialize, specialize_ref, word1, EPattern, PInParens, PRecord, backtrackable, optional, specialize, specialize_ref, word1, EPattern, PInParens, PRecord,
ParseResult, Parser, State, SyntaxError, ParseResult, Parser, State,
}; };
use bumpalo::collections::string::String; use bumpalo::collections::string::String;
use bumpalo::collections::Vec; use bumpalo::collections::Vec;
@ -290,10 +290,6 @@ fn loc_ident_pattern_help<'a>(
} }
} }
pub fn underscore_pattern<'a>() -> impl Parser<'a, Pattern<'a>, SyntaxError<'a>> {
specialize(|e, _, _| SyntaxError::Pattern(e), underscore_pattern_help())
}
fn underscore_pattern_help<'a>() -> impl Parser<'a, Pattern<'a>, EPattern<'a>> { fn underscore_pattern_help<'a>() -> impl Parser<'a, Pattern<'a>, EPattern<'a>> {
move |arena: &'a Bump, state: State<'a>| { move |arena: &'a Bump, state: State<'a>| {
let (_, _, next_state) = word1(b'_', EPattern::Underscore).parse(arena, state)?; let (_, _, next_state) = word1(b'_', EPattern::Underscore).parse(arena, state)?;
@ -318,13 +314,6 @@ fn lowercase_ident_pattern<'a>(
specialize(move |_, _, _| EPattern::End(row, col), lowercase_ident()).parse(arena, state) specialize(move |_, _, _| EPattern::End(row, col), lowercase_ident()).parse(arena, state)
} }
pub fn record_pattern<'a>(min_indent: u16) -> impl Parser<'a, Pattern<'a>, SyntaxError<'a>> {
specialize(
|e, r, c| SyntaxError::Pattern(EPattern::Record(e, r, c)),
record_pattern_help(min_indent),
)
}
#[inline(always)] #[inline(always)]
fn record_pattern_help<'a>(min_indent: u16) -> impl Parser<'a, Pattern<'a>, PRecord<'a>> { fn record_pattern_help<'a>(min_indent: u16) -> impl Parser<'a, Pattern<'a>, PRecord<'a>> {
move |arena, state| { move |arena, state| {

View file

@ -32,7 +32,7 @@ pub fn parse_loc_with<'a>(
let state = State::new(input.trim().as_bytes()); let state = State::new(input.trim().as_bytes());
match crate::expr::test_parse_expr(0, arena, state) { match crate::expr::test_parse_expr(0, arena, state) {
Ok((loc_expr, _state)) => Ok(loc_expr), Ok(loc_expr) => Ok(loc_expr),
Err(fail) => Err(SyntaxError::Expr(fail)), Err(fail) => Err(SyntaxError::Expr(fail)),
} }
} }

View file

@ -5,18 +5,12 @@ use crate::parser::{
allocated, backtrackable, optional, specialize, specialize_ref, word1, word2, ParseResult, allocated, backtrackable, optional, specialize, specialize_ref, word1, word2, ParseResult,
Parser, Parser,
Progress::{self, *}, Progress::{self, *},
State, SyntaxError, TApply, TInParens, TRecord, TTagUnion, Type, State, TApply, TInParens, TRecord, TTagUnion, Type,
}; };
use bumpalo::collections::vec::Vec; use bumpalo::collections::vec::Vec;
use bumpalo::Bump; use bumpalo::Bump;
use roc_region::all::{Located, Region}; use roc_region::all::{Located, Region};
pub fn located<'a>(
min_indent: u16,
) -> impl Parser<'a, Located<TypeAnnotation<'a>>, SyntaxError<'a>> {
specialize(|x, _, _| SyntaxError::Type(x), expression(min_indent))
}
pub fn located_help<'a>(min_indent: u16) -> impl Parser<'a, Located<TypeAnnotation<'a>>, Type<'a>> { pub fn located_help<'a>(min_indent: u16) -> impl Parser<'a, Located<TypeAnnotation<'a>>, Type<'a>> {
expression(min_indent) expression(min_indent)
} }

View file

@ -11,8 +11,6 @@ use roc_collections::all::{ImMap, MutMap, SendSet};
use roc_constrain::expr::constrain_expr; use roc_constrain::expr::constrain_expr;
use roc_constrain::module::{constrain_imported_values, Import}; use roc_constrain::module::{constrain_imported_values, Import};
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds}; use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
use roc_parse::ast;
use roc_parse::parser::{State, SyntaxError};
use roc_problem::can::Problem; use roc_problem::can::Problem;
use roc_region::all::Located; use roc_region::all::Located;
use roc_solve::solve; use roc_solve::solve;
@ -99,27 +97,9 @@ pub struct CanExprOut {
pub constraint: Constraint, pub constraint: Constraint,
} }
#[allow(dead_code)]
pub fn parse_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)
}
#[allow(dead_code)]
pub fn parse_loc_with<'a>(
arena: &'a Bump,
input: &'a str,
) -> Result<Located<ast::Expr<'a>>, SyntaxError<'a>> {
let state = State::new(input.trim().as_bytes());
match roc_parse::expr::test_parse_expr(0, arena, state) {
Ok((loc_expr, _state)) => Ok(loc_expr),
Err(fail) => Err(SyntaxError::Expr(fail)),
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct ParseErrOut<'a> { pub struct ParseErrOut<'a> {
pub fail: SyntaxError<'a>, pub fail: roc_parse::parser::SyntaxError<'a>,
pub home: ModuleId, pub home: ModuleId,
pub interns: Interns, pub interns: Interns,
} }
@ -130,7 +110,7 @@ pub fn can_expr_with<'a>(
home: ModuleId, home: ModuleId,
expr_str: &'a str, expr_str: &'a str,
) -> Result<CanExprOut, ParseErrOut<'a>> { ) -> Result<CanExprOut, ParseErrOut<'a>> {
let loc_expr = match parse_loc_with(&arena, expr_str) { let loc_expr = match roc_parse::test_helpers::parse_loc_with(&arena, expr_str) {
Ok(e) => e, Ok(e) => e,
Err(fail) => { Err(fail) => {
let interns = Interns::default(); let interns = Interns::default();

View file

@ -234,11 +234,9 @@ pub fn str_to_expr2<'a>(
scope: &mut Scope, scope: &mut Scope,
region: Region, region: Region,
) -> Result<(Expr2, self::Output), SyntaxError<'a>> { ) -> Result<(Expr2, self::Output), SyntaxError<'a>> {
let state = State::new(input.trim().as_bytes()); match roc_parse::test_helpers::parse_loc_with(arena, input.trim()) {
Ok(loc_expr) => Ok(to_expr2(env, scope, arena.alloc(loc_expr.value), region)),
match roc_parse::expr::test_parse_expr(0, arena, state) { Err(fail) => Err(fail),
Ok((loc_expr, _state)) => Ok(to_expr2(env, scope, arena.alloc(loc_expr.value), region)),
Err(fail) => Err(SyntaxError::Expr(fail)),
} }
} }