Skip to content

Commit

Permalink
Factor out logic for processing trailing backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jun 24, 2023
1 parent 07ffd04 commit e2923c3
Showing 1 changed file with 33 additions and 52 deletions.
85 changes: 33 additions & 52 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn string(input: Cursor) -> Result<Cursor, Reject> {
}
}

fn cooked_string(input: Cursor) -> Result<Cursor, Reject> {
fn cooked_string(mut input: Cursor) -> Result<Cursor, Reject> {
let mut chars = input.char_indices().peekable();

while let Some((i, ch)) = chars.next() {
Expand All @@ -393,21 +393,10 @@ fn cooked_string(input: Cursor) -> Result<Cursor, Reject> {
Some((_, 'u')) => {
backslash_u(&mut chars)?;
}
Some((_, ch @ '\n')) | Some((_, ch @ '\r')) => {
let mut last = ch;
loop {
if last == '\r' && chars.next().map_or(true, |(_, ch)| ch != '\n') {
return Err(Reject);
}
match chars.peek() {
Some((_, ch @ ' ')) | Some((_, ch @ '\t')) | Some((_, ch @ '\n'))
| Some((_, ch @ '\r')) => {
last = *ch;
chars.next();
}
_ => break,
}
}
Some((newline, ch @ '\n')) | Some((newline, ch @ '\r')) => {
input = input.advance(newline + 1);
trailing_backslash(&mut input, ch as u8)?;
chars = input.char_indices().peekable();
}
_ => break,
},
Expand Down Expand Up @@ -465,26 +454,9 @@ fn cooked_byte_string(mut input: Cursor) -> Result<Cursor, Reject> {
Some((_, b'n')) | Some((_, b'r')) | Some((_, b't')) | Some((_, b'\\'))
| Some((_, b'0')) | Some((_, b'\'')) | Some((_, b'"')) => {}
Some((newline, b @ b'\n')) | Some((newline, b @ b'\r')) => {
let mut last = b;
let rest = input.advance(newline + 1);
let mut whitespace = rest.bytes().enumerate();
loop {
if last == b'\r' && whitespace.next().map_or(true, |(_, b)| b != b'\n') {
return Err(Reject);
}
match whitespace.next() {
Some((_, b @ b' ')) | Some((_, b @ b'\t')) | Some((_, b @ b'\n'))
| Some((_, b @ b'\r')) => {
last = b;
}
Some((offset, _)) => {
input = rest.advance(offset);
bytes = input.bytes().enumerate();
break;
}
None => return Err(Reject),
}
}
input = input.advance(newline + 1);
trailing_backslash(&mut input, b)?;
bytes = input.bytes().enumerate();
}
_ => break,
},
Expand Down Expand Up @@ -567,7 +539,7 @@ fn raw_c_string(input: Cursor) -> Result<Cursor, Reject> {
Err(Reject)
}

fn cooked_c_string(input: Cursor) -> Result<Cursor, Reject> {
fn cooked_c_string(mut input: Cursor) -> Result<Cursor, Reject> {
let mut chars = input.char_indices().peekable();

while let Some((i, ch)) = chars.next() {
Expand All @@ -591,21 +563,10 @@ fn cooked_c_string(input: Cursor) -> Result<Cursor, Reject> {
break;
}
}
Some((_, ch @ '\n')) | Some((_, ch @ '\r')) => {
let mut last = ch;
loop {
if last == '\r' && chars.next().map_or(true, |(_, ch)| ch != '\n') {
return Err(Reject);
}
match chars.peek() {
Some((_, ch @ ' ')) | Some((_, ch @ '\t')) | Some((_, ch @ '\n'))
| Some((_, ch @ '\r')) => {
last = *ch;
chars.next();
}
_ => break,
}
}
Some((newline, ch @ '\n')) | Some((newline, ch @ '\r')) => {
input = input.advance(newline + 1);
trailing_backslash(&mut input, ch as u8)?;
chars = input.char_indices().peekable();
}
_ => break,
},
Expand Down Expand Up @@ -730,6 +691,26 @@ where
Err(Reject)
}

fn trailing_backslash(input: &mut Cursor, mut last: u8) -> Result<(), Reject> {
let mut whitespace = input.bytes().enumerate();
loop {
if last == b'\r' && whitespace.next().map_or(true, |(_, b)| b != b'\n') {
return Err(Reject);
}
match whitespace.next() {
Some((_, b @ b' ')) | Some((_, b @ b'\t')) | Some((_, b @ b'\n'))
| Some((_, b @ b'\r')) => {
last = b;
}
Some((offset, _)) => {
*input = input.advance(offset);
return Ok(());
}
None => return Err(Reject),
}
}
}

fn float(input: Cursor) -> Result<Cursor, Reject> {
let mut rest = float_digits(input)?;
if let Some(ch) = rest.chars().next() {
Expand Down

0 comments on commit e2923c3

Please sign in to comment.