mirror of
https://gitee.com/openharmony/third_party_rust_syn
synced 2024-11-27 01:41:05 +00:00
Put remaining cursor last
This commit is contained in:
parent
65729483a7
commit
f4aa6b4946
@ -427,11 +427,11 @@ pub mod parsing {
|
||||
let literal = lit.to_string();
|
||||
if literal.starts_with("//") || literal.starts_with("/*") {
|
||||
Ok((
|
||||
rest,
|
||||
TokenTree {
|
||||
span: span,
|
||||
kind: TokenNode::Literal(lit),
|
||||
},
|
||||
rest,
|
||||
))
|
||||
} else {
|
||||
parse_error()
|
||||
|
@ -285,7 +285,7 @@ mod parsing {
|
||||
parse: fn(Cursor) -> PResult<T>,
|
||||
) -> PResult<Self> {
|
||||
match Self::parse(input, parse, false) {
|
||||
Ok((_, ref b)) if b.is_empty() => parse_error(),
|
||||
Ok((ref b, _)) if b.is_empty() => parse_error(),
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
@ -302,7 +302,7 @@ mod parsing {
|
||||
parse: fn(Cursor) -> PResult<T>,
|
||||
) -> PResult<Self> {
|
||||
match Self::parse(input, parse, true) {
|
||||
Ok((_, ref b)) if b.is_empty() => parse_error(),
|
||||
Ok((ref b, _)) if b.is_empty() => parse_error(),
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
@ -316,8 +316,8 @@ mod parsing {
|
||||
|
||||
// get the first element
|
||||
match parse(input) {
|
||||
Err(_) => Ok((input, res)),
|
||||
Ok((i, o)) => {
|
||||
Err(_) => Ok((res, input)),
|
||||
Ok((o, i)) => {
|
||||
if i == input {
|
||||
return parse_error();
|
||||
}
|
||||
@ -325,13 +325,13 @@ mod parsing {
|
||||
res.push(o);
|
||||
|
||||
// get the separator first
|
||||
while let Ok((i2, s)) = D::parse(input) {
|
||||
while let Ok((s, i2)) = D::parse(input) {
|
||||
if i2 == input {
|
||||
break;
|
||||
}
|
||||
|
||||
// get the element next
|
||||
if let Ok((i3, o3)) = parse(i2) {
|
||||
if let Ok((o3, i3)) = parse(i2) {
|
||||
if i3 == i2 {
|
||||
break;
|
||||
}
|
||||
@ -343,12 +343,12 @@ mod parsing {
|
||||
}
|
||||
}
|
||||
if terminated {
|
||||
if let Ok((after, sep)) = D::parse(input) {
|
||||
if let Ok((sep, after)) = D::parse(input) {
|
||||
res.push_trailing(sep);
|
||||
input = after;
|
||||
}
|
||||
}
|
||||
Ok((input, res))
|
||||
Ok((res, input))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use cursor::Cursor;
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
/// The result of a parser
|
||||
pub type PResult<'a, O> = Result<(Cursor<'a>, O), ParseError>;
|
||||
pub type PResult<'a, O> = Result<(O, Cursor<'a>), ParseError>;
|
||||
|
||||
/// An error with a default error message.
|
||||
///
|
||||
|
@ -251,11 +251,11 @@ pub mod parsing {
|
||||
}
|
||||
|
||||
Ok((
|
||||
rest,
|
||||
Ident {
|
||||
span: span,
|
||||
term: term,
|
||||
},
|
||||
rest,
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -184,7 +184,7 @@ where
|
||||
let buf = SynomBuffer::new(tokens);
|
||||
let result = T::parse(buf.begin());
|
||||
let err = match result {
|
||||
Ok((rest, t)) => {
|
||||
Ok((t, rest)) => {
|
||||
if rest.eof() {
|
||||
return Ok(t);
|
||||
} else if rest == buf.begin() {
|
||||
|
@ -108,11 +108,11 @@ pub mod parsing {
|
||||
}
|
||||
|
||||
Ok((
|
||||
rest,
|
||||
Lifetime {
|
||||
term: term,
|
||||
span: span,
|
||||
},
|
||||
rest,
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -106,11 +106,11 @@ pub mod parsing {
|
||||
fn parse(input: Cursor) -> PResult<Self> {
|
||||
match input.literal() {
|
||||
Some((span, lit, rest)) => Ok((
|
||||
rest,
|
||||
Lit {
|
||||
span: span,
|
||||
value: LitKind::Other(lit),
|
||||
},
|
||||
rest,
|
||||
)),
|
||||
_ => match input.term() {
|
||||
Some((span, term, rest)) => {
|
||||
@ -123,11 +123,11 @@ pub mod parsing {
|
||||
};
|
||||
|
||||
Ok((
|
||||
rest,
|
||||
Lit {
|
||||
span: span,
|
||||
value: kind,
|
||||
},
|
||||
rest,
|
||||
))
|
||||
}
|
||||
_ => parse_error(),
|
||||
|
@ -89,7 +89,7 @@ macro_rules! call {
|
||||
eprintln!(concat!(" -> ", stringify!($fun), " @ {:?}"), i);
|
||||
let r = $fun(i $(, $args)*);
|
||||
match r {
|
||||
Ok((i, _)) => eprintln!(concat!("OK ", stringify!($fun), " @ {:?}"), i),
|
||||
Ok((_, i)) => eprintln!(concat!("OK ", stringify!($fun), " @ {:?}"), i),
|
||||
Err(_) => eprintln!(concat!("ERR ", stringify!($fun), " @ {:?}"), i),
|
||||
}
|
||||
r
|
||||
@ -173,8 +173,8 @@ macro_rules! map {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((i, $crate::parsers::invoke($g, o))),
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
::std::result::Result::Ok(($crate::parsers::invoke($g, o), i)),
|
||||
}
|
||||
};
|
||||
|
||||
@ -220,7 +220,7 @@ macro_rules! not {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Ok(_) => $crate::parse_error(),
|
||||
::std::result::Result::Err(_) =>
|
||||
::std::result::Result::Ok(($i, ())),
|
||||
::std::result::Result::Ok(((), $i)),
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -286,12 +286,12 @@ macro_rules! cond {
|
||||
($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
|
||||
if $cond {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((i, ::std::option::Option::Some(o))),
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
::std::result::Result::Ok((::std::option::Option::Some(o), i)),
|
||||
::std::result::Result::Err(x) => ::std::result::Result::Err(x),
|
||||
}
|
||||
} else {
|
||||
::std::result::Result::Ok(($i, ::std::option::Option::None))
|
||||
::std::result::Result::Ok((::std::option::Option::None, $i))
|
||||
}
|
||||
};
|
||||
|
||||
@ -425,16 +425,16 @@ macro_rules! many0 {
|
||||
|
||||
loop {
|
||||
if input.eof() {
|
||||
ret = ::std::result::Result::Ok((input, res));
|
||||
ret = ::std::result::Result::Ok((res, input));
|
||||
break;
|
||||
}
|
||||
|
||||
match $submac!(input, $($args)*) {
|
||||
::std::result::Result::Err(_) => {
|
||||
ret = ::std::result::Result::Ok((input, res));
|
||||
ret = ::std::result::Result::Ok((res, input));
|
||||
break;
|
||||
}
|
||||
::std::result::Result::Ok((i, o)) => {
|
||||
::std::result::Result::Ok((o, i)) => {
|
||||
// loop trip must always consume (otherwise infinite loops)
|
||||
if i == input {
|
||||
ret = $crate::parse_error();
|
||||
@ -465,14 +465,14 @@ pub fn many0<T>(mut input: Cursor, f: fn(Cursor) -> PResult<T>) -> PResult<Vec<T
|
||||
|
||||
loop {
|
||||
if input.eof() {
|
||||
return Ok((input, res));
|
||||
return Ok((res, input));
|
||||
}
|
||||
|
||||
match f(input) {
|
||||
Err(_) => {
|
||||
return Ok((input, res));
|
||||
return Ok((res, input));
|
||||
}
|
||||
Ok((i, o)) => {
|
||||
Ok((o, i)) => {
|
||||
// loop trip must always consume (otherwise infinite loops)
|
||||
if i == input {
|
||||
return parse_error();
|
||||
@ -561,7 +561,7 @@ macro_rules! switch {
|
||||
($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) => ::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, o)) => match o {
|
||||
::std::result::Result::Ok((o, i)) => match o {
|
||||
$(
|
||||
$p => $subrule!(i, $($args2)*),
|
||||
)*
|
||||
@ -651,7 +651,7 @@ macro_rules! switch {
|
||||
#[macro_export]
|
||||
macro_rules! value {
|
||||
($i:expr, $res:expr) => {
|
||||
::std::result::Result::Ok(($i, $res))
|
||||
::std::result::Result::Ok(($res, $i))
|
||||
};
|
||||
}
|
||||
|
||||
@ -721,7 +721,7 @@ macro_rules! tuple_parser {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
tuple_parser!(i, (o), $($rest)*),
|
||||
}
|
||||
};
|
||||
@ -730,7 +730,7 @@ macro_rules! tuple_parser {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
tuple_parser!(i, ($($parsed)* , o), $($rest)*),
|
||||
}
|
||||
};
|
||||
@ -747,13 +747,13 @@ macro_rules! tuple_parser {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((i, ($($parsed),*, o))),
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
::std::result::Result::Ok((($($parsed),*, o), i)),
|
||||
}
|
||||
};
|
||||
|
||||
($i:expr, ($($parsed:expr),*)) => {
|
||||
::std::result::Result::Ok(($i, ($($parsed),*)))
|
||||
::std::result::Result::Ok((($($parsed),*), $i))
|
||||
};
|
||||
}
|
||||
|
||||
@ -796,8 +796,8 @@ macro_rules! alt {
|
||||
|
||||
($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
|
||||
match $subrule!($i, $($args)*) {
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((i, $crate::parsers::invoke($gen, o))),
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
::std::result::Result::Ok(($crate::parsers::invoke($gen, o), i)),
|
||||
::std::result::Result::Err(_) => alt!($i, $($rest)*),
|
||||
}
|
||||
};
|
||||
@ -812,8 +812,8 @@ macro_rules! alt {
|
||||
|
||||
($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
|
||||
match $subrule!($i, $($args)*) {
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((i, $crate::parsers::invoke($gen, o))),
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
::std::result::Result::Ok(($crate::parsers::invoke($gen, o), i)),
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
}
|
||||
@ -876,7 +876,7 @@ macro_rules! alt {
|
||||
#[macro_export]
|
||||
macro_rules! do_parse {
|
||||
($i:expr, ( $($rest:expr),* )) => {
|
||||
::std::result::Result::Ok(($i, ( $($rest),* )))
|
||||
::std::result::Result::Ok((( $($rest),* ), $i))
|
||||
};
|
||||
|
||||
($i:expr, $e:ident >> $($rest:tt)*) => {
|
||||
@ -887,7 +887,7 @@ macro_rules! do_parse {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, _)) =>
|
||||
::std::result::Result::Ok((_, i)) =>
|
||||
do_parse!(i, $($rest)*),
|
||||
}
|
||||
};
|
||||
@ -900,7 +900,7 @@ macro_rules! do_parse {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, o)) => {
|
||||
::std::result::Result::Ok((o, i)) => {
|
||||
let $field = o;
|
||||
do_parse!(i, $($rest)*)
|
||||
},
|
||||
@ -915,7 +915,7 @@ macro_rules! do_parse {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
::std::result::Result::Ok((i, o)) => {
|
||||
::std::result::Result::Ok((o, i)) => {
|
||||
let mut $field = o;
|
||||
do_parse!(i, $($rest)*)
|
||||
},
|
||||
@ -985,7 +985,7 @@ macro_rules! input_end {
|
||||
#[doc(hidden)]
|
||||
pub fn input_end(input: Cursor) -> PResult<'static, ()> {
|
||||
if input.eof() {
|
||||
Ok((Cursor::empty(), ()))
|
||||
Ok(((), Cursor::empty()))
|
||||
} else {
|
||||
parse_error()
|
||||
}
|
||||
@ -1036,10 +1036,10 @@ pub fn input_end(input: Cursor) -> PResult<'static, ()> {
|
||||
macro_rules! option {
|
||||
($i:expr, $submac:ident!( $($args:tt)* )) => {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Ok((i, o)) =>
|
||||
::std::result::Result::Ok((i, Some(o))),
|
||||
::std::result::Result::Ok((o, i)) =>
|
||||
::std::result::Result::Ok((Some(o), i)),
|
||||
::std::result::Result::Err(_) =>
|
||||
::std::result::Result::Ok(($i, None)),
|
||||
::std::result::Result::Ok((None, $i)),
|
||||
}
|
||||
};
|
||||
|
||||
@ -1086,7 +1086,7 @@ macro_rules! option {
|
||||
#[macro_export]
|
||||
macro_rules! epsilon {
|
||||
($i:expr,) => {
|
||||
::std::result::Result::Ok(($i, ()))
|
||||
::std::result::Result::Ok(((), $i))
|
||||
};
|
||||
}
|
||||
|
||||
@ -1102,10 +1102,10 @@ macro_rules! epsilon {
|
||||
macro_rules! tap {
|
||||
($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => {
|
||||
match $submac!($i, $($args)*) {
|
||||
::std::result::Result::Ok((i, o)) => {
|
||||
::std::result::Result::Ok((o, i)) => {
|
||||
let $name = o;
|
||||
$e;
|
||||
::std::result::Result::Ok((i, ()))
|
||||
::std::result::Result::Ok(((), i))
|
||||
}
|
||||
::std::result::Result::Err(err) =>
|
||||
::std::result::Result::Err(err),
|
||||
|
@ -37,7 +37,7 @@ pub trait Synom: Sized {
|
||||
|
||||
impl Synom for TokenStream {
|
||||
fn parse(input: Cursor) -> PResult<Self> {
|
||||
Ok((Cursor::empty(), input.token_stream()))
|
||||
Ok((input.token_stream(), Cursor::empty()))
|
||||
}
|
||||
|
||||
fn description() -> Option<&'static str> {
|
||||
|
@ -523,13 +523,13 @@ mod parsing {
|
||||
_ => return parse_error(),
|
||||
}
|
||||
}
|
||||
Ok((tokens, new(T::from_spans(&spans))))
|
||||
Ok((new(T::from_spans(&spans)), tokens))
|
||||
}
|
||||
|
||||
pub fn keyword<'a, T>(keyword: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> {
|
||||
if let Some((span, term, rest)) = tokens.term() {
|
||||
if term.as_str() == keyword {
|
||||
return Ok((rest, new(span)));
|
||||
return Ok((new(span), rest));
|
||||
}
|
||||
}
|
||||
parse_error()
|
||||
@ -555,9 +555,9 @@ mod parsing {
|
||||
|
||||
if let Some((inside, span, rest)) = tokens.group(delim) {
|
||||
match f(inside) {
|
||||
Ok((remaining, ret)) => {
|
||||
Ok((ret, remaining)) => {
|
||||
if remaining.eof() {
|
||||
return Ok((rest, (new(span), ret)));
|
||||
return Ok(((new(span), ret), rest));
|
||||
}
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
|
@ -23,7 +23,7 @@ pub fn delimited(input: Cursor) -> PResult<(MacroDelimiter, TokenStream)> {
|
||||
Delimiter::Bracket => MacroDelimiter::Bracket(Bracket(span)),
|
||||
Delimiter::None => return parse_error(),
|
||||
};
|
||||
Ok((rest, (delimiter, tts)))
|
||||
Ok(((delimiter, tts), rest))
|
||||
}
|
||||
_ => parse_error(),
|
||||
}
|
||||
@ -38,7 +38,7 @@ pub fn braced(input: Cursor) -> PResult<(Brace, TokenStream)> {
|
||||
kind: TokenNode::Group(Delimiter::Brace, tts),
|
||||
},
|
||||
rest,
|
||||
)) => Ok((rest, (Brace(span), tts))),
|
||||
)) => Ok(((Brace(span), tts), rest)),
|
||||
_ => parse_error(),
|
||||
}
|
||||
}
|
||||
@ -52,7 +52,7 @@ pub fn parenthesized(input: Cursor) -> PResult<(Paren, TokenStream)> {
|
||||
kind: TokenNode::Group(Delimiter::Parenthesis, tts),
|
||||
},
|
||||
rest,
|
||||
)) => Ok((rest, (Paren(span), tts))),
|
||||
)) => Ok(((Paren(span), tts), rest)),
|
||||
_ => parse_error(),
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ pub fn syn<T: Synom>(tokens: proc_macro2::TokenStream) -> T {
|
||||
let buf = SynomBuffer::new(tokens);
|
||||
let result = T::parse(buf.begin());
|
||||
match result {
|
||||
Ok((rest, t)) => {
|
||||
Ok((t, rest)) => {
|
||||
if rest.eof() {
|
||||
t
|
||||
} else if rest == buf.begin() {
|
||||
|
@ -119,7 +119,7 @@ fn run_test<T: Into<MetaItem>>(input: &str, expected: T) {
|
||||
let tokens = input.parse::<TokenStream>().unwrap();
|
||||
let buf = SynomBuffer::new(tokens);
|
||||
let attr = match Attribute::parse_outer(buf.begin()) {
|
||||
Ok((rest, e)) => {
|
||||
Ok((e, rest)) => {
|
||||
assert!(rest.eof());
|
||||
e
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user