Skip to content
Closed
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
26 changes: 23 additions & 3 deletions crates/moxy-token/src/lit/byte_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, 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)?;
Expand Down Expand Up @@ -192,6 +195,14 @@ fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, 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<Cursor<'_>, LexError> {
match c.first() {
Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()),
Expand All @@ -214,10 +225,19 @@ fn decode_string_body(repr: &str) -> Option<String> {
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)?);
}
}

Expand Down
26 changes: 23 additions & 3 deletions crates/moxy-token/src/lit/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, 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)?;
Expand Down Expand Up @@ -192,6 +195,14 @@ fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, 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<Cursor<'_>, LexError> {
match c.first() {
Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()),
Expand All @@ -214,10 +225,19 @@ fn decode_string_body(repr: &str) -> Option<String> {
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)?);
}
}

Expand Down
20 changes: 10 additions & 10 deletions crates/moxy-token/src/lit/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl From<LitInt> 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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
26 changes: 23 additions & 3 deletions crates/moxy-token/src/lit/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, 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)?;
Expand Down Expand Up @@ -201,6 +204,14 @@ fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, 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<Cursor<'_>, LexError> {
match c.first() {
Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()),
Expand All @@ -223,10 +234,19 @@ fn decode_string_body(repr: &str) -> Option<String> {
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)?);
}
}

Expand Down
38 changes: 38 additions & 0 deletions crates/moxy-token/tests/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<moxy_token::Lit> = 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);
}
}