mirror of
https://github.com/roc-lang/roc.git
synced 2025-07-23 14:35:12 +00:00
Remove deprecated string interpolation syntax
This commit is contained in:
parent
72011fba73
commit
cb98c45e88
15 changed files with 17 additions and 94 deletions
|
@ -1045,20 +1045,6 @@ fn desugar_str_segments<'a>(
|
||||||
StrSegment::Plaintext(_) | StrSegment::Unicode(_) | StrSegment::EscapedChar(_) => {
|
StrSegment::Plaintext(_) | StrSegment::Unicode(_) | StrSegment::EscapedChar(_) => {
|
||||||
*segment
|
*segment
|
||||||
}
|
}
|
||||||
StrSegment::DeprecatedInterpolated(loc_expr) => {
|
|
||||||
let loc_desugared = desugar_expr(
|
|
||||||
env,
|
|
||||||
scope,
|
|
||||||
env.arena.alloc(Loc {
|
|
||||||
region: loc_expr.region,
|
|
||||||
value: *loc_expr.value,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
StrSegment::DeprecatedInterpolated(Loc {
|
|
||||||
region: loc_desugared.region,
|
|
||||||
value: env.arena.alloc(loc_desugared.value),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
StrSegment::Interpolated(loc_expr) => {
|
StrSegment::Interpolated(loc_expr) => {
|
||||||
let loc_desugared = desugar_expr(
|
let loc_desugared = desugar_expr(
|
||||||
env,
|
env,
|
||||||
|
|
|
@ -2515,9 +2515,7 @@ pub fn is_valid_interpolation(expr: &ast::Expr<'_>) -> bool {
|
||||||
| ast::StrSegment::Plaintext(_) => true,
|
| ast::StrSegment::Plaintext(_) => true,
|
||||||
// Disallow nested interpolation. Alternatively, we could allow it but require
|
// Disallow nested interpolation. Alternatively, we could allow it but require
|
||||||
// a comment above it apologizing to the next person who has to read the code.
|
// a comment above it apologizing to the next person who has to read the code.
|
||||||
ast::StrSegment::Interpolated(_) | ast::StrSegment::DeprecatedInterpolated(_) => {
|
ast::StrSegment::Interpolated(_) => false,
|
||||||
false
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
ast::Expr::Record(fields) => fields.iter().all(|loc_field| match loc_field.value {
|
ast::Expr::Record(fields) => fields.iter().all(|loc_field| match loc_field.value {
|
||||||
|
@ -2648,7 +2646,7 @@ fn flatten_str_lines<'a>(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Interpolated(loc_expr) | DeprecatedInterpolated(loc_expr) => {
|
Interpolated(loc_expr) => {
|
||||||
if is_valid_interpolation(loc_expr.value) {
|
if is_valid_interpolation(loc_expr.value) {
|
||||||
// Interpolations desugar to Str.concat calls
|
// Interpolations desugar to Str.concat calls
|
||||||
output.references.insert_call(Symbol::STR_CONCAT);
|
output.references.insert_call(Symbol::STR_CONCAT);
|
||||||
|
|
|
@ -1109,7 +1109,7 @@ fn flatten_str_lines(lines: &[&[StrSegment<'_>]]) -> Pattern {
|
||||||
Unicode(loc_digits) => {
|
Unicode(loc_digits) => {
|
||||||
todo!("parse unicode digits {:?}", loc_digits);
|
todo!("parse unicode digits {:?}", loc_digits);
|
||||||
}
|
}
|
||||||
Interpolated(loc_expr) | DeprecatedInterpolated(loc_expr) => {
|
Interpolated(loc_expr) => {
|
||||||
return Pattern::UnsupportedPattern(loc_expr.region);
|
return Pattern::UnsupportedPattern(loc_expr.region);
|
||||||
}
|
}
|
||||||
EscapedChar(escaped) => buf.push(escaped.unescape()),
|
EscapedChar(escaped) => buf.push(escaped.unescape()),
|
||||||
|
|
|
@ -662,7 +662,7 @@ fn format_str_segment(seg: &StrSegment, buf: &mut Buf, indent: u16) {
|
||||||
buf.push('\\');
|
buf.push('\\');
|
||||||
buf.push(escaped.to_parsed_char());
|
buf.push(escaped.to_parsed_char());
|
||||||
}
|
}
|
||||||
DeprecatedInterpolated(loc_expr) | Interpolated(loc_expr) => {
|
Interpolated(loc_expr) => {
|
||||||
buf.push_str("$(");
|
buf.push_str("$(");
|
||||||
// e.g. (name) in "Hi, $(name)!"
|
// e.g. (name) in "Hi, $(name)!"
|
||||||
loc_expr.value.format_with_options(
|
loc_expr.value.format_with_options(
|
||||||
|
|
|
@ -5657,7 +5657,7 @@ mod test_reporting {
|
||||||
test_report!(
|
test_report!(
|
||||||
weird_escape,
|
weird_escape,
|
||||||
r#""abc\qdef""#,
|
r#""abc\qdef""#,
|
||||||
@r###"
|
@r#"
|
||||||
── WEIRD ESCAPE in tmp/weird_escape/Test.roc ───────────────────────────────────
|
── WEIRD ESCAPE in tmp/weird_escape/Test.roc ───────────────────────────────────
|
||||||
|
|
||||||
I was partway through parsing a string literal, but I got stuck here:
|
I was partway through parsing a string literal, but I got stuck here:
|
||||||
|
@ -5674,8 +5674,7 @@ mod test_reporting {
|
||||||
- An escaped quote: \"
|
- An escaped quote: \"
|
||||||
- An escaped backslash: \\
|
- An escaped backslash: \\
|
||||||
- A unicode code point: \u(00FF)
|
- A unicode code point: \u(00FF)
|
||||||
- An interpolated string: $(myVariable)
|
"#
|
||||||
"###
|
|
||||||
);
|
);
|
||||||
|
|
||||||
test_report!(
|
test_report!(
|
||||||
|
|
|
@ -281,11 +281,10 @@ pub struct WhenPattern<'a> {
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub enum StrSegment<'a> {
|
pub enum StrSegment<'a> {
|
||||||
Plaintext(&'a str), // e.g. "foo"
|
Plaintext(&'a str), // e.g. "foo"
|
||||||
Unicode(Loc<&'a str>), // e.g. "00A0" in "\u(00A0)"
|
Unicode(Loc<&'a str>), // e.g. "00A0" in "\u(00A0)"
|
||||||
EscapedChar(EscapedChar), // e.g. '\n' in "Hello!\n"
|
EscapedChar(EscapedChar), // e.g. '\n' in "Hello!\n"
|
||||||
Interpolated(Loc<&'a Expr<'a>>),
|
Interpolated(Loc<&'a Expr<'a>>), // e.g. "$(expr)"
|
||||||
DeprecatedInterpolated(Loc<&'a Expr<'a>>), // The old "$(...)" syntax - will be removed someday
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
@ -379,7 +378,6 @@ impl<'a> TryFrom<StrSegment<'a>> for SingleQuoteSegment<'a> {
|
||||||
StrSegment::Unicode(s) => Ok(SingleQuoteSegment::Unicode(s)),
|
StrSegment::Unicode(s) => Ok(SingleQuoteSegment::Unicode(s)),
|
||||||
StrSegment::EscapedChar(s) => Ok(SingleQuoteSegment::EscapedChar(s)),
|
StrSegment::EscapedChar(s) => Ok(SingleQuoteSegment::EscapedChar(s)),
|
||||||
StrSegment::Interpolated(_) => Err(ESingleQuote::InterpolationNotAllowed),
|
StrSegment::Interpolated(_) => Err(ESingleQuote::InterpolationNotAllowed),
|
||||||
StrSegment::DeprecatedInterpolated(_) => Err(ESingleQuote::InterpolationNotAllowed),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2516,9 +2514,7 @@ impl<'a> Malformed for StrSegment<'a> {
|
||||||
fn is_malformed(&self) -> bool {
|
fn is_malformed(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
StrSegment::Plaintext(_) | StrSegment::Unicode(_) | StrSegment::EscapedChar(_) => false,
|
StrSegment::Plaintext(_) | StrSegment::Unicode(_) | StrSegment::EscapedChar(_) => false,
|
||||||
StrSegment::Interpolated(expr) | StrSegment::DeprecatedInterpolated(expr) => {
|
StrSegment::Interpolated(expr) => expr.is_malformed(),
|
||||||
expr.is_malformed()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -649,13 +649,6 @@ fn normalize_str_segments<'a>(
|
||||||
}
|
}
|
||||||
new_segments.push(StrSegment::Interpolated(e.normalize(arena)));
|
new_segments.push(StrSegment::Interpolated(e.normalize(arena)));
|
||||||
}
|
}
|
||||||
StrSegment::DeprecatedInterpolated(e) => {
|
|
||||||
if !last_text.is_empty() {
|
|
||||||
let text = std::mem::replace(last_text, String::new_in(arena));
|
|
||||||
new_segments.push(StrSegment::Plaintext(text.into_bump_str()));
|
|
||||||
}
|
|
||||||
new_segments.push(StrSegment::Interpolated(e.normalize(arena)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -680,7 +673,6 @@ impl<'a> Normalize<'a> for StrSegment<'a> {
|
||||||
StrSegment::Unicode(t) => StrSegment::Unicode(t.normalize(arena)),
|
StrSegment::Unicode(t) => StrSegment::Unicode(t.normalize(arena)),
|
||||||
StrSegment::EscapedChar(c) => StrSegment::EscapedChar(c),
|
StrSegment::EscapedChar(c) => StrSegment::EscapedChar(c),
|
||||||
StrSegment::Interpolated(t) => StrSegment::Interpolated(t.normalize(arena)),
|
StrSegment::Interpolated(t) => StrSegment::Interpolated(t.normalize(arena)),
|
||||||
StrSegment::DeprecatedInterpolated(t) => StrSegment::Interpolated(t.normalize(arena)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -364,36 +364,6 @@ pub fn parse_str_like_literal<'a>() -> impl Parser<'a, StrLikeLiteral<'a>, EStri
|
||||||
// This is the start of a new escape. Look at the next byte
|
// This is the start of a new escape. Look at the next byte
|
||||||
// to figure out what type of escape it is.
|
// to figure out what type of escape it is.
|
||||||
match bytes.next() {
|
match bytes.next() {
|
||||||
Some(b'(') => {
|
|
||||||
// Advance past the `\(` before using the expr parser
|
|
||||||
state.advance_mut(2);
|
|
||||||
|
|
||||||
let original_byte_count = state.bytes().len();
|
|
||||||
|
|
||||||
// This is an interpolated variable.
|
|
||||||
// Parse an arbitrary expression, then give a
|
|
||||||
// canonicalization error if that expression variant
|
|
||||||
// is not allowed inside a string interpolation.
|
|
||||||
let (_progress, loc_expr, new_state) = skip_second(
|
|
||||||
specialize_err_ref(
|
|
||||||
EString::Format,
|
|
||||||
loc(allocated(reset_min_indent(expr::expr_help()))),
|
|
||||||
),
|
|
||||||
byte(b')', EString::FormatEnd),
|
|
||||||
)
|
|
||||||
.parse(arena, state, min_indent)?;
|
|
||||||
|
|
||||||
// Advance the iterator past the expr we just parsed.
|
|
||||||
for _ in 0..(original_byte_count - new_state.bytes().len()) {
|
|
||||||
bytes.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
segments.push(StrSegment::DeprecatedInterpolated(loc_expr));
|
|
||||||
|
|
||||||
// Reset the segment
|
|
||||||
segment_parsed_bytes = 0;
|
|
||||||
state = new_state;
|
|
||||||
}
|
|
||||||
Some(b'u') => {
|
Some(b'u') => {
|
||||||
// Advance past the `\u` before using the expr parser
|
// Advance past the `\u` before using the expr parser
|
||||||
state.advance_mut(2);
|
state.advance_mut(2);
|
||||||
|
@ -444,8 +414,8 @@ pub fn parse_str_like_literal<'a>() -> impl Parser<'a, StrLikeLiteral<'a>, EStri
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Invalid escape! A backslash must be followed
|
// Invalid escape! A backslash must be followed
|
||||||
// by either an open paren or else one of the
|
// by one of these escapable characters:
|
||||||
// escapable characters (\n, \t, \", \\, etc)
|
// (\n, \t, \", \\, etc)
|
||||||
return Err((MadeProgress, EString::UnknownEscape(state.pos())));
|
return Err((MadeProgress, EString::UnknownEscape(state.pos())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Expr(Str(UnknownEscape(@1), @0), @0)
|
|
@ -0,0 +1 @@
|
||||||
|
"\(e)"
|
|
@ -1 +0,0 @@
|
||||||
"$(e)"
|
|
|
@ -1,17 +0,0 @@
|
||||||
SpaceAfter(
|
|
||||||
Str(
|
|
||||||
Line(
|
|
||||||
[
|
|
||||||
DeprecatedInterpolated(
|
|
||||||
@3-4 Var {
|
|
||||||
module_name: "",
|
|
||||||
ident: "e",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
[
|
|
||||||
Newline,
|
|
||||||
],
|
|
||||||
)
|
|
|
@ -1 +0,0 @@
|
||||||
"\(e)"
|
|
|
@ -192,6 +192,7 @@ mod test_snapshots {
|
||||||
fail/comment_with_tab.expr,
|
fail/comment_with_tab.expr,
|
||||||
fail/def_missing_final_expression.expr,
|
fail/def_missing_final_expression.expr,
|
||||||
fail/def_without_newline.expr,
|
fail/def_without_newline.expr,
|
||||||
|
fail/deprecated_interpolated_string.expr,
|
||||||
fail/double_plus.expr,
|
fail/double_plus.expr,
|
||||||
fail/elm_function_syntax.expr,
|
fail/elm_function_syntax.expr,
|
||||||
fail/empty_or_pattern.expr,
|
fail/empty_or_pattern.expr,
|
||||||
|
@ -303,11 +304,10 @@ mod test_snapshots {
|
||||||
pass/comment_with_non_ascii.expr,
|
pass/comment_with_non_ascii.expr,
|
||||||
pass/control_characters_in_scalar.expr,
|
pass/control_characters_in_scalar.expr,
|
||||||
pass/crash.expr,
|
pass/crash.expr,
|
||||||
|
pass/dbg.expr,
|
||||||
pass/dbg_stmt.expr,
|
pass/dbg_stmt.expr,
|
||||||
pass/dbg_stmt_multiline.expr,
|
pass/dbg_stmt_multiline.expr,
|
||||||
pass/dbg.expr,
|
|
||||||
pass/defs_suffixed_middle_extra_indents.moduledefs,
|
pass/defs_suffixed_middle_extra_indents.moduledefs,
|
||||||
pass/deprecated_interpolated_string.expr,
|
|
||||||
pass/destructure_tag_assignment.expr,
|
pass/destructure_tag_assignment.expr,
|
||||||
pass/docs.expr,
|
pass/docs.expr,
|
||||||
pass/empty_app_header.header,
|
pass/empty_app_header.header,
|
||||||
|
|
|
@ -995,7 +995,6 @@ fn to_str_report<'a>(
|
||||||
suggestion("An escaped quote: ", "\\\""),
|
suggestion("An escaped quote: ", "\\\""),
|
||||||
suggestion("An escaped backslash: ", "\\\\"),
|
suggestion("An escaped backslash: ", "\\\\"),
|
||||||
suggestion("A unicode code point: ", "\\u(00FF)"),
|
suggestion("A unicode code point: ", "\\u(00FF)"),
|
||||||
suggestion("An interpolated string: ", "$(myVariable)"),
|
|
||||||
])
|
])
|
||||||
.indent(4),
|
.indent(4),
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue