This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.rs
82 lines (74 loc) · 2.8 KB
/
compiler.rs
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
80
81
82
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::process;
fn main() {
let args: Vec<String> = env::args().collect();
let mut resulting_html = "<!--Compiled using Rohanasantml compiler -->\n".to_string();
if args.len() == 1 {
println!("Error: Please enter input and output file paths.");
process::exit(1);
}
if args.len() == 2 {
println!("Error: Please enter output file path.");
process::exit(1);
}
let file = File::open(&args[1]).unwrap_or_else(|_| {
println!("Error: Please enter valid file path.");
process::exit(1);
});
let mut reader = BufReader::new(file);
let mut line_count = 0;
let mut started = 0; // Count the number of tags that are starting
let mut ended = 0; // Count the number of tags that are ending
loop {
line_count += 1;
let mut line = String::new();
let bytes_read = reader.read_line(&mut line).expect("Failed to read line");
if bytes_read == 0 {
break;
}
line = line.trim().to_string(); // remove white space
let tokens: Vec<&str> = line.split_whitespace().collect(); // tokenize
if line.is_empty() {
continue; // Its an empty line
} else if line.starts_with("//") {
continue; // its a comment
} else if line.starts_with("{") {
if !line.ends_with("}") {
println!(
"Error: Did you forget to end the line {} with a '}}' ?",
line_count
);
process::exit(1);
}
resulting_html += &(line.replace("{", "").replace("}", "") + "\n");
} else if tokens.len() > 1 && tokens[1] == "end" {
resulting_html += &format!("</{}>\n", tokens[0]);
ended += 1;
} else if tokens.len() > 0 && tokens[tokens.len() - 1] == "end" {
let mut joined = String::new();
for token in &tokens[..tokens.len() - 1] {
joined += &format!("{} ", token);
}
resulting_html += &format!("<{}/>\n", joined.trim());
} else {
resulting_html += &format!("<{}>\n", line);
started += 1;
}
}
let mut output_file = File::create(&args[2]).unwrap_or_else(|_| {
println!("Error: Please enter a valid output file path.");
process::exit(1);
});
output_file
.write_all(resulting_html.as_bytes())
.expect("Failed to write to file");
if started > ended {
println!("Warning: Number of tags you created don't have their corresponding ending tags!");
}
if started < ended {
println!("Warning: Number of ending tags are greater than the number of starting tags.");
}
println!("Generated {}", args[2]);
}