use std::cmp;
use std::collections::HashMap;
use crate::lexer::token::Token;
use crate::lexer::token::TokenKind;
pub fn print(tokens: &[Token]) -> String {
let mut lines: HashMap<usize, Vec<&Token>> = HashMap::new();
let mut max_line = 0;
for token in tokens {
lines.entry(token.span.line).or_default().push(token);
max_line = cmp::max(max_line, token.span.line);
}
let mut output = vec![];
let mut last = 0;
for line in 1..=max_line {
if line < last {
continue;
}
last = line;
let representation = match lines.get(&line) {
Some(tokens) => {
let mut representation = "".to_owned();
for token in tokens {
if token.kind == TokenKind::Eof {
break;
}
let repeat = token.span.column - representation.len() - 1;
representation.push_str(&" ".repeat(repeat));
representation.push_str(&token.value.to_string());
}
let mut result = vec![];
let lines = representation.lines();
last += lines.clone().count();
for line in lines {
result.push(line);
}
result.join("\n")
}
None => "".to_owned(),
};
output.push(representation);
}
output.join("\n")
}