|
| 1 | +extern crate sanny_builder_core as sb; |
| 2 | +use sb::dictionary::dictionary_num_by_str::DictNumByStr; |
| 3 | +use sb::dictionary::ffi::*; |
| 4 | +use sb::namespaces::namespaces::Namespaces; |
| 5 | +use std::ffi::CString; |
| 6 | + |
1 | 7 | fn main() {
|
2 | 8 | if let Some(input_file) = std::env::args().nth(1) {
|
3 | 9 | match std::fs::read(input_file) {
|
4 | 10 | Ok(buf) => {
|
5 | 11 | let file_content = String::from_utf8_lossy(&buf);
|
6 | 12 | let mut uniq_opcodes = vec![];
|
7 | 13 | let mut output = vec![];
|
| 14 | + let mut keywords = DictNumByStr::new( |
| 15 | + Duplicates::Replace, |
| 16 | + CaseFormat::NoFormat, |
| 17 | + String::from(";"), |
| 18 | + String::from("=,"), |
| 19 | + true, |
| 20 | + true, |
| 21 | + ); |
| 22 | + let mut classes = Namespaces::new(); |
| 23 | + |
| 24 | + // todo: read from command line |
| 25 | + let has_keywords = keywords.load_file("keywords.txt").is_some(); |
| 26 | + let has_classes = classes.load_classes("classes.db").is_some(); |
8 | 27 |
|
9 | 28 | for line in file_content.lines() {
|
10 | 29 | let line = line.trim();
|
| 30 | + |
| 31 | + // opcode |
11 | 32 | if let Some(':') = line.chars().nth(4) {
|
12 |
| - let opcode = line.get(0..4).unwrap(); |
13 |
| - if !uniq_opcodes.contains(&opcode) { |
14 |
| - uniq_opcodes.push(opcode); |
15 |
| - output.push(line); |
| 33 | + if let Some(opcode) = line.get(0..4) { |
| 34 | + if !uniq_opcodes.contains(&opcode) { |
| 35 | + uniq_opcodes.push(opcode); |
| 36 | + output.push(line); |
| 37 | + continue; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if !has_classes && !has_keywords { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + if let Some(first_word) = line |
| 47 | + .split(|c: char| { |
| 48 | + c.is_ascii_whitespace() || c == '(' || c == ')' || c == ',' |
| 49 | + }) |
| 50 | + .next() |
| 51 | + { |
| 52 | + // keyword |
| 53 | + if has_keywords { |
| 54 | + if let Some(key) = CString::new(first_word).ok() { |
| 55 | + if keywords.map.contains_key(&key) { |
| 56 | + if !uniq_opcodes.contains(&first_word) { |
| 57 | + uniq_opcodes.push(first_word); |
| 58 | + output.push(line); |
| 59 | + continue; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // classes |
| 66 | + if has_classes { |
| 67 | + if first_word.contains('.') { |
| 68 | + let mut class = first_word.split(|c| c == '.'); |
| 69 | + |
| 70 | + if let Some(class_name) = class.next() { |
| 71 | + if let Some(member_name) = class.next() { |
| 72 | + // bug: see https://github.com/sannybuilder/dev/issues/92 |
| 73 | + if classes |
| 74 | + .get_opcode_index_by_name(class_name, member_name) |
| 75 | + .is_some() |
| 76 | + { |
| 77 | + if !uniq_opcodes.contains(&first_word) { |
| 78 | + uniq_opcodes.push(first_word); |
| 79 | + output.push(line); |
| 80 | + continue; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
16 | 86 | }
|
17 | 87 | }
|
18 | 88 | }
|
|
0 commit comments