diff --git a/crates/moxy-token/src/lit/byte_str.rs b/crates/moxy-token/src/lit/byte_str.rs index eeb2f845..f3ad6caa 100644 --- a/crates/moxy-token/src/lit/byte_str.rs +++ b/crates/moxy-token/src/lit/byte_str.rs @@ -162,6 +162,9 @@ fn escape(c: Cursor<'_>) -> Result, LexError> { match c.first() { None => c.error().into(), Some('n' | 'r' | 't' | '\\' | '\'' | '"' | '0') => Ok(c.advance()), + // A line continuation: the newline and the whitespace after it are skipped + Some('\n') => Ok(skip_continuation(c.advance())), + Some('\r') if c.advance().first() == Some('\n') => Ok(skip_continuation(c.advance_by(2))), Some('x') => { let c = c.advance(); let c = hex_digit(c)?; @@ -192,6 +195,14 @@ fn escape(c: Cursor<'_>) -> Result, LexError> { } } +fn skip_continuation(mut c: Cursor<'_>) -> Cursor<'_> { + while let Some(' ' | '\t' | '\n' | '\r') = c.first() { + c = c.advance(); + } + + c +} + fn hex_digit(c: Cursor<'_>) -> Result, LexError> { match c.first() { Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()), @@ -214,10 +225,19 @@ fn decode_string_body(repr: &str) -> Option { let mut chars = inner.chars(); while let Some(c) = chars.next() { - if c == '\\' { - out.push(decode_escape(&mut chars)?); - } else { + if c != '\\' { out.push(c); + } else if chars.clone().next().is_some_and(|next| next == '\n' || next == '\r') { + // A line continuation adds nothing, and the whitespace after it is skipped + while chars + .clone() + .next() + .is_some_and(|next| matches!(next, ' ' | '\t' | '\n' | '\r')) + { + chars.next(); + } + } else { + out.push(decode_escape(&mut chars)?); } } diff --git a/crates/moxy-token/src/lit/c_str.rs b/crates/moxy-token/src/lit/c_str.rs index 299fe6eb..a8a074fb 100644 --- a/crates/moxy-token/src/lit/c_str.rs +++ b/crates/moxy-token/src/lit/c_str.rs @@ -162,6 +162,9 @@ fn escape(c: Cursor<'_>) -> Result, LexError> { match c.first() { None => c.error().into(), Some('n' | 'r' | 't' | '\\' | '\'' | '"' | '0') => Ok(c.advance()), + // A line continuation: the newline and the whitespace after it are skipped + Some('\n') => Ok(skip_continuation(c.advance())), + Some('\r') if c.advance().first() == Some('\n') => Ok(skip_continuation(c.advance_by(2))), Some('x') => { let c = c.advance(); let c = hex_digit(c)?; @@ -192,6 +195,14 @@ fn escape(c: Cursor<'_>) -> Result, LexError> { } } +fn skip_continuation(mut c: Cursor<'_>) -> Cursor<'_> { + while let Some(' ' | '\t' | '\n' | '\r') = c.first() { + c = c.advance(); + } + + c +} + fn hex_digit(c: Cursor<'_>) -> Result, LexError> { match c.first() { Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()), @@ -214,10 +225,19 @@ fn decode_string_body(repr: &str) -> Option { let mut chars = inner.chars(); while let Some(c) = chars.next() { - if c == '\\' { - out.push(decode_escape(&mut chars)?); - } else { + if c != '\\' { out.push(c); + } else if chars.clone().next().is_some_and(|next| next == '\n' || next == '\r') { + // A line continuation adds nothing, and the whitespace after it is skipped + while chars + .clone() + .next() + .is_some_and(|next| matches!(next, ' ' | '\t' | '\n' | '\r')) + { + chars.next(); + } + } else { + out.push(decode_escape(&mut chars)?); } } diff --git a/crates/moxy-token/src/lit/int.rs b/crates/moxy-token/src/lit/int.rs index 21be24bb..41fee770 100644 --- a/crates/moxy-token/src/lit/int.rs +++ b/crates/moxy-token/src/lit/int.rs @@ -127,7 +127,7 @@ impl From for String { impl Lit { pub fn u8_suffixed(value: u8) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}u8").into(), suffix: IntSuffix::U8, value: value as u128, ..Default::default() @@ -144,7 +144,7 @@ impl Lit { pub fn u16_suffixed(value: u16) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}u16").into(), suffix: IntSuffix::U16, value: value as u128, ..Default::default() @@ -161,7 +161,7 @@ impl Lit { pub fn u32_suffixed(value: u32) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}u32").into(), suffix: IntSuffix::U32, value: value as u128, ..Default::default() @@ -178,7 +178,7 @@ impl Lit { pub fn u64_suffixed(value: u64) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}u64").into(), suffix: IntSuffix::U64, value: value as u128, ..Default::default() @@ -195,7 +195,7 @@ impl Lit { pub fn usize_suffixed(value: usize) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}usize").into(), suffix: IntSuffix::USize, value: value as u128, ..Default::default() @@ -212,7 +212,7 @@ impl Lit { pub fn i8_suffixed(value: i8) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}i8").into(), suffix: IntSuffix::I8, value: value as u128, ..Default::default() @@ -229,7 +229,7 @@ impl Lit { pub fn i16_suffixed(value: i16) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}i16").into(), suffix: IntSuffix::I16, value: value as u128, ..Default::default() @@ -246,7 +246,7 @@ impl Lit { pub fn i32_suffixed(value: i32) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}i32").into(), suffix: IntSuffix::I32, value: value as u128, ..Default::default() @@ -263,7 +263,7 @@ impl Lit { pub fn i64_suffixed(value: i64) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}i64").into(), suffix: IntSuffix::I64, value: value as u128, ..Default::default() @@ -280,7 +280,7 @@ impl Lit { pub fn isize_suffixed(value: isize) -> Self { Self::Int(LitInt { - repr: value.to_string().into(), + repr: format!("{value}isize").into(), suffix: IntSuffix::ISize, value: value as u128, ..Default::default() diff --git a/crates/moxy-token/src/lit/str.rs b/crates/moxy-token/src/lit/str.rs index cf46c308..629481fa 100644 --- a/crates/moxy-token/src/lit/str.rs +++ b/crates/moxy-token/src/lit/str.rs @@ -171,6 +171,9 @@ fn escape(c: Cursor<'_>) -> Result, LexError> { match c.first() { None => c.error().into(), Some('n' | 'r' | 't' | '\\' | '\'' | '"' | '0') => Ok(c.advance()), + // A line continuation: the newline and the whitespace after it are skipped + Some('\n') => Ok(skip_continuation(c.advance())), + Some('\r') if c.advance().first() == Some('\n') => Ok(skip_continuation(c.advance_by(2))), Some('x') => { let c = c.advance(); let c = hex_digit(c)?; @@ -201,6 +204,14 @@ fn escape(c: Cursor<'_>) -> Result, LexError> { } } +fn skip_continuation(mut c: Cursor<'_>) -> Cursor<'_> { + while let Some(' ' | '\t' | '\n' | '\r') = c.first() { + c = c.advance(); + } + + c +} + fn hex_digit(c: Cursor<'_>) -> Result, LexError> { match c.first() { Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()), @@ -223,10 +234,19 @@ fn decode_string_body(repr: &str) -> Option { let mut chars = inner.chars(); while let Some(c) = chars.next() { - if c == '\\' { - out.push(decode_escape(&mut chars)?); - } else { + if c != '\\' { out.push(c); + } else if chars.clone().next().is_some_and(|next| next == '\n' || next == '\r') { + // A line continuation adds nothing, and the whitespace after it is skipped + while chars + .clone() + .next() + .is_some_and(|next| matches!(next, ' ' | '\t' | '\n' | '\r')) + { + chars.next(); + } + } else { + out.push(decode_escape(&mut chars)?); } } diff --git a/crates/moxy-token/tests/lit.rs b/crates/moxy-token/tests/lit.rs index b6b90dcc..aa4b8c77 100644 --- a/crates/moxy-token/tests/lit.rs +++ b/crates/moxy-token/tests/lit.rs @@ -40,3 +40,41 @@ fn reject() { assert!(!token.is_literal()); } } + +#[test] +fn string_line_continuations_skip_the_newline_and_indentation() { + let tokens: TokenStream = "\"first \\\n second\" 'x' b\"one \\\r\n\ttwo\" c\"a \\\n b\"" + .parse() + .unwrap(); + let literals: Vec = tokens + .iter() + .map(|token| token.as_literal().expect("expected a literal").clone()) + .collect(); + + assert_eq!(literals.len(), 4); + let moxy_token::Lit::Str(text) = &literals[0] else { + panic!("expected a string"); + }; + assert_eq!(text.value(), "first second"); + let moxy_token::Lit::ByteStr(bytes) = &literals[2] else { + panic!("expected a byte string"); + }; + assert_eq!(bytes.value(), b"one two"); + let moxy_token::Lit::CStr(c_text) = &literals[3] else { + panic!("expected a C string"); + }; + assert_eq!(c_text.value(), b"a b"); +} + +#[test] +fn suffixed_integer_constructors_keep_the_suffix() { + for (literal, text) in [ + (moxy_token::Lit::u8_suffixed(7), "7u8"), + (moxy_token::Lit::u64_suffixed(10923128598635462381), "10923128598635462381u64"), + (moxy_token::Lit::usize_suffixed(3), "3usize"), + (moxy_token::Lit::i32_suffixed(5), "5i32"), + (moxy_token::Lit::u64_unsuffixed(9), "9"), + ] { + assert_eq!(literal.to_string(), text); + } +}