implement meta token replacement

This commit is contained in:
2025-08-24 20:54:20 +02:00
parent ddba3423df
commit 015de5dc0a
7 changed files with 128 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
use crate::tokenizer::Token;
use regex::{Captures, Regex};
use regex::{Captures, Match, Regex};
use toml::{Table, Value};
// MetaRules
@@ -8,7 +8,7 @@ pub struct MetaRules {
replacement_rules: Vec<(String, (String, String))>,
interpolation_rules: Vec<(String, (String, String))>,
token_rules: Vec<(String, String)>,
special_tokens: Vec<Token>,
pub special_tokens: Vec<(String, Token)>,
}
// Implementation of MetaRules
@@ -24,7 +24,7 @@ impl MetaRules {
let mut replacements: Vec<(String, (String, String))> = vec![];
let mut interpolation: Vec<(String, (String, String))> = vec![];
let mut meta_token_rules: Vec<(String, String)> = vec![];
let meta_tokens: Vec<Token> = vec![];
let meta_tokens: Vec<(String, Token)> = vec![];
let configuration = gtoml::parse(configuration_content.as_str())
.expect("[ERROR] TOML invalid in preprocessor!");
let configuration_unpacked: Table = Table::try_from(configuration).unwrap();
@@ -125,22 +125,15 @@ impl MetaRules {
println!("[INFO] Applying rule {}", rule.0);
let base_pattern: Regex = Regex::new((rule.1 .0).as_str()).unwrap();
let processed_code_replacement = processed_code.clone();
let parameter = if &base_pattern
.captures(processed_code_replacement.as_str())
.unwrap()
.len()
> 0
{
&base_pattern
.captures(processed_code_replacement.as_str())
.unwrap()[0]
} else {
&base_pattern
.captures(processed_code_replacement.as_str())
.unwrap()
let captures: Option<Captures> =
base_pattern.captures(processed_code_replacement.as_str());
let directive: String;
match captures {
Some(n) => directive = n.get(0).map_or("", |m| m.as_str()).to_string(),
None => continue,
};
let command: &str = &base_pattern.replace(parameter, rule.1 .1.as_str());
println!("{:?}", &command);
let command: &str = &base_pattern.replace(directive.as_str(), rule.1 .1.as_str());
let subprocess = std::process::Command::new("/bin/bash")
.arg("-c")
.arg(String::from("echo \"$(") + command + ")\"")
@@ -154,6 +147,43 @@ impl MetaRules {
.to_string();
}
for token_style in self.token_rules.iter() {
println!("[INFO] Searching meta tokens of style {}", token_style.0);
// Search all occurrences
let token_pattern: Regex =
Regex::new(token_style.1.as_str()).expect("Could not assign pattern.");
let match_list: Match;
match_list = match token_pattern.find(processed_code.as_str()) {
Some(n) => n,
None => continue,
};
// Create id for each occurrence
let meta_id: String = String::from("meta_token_")
+ match_list.start().to_string().as_str()
+ "__"
+ match_list.end().to_string().as_str();
// Replace token by id
let meta_value: String = match_list.as_str().to_string();
let value_regex: Regex =
Regex::new(meta_value.as_str()).expect("Could not create pattern.");
processed_code = value_regex
.replace(processed_code.as_str(), meta_id.as_str())
.to_string();
println!("Replace {} with {}.", meta_value, meta_id);
// Safe id and token
self.special_tokens.push((
meta_id,
Token {
token: meta_value,
token_type: crate::TokenType::IDENTIFIER,
},
));
}
return processed_code;
}
}