Skip to content

Commit 231e8c8

Browse files
committed
Return Result from \x parser
1 parent 3f6d84b commit 231e8c8

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/parse.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,7 @@ fn cooked_string(input: Cursor) -> Result<Cursor, Reject> {
386386
},
387387
'\\' => match chars.next() {
388388
Some((_, 'x')) => {
389-
if !backslash_x_char(&mut chars) {
390-
break;
391-
}
389+
backslash_x_char(&mut chars)?;
392390
}
393391
Some((_, 'n')) | Some((_, 'r')) | Some((_, 't')) | Some((_, '\\'))
394392
| Some((_, '\'')) | Some((_, '"')) | Some((_, '0')) => {}
@@ -462,9 +460,7 @@ fn cooked_byte_string(mut input: Cursor) -> Result<Cursor, Reject> {
462460
},
463461
b'\\' => match bytes.next() {
464462
Some((_, b'x')) => {
465-
if !backslash_x_byte(&mut bytes) {
466-
break;
467-
}
463+
backslash_x_byte(&mut bytes)?;
468464
}
469465
Some((_, b'n')) | Some((_, b'r')) | Some((_, b't')) | Some((_, b'\\'))
470466
| Some((_, b'0')) | Some((_, b'\'')) | Some((_, b'"')) => {}
@@ -586,9 +582,7 @@ fn cooked_c_string(input: Cursor) -> Result<Cursor, Reject> {
586582
},
587583
'\\' => match chars.next() {
588584
Some((_, 'x')) => {
589-
if !backslash_x_nonzero(&mut chars) {
590-
break;
591-
}
585+
backslash_x_nonzero(&mut chars)?;
592586
}
593587
Some((_, 'n')) | Some((_, 'r')) | Some((_, 't')) | Some((_, '\\'))
594588
| Some((_, '\'')) | Some((_, '"')) => {}
@@ -627,7 +621,7 @@ fn byte(input: Cursor) -> Result<Cursor, Reject> {
627621
let mut bytes = input.bytes().enumerate();
628622
let ok = match bytes.next().map(|(_, b)| b) {
629623
Some(b'\\') => match bytes.next().map(|(_, b)| b) {
630-
Some(b'x') => backslash_x_byte(&mut bytes),
624+
Some(b'x') => backslash_x_byte(&mut bytes).is_ok(),
631625
Some(b'n') | Some(b'r') | Some(b't') | Some(b'\\') | Some(b'0') | Some(b'\'')
632626
| Some(b'"') => true,
633627
_ => false,
@@ -650,7 +644,7 @@ fn character(input: Cursor) -> Result<Cursor, Reject> {
650644
let mut chars = input.char_indices();
651645
let ok = match chars.next().map(|(_, ch)| ch) {
652646
Some('\\') => match chars.next().map(|(_, ch)| ch) {
653-
Some('x') => backslash_x_char(&mut chars),
647+
Some('x') => backslash_x_char(&mut chars).is_ok(),
654648
Some('u') => backslash_u(&mut chars).is_ok(),
655649
Some('n') | Some('r') | Some('t') | Some('\\') | Some('0') | Some('\'') | Some('"') => {
656650
true
@@ -672,48 +666,49 @@ macro_rules! next_ch {
672666
match $chars.next() {
673667
Some((_, ch)) => match ch {
674668
$pat $(| $rest)* => ch,
675-
_ => return false,
669+
_ => return Err(Reject),
676670
},
677-
None => return false,
671+
None => return Err(Reject),
678672
}
679673
};
680674
}
681675

682-
fn backslash_x_char<I>(chars: &mut I) -> bool
676+
fn backslash_x_char<I>(chars: &mut I) -> Result<(), Reject>
683677
where
684678
I: Iterator<Item = (usize, char)>,
685679
{
686680
next_ch!(chars @ '0'..='7');
687681
next_ch!(chars @ '0'..='9' | 'a'..='f' | 'A'..='F');
688-
true
682+
Ok(())
689683
}
690684

691-
fn backslash_x_byte<I>(chars: &mut I) -> bool
685+
fn backslash_x_byte<I>(chars: &mut I) -> Result<(), Reject>
692686
where
693687
I: Iterator<Item = (usize, u8)>,
694688
{
695689
next_ch!(chars @ b'0'..=b'9' | b'a'..=b'f' | b'A'..=b'F');
696690
next_ch!(chars @ b'0'..=b'9' | b'a'..=b'f' | b'A'..=b'F');
697-
true
691+
Ok(())
698692
}
699693

700-
fn backslash_x_nonzero<I>(chars: &mut I) -> bool
694+
fn backslash_x_nonzero<I>(chars: &mut I) -> Result<(), Reject>
701695
where
702696
I: Iterator<Item = (usize, char)>,
703697
{
704698
let first = next_ch!(chars @ '0'..='9' | 'a'..='f' | 'A'..='F');
705699
let second = next_ch!(chars @ '0'..='9' | 'a'..='f' | 'A'..='F');
706-
!(first == '0' && second == '0')
700+
if first == '0' && second == '0' {
701+
Err(Reject)
702+
} else {
703+
Ok(())
704+
}
707705
}
708706

709707
fn backslash_u<I>(chars: &mut I) -> Result<char, Reject>
710708
where
711709
I: Iterator<Item = (usize, char)>,
712710
{
713-
match chars.next() {
714-
Some((_, '{')) => {}
715-
_ => return Err(Reject),
716-
}
711+
next_ch!(chars @ '{');
717712
let mut value = 0;
718713
let mut len = 0;
719714
for (_, ch) in chars {

0 commit comments

Comments
 (0)