midenc-session 0.10.0

Session management for the Midenc compiler
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#[cfg(feature = "std")]
use alloc::format;
use alloc::{borrow::Cow, string::String, vec, vec::Vec};
use core::fmt;

use crate::{Path, PathBuf};

#[derive(Clone)]
pub struct FileName {
    name: Cow<'static, str>,
    is_path: bool,
}
impl Eq for FileName {}
impl PartialEq for FileName {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}
impl PartialOrd for FileName {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for FileName {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.name.cmp(&other.name)
    }
}
impl fmt::Debug for FileName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}
impl fmt::Display for FileName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}
impl AsRef<Path> for FileName {
    fn as_ref(&self) -> &Path {
        self.name.as_ref().as_ref()
    }
}
impl From<PathBuf> for FileName {
    fn from(path: PathBuf) -> Self {
        Self {
            name: path.to_string_lossy().into_owned().into(),
            is_path: true,
        }
    }
}
impl From<&'static str> for FileName {
    fn from(name: &'static str) -> Self {
        Self {
            name: Cow::Borrowed(name),
            is_path: false,
        }
    }
}
impl From<String> for FileName {
    fn from(name: String) -> Self {
        Self {
            name: Cow::Owned(name),
            is_path: false,
        }
    }
}
impl AsRef<str> for FileName {
    fn as_ref(&self) -> &str {
        self.name.as_ref()
    }
}
impl FileName {
    pub fn is_path(&self) -> bool {
        self.is_path
    }

    pub fn as_path(&self) -> &Path {
        self.as_ref()
    }

    pub fn as_str(&self) -> &str {
        self.name.as_ref()
    }

    pub fn file_name(&self) -> Option<&str> {
        self.as_path().file_name().and_then(|name| name.to_str())
    }

    pub fn file_stem(&self) -> Option<&str> {
        self.as_path().file_stem().and_then(|name| name.to_str())
    }
}

/// An error that occurs when detecting the file type of an input
#[derive(Debug, thiserror::Error)]
pub enum InvalidInputError {
    /// Occurs if an unsupported file type is given as an input
    #[error("invalid input file '{}': unsupported file type", .0.display())]
    UnsupportedFileType(PathBuf),
    /// We attempted to detecth the file type from the raw bytes, but failed
    #[error("could not detect file type of input")]
    UnrecognizedFileType,
    /// Unable to read input file
    #[cfg(feature = "std")]
    #[error(transparent)]
    Io(#[from] std::io::Error),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputType {
    Real(PathBuf),
    Stdin { name: FileName, input: Vec<u8> },
}

/// This enum represents the types of raw inputs provided to the compiler
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InputFile {
    pub file: InputType,
    file_type: FileType,
}
impl InputFile {
    pub fn new(ty: FileType, file: InputType) -> Self {
        Self {
            file,
            file_type: ty,
        }
    }

    /// Returns an [InputFile] representing an empty WebAssembly module binary
    pub fn empty() -> Self {
        Self {
            file: InputType::Stdin {
                name: "empty".into(),
                input: vec![],
            },
            file_type: FileType::Wasm,
        }
    }

    /// Get an [InputFile] representing the contents of `path`.
    ///
    /// This function returns an error if the contents are not a valid supported file type.
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, InvalidInputError> {
        let path = path.as_ref();
        let file_type = FileType::try_from(path)?;
        Ok(Self {
            file: InputType::Real(path.to_path_buf()),
            file_type,
        })
    }

    /// Get an [InputFile] representing the contents received from standard input.
    ///
    /// This function returns an error if the contents are not a valid supported file type.
    #[cfg(feature = "std")]
    pub fn from_stdin(name: FileName) -> Result<Self, InvalidInputError> {
        use std::io::Read;

        let mut input = Vec::with_capacity(1024);
        std::io::stdin().read_to_end(&mut input)?;
        Self::from_bytes(input, name)
    }

