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

Factor out logic for processing trailing backslashes #396

Merged
merged 2 commits into from
Jun 24, 2023
Merged
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
89 changes: 35 additions & 54 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ fn string(input: Cursor) -> Result<Cursor, Reject> {
}
}

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

while let Some((i, ch)) = chars.next() {
match ch {
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();
}
_ => 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,8 +539,8 @@ fn raw_c_string(input: Cursor) -> Result<Cursor, Reject> {
Err(Reject)
}

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

while let Some((i, ch)) = chars.next() {
match ch {
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();
}
_ => 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