Switch from IResult<I, O> to Result<(I, O), LexError>
diff --git a/src/stable.rs b/src/stable.rs
index 08afc7f..02cd626 100644
--- a/src/stable.rs
+++ b/src/stable.rs
@@ -11,7 +11,7 @@
use proc_macro;
use unicode_xid::UnicodeXID;
-use strnom::{IResult, skip_whitespace, block_comment, whitespace};
+use strnom::{PResult, skip_whitespace, block_comment, whitespace};
use {TokenTree, TokenKind, Delimiter, OpKind};
@@ -38,14 +38,14 @@
fn from_str(src: &str) -> Result<TokenStream, LexError> {
match token_stream(src) {
- IResult::Done(input, output) => {
+ Ok((input, output)) => {
if skip_whitespace(input).len() != 0 {
Err(LexError)
} else {
Ok(output)
}
}
- IResult::Error => Err(LexError),
+ Err(LexError) => Err(LexError),
}
}
}
@@ -390,35 +390,35 @@
)
));
-fn word(mut input: &str) -> IResult<&str, &str> {
+fn word(mut input: &str) -> PResult<&str> {
input = skip_whitespace(input);
let mut chars = input.char_indices();
match chars.next() {
Some((_, ch)) if UnicodeXID::is_xid_start(ch) || ch == '_' => {}
- _ => return IResult::Error,
+ _ => return Err(LexError),
}
for (i, ch) in chars {
if !UnicodeXID::is_xid_continue(ch) {
- return IResult::Done(&input[i..], &input[..i])
+ return Ok((&input[i..], &input[..i]))
}
}
- IResult::Done("", input)
+ Ok(("", input))
}
-fn literal(input: &str) -> IResult<&str, Literal> {
+fn literal(input: &str) -> PResult<Literal> {
let input_no_ws = skip_whitespace(input);
match literal_nocapture(input_no_ws) {
- IResult::Done(a, ()) => {
+ Ok((a, ())) => {
let start = input.len() - input_no_ws.len();
let len = input_no_ws.len() - a.len();
let end = start + len;
- IResult::Done(a, Literal(input[start..end].to_string()))
+ Ok((a, Literal(input[start..end].to_string())))
}
- IResult::Error => IResult::Error,
+ Err(LexError) => Err(LexError),
}
}
@@ -455,12 +455,12 @@
tag!("\"")
));
-fn cooked_string(input: &str) -> IResult<&str, ()> {
+fn cooked_string(input: &str) -> PResult<()> {
let mut chars = input.char_indices().peekable();
while let Some((byte_offset, ch)) = chars.next() {
match ch {
'"' => {
- return IResult::Done(&input[byte_offset..], ());
+ return Ok((&input[byte_offset..], ()));
}
'\r' => {
if let Some((_, '\n')) = chars.next() {
@@ -503,7 +503,7 @@
_ch => {}
}
}
- IResult::Error
+ Err(LexError)
}
named!(byte_string -> (), alt!(
@@ -519,12 +519,12 @@
) => { |_| () }
));
-fn cooked_byte_string(mut input: &str) -> IResult<&str, ()> {
+fn cooked_byte_string(mut input: &str) -> PResult<()> {
let mut bytes = input.bytes().enumerate();
'outer: while let Some((offset, b)) = bytes.next() {
match b {
b'"' => {
- return IResult::Done(&input[offset..], ());
+ return Ok((&input[offset..], ()));
}
b'\r' => {
if let Some((_, b'\n')) = bytes.next() {
@@ -566,10 +566,10 @@
_ => break,
}
}
- IResult::Error
+ Err(LexError)
}
-fn raw_string(input: &str) -> IResult<&str, ()> {
+fn raw_string(input: &str) -> PResult<()> {
let mut chars = input.char_indices();
let mut n = 0;
while let Some((byte_offset, ch)) = chars.next() {
@@ -579,20 +579,20 @@
break;
}
'#' => {}
- _ => return IResult::Error,
+ _ => return Err(LexError),
}
}
for (byte_offset, ch) in chars {
match ch {
'"' if input[byte_offset + 1..].starts_with(&input[..n]) => {
let rest = &input[byte_offset + 1 + n..];
- return IResult::Done(rest, ())
+ return Ok((rest, ()))
}
'\r' => {}
_ => {}
}
}
- IResult::Error
+ Err(LexError)
}
named!(byte -> (), do_parse!(
@@ -603,7 +603,7 @@
(())
));
-fn cooked_byte(input: &str) -> IResult<&str, ()> {
+fn cooked_byte(input: &str) -> PResult<()> {
let mut bytes = input.bytes().enumerate();
let ok = match bytes.next().map(|(_, b)| b) {
Some(b'\\') => {
@@ -623,11 +623,11 @@
};
if ok {
match bytes.next() {
- Some((offset, _)) => IResult::Done(&input[offset..], ()),
- None => IResult::Done("", ()),
+ Some((offset, _)) => Ok((&input[offset..], ())),
+ None => Ok(("", ())),
}
} else {
- IResult::Error
+ Err(LexError)
}
}
@@ -638,7 +638,7 @@
(())
));
-fn cooked_char(input: &str) -> IResult<&str, ()> {
+fn cooked_char(input: &str) -> PResult<()> {
let mut chars = input.char_indices();
let ok = match chars.next().map(|(_, ch)| ch) {
Some('\\') => {
@@ -658,9 +658,9 @@
ch => ch.is_some(),
};
if ok {
- IResult::Done(chars.as_str(), ())
+ Ok((chars.as_str(), ()))
} else {
- IResult::Error
+ Err(LexError)
}
}
@@ -733,11 +733,11 @@
(())
));
-fn float_string(input: &str) -> IResult<&str, ()> {
+fn float_string(input: &str) -> PResult<()> {
let mut chars = input.chars().peekable();
match chars.next() {
Some(ch) if ch >= '0' && ch <= '9' => {}
- _ => return IResult::Error,
+ _ => return Err(LexError),
}
let mut len = 1;
@@ -757,7 +757,7 @@
if chars.peek()
.map(|&ch| ch == '.' || UnicodeXID::is_xid_start(ch))
.unwrap_or(false) {
- return IResult::Error;
+ return Err(LexError);
}
len += 1;
has_dot = true;
@@ -774,7 +774,7 @@
let rest = &input[len..];
if !(has_dot || has_exp || rest.starts_with("f32") || rest.starts_with("f64")) {
- return IResult::Error;
+ return Err(LexError);
}
if has_exp {
@@ -801,11 +801,11 @@
}
}
if !has_exp_value {
- return IResult::Error;
+ return Err(LexError);
}
}
- IResult::Done(&input[len..], ())
+ Ok((&input[len..], ()))
}
named!(int -> (), do_parse!(
@@ -840,7 +840,7 @@
(())
));
-fn digits(mut input: &str) -> IResult<&str, ()> {
+fn digits(mut input: &str) -> PResult<()> {
let base = if input.starts_with("0x") {
input = &input[2..];
16
@@ -863,7 +863,7 @@
b'A'...b'F' => 10 + (b - b'A') as u64,
b'_' => {
if empty && base == 10 {
- return IResult::Error;
+ return Err(LexError);
}
len += 1;
continue;
@@ -871,15 +871,15 @@
_ => break,
};
if digit >= base {
- return IResult::Error;
+ return Err(LexError);
}
len += 1;
empty = false;
}
if empty {
- IResult::Error
+ Err(LexError)
} else {
- IResult::Done(&input[len..], ())
+ Ok((&input[len..], ()))
}
}
@@ -889,33 +889,33 @@
keyword!("false") => { |_| () }
));
-fn op(input: &str) -> IResult<&str, (char, OpKind)> {
+fn op(input: &str) -> PResult<(char, OpKind)> {
let input = skip_whitespace(input);
match op_char(input) {
- IResult::Done(rest, ch) => {
+ Ok((rest, ch)) => {
let kind = match op_char(rest) {
- IResult::Done(_, _) => OpKind::Joint,
- IResult::Error => OpKind::Alone,
+ Ok(_) => OpKind::Joint,
+ Err(LexError) => OpKind::Alone,
};
- IResult::Done(rest, (ch, kind))
+ Ok((rest, (ch, kind)))
}
- IResult::Error => IResult::Error,
+ Err(LexError) => Err(LexError),
}
}
-fn op_char(input: &str) -> IResult<&str, char> {
+fn op_char(input: &str) -> PResult<char> {
let mut chars = input.chars();
let first = match chars.next() {
Some(ch) => ch,
None => {
- return IResult::Error;
+ return Err(LexError);
}
};
let recognized = "~!@#$%^&*-=+|;:,<.>/?";
if recognized.contains(first) {
- IResult::Done(chars.as_str(), first)
+ Ok((chars.as_str(), first))
} else {
- IResult::Error
+ Err(LexError)
}
}
diff --git a/src/strnom.rs b/src/strnom.rs
index b4d78ac..875c4bf 100644
--- a/src/strnom.rs
+++ b/src/strnom.rs
@@ -1,19 +1,14 @@
-//! Adapted from [`nom`](https://github.com/Geal/nom) by removing the
-//! `IResult::Incomplete` variant.
+//! Adapted from [`nom`](https://github.com/Geal/nom).
use unicode_xid::UnicodeXID;
-pub enum IResult<I, O> {
- /// Parsing succeeded. The first field contains the rest of the unparsed
- /// data and the second field contains the parse result.
- Done(I, O),
- /// Parsing failed.
- Error,
-}
+use imp::LexError;
-pub fn whitespace(input: &str) -> IResult<&str, ()> {
+pub type PResult<'a, O> = Result<(&'a str, O), LexError>;
+
+pub fn whitespace(input: &str) -> PResult<()> {
if input.is_empty() {
- return IResult::Error;
+ return Err(LexError);
}
let bytes = input.as_bytes();
@@ -30,15 +25,9 @@
break;
} else if s.starts_with("/*") && (!s.starts_with("/**") || s.starts_with("/***")) &&
!s.starts_with("/*!") {
- match block_comment(s) {
- IResult::Done(_, com) => {
- i += com.len();
- continue;
- }
- IResult::Error => {
- return IResult::Error;
- }
- }
+ let (_, com) = block_comment(s)?;
+ i += com.len();
+ continue;
}
}
match bytes[i] {
@@ -56,17 +45,17 @@
}
}
return if i > 0 {
- IResult::Done(s, ())
+ Ok((s, ()))
} else {
- IResult::Error
+ Err(LexError)
};
}
- IResult::Done("", ())
+ Ok(("", ()))
}
-pub fn block_comment(input: &str) -> IResult<&str, &str> {
+pub fn block_comment(input: &str) -> PResult<&str> {
if !input.starts_with("/*") {
- return IResult::Error;
+ return Err(LexError);
}
let mut depth = 0;
@@ -80,19 +69,19 @@
} else if bytes[i] == b'*' && bytes[i + 1] == b'/' {
depth -= 1;
if depth == 0 {
- return IResult::Done(&input[i + 2..], &input[..i + 2]);
+ return Ok((&input[i + 2..], &input[..i + 2]));
}
i += 1; // eat '/'
}
i += 1;
}
- IResult::Error
+ Err(LexError)
}
pub fn skip_whitespace(input: &str) -> &str {
match whitespace(input) {
- IResult::Done(rest, _) => rest,
- IResult::Error => input,
+ Ok((rest, _)) => rest,
+ Err(LexError) => input,
}
}
@@ -101,16 +90,16 @@
ch.is_whitespace() || ch == '\u{200e}' || ch == '\u{200f}'
}
-fn word_break(input: &str) -> IResult<&str, ()> {
+fn word_break(input: &str) -> PResult<()> {
match input.chars().next() {
- Some(ch) if UnicodeXID::is_xid_continue(ch) => IResult::Error,
- Some(_) | None => IResult::Done(input, ()),
+ Some(ch) if UnicodeXID::is_xid_continue(ch) => Err(LexError),
+ Some(_) | None => Ok((input, ())),
}
}
macro_rules! named {
($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
- fn $name(i: &str) -> $crate::strnom::IResult<&str, $o> {
+ fn $name(i: &str) -> $crate::strnom::PResult<$o> {
$submac!(i, $($args)*)
}
};
@@ -123,15 +112,15 @@
($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
match $subrule!($i, $($args)*) {
- res @ $crate::strnom::IResult::Done(_, _) => res,
+ res @ Ok(_) => res,
_ => alt!($i, $($rest)*)
}
};
($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
match $subrule!($i, $($args)*) {
- $crate::strnom::IResult::Done(i, o) => $crate::strnom::IResult::Done(i, $gen(o)),
- $crate::strnom::IResult::Error => alt!($i, $($rest)*)
+ Ok((i, o)) => Ok((i, $gen(o))),
+ Err(LexError) => alt!($i, $($rest)*)
}
};
@@ -145,8 +134,8 @@
($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
match $subrule!($i, $($args)*) {
- $crate::strnom::IResult::Done(i, o) => $crate::strnom::IResult::Done(i, $gen(o)),
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
+ Ok((i, o)) => Ok((i, $gen(o))),
+ Err(LexError) => Err(LexError),
}
};
@@ -161,7 +150,7 @@
macro_rules! do_parse {
($i:expr, ( $($rest:expr),* )) => {
- $crate::strnom::IResult::Done($i, ( $($rest),* ))
+ Ok(($i, ( $($rest),* )))
};
($i:expr, $e:ident >> $($rest:tt)*) => {
@@ -170,9 +159,8 @@
($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Done(i, _) =>
- do_parse!(i, $($rest)*),
+ Err(LexError) => Err(LexError),
+ Ok((i, _)) => do_parse!(i, $($rest)*),
}
};
@@ -182,8 +170,8 @@
($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Done(i, o) => {
+ Err(LexError) => Err(LexError),
+ Ok((i, o)) => {
let $field = o;
do_parse!(i, $($rest)*)
},
@@ -194,8 +182,8 @@
macro_rules! peek {
($i:expr, $submac:ident!( $($args:tt)* )) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Done(_, o) => $crate::strnom::IResult::Done($i, o),
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
+ Ok((_, o)) => Ok(($i, o)),
+ Err(LexError) => Err(LexError),
}
};
}
@@ -209,8 +197,8 @@
macro_rules! option {
($i:expr, $f:expr) => {
match $f($i) {
- $crate::strnom::IResult::Done(i, o) => $crate::strnom::IResult::Done(i, Some(o)),
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Done($i, None),
+ Ok((i, o)) => Ok((i, Some(o))),
+ Err(LexError) => Ok(($i, None)),
}
};
}
@@ -218,7 +206,7 @@
macro_rules! take_until {
($i:expr, $substr:expr) => {{
if $substr.len() > $i.len() {
- $crate::strnom::IResult::Error
+ Err(LexError)
} else {
let substr_vec: Vec<char> = $substr.chars().collect();
let mut window: Vec<char> = vec![];
@@ -240,9 +228,9 @@
}
}
if parsed {
- $crate::strnom::IResult::Done(&$i[offset..], &$i[..offset])
+ Ok((&$i[offset..], &$i[..offset]))
} else {
- $crate::strnom::IResult::Error
+ Err(LexError)
}
}
}};
@@ -262,17 +250,15 @@
($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Done(i, o) =>
- tuple_parser!(i, (o), $($rest)*),
+ Err(LexError) => Err(LexError),
+ Ok((i, o)) => tuple_parser!(i, (o), $($rest)*),
}
};
($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Done(i, o) =>
- tuple_parser!(i, ($($parsed)* , o), $($rest)*),
+ Err(LexError) => Err(LexError),
+ Ok((i, o)) => tuple_parser!(i, ($($parsed)* , o), $($rest)*),
}
};
@@ -286,21 +272,21 @@
($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Done(i, o) => $crate::strnom::IResult::Done(i, ($($parsed),*, o))
+ Err(LexError) => Err(LexError),
+ Ok((i, o)) => Ok((i, ($($parsed),*, o)))
}
};
($i:expr, ($($parsed:expr),*)) => {
- $crate::strnom::IResult::Done($i, ($($parsed),*))
+ Ok(($i, ($($parsed),*)))
};
}
macro_rules! not {
($i:expr, $submac:ident!( $($args:tt)* )) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Done(_, _) => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Done($i, ()),
+ Ok((_, _)) => Err(LexError),
+ Err(LexError) => Ok(($i, ())),
}
};
}
@@ -308,9 +294,9 @@
macro_rules! tag {
($i:expr, $tag:expr) => {
if $i.starts_with($tag) {
- $crate::strnom::IResult::Done(&$i[$tag.len()..], &$i[..$tag.len()])
+ Ok((&$i[$tag.len()..], &$i[..$tag.len()]))
} else {
- $crate::strnom::IResult::Error
+ Err(LexError)
}
};
}
@@ -322,12 +308,12 @@
}
/// Do not use directly. Use `punct!`.
-pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> {
+pub fn punct<'a>(input: &'a str, token: &'static str) -> PResult<'a, &'a str> {
let input = skip_whitespace(input);
if input.starts_with(token) {
- IResult::Done(&input[token.len()..], token)
+ Ok((&input[token.len()..], token))
} else {
- IResult::Error
+ Err(LexError)
}
}
@@ -338,29 +324,29 @@
}
/// Do not use directly. Use `keyword!`.
-pub fn keyword<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> {
+pub fn keyword<'a>(input: &'a str, token: &'static str) -> PResult<'a, &'a str> {
match punct(input, token) {
- IResult::Done(rest, _) => {
+ Ok((rest, _)) => {
match word_break(rest) {
- IResult::Done(_, _) => IResult::Done(rest, token),
- IResult::Error => IResult::Error,
+ Ok((_, _)) => Ok((rest, token)),
+ Err(LexError) => Err(LexError),
}
}
- IResult::Error => IResult::Error,
+ Err(LexError) => Err(LexError),
}
}
macro_rules! epsilon {
($i:expr,) => {
- $crate::strnom::IResult::Done($i, ())
+ Ok(($i, ()))
};
}
macro_rules! preceded {
($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
- $crate::strnom::IResult::Done(remaining, (_, o)) => $crate::strnom::IResult::Done(remaining, o),
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
+ Ok((remaining, (_, o))) => Ok((remaining, o)),
+ Err(LexError) => Err(LexError),
}
};
@@ -372,8 +358,8 @@
macro_rules! delimited {
($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Done(i1, (_, o, _)) => $crate::strnom::IResult::Done(i1, o)
+ Err(LexError) => Err(LexError),
+ Ok((i1, (_, o, _))) => Ok((i1, o))
}
};
}
@@ -381,10 +367,8 @@
macro_rules! map {
($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
match $submac!($i, $($args)*) {
- $crate::strnom::IResult::Error => $crate::strnom::IResult::Error,
- $crate::strnom::IResult::Done(i, o) => {
- $crate::strnom::IResult::Done(i, call!(o, $g))
- }
+ Err(LexError) => Err(LexError),
+ Ok((i, o)) => Ok((i, call!(o, $g)))
}
};
@@ -401,19 +385,19 @@
loop {
if input.is_empty() {
- ret = $crate::strnom::IResult::Done(input, res);
+ ret = Ok((input, res));
break;
}
match $f(input) {
- $crate::strnom::IResult::Error => {
- ret = $crate::strnom::IResult::Done(input, res);
+ Err(LexError) => {
+ ret = Ok((input, res));
break;
}
- $crate::strnom::IResult::Done(i, o) => {
+ Ok((i, o)) => {
// loop trip must always consume (otherwise infinite loops)
if i.len() == input.len() {
- ret = $crate::strnom::IResult::Error;
+ ret = Err(LexError);
break;
}