libasm/
lib.rs

1extern crate cc;
2extern crate proc_macro2;
3extern crate syn;
4
5use std::path::{Path, PathBuf};
6use syn::Item;
7
8mod generate;
9mod extract;
10use extract::extract_asm;
11
12use std::fs::{read_dir, File};
13use std::io::Read;
14use std::env;
15
16#[macro_export]
17macro_rules! lasm {
18    ($abi:tt fn $name:tt {
19        $($dontcare:tt)*
20    } $($more:tt)*) => (lasm!($($more)*););
21
22    () => {};
23}
24
25#[derive(Clone, Debug, PartialEq)]
26pub struct Asm {
27    pub name: String,
28    pub body: Vec<String>,
29}
30
31pub fn parse_file(code: String) {
32    let syntax = syn::parse_file(&code).expect("Unable to parse file");
33    for item in syntax.items {
34        match item {
35            Item::Macro(macro_item) => {
36                let mac = macro_item.mac;
37                if mac.path == "lasm".into() {
38                    extract_asm(mac.tts);
39                }
40            }
41            _ => {}
42        }
43    }
44}
45
46pub fn parse_dir(dir: &Path) {
47    for item in read_dir(dir).unwrap() {
48        match item {
49            Ok(item) => {
50                let item_type = item.file_type().unwrap();
51                if item_type.is_dir() {
52                    parse_dir(&item.path())
53                } else if item.path().to_str().unwrap().ends_with(".rs") {
54                    let mut file = File::open(item.path()).unwrap();
55                    let mut content = String::new();
56                    file.read_to_string(&mut content).unwrap();
57                    parse_file(content);
58                }
59            }
60            _ => println!("cargo:warning=unable to read all source files"),
61        }
62    }
63}
64
65pub fn parse() {
66    let dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("src");
67    parse_dir(&dir);
68}
69
70#[cfg(test)]
71mod tests {
72    #[test]
73    fn it_works() {
74        assert_eq!(2 + 2, 4);
75    }
76}