mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
use new def parser for module defs
This commit is contained in:
parent
cf905a13fa
commit
38d925fea6
4 changed files with 51 additions and 24 deletions
|
@ -829,6 +829,7 @@ fn append_alias_definition<'a>(
|
|||
defs.push(arena.alloc(loc_def));
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct DefState<'a> {
|
||||
defs: Vec<'a, &'a Located<Def<'a>>>,
|
||||
spaces_after: &'a [CommentOrNewline<'a>],
|
||||
|
@ -1576,7 +1577,52 @@ pub fn def<'a>(min_indent: u16) -> impl Parser<'a, Def<'a>, SyntaxError<'a>> {
|
|||
specialize(|e, _, _| SyntaxError::Expr(e), def_help(min_indent))
|
||||
}
|
||||
|
||||
pub fn def_help<'a>(min_indent: u16) -> impl Parser<'a, Def<'a>, EExpr<'a>> {
|
||||
pub fn def_help_help<'a>(min_indent: u16) -> impl Parser<'a, Vec<'a, Located<Def<'a>>>, EExpr<'a>> {
|
||||
move |arena, state: State<'a>| {
|
||||
let def_state = DefState {
|
||||
defs: Vec::new_in(arena),
|
||||
spaces_after: &[],
|
||||
};
|
||||
|
||||
let (_, initial_space, state) =
|
||||
space0_e(min_indent, EExpr::Space, EExpr::IndentEnd).parse(arena, state)?;
|
||||
|
||||
let start = state.get_position();
|
||||
let (_, def_state, state) = parse_defs_end(start, def_state, arena, state)?;
|
||||
|
||||
let (_, final_space, state) =
|
||||
space0_e(start.col, EExpr::Space, EExpr::IndentEnd).parse(arena, state)?;
|
||||
|
||||
let mut output = Vec::with_capacity_in(def_state.defs.len(), arena);
|
||||
|
||||
if !def_state.defs.is_empty() {
|
||||
let first = 0;
|
||||
let last = def_state.defs.len() - 1;
|
||||
|
||||
for (i, ref_def) in def_state.defs.into_iter().enumerate() {
|
||||
let mut def = ref_def.clone();
|
||||
|
||||
if i == first {
|
||||
def = arena
|
||||
.alloc(def.value)
|
||||
.with_spaces_before(initial_space, def.region)
|
||||
}
|
||||
|
||||
if i == last {
|
||||
def = arena
|
||||
.alloc(def.value)
|
||||
.with_spaces_after(final_space, def.region)
|
||||
}
|
||||
|
||||
output.push(def);
|
||||
}
|
||||
}
|
||||
|
||||
Ok((MadeProgress, output, state))
|
||||
}
|
||||
}
|
||||
|
||||
fn def_help<'a>(min_indent: u16) -> impl Parser<'a, Def<'a>, EExpr<'a>> {
|
||||
let indented_more = min_indent + 1;
|
||||
|
||||
enum DefKind {
|
||||
|
|
|
@ -265,19 +265,12 @@ fn end_of_file<'a>() -> impl Parser<'a, (), SyntaxError<'a>> {
|
|||
|
||||
#[inline(always)]
|
||||
pub fn module_defs<'a>() -> impl Parser<'a, Vec<'a, Located<Def<'a>>>, SyntaxError<'a>> {
|
||||
use crate::parser::EExpr;
|
||||
// force that we pare until the end of the input
|
||||
let min_indent = 0;
|
||||
skip_second!(
|
||||
specialize(
|
||||
|e, _, _| SyntaxError::Expr(e),
|
||||
zero_or_more!(crate::blankspace::space0_around_ee(
|
||||
loc!(crate::expr::def_help(min_indent)),
|
||||
min_indent,
|
||||
EExpr::Space,
|
||||
EExpr::IndentStart,
|
||||
EExpr::IndentEnd,
|
||||
))
|
||||
crate::expr::def_help_help(min_indent),
|
||||
),
|
||||
end_of_file()
|
||||
)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::ast;
|
||||
use crate::module::module_defs;
|
||||
use crate::parser::{Parser, State, SyntaxError};
|
||||
use bumpalo::collections::Vec;
|
||||
// use crate::module::module_defs;
|
||||
use crate::parser::{State, SyntaxError};
|
||||
use bumpalo::Bump;
|
||||
use roc_region::all::Located;
|
||||
|
||||
|
@ -12,18 +11,6 @@ pub fn parse_expr_with<'a>(
|
|||
parse_loc_with(arena, input).map(|loc_expr| loc_expr.value)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn parse_defs_with<'a>(
|
||||
arena: &'a Bump,
|
||||
input: &'a str,
|
||||
) -> Result<Vec<'a, Located<ast::Def<'a>>>, SyntaxError<'a>> {
|
||||
let state = State::new(input.trim().as_bytes());
|
||||
let answer = module_defs().parse(arena, state);
|
||||
answer
|
||||
.map(|(_, loc_expr, _)| loc_expr)
|
||||
.map_err(|(_, fail, _)| fail)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn parse_loc_with<'a>(
|
||||
arena: &'a Bump,
|
||||
|
|
|
@ -2878,6 +2878,7 @@ mod test_parse {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn standalone_module_defs() {
|
||||
use Def::*;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue