use super::parser::Parser;
use super::scanner::Res;
use super::token::{Token, TokenType};
fn join_comment(target: &mut Vec<u8>, extra: &[u8]) {
if extra.is_empty() {
return;
}
if !target.is_empty() {
target.push(b'\n');
}
target.extend_from_slice(extra);
}
impl Parser {
pub(super) fn peek_token(&mut self) -> Res<Token> {
if !self.token_available {
self.fetch_more_tokens()?;
}
let token = self.tokens[self.tokens_head].clone();
self.unfold_comments(&token);
Ok(token)
}
pub(super) fn unfold_comments(&mut self, token: &Token) {
while self.comments_head < self.comments.len()
&& token.start_mark.index >= self.comments[self.comments_head].token_mark.index
{
let comment = std::mem::take(&mut self.comments[self.comments_head]);
if !comment.head.is_empty() {
if token.typ == TokenType::BlockEnd {
self.comments[self.comments_head] = comment;
break;
}
join_comment(&mut self.head_comment, &comment.head);
}
join_comment(&mut self.foot_comment, &comment.foot);
join_comment(&mut self.line_comment, &comment.line);
self.comments_head += 1;
}
}
pub(super) fn skip_token(&mut self) {
self.token_available = false;
self.tokens_parsed += 1;
self.stream_end_produced = self.tokens[self.tokens_head].typ == TokenType::StreamEnd;
self.tokens_head += 1;
}
}