    pub fn from_bytes(bytes: Vec<u8>, name: FileName) -> Result<Self, InvalidInputError> {
        let file_type = FileType::detect(&bytes)?;
        Ok(Self {
            file: InputType::Stdin { name, input: bytes },
            file_type,
        })
    }

    pub fn file_type(&self) -> FileType {
        self.file_type
    }

    pub fn file_name(&self) -> FileName {
        match &self.file {
            InputType::Real(path) => path.clone().into(),
            InputType::Stdin { name, .. } => name.clone(),
        }
    }

    pub fn as_path(&self) -> Option<&Path> {
        match &self.file {
            InputType::Real(path) => Some(path),
            _ => None,
        }
    }

    pub fn is_real(&self) -> bool {
        matches!(self.file, InputType::Real(_))
    }

    pub fn filestem(&self) -> &str {
        match &self.file {
            InputType::Real(path) => path.file_stem().unwrap().to_str().unwrap(),
            InputType::Stdin { .. } => "noname",
        }
    }
}

#[cfg(feature = "std")]
impl clap::builder::ValueParserFactory for InputFile {
    type Parser = InputFileParser;

    fn value_parser() -> Self::Parser {
        InputFileParser
    }
}

#[doc(hidden)]
#[derive(Clone)]
#[cfg(feature = "std")]
pub struct InputFileParser;

#[cfg(feature = "std")]
impl clap::builder::TypedValueParser for InputFileParser {
    type Value = InputFile;

    fn parse_ref(
        &self,
        _cmd: &clap::Command,
        _arg: Option<&clap::Arg>,
        value: &std::ffi::OsStr,
    ) -> Result<Self::Value, clap::error::Error> {
        use clap::error::{Error, ErrorKind};

        let input_file = match value.to_str() {
            Some("-") => InputFile::from_stdin("stdin".into()).map_err(|err| match err {
                InvalidInputError::Io(err) => Error::raw(ErrorKind::Io, err),
                err => Error::raw(ErrorKind::ValueValidation, err),
            })?,
            Some(_) | None => {
                InputFile::from_path(PathBuf::from(value)).map_err(|err| match err {
                    InvalidInputError::Io(err) => Error::raw(ErrorKind::Io, err),
                    err => Error::raw(ErrorKind::ValueValidation, err),
                })?
            }
        };

        match &input_file.file {
            InputType::Real(path) => {
                if path.exists() {
                    if path.is_file() {
                        Ok(input_file)
                    } else {
                        Err(Error::raw(
                            ErrorKind::ValueValidation,
                            format!("invalid input '{}': not a file", path.display()),
                        ))
                    }
                } else {
                    Err(Error::raw(
                        ErrorKind::ValueValidation,
                        format!("invalid input '{}': file does not exist", path.display()),
                    ))
                }
            }
            InputType::Stdin { .. } => Ok(input_file),
        }
    }
}

/// This represents the file types recognized by the compiler
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FileType {
    Hir,
    Masm,
    Masp,
    Rust,
    Toml,
    Wasm,
    Wat,
}

impl fmt::Display for FileType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Hir => f.write_str("hir"),
            Self::Masm => f.write_str("masm"),
            Self::Masp => f.write_str("masp"),
            Self::Rust => f.write_str("rs"),
            Self::Toml => f.write_str("toml"),
            Self::Wasm => f.write_str("wasm"),
            Self::Wat => f.write_str("wat"),
        }
    }
}

