mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
cleanup blankspace function usage
This commit is contained in:
parent
92cff4c32a
commit
e214674016
5 changed files with 51 additions and 66 deletions
|
@ -15,7 +15,7 @@ use roc_region::all::{Located, Region};
|
|||
/// Returns a Located<Expr> where the location is around the Expr, ignoring the spaces.
|
||||
/// If any newlines or comments were found, the Expr will be wrapped in a SpaceBefore and/or
|
||||
/// SpaceAfter as appropriate.
|
||||
pub fn space0_around<'a, P, S>(
|
||||
fn space0_around<'a, P, S>(
|
||||
parser: P,
|
||||
min_indent: u16,
|
||||
) -> impl Parser<'a, Located<S>, SyntaxError<'a>>
|
||||
|
@ -119,7 +119,7 @@ where
|
|||
/// Returns a Located<Expr> where the location is around the Expr, ignoring the spaces.
|
||||
/// If any newlines or comments were found, the Expr will be wrapped in a SpaceBefore and/or
|
||||
/// SpaceAfter as appropriate.
|
||||
pub fn space1_around<'a, P, S>(
|
||||
fn space1_around<'a, P, S>(
|
||||
parser: P,
|
||||
min_indent: u16,
|
||||
) -> impl Parser<'a, Located<S>, SyntaxError<'a>>
|
||||
|
@ -160,7 +160,7 @@ where
|
|||
/// Parses the given expression with 0 or more (spaces/comments/newlines) before it.
|
||||
/// Returns a Located<Expr> where the location is around the Expr, ignoring the spaces.
|
||||
/// The Expr will be wrapped in a SpaceBefore if there were any newlines or comments found.
|
||||
pub fn space0_before<'a, P, S>(
|
||||
fn space0_before<'a, P, S>(
|
||||
parser: P,
|
||||
min_indent: u16,
|
||||
) -> impl Parser<'a, Located<S>, SyntaxError<'a>>
|
||||
|
@ -241,7 +241,7 @@ where
|
|||
/// Parses the given expression with 1 or more (spaces/comments/newlines) before it.
|
||||
/// Returns a Located<Expr> where the location is around the Expr, ignoring the spaces.
|
||||
/// The Expr will be wrapped in a SpaceBefore if there were any newlines or comments found.
|
||||
pub fn space1_before<'a, P, S>(
|
||||
fn space1_before<'a, P, S>(
|
||||
parser: P,
|
||||
min_indent: u16,
|
||||
) -> impl Parser<'a, Located<S>, SyntaxError<'a>>
|
||||
|
@ -268,7 +268,7 @@ where
|
|||
/// Parses the given expression with 0 or more (spaces/comments/newlines) after it.
|
||||
/// Returns a Located<Expr> where the location is around the Expr, ignoring the spaces.
|
||||
/// The Expr will be wrapped in a SpaceAfter if there were any newlines or comments found.
|
||||
pub fn space0_after<'a, P, S>(
|
||||
fn space0_after<'a, P, S>(
|
||||
parser: P,
|
||||
min_indent: u16,
|
||||
) -> impl Parser<'a, Located<S>, SyntaxError<'a>>
|
||||
|
@ -295,7 +295,7 @@ where
|
|||
/// Parses the given expression with 1 or more (spaces/comments/newlines) after it.
|
||||
/// Returns a Located<Expr> where the location is around the Expr, ignoring the spaces.
|
||||
/// The Expr will be wrapped in a SpaceAfter if there were any newlines or comments found.
|
||||
pub fn space1_after<'a, P, S>(
|
||||
fn space1_after<'a, P, S>(
|
||||
parser: P,
|
||||
min_indent: u16,
|
||||
) -> impl Parser<'a, Located<S>, SyntaxError<'a>>
|
||||
|
@ -320,7 +320,7 @@ where
|
|||
}
|
||||
|
||||
/// Zero or more (spaces/comments/newlines).
|
||||
pub fn space0<'a>(min_indent: u16) -> impl Parser<'a, &'a [CommentOrNewline<'a>], SyntaxError<'a>> {
|
||||
fn space0<'a>(min_indent: u16) -> impl Parser<'a, &'a [CommentOrNewline<'a>], SyntaxError<'a>> {
|
||||
spaces(false, min_indent)
|
||||
}
|
||||
|
||||
|
@ -360,7 +360,7 @@ where
|
|||
}
|
||||
|
||||
/// One or more (spaces/comments/newlines).
|
||||
pub fn space1<'a>(min_indent: u16) -> impl Parser<'a, &'a [CommentOrNewline<'a>], SyntaxError<'a>> {
|
||||
fn space1<'a>(min_indent: u16) -> impl Parser<'a, &'a [CommentOrNewline<'a>], SyntaxError<'a>> {
|
||||
// TODO try benchmarking a short-circuit for the typical case: see if there is
|
||||
// exactly one space followed by char that isn't [' ', '\n', or '#'], and
|
||||
// if so, return empty slice. The case where there's exactly 1 space should
|
||||
|
|
|
@ -20,6 +20,24 @@ use roc_region::all::{Located, Region};
|
|||
|
||||
use crate::parser::Progress::{self, *};
|
||||
|
||||
pub fn test_parse_expr<'a>(
|
||||
min_indent: u16,
|
||||
arena: &'a bumpalo::Bump,
|
||||
state: State<'a>,
|
||||
) -> Result<(Located<Expr<'a>>, State<'a>), EExpr<'a>> {
|
||||
let parser = space0_before_e(
|
||||
loc!(|a, s| parse_expr_help(min_indent, a, s)),
|
||||
min_indent,
|
||||
EExpr::Space,
|
||||
EExpr::IndentStart,
|
||||
);
|
||||
|
||||
match parser.parse(arena, state) {
|
||||
Ok((_, expression, state)) => Ok((expression, state)),
|
||||
Err((_, fail, _)) => Err(fail),
|
||||
}
|
||||
}
|
||||
|
||||
// public for testing purposes
|
||||
pub fn expr<'a>(min_indent: u16) -> impl Parser<'a, Expr<'a>, SyntaxError<'a>> {
|
||||
// Recursive parsers must not directly invoke functions which return (impl Parser),
|
||||
|
@ -718,47 +736,6 @@ fn assigned_expr_field_to_pattern_help<'a>(
|
|||
})
|
||||
}
|
||||
|
||||
/// A def beginning with a parenthetical pattern, for example:
|
||||
///
|
||||
/// (UserId userId) = ...
|
||||
///
|
||||
/// Note: Parenthetical patterns are a shorthand convenience, and may not have type annotations.
|
||||
/// It would be too weird to parse; imagine `(UserId userId) : ...` above `(UserId userId) = ...`
|
||||
/// !!!! THIS IS NOT USED !!!!
|
||||
// fn loc_parenthetical_def<'a>(min_indent: u16) -> impl Parser<'a, Located<Expr<'a>>> {
|
||||
// move |arena, state| {
|
||||
// let (loc_tuple, state) = loc!(and!(
|
||||
// space0_after(
|
||||
// between!(
|
||||
// ascii_char(b'('),
|
||||
// space0_around(loc_pattern(min_indent), min_indent),
|
||||
// ascii_char(b')')
|
||||
// ),
|
||||
// min_indent,
|
||||
// ),
|
||||
// equals_with_indent()
|
||||
// ))
|
||||
// .parse(arena, state)?;
|
||||
|
||||
// let region = loc_tuple.region;
|
||||
// let (loc_first_pattern, equals_sign_indent) = loc_tuple.value;
|
||||
|
||||
// // Continue parsing the expression as a Def.
|
||||
// let (spaces_after_equals, state) = space0(min_indent).parse(arena, state)?;
|
||||
// let (value, state) = parse_def_expr(
|
||||
// region.start_col,
|
||||
// min_indent,
|
||||
// equals_sign_indent,
|
||||
// arena,
|
||||
// state,
|
||||
// loc_first_pattern,
|
||||
// spaces_after_equals,
|
||||
// )?;
|
||||
|
||||
// Ok((Located { value, region }, state))
|
||||
// }
|
||||
// }
|
||||
|
||||
fn parse_defs_help<'a>(
|
||||
min_indent: u16,
|
||||
) -> impl Parser<'a, Vec<'a, &'a Located<Def<'a>>>, EExpr<'a>> {
|
||||
|
@ -795,7 +772,7 @@ pub fn def<'a>(min_indent: u16) -> impl Parser<'a, Def<'a>, SyntaxError<'a>> {
|
|||
specialize(|e, _, _| SyntaxError::Expr(e), def_help(min_indent))
|
||||
}
|
||||
|
||||
fn def_help<'a>(min_indent: u16) -> impl Parser<'a, Def<'a>, EExpr<'a>> {
|
||||
pub fn def_help<'a>(min_indent: u16) -> impl Parser<'a, Def<'a>, EExpr<'a>> {
|
||||
let indented_more = min_indent + 1;
|
||||
|
||||
enum DefKind {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::ast::{CommentOrNewline, Def, Module};
|
||||
use crate::blankspace::{space0_around, space0_before_e, space0_e};
|
||||
use crate::expr::def;
|
||||
use crate::blankspace::{space0_before_e, space0_e};
|
||||
use crate::header::{
|
||||
package_entry, package_name, package_or_path, AppHeader, Effects, ExposesEntry, ImportsEntry,
|
||||
InterfaceHeader, ModuleName, PackageEntry, PlatformHeader, To, TypedIdent,
|
||||
|
@ -252,8 +251,22 @@ fn platform_header<'a>() -> impl Parser<'a, PlatformHeader<'a>, EHeader<'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
|
||||
skip_second!(zero_or_more!(space0_around(loc!(def(0)), 0)), end_of_file())
|
||||
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,
|
||||
))
|
||||
),
|
||||
end_of_file()
|
||||
)
|
||||
}
|
||||
#[derive(Debug)]
|
||||
struct ProvidesTo<'a> {
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use crate::ast;
|
||||
use crate::blankspace::space0_before;
|
||||
use crate::expr::expr;
|
||||
use crate::module::module_defs;
|
||||
use crate::parser::{loc, Parser, State, SyntaxError};
|
||||
|
@ -32,10 +31,9 @@ pub fn parse_loc_with<'a>(
|
|||
input: &'a str,
|
||||
) -> Result<Located<ast::Expr<'a>>, SyntaxError<'a>> {
|
||||
let state = State::new_in(arena, input.trim().as_bytes());
|
||||
let parser = space0_before(loc(expr(0)), 0);
|
||||
let answer = parser.parse(&arena, state);
|
||||
|
||||
answer
|
||||
.map(|(_, loc_expr, _)| loc_expr)
|
||||
.map_err(|(_, fail, _)| fail)
|
||||
match crate::expr::test_parse_expr(0, arena, state) {
|
||||
Ok((loc_expr, _state)) => Ok(loc_expr),
|
||||
Err(fail) => Err(SyntaxError::Expr(fail)),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue