Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ! suffix to parser #6586

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/compiler/can/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ pub fn canonicalize_expr<'a>(
bad_expr
);
}
ast::Expr::Suffixed(_) => todo!(),
};

// At the end, diff used_idents and defined_idents to see which were unused.
Expand Down Expand Up @@ -2506,6 +2507,7 @@ pub fn is_valid_interpolation(expr: &ast::Expr<'_>) -> bool {
ast::RecordBuilderField::SpaceBefore(_, _)
| ast::RecordBuilderField::SpaceAfter(_, _) => false,
}),
ast::Expr::Suffixed(_) => todo!(),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/compiler/can/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ pub fn desugar_expr<'a>(
})
}
LowLevelDbg(_, _, _) => unreachable!("Only exists after desugaring"),
Suffixed(_) => todo!(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be completed in follow up PRs. One for each flavour of sugar.

}
}

Expand Down
5 changes: 5 additions & 0 deletions crates/compiler/fmt/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl<'a> Formattable for Expr<'a> {
Tuple(fields) => is_collection_multiline(fields),
RecordUpdate { fields, .. } => is_collection_multiline(fields),
RecordBuilder(fields) => is_collection_multiline(fields),
Suffixed(subexpr) => subexpr.is_multiline(),
}
}

Expand Down Expand Up @@ -512,6 +513,10 @@ impl<'a> Formattable for Expr<'a> {
MultipleRecordBuilders { .. } => {}
UnappliedRecordBuilder { .. } => {}
IngestedFile(_, _) => {}
Suffixed(sub_expr) => {
sub_expr.format_with_options(buf, parens, newlines, indent);
buf.push('!');
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/fmt/src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ impl<'a> RemoveSpaces<'a> for Expr<'a> {
Expr::SpaceBefore(a, _) => a.remove_spaces(arena),
Expr::SpaceAfter(a, _) => a.remove_spaces(arena),
Expr::SingleQuote(a) => Expr::Num(a),
Expr::Suffixed(a) => a.remove_spaces(arena),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/compiler/parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ pub enum Expr<'a> {
// Collection Literals
List(Collection<'a, &'a Loc<Expr<'a>>>),

/// An expression followed by `!``
Suffixed(&'a Expr<'a>),

RecordUpdate {
update: &'a Loc<Expr<'a>>,
fields: Collection<'a, Loc<AssignedField<'a, Expr<'a>>>>,
Expand Down Expand Up @@ -1568,6 +1571,7 @@ impl<'a> Malformed for Expr<'a> {
PrecedenceConflict(_) |
MultipleRecordBuilders(_) |
UnappliedRecordBuilder(_) => true,
Suffixed(expr) => expr.is_malformed(),
}
}
}
Expand Down
40 changes: 33 additions & 7 deletions crates/compiler/parse/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,20 @@ fn expr_start<'a>(options: ExprParseOptions) -> impl Parser<'a, Loc<Expr<'a>>, E

fn expr_operator_chain<'a>(options: ExprParseOptions) -> impl Parser<'a, Expr<'a>, EExpr<'a>> {
line_min_indent(move |arena, state: State<'a>, min_indent: u32| {
let (_, expr, state) =
loc_possibly_negative_or_negated_term(options).parse(arena, state, min_indent)?;
let (_, expr, state) = loc_possibly_negative_or_negated_term(options)
.parse(arena, state, min_indent)
.map(|(progress, expr, state)| {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this wouldn't be needed as the map in parse_expr_end would wrap the expressions in Suffix, but the loc_possibly_negative_or_negated_term parser doesn't seem to catch everything.

// If the next thing after the expression is a `!`, then it's Suffixed
if state.bytes().starts_with(b"!") {
(
progress,
Loc::at(expr.region, Expr::Suffixed(arena.alloc(expr.value))),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really unsure about this. My concern is the that Loc in expr here points to just the ident and doesn't include the ! in it. I'm not sure if that is an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, we'll see this in error messages when we get to testing those - the red underline either will or won't include the ! depending on this. That might actually be what we want in some cases, but maybe not others!

state.advance(1),
)
} else {
(progress, expr, state)
}
})?;

let initial_state = state.clone();
let end = state.pos();
Expand Down Expand Up @@ -1640,7 +1652,7 @@ fn parse_expr_end<'a>(
Err((MadeProgress, f)) => Err((MadeProgress, f)),
Ok((
_,
has @ Loc {
implements @ Loc {
value:
Expr::Var {
module_name: "",
Expand Down Expand Up @@ -1672,17 +1684,17 @@ fn parse_expr_end<'a>(
}

// Attach any spaces to the `implements` keyword
let has = if !expr_state.spaces_after.is_empty() {
let implements = if !expr_state.spaces_after.is_empty() {
arena
.alloc(Implements::Implements)
.with_spaces_before(expr_state.spaces_after, has.region)
.with_spaces_before(expr_state.spaces_after, implements.region)
} else {
Loc::at(has.region, Implements::Implements)
Loc::at(implements.region, Implements::Implements)
};

let args = arguments.into_bump_slice();
let (_, (type_def, def_region), state) =
finish_parsing_ability_def_help(min_indent, name, args, has, arena, state)?;
finish_parsing_ability_def_help(min_indent, name, args, implements, arena, state)?;

let mut defs = Defs::default();

Expand Down Expand Up @@ -1817,6 +1829,18 @@ fn parse_expr_end<'a>(
}
}
}
.map(|(progress, expr, state)| {
// If the next thing after the expression is a `!`, then it's Suffixed
if state.bytes().starts_with(b"!") {
(
progress,
Expr::Suffixed(arena.alloc(expr)),
state.advance(1),
)
} else {
(progress, expr, state)
}
})
}

pub fn loc_expr<'a>(accept_multi_backpassing: bool) -> impl Parser<'a, Loc<Expr<'a>>, EExpr<'a>> {
Expand Down Expand Up @@ -1945,6 +1969,7 @@ fn expr_to_pattern_help<'a>(arena: &'a Bump, expr: &Expr<'a>) -> Result<Pattern<
Expr::Str(string) => Pattern::StrLiteral(string),
Expr::SingleQuote(string) => Pattern::SingleQuote(string),
Expr::MalformedIdent(string, problem) => Pattern::MalformedIdent(string, problem),
Expr::Suffixed(_) => todo!(),
};

// Now we re-add the spaces
Expand Down Expand Up @@ -2997,6 +3022,7 @@ where
Err((NoProgress, to_error("->", state.pos())))
}
"<-" => good!(BinOp::Backpassing, 2),
"!" => Err((NoProgress, to_error("!", state.pos()))),
_ => bad_made_progress!(chomped),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Suffixed(
Var {
module_name: "Stdout",
ident: "line",
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stdout.line!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo! (bar! baz) (blah stuff)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Apply(
@0-3 Suffixed(
Var {
module_name: "",
ident: "foo",
},
),
[
@9-17 ParensAround(
Apply(
@9-12 Suffixed(
Var {
module_name: "",
ident: "bar",
},
),
[
@14-17 Var {
module_name: "",
ident: "baz",
},
],
Space,
),
),
@22-32 ParensAround(
Apply(
@22-26 Var {
module_name: "",
ident: "blah",
},
[
@27-32 Var {
module_name: "",
ident: "stuff",
},
],
Space,
),
),
],
Space,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo! ( bar! baz) ( blah stuff)
2 changes: 2 additions & 0 deletions crates/compiler/test_syntax/tests/test_snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ mod test_snapshots {
pass/string_without_escape.expr,
pass/sub_var_with_spaces.expr,
pass/sub_with_spaces.expr,
pass/suffixed.expr,
pass/suffixed_nested.expr,
pass/tag_pattern.expr,
pass/ten_times_eleven.expr,
pass/three_arg_closure.expr,
Expand Down
1 change: 1 addition & 0 deletions crates/lang_srv/src/analysis/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ impl IterTokens for Loc<Expr<'_>> {
Expr::MalformedIdent(_, _) | Expr::MalformedClosure | Expr::PrecedenceConflict(_) => {
bumpvec![in arena;]
}
Expr::Suffixed(_) => todo!(),
}
}
}
Expand Down