impl FileType {
    pub fn detect(bytes: &[u8]) -> Result<Self, InvalidInputError> {
        if bytes.starts_with(b"\0asm") {
            return Ok(FileType::Wasm);
        }

        if bytes.starts_with(b"MASP\0") {
            return Ok(FileType::Masp);
        }

        fn is_rust_top_level_item(line: &str) -> bool {
            line.starts_with("//")
                || line.starts_with("#[")
                || line.starts_with("#![")
                || line.starts_with("pub fn")
                || line.starts_with("fn ")
        }

        fn is_masm_top_level_item(line: &str) -> bool {
            line.starts_with("namespace")
                || line.starts_with("extern package")
                || line.starts_with("pub proc")
                || line.starts_with("proc ")
                || line.starts_with("adv_map")
                || line.starts_with("const ")
                || line.starts_with("begin ")
        }

        fn is_project_toml_item(line: &str) -> bool {
            line.starts_with("[workspace]") || line.starts_with("[package]")
        }

        if let Ok(content) = core::str::from_utf8(bytes) {
            // Skip comment lines and empty lines
            let first_line = content.lines().find(|line| {
                if line.trim().is_empty() {
                    return false;
                }
                if line.starts_with('#') && !(line.starts_with("#[") || line.starts_with("#![")) {
                    return false;
                }
                !line.starts_with(';')
            });
            if let Some(first_line) = first_line {
                if first_line.starts_with("builtin.") {
                    return Ok(FileType::Hir);
                }
                if first_line.starts_with("(module") {
                    return Ok(FileType::Wat);
                }
                if is_rust_top_level_item(first_line) {
                    return Ok(FileType::Rust);
                }
                if is_masm_top_level_item(first_line) {
                    return Ok(FileType::Masm);
                }
                if is_project_toml_item(first_line) {
                    return Ok(FileType::Toml);
                }
            }

            // We could not detect from the first line alone, so fall back to attempting to parse
            // the most common types - a failure is assumed to mean that the input does not match.
            //
            // We try in an order such that the faster checks are performed first - in the case of
            // Rust, we have to actually invoke `rustc` to try and check if the input parses, so
            // it is the slowest. We can't actually attempt to parse HIR here, but we also control
            // its output format, so it is unlikely valid HIR reaches here anyway. We don't attempt
            // to parse WAT here currently - if the input doesn't look like WAT we'd expect, it
            // will get rejected.
            let parsed = miden_assembly_syntax_cst::parse_text(content);
            if !parsed.has_errors() {
                return Ok(FileType::Masm);
            }

            if parses_as_rust(content) {
                return Ok(FileType::Rust);
            }
        }

        Err(InvalidInputError::UnrecognizedFileType)
    }
}

impl TryFrom<&Path> for FileType {
    type Error = InvalidInputError;

    fn try_from(path: &Path) -> Result<Self, Self::Error> {
        match path.extension().and_then(|ext| ext.to_str()) {
            Some("hir") => Ok(FileType::Hir),
            Some("masm") => Ok(FileType::Masm),
            Some("masp") => Ok(FileType::Masp),
            Some("rs") => Ok(FileType::Rust),
            Some("toml") => Ok(FileType::Toml),
            Some("wasm") => Ok(FileType::Wasm),
            Some("wat") => Ok(FileType::Wat),
            _ => Err(InvalidInputError::UnsupportedFileType(path.to_path_buf())),
        }
    }
}

#[cfg(feature = "std")]
fn parses_as_rust(src: &str) -> bool {
    use std::{
        io::Write,
        process::{Command, Stdio},
    };

    let mut command = Command::new("rustc");
    command
        .arg("+nightly")
        .arg("-Zno-analysis")
        .arg("-Zparse-crate-root-only=yes")
        .arg("-")
        .stderr(Stdio::null())
        .stdout(Stdio::null())
        .stdin(Stdio::piped());

    let Ok(mut child) = command.spawn() else {
        return false;
    };

    let Some(mut stdin) = child.stdin.take() else {
        let _ = child.kill();
        return false;
    };

    let Ok(_) = stdin.write_all(src.as_bytes()) else {
        let _ = child.kill();
        return false;
    };

    let Ok(status) = child.wait() else {
        return false;
    };

    status.success()
}

#[cfg(not(feature = "std"))]
fn parses_as_rust(_src: &str) -> bool {
    false
}