feat: regex for keywords, expect() to function as a parser
This commit is contained in:
+51
-5
@@ -8,6 +8,8 @@ use std::io::{BufRead, BufReader};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use plume_log::{PlumeBuilder, success};
|
||||
use regex::Regex;
|
||||
use std::io::BufWriter;
|
||||
use std::io::Write;
|
||||
|
||||
struct SymbolRow {
|
||||
sym: String,
|
||||
@@ -18,16 +20,56 @@ struct SymbolRow {
|
||||
fn build_sym_table(stream: &Vec::<&str>) -> Vec<SymbolRow> {
|
||||
let sym_table: Vec<SymbolRow> = vec![];
|
||||
|
||||
let rx_func = Regex::new(r"int[0-9]+|uint[0-9]+|string|char").unwrap();
|
||||
for token in stream {
|
||||
if rx_func.is_match(token){
|
||||
print!("{token} ");
|
||||
let is_keyword_regex = Regex::new(r"bool|int[0-9]*|uint[0-9]*|string|char").unwrap();
|
||||
|
||||
|
||||
|
||||
for (index, token) in stream.iter().enumerate() {
|
||||
if expect(stream, 0, "use") {
|
||||
println!("found");
|
||||
}
|
||||
// Regex match for keyword
|
||||
if is_keyword_regex.is_match(token){
|
||||
if token == &"uint" {
|
||||
print!("uint16+");
|
||||
continue;
|
||||
}
|
||||
print!("{token}+");
|
||||
continue;
|
||||
}
|
||||
|
||||
print!("{token}+");
|
||||
|
||||
}
|
||||
return sym_table;
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn expect(stream: &Vec::<&str>, index: usize, to_be_expect:&str) -> bool {
|
||||
let found: Option<&&str> = stream.get(index);
|
||||
let Some(s) = found else { todo!()};
|
||||
if *s == to_be_expect {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
fn expect(stream: &Vec::<&str>, index: usize, expect_vector: &Vec::<&str>) -> bool {
|
||||
let found: Option<&&str> = stream.get(index);
|
||||
let Some(s) = found else { todo!()};
|
||||
for expect in expect_vector {
|
||||
if &*s == expect{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
fn main() -> std::io::Result<()>{
|
||||
PlumeBuilder::new()
|
||||
.with_level(log::LevelFilter::Trace)
|
||||
@@ -37,15 +79,19 @@ fn main() -> std::io::Result<()>{
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let file_path = &args[1];
|
||||
let extension = &file_path[file_path.len()-2..file_path.len()];
|
||||
// let new_file_name = format!("{}_tmp.sr", &file_path[..file_path.len() - 2]);
|
||||
|
||||
if extension != "sr" {
|
||||
error!("File is not in .sr");
|
||||
std::process::exit(-1);
|
||||
}
|
||||
info!("File: {}", file_path);
|
||||
// info!("File: {} | Copied To: {}", file_path, new_file_name);
|
||||
|
||||
let content = fs::read_to_string(file_path)
|
||||
.expect("Should've read the file");
|
||||
|
||||
let re = Regex::new(r"[ \n\t]+").unwrap();
|
||||
let re = Regex::new(r"[ \t]+").unwrap();
|
||||
let tokens: Vec<&str> = re.split(&content).filter(|s| !s.is_empty()).collect();
|
||||
build_sym_table(&tokens);
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user