This repository was archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathprinter.rs
More file actions
79 lines (66 loc) · 1.9 KB
/
Copy pathprinter.rs
File metadata and controls
79 lines (66 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::cmp;
use std::collections::HashMap;
use crate::lexer::token::Token;
use crate::lexer::token::TokenKind;
/// Prints the tokens as a string
///
/// # Example
///
/// ```
/// use pretty_assertions::assert_str_eq;
/// use php_parser_rs::lexer::Lexer;
/// use php_parser_rs::printer::print;
///
/// let code = r#"
/// <?php
///
/// $a = 1;
/// $b = ['a', 'b', 'c'];
/// $c = "'Hello, World'? 'Hello, World'!";
///
/// __halt_compiler();
/// "#;
///
/// let tokens = Lexer::new().tokenize(code.as_bytes()).unwrap();
///
/// assert_str_eq!(print(&tokens), code);
/// ```
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")
}