-
I am trying to figure out how I could get the position of the next unparsed character in the input if there is remaining text in the input. E.g.: Let's consider the following grammar:
And let's consider the following input is read from the network: ts: 4533, id: 1, data: 55467 ts: 4537, id: 3, data: 55241 ts: 4601, id: 2, da With my naive understanding I now would go ahead and parse in the following manner: pub fn parse_packet (packet: &str) -> Result<NetMessage, ParsePacketError> {
let when = Local::now ();
let mut ts: u32 = 0;
let mut id: u32 = 0;
let mut data: u32 = 0;
match NetParser::parse (Rule::NetMessage, packet) {
Ok (parse) => {
for pair in parse.into_iter () {
match pair.as_rule () {
Rule::NetMessage => {
for inner in pair.into_inner ().into_iter () {
match inner.as_rule () {
Rule::TS => ts = inner.as_str ().parse ().unwrap (),
Rule::ID => id = inner.as_str ().parse ().unwrap (),
Rule::DATA => data = inner.as_str ().parse ().unwrap (),
_ => unreachable! (),
};
}
}
_ => unreachable! (),
}
}
Ok (NetMessage {
id: id as u8,
value: data,
ts,
when,
})
}
Err (err) => Err (ParsePacketError::NotParsable (err)),
}
} Now I have the problem that I don't understand how to get the last token and its end position. The loop consumes the pairs so I cannot go ahead and use I must be overlooking something, I feel I misunderstand some concept but I couldn't find anything in the book. I found in the API that the tokens store the position and allow public usage of it with There must exist some solution to this, can anyone enlighten me? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I wasn't paying attention well enough - of course I can access the position, by reading the span with |
Beta Was this translation helpful? Give feedback.
I wasn't paying attention well enough - of course I can access the position, by reading the span with
pair.as_span().end()
.