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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#![allow(dead_code)]
//! Structs related to ASN.1 Compiler

use std::collections::HashMap;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};

use topological_sort::TopologicalSort;

use crate::error::Error;

use crate::parser::asn::structs::module::Asn1Module;

use crate::generator::{Codec, Derive, Generator, Visibility};
use crate::resolver::Resolver;

/// ASN.1 Compiler Struct.
///
/// An application should create a Compiler Structure and will call Public API functions on the
/// compiler.
#[derive(Debug)]
pub struct Asn1Compiler {
    // Modules belonging to this 'invocation' of compiler. Modules are maintined inside a `RefCell`
    // because we need Interior Mutability with the modules (for example to 'resolve' definitions
    // within the modules.
    modules: HashMap<String, Asn1Module>,

    // Holds the 'Resolver' that is used for 'resolv'ing definitions.
    resolver: Resolver,

    // Holds the 'Generator' that is used for 'generate'ing the code for the 'resolved types'.
    generator: Generator,

    // Holds the file name for the output module.
    output_filename: String,

    // Debug Print during Code generation
    debug: bool,
}

impl Default for Asn1Compiler {
    fn default() -> Self {
        Asn1Compiler::new(
            "default.rs",
            false,
            &Visibility::Public,
            vec![Codec::Aper],
            vec![],
        )
    }
}

impl Asn1Compiler {
    /// Create a new Instance of the Compiler structure.
    pub fn new(
        output: &str,
        debug: bool,
        visibility: &Visibility,
        codecs: Vec<Codec>,
        derives: Vec<Derive>,
    ) -> Self {
        Asn1Compiler {
            modules: HashMap::new(),
            resolver: Resolver::new(),
            generator: Generator::new(visibility, codecs, derives), // FIXME: Hard coded
            output_filename: output.to_string(),
            debug,
        }
    }

    /// Add a module to the list of known modules.
    ///
    /// If the module alredy exists, returns `false` else returns `true`.
    pub fn add_module(&mut self, module: Asn1Module) -> bool {
        self.modules
            .insert(module.get_module_name(), module)
            .is_some()
    }

    /// Resolve Modules order and definitions within those modules.
    ///
    /// First Makes sure that all the modules that have IMPORTs are indeed added to us. Then
    /// definitions in each of the modules are 'resolved'. Calls the `Resolver` functions to do
    /// that. Modules are Topologically Sorted before they are resolved and definitions within
    /// modules are topologically sorted as well. This makes Error handling for undefined
    /// definitions much easier.
    // FIXME: Support the case where module is imported by a name different from it's actual name.
    pub fn resolve_modules(&mut self) -> Result<(), Error> {
        self.resolve_imports()?;
        self.resolve_definitions()
    }

    /// Generate the code
    pub fn generate(&mut self) -> Result<(), Error> {
        let input_text = self.generator.generate(&self.resolver)?;
        let output_text = self.rustfmt_generated_code(&input_text)?;

        let mut output_file = File::create(&self.output_filename).map_err(|e| {
            let errorstr = format!("Error {} Creating File: {}", e, self.output_filename);
            Error::CodeGenerationError(errorstr)
        })?;

        output_file
            .write_all(output_text.as_bytes())
            .map_err(|e| Error::CodeGenerationError(e.to_string()))?;

        eprintln!("\n\nWrote generated code to '{}'.", self.output_filename);

        Ok(())
    }

    /// The Actual compilation driver
    pub fn compile_files<T: AsRef<Path>>(&mut self, files: &[T]) -> Result<(), Error> {
        for file in files {
            let file = File::open(file).map_err(|e| io_error!("{:#?}", e))?;
            let mut tokens = crate::tokenizer::tokenize(file)?;
            let mut modules = crate::parser::parse(&mut tokens)?;

            loop {
                let module = modules.pop();
                if module.is_none() {
                    break;
                }
                let module = module.unwrap();
                self.add_module(module);
            }
        }
        self.resolve_modules()?;

        self.generate()
    }

    fn rustfmt_generated_code(&self, code: &str) -> Result<String, Error> {
        let rustfmt_binary = "rustfmt"; // TODO: Get from `env` , 'custom path' etc.
        let mut cmd = Command::new(rustfmt_binary);

        cmd.stdin(Stdio::piped()).stdout(Stdio::piped());

        let mut child = cmd.spawn().map_err(|e| resolve_error!("{:#?}", e))?;
        let mut child_stdin = child.stdin.take().unwrap();
        let mut child_stdout = child.stdout.take().unwrap();

        let code = code.to_owned();
        let stdin_handle =
            ::std::thread::spawn(move || match child_stdin.write_all(code.as_bytes()) {
                Ok(_) => code,
                Err(_) => "write error in rustfmt".to_owned(),
            });

        let mut output = vec![];
        io::copy(&mut child_stdout, &mut output).map_err(|e| resolve_error!("{:#?}", e))?;

        let status = child.wait().map_err(|e| resolve_error!("{:#?}", e))?;

        match String::from_utf8(output) {
            Ok(formatted_output) => match status.code() {
                Some(0) => Ok(formatted_output),
                _ => Err(resolve_error!("`rustfmt` failed to write some bindings.")),
            },
            _ => Ok(stdin_handle.join().unwrap()),
        }
    }

    fn resolve_imports(&self) -> Result<(), Error> {
        for (_, module) in self.modules.iter() {
            for (import, module_name) in module.get_imported_defs() {
                let target = self.modules.get(module_name.name_as_str());
                if target.is_none() {
                    return Err(resolve_error!(
                        "Module '{}', corresponding to definition '{}' not found!",
                        module_name.name_as_str(),
                        import
                    ));
                }
            }
        }
        if self.debug {
            eprintln!("All IMPORTS in All Modules Resolved!");
        }
        Ok(())
    }

    fn sorted_modules(&self) -> Vec<String> {
        let mut ts = TopologicalSort::<String>::new();

        for module in self.modules.values() {
            let imports = module.get_imported_defs();
            for (_, m) in imports {
                ts.add_dependency(m.name(), module.get_module_name())
            }
            ts.insert(module.get_module_name());
        }

        let mut out_vec = vec![];
        loop {
            let popped = ts.pop_all();
            if popped.is_empty() {
                break;
            } else {
                out_vec.extend(popped);
            }
        }
        out_vec
    }

    fn resolve_definitions(&mut self) -> Result<(), Error> {
        let module_names = self.sorted_modules();
        for name in module_names {
            let module = self.modules.get_mut(&name).unwrap();

            //let module_definitions = module.definitions_sorted();
            self.resolver.resolve_definitions(module)?;
        }
        if self.debug {
            eprintln!(
                "Resolved Definitions: {:#?}",
                self.resolver.resolved_defs.keys()
            );
            eprintln!(
                "Parameterized Types: {:#?}",
                self.resolver.parameterized_defs.keys()
            );
            eprintln!("Object Classes: {:#?}", self.resolver.classes.keys());
        }
        Ok(())
    }
}