Move backtracking for implements to specific spaces case that it's actually needed for, to avoid excess parsing work in extreme cases

This commit is contained in:
Joshua Warner 2025-01-03 19:29:19 -08:00
parent 6edfc0aa90
commit 090473434d
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
4 changed files with 6 additions and 24 deletions

View file

@ -19,8 +19,8 @@ use roc_error_macros::internal_error;
use roc_parse::ast::{
AbilityMember, Defs, Expr, ExtractSpaces, ImportAlias, ImportAsKeyword, ImportExposingKeyword,
ImportedModuleName, IngestedFileAnnotation, IngestedFileImport, ModuleImport,
ModuleImportParams, Pattern, PatternApplyStyle, Spaceable, Spaces, SpacesAfter, SpacesBefore,
StrLiteral, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
ModuleImportParams, Pattern, PatternApplyStyle, Spaces, SpacesBefore, StrLiteral,
TypeAnnotation, TypeDef, TypeHeader, ValueDef,
};
use roc_parse::expr::merge_spaces;
use roc_parse::header::Keyword;
@ -94,22 +94,6 @@ pub fn def_lift_spaces<'a, 'b: 'a>(
}
}
fn lift_spaces_after<'a, 'b: 'a, T: 'b + ExtractSpaces<'a> + Spaceable<'a> + std::fmt::Debug>(
arena: &'a Bump,
item: T,
) -> SpacesAfter<'a, <T as ExtractSpaces<'a>>::Item>
where
<T as ExtractSpaces<'a>>::Item: Spaceable<'a>,
{
dbg!(item);
let spaces = item.extract_spaces();
SpacesAfter {
item: spaces.item.maybe_before(arena, spaces.before),
after: spaces.after,
}
}
pub fn tydef_lift_spaces<'a, 'b: 'a>(arena: &'a Bump, def: TypeDef<'b>) -> Spaces<'a, TypeDef<'a>> {
match def {
TypeDef::Alias { header, ann } => {
@ -477,7 +461,7 @@ impl<'a> Formattable for TypeDef<'a> {
if let Some(has_abilities) = has_abilities {
buf.spaces(1);
has_abilities.format_with_options(
(*has_abilities).format_with_options(
buf,
Parens::NotNeeded,
Newlines::from_bool(make_multiline),

View file

@ -186,7 +186,7 @@ where
E: 'a + SpaceProblem,
{
parser::map(
and(space0_e(indent_problem), parser),
and(backtrackable(space0_e(indent_problem)), parser),
|(space_list, item): (&'a [CommentOrNewline<'a>], S)| SpacesBefore {
before: space_list,
item,

View file

@ -1163,10 +1163,7 @@ fn opaque_signature<'a>(
and(
specialize_err(EExpr::Type, type_annotation::located_opaque_signature(true)),
optional(map_with_arena(
backtrackable(specialize_err(
EExpr::Type,
type_annotation::implements_abilities(),
)),
specialize_err(EExpr::Type, type_annotation::implements_abilities()),
|arena, item| &*arena.alloc(item),
)),
)

View file

@ -28,5 +28,6 @@ fn main() {
let input = kind.with_text(&text);
let arena = Bump::new();
let output = input.parse_in(&arena);
eprintln!("memory used: {}", arena.allocated_bytes());
println!("{:#?}", output);
}