2020-04-08 16:18:18 -04:00
|
|
|
// TAME compiler
|
|
|
|
//
|
2023-01-17 23:09:25 -05:00
|
|
|
// Copyright (C) 2014-2023 Ryan Specialty, LLC.
|
2020-04-08 16:18:18 -04:00
|
|
|
//
|
|
|
|
// This file is part of TAME.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
tamer: Integrate clippy
This invokes clippy as part of `make check` now, which I had previously
avoided doing (I'll elaborate on that below).
This commit represents the changes needed to resolve all the warnings
presented by clippy. Many changes have been made where I find the lints to
be useful and agreeable, but there are a number of lints, rationalized in
`src/lib.rs`, where I found the lints to be disagreeable. I have provided
rationale, primarily for those wondering why I desire to deviate from the
default lints, though it does feel backward to rationalize why certain lints
ought to be applied (the reverse should be true).
With that said, this did catch some legitimage issues, and it was also
helpful in getting some older code up-to-date with new language additions
that perhaps I used in new code but hadn't gone back and updated old code
for. My goal was to get clippy working without errors so that, in the
future, when others get into TAMER and are still getting used to Rust,
clippy is able to help guide them in the right direction.
One of the reasons I went without clippy for so long (though I admittedly
forgot I wasn't using it for a period of time) was because there were a
number of suggestions that I found disagreeable, and I didn't take the time
to go through them and determine what I wanted to follow. Furthermore, it
was hard to make that judgment when I was new to the language and lacked
the necessary experience to do so.
One thing I would like to comment further on is the use of `format!` with
`expect`, which is also what the diagnostic system convenience methods
do (which clippy does not cover). Because of all the work I've done trying
to understand Rust and looking at disassemblies and seeing what it
optimizes, I falsely assumed that Rust would convert such things into
conditionals in my otherwise-pure code...but apparently that's not the case,
when `format!` is involved.
I noticed that, after making the suggested fix with `get_ident`, Rust
proceeded to then inline it into each call site and then apply further
optimizations. It was also previously invoking the thread lock (for the
interner) unconditionally and invoking the `Display` implementation. That
is not at all what I intended for, despite knowing the eager semantics of
function calls in Rust.
Anyway, possibly more to come on that, I'm just tired of typing and need to
move on. I'll be returning to investigate further diagnostic messages soon.
2023-01-12 10:46:48 -05:00
|
|
|
// Use your judgment;
|
|
|
|
// a `match` may be more clear within a given context.
|
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2022-04-13 15:11:29 -04:00
|
|
|
//! This is the TAME compiler.
|
|
|
|
//!
|
|
|
|
//! `tamec` compiles source code into object files that are later linked
|
|
|
|
//! into a final executable using [`tameld`](../tameld).
|
2020-04-08 16:18:18 -04:00
|
|
|
|
|
|
|
extern crate tamer;
|
|
|
|
|
|
|
|
use getopts::{Fail, Options};
|
2022-04-13 15:11:29 -04:00
|
|
|
use std::{
|
2023-02-21 16:40:36 -05:00
|
|
|
convert::Infallible,
|
2022-04-13 15:11:29 -04:00
|
|
|
env,
|
|
|
|
error::Error,
|
tamer: Introduce NIR (accepting only)
This introduces NIR, but only as an accepting grammar; it doesn't yet emit
the NIR IR, beyond TODOs.
This modifies `tamec` to, while copying XIR, also attempt to lower NIR to
produce parser errors, if any. It does not yet fail compilation, as I just
want to be cautious and observe that everything's working properly for a
little while as people use it, before I potentially break builds.
This is the culmination of months of supporting effort. The NIR grammar is
derived from our existing TAME sources internally, which I use for now as a
test case until I introduce test cases directly into TAMER later on (I'd do
it now, if I hadn't spent so much time on this; I'll start introducing tests
as I begin emitting NIR tokens). This is capable of fully parsing our
largest system with >900 packages, as well as `core`.
`tamec`'s lowering is a mess; that'll be cleaned up in future commits. The
same can be said about `tameld`.
NIR's grammar has some initial documentation, but this will improve over
time as well.
The generated docs still need some improvement, too, especially with
generated identifiers; I just want to get this out here for testing.
DEV-7145
2022-08-29 15:28:03 -04:00
|
|
|
fmt::{self, Display, Write},
|
2022-10-19 14:38:45 -04:00
|
|
|
fs::{self, File},
|
|
|
|
io::{self, BufReader, BufWriter},
|
2022-04-13 15:11:29 -04:00
|
|
|
path::Path,
|
|
|
|
};
|
|
|
|
use tamer::{
|
2023-05-26 11:45:27 -04:00
|
|
|
asg::{air::Air, AsgError, DefaultAsg},
|
2022-04-20 12:08:46 -04:00
|
|
|
diagnose::{
|
|
|
|
AnnotatedSpan, Diagnostic, FsSpanResolver, Reporter, VisualReporter,
|
|
|
|
},
|
2023-05-26 11:45:27 -04:00
|
|
|
nir::{InterpError, Nir, NirToAirError, XirfToNirError},
|
2023-05-26 23:25:08 -04:00
|
|
|
parse::{lowerable, FinalizeError, ParseError, Token, UnknownToken},
|
2023-05-26 11:45:27 -04:00
|
|
|
pipeline::parse_package_xml,
|
tamer: Introduce NIR (accepting only)
This introduces NIR, but only as an accepting grammar; it doesn't yet emit
the NIR IR, beyond TODOs.
This modifies `tamec` to, while copying XIR, also attempt to lower NIR to
produce parser errors, if any. It does not yet fail compilation, as I just
want to be cautious and observe that everything's working properly for a
little while as people use it, before I potentially break builds.
This is the culmination of months of supporting effort. The NIR grammar is
derived from our existing TAME sources internally, which I use for now as a
test case until I introduce test cases directly into TAMER later on (I'd do
it now, if I hadn't spent so much time on this; I'll start introducing tests
as I begin emitting NIR tokens). This is capable of fully parsing our
largest system with >900 packages, as well as `core`.
`tamec`'s lowering is a mess; that'll be cleaned up in future commits. The
same can be said about `tameld`.
NIR's grammar has some initial documentation, but this will improve over
time as well.
The generated docs still need some improvement, too, especially with
generated identifiers; I just want to get this out here for testing.
DEV-7145
2022-08-29 15:28:03 -04:00
|
|
|
xir::{
|
|
|
|
self,
|
2023-05-26 11:45:27 -04:00
|
|
|
flat::{RefinedText, XirToXirfError, XirfToken},
|
2022-10-19 14:38:45 -04:00
|
|
|
reader::XmlXirReader,
|
2023-05-26 11:45:27 -04:00
|
|
|
DefaultEscaper, Token as XirToken,
|
tamer: Introduce NIR (accepting only)
This introduces NIR, but only as an accepting grammar; it doesn't yet emit
the NIR IR, beyond TODOs.
This modifies `tamec` to, while copying XIR, also attempt to lower NIR to
produce parser errors, if any. It does not yet fail compilation, as I just
want to be cautious and observe that everything's working properly for a
little while as people use it, before I potentially break builds.
This is the culmination of months of supporting effort. The NIR grammar is
derived from our existing TAME sources internally, which I use for now as a
test case until I introduce test cases directly into TAMER later on (I'd do
it now, if I hadn't spent so much time on this; I'll start introducing tests
as I begin emitting NIR tokens). This is capable of fully parsing our
largest system with >900 packages, as well as `core`.
`tamec`'s lowering is a mess; that'll be cleaned up in future commits. The
same can be said about `tameld`.
NIR's grammar has some initial documentation, but this will improve over
time as well.
The generated docs still need some improvement, too, especially with
generated identifiers; I just want to get this out here for testing.
DEV-7145
2022-08-29 15:28:03 -04:00
|
|
|
},
|
2022-04-13 15:11:29 -04:00
|
|
|
};
|
tamer: Initial frontend concept
This introduces the beginnings of frontends for TAMER, gated behind a
`wip-features` flag.
This will be introduced in stages:
1. Replace the existing copy with a parser-based copy (echo back out the
tokens), when the flag is on.
2. Begin to parse portions of the source, augmenting the output xmlo (xmli
at the moment). The XSLT-based compiler will be modified to skip
compilation steps as necessary.
As portions of the compilation are implemented in TAMER, they'll be placed
behind their own feature flags and stabalized, which will incrementally
remove the compilation steps from the XSLT-based system. The result should
be substantial incremental performance improvements.
Short-term, the priorities are for loading identifiers into an IR
are (though the order may change):
1. Echo
2. Imports
3. Extern declarations.
4. Simple identifiers (e.g. param, const, template, etc).
5. Classifications.
6. Documentation expressions.
7. Calculation expressions.
8. Template applications.
9. Template definitions.
10. Inline templates.
After each of those are done, the resulting xmlo (xmli) will have fully
reconstructed the source document from the IR produced during parsing.
2021-07-23 22:24:08 -04:00
|
|
|
|
2020-04-08 16:18:18 -04:00
|
|
|
/// Types of commands
|
|
|
|
enum Command {
|
|
|
|
Compile(String, String, String),
|
|
|
|
Usage,
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:38:45 -04:00
|
|
|
/// Create a [`XmlXirReader`] for a source file.
|
|
|
|
///
|
|
|
|
/// The provided escaper must be shared between all readers and writers in
|
|
|
|
/// order to benefit from its caching.
|
|
|
|
fn src_reader<'a>(
|
|
|
|
input: &'a String,
|
|
|
|
escaper: &'a DefaultEscaper,
|
2022-10-26 11:29:26 -04:00
|
|
|
) -> Result<XmlXirReader<'a, BufReader<File>>, UnrecoverableError> {
|
2022-10-19 14:38:45 -04:00
|
|
|
use tamer::fs::{File, PathFile};
|
|
|
|
|
|
|
|
let source = Path::new(input);
|
|
|
|
|
|
|
|
let PathFile(_, file, ctx): PathFile<BufReader<fs::File>> =
|
|
|
|
PathFile::open(source)?;
|
|
|
|
|
|
|
|
Ok(XmlXirReader::new(file, escaper, ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write each parsed token to the provided buffer.
|
|
|
|
///
|
|
|
|
/// This is intended to be a temporary function that exists during a
|
|
|
|
/// transition period between the XSLT-based TAME and TAMER.
|
|
|
|
/// Writing XIR proves that the source file is being successfully parsed and
|
|
|
|
/// helps to evaluate system performance.
|
2023-02-01 13:03:23 -05:00
|
|
|
#[cfg(not(feature = "wip-asg-derived-xmli"))]
|
2022-10-19 14:38:45 -04:00
|
|
|
fn copy_xml_to<'e, W: io::Write + 'e>(
|
|
|
|
mut fout: W,
|
|
|
|
escaper: &'e DefaultEscaper,
|
2023-05-26 11:45:27 -04:00
|
|
|
) -> impl FnMut(&Result<XirToken, tamer::xir::Error>) + 'e {
|
2023-02-21 15:10:13 -05:00
|
|
|
use tamer::xir::writer::XmlWriter;
|
2023-02-01 13:03:23 -05:00
|
|
|
|
2022-10-19 14:38:45 -04:00
|
|
|
let mut xmlwriter = Default::default();
|
|
|
|
|
|
|
|
move |tok_result| match tok_result {
|
2023-02-21 15:10:13 -05:00
|
|
|
Ok(tok) => {
|
2022-10-19 14:38:45 -04:00
|
|
|
xmlwriter = tok.write(&mut fout, xmlwriter, escaper).unwrap();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:58:05 -04:00
|
|
|
/// Compile a source file,
|
|
|
|
/// writing to the provided destination path.
|
|
|
|
///
|
|
|
|
/// NB: Output is presently a _copy_ of the input,
|
|
|
|
/// with formatting partially removed.
|
|
|
|
fn compile<R: Reporter>(
|
|
|
|
src_path: &String,
|
|
|
|
dest_path: &String,
|
2022-10-25 23:44:53 -04:00
|
|
|
reporter: &mut R,
|
2022-10-26 11:29:26 -04:00
|
|
|
) -> Result<(), UnrecoverableError> {
|
2022-10-19 14:58:05 -04:00
|
|
|
let dest = Path::new(&dest_path);
|
2023-02-01 13:03:23 -05:00
|
|
|
#[allow(unused_mut)] // wip-asg-derived-xmli
|
|
|
|
let mut fout = BufWriter::new(fs::File::create(dest)?);
|
2022-10-19 14:58:05 -04:00
|
|
|
|
|
|
|
let escaper = DefaultEscaper::default();
|
|
|
|
|
2022-10-25 23:44:53 -04:00
|
|
|
let mut ebuf = String::new();
|
2022-10-19 14:58:05 -04:00
|
|
|
|
2023-06-01 15:26:14 -04:00
|
|
|
let report_err = |result: Result<(), RecoverableError>| {
|
|
|
|
result.or_else(|e| {
|
|
|
|
// See below note about buffering.
|
|
|
|
ebuf.clear();
|
|
|
|
writeln!(ebuf, "{}", reporter.render(&e))?;
|
|
|
|
println!("{ebuf}");
|
|
|
|
|
|
|
|
Ok::<_, UnrecoverableError>(())
|
|
|
|
})
|
2023-05-26 11:45:27 -04:00
|
|
|
};
|
2022-10-20 15:38:59 -04:00
|
|
|
|
2022-10-25 23:35:57 -04:00
|
|
|
// TODO: We're just echoing back out XIR,
|
|
|
|
// which will be the same sans some formatting.
|
2023-02-21 15:10:13 -05:00
|
|
|
let src = &mut lowerable(src_reader(src_path, &escaper)?.inspect({
|
|
|
|
#[cfg(not(feature = "wip-asg-derived-xmli"))]
|
|
|
|
{
|
|
|
|
copy_xml_to(fout, &escaper)
|
|
|
|
}
|
|
|
|
#[cfg(feature = "wip-asg-derived-xmli")]
|
|
|
|
{
|
|
|
|
|_| ()
|
|
|
|
}
|
2023-05-26 11:45:27 -04:00
|
|
|
}));
|
2022-10-25 23:35:57 -04:00
|
|
|
|
2022-12-13 13:46:31 -05:00
|
|
|
// TODO: Determine a good default capacity once we have this populated
|
|
|
|
// and can come up with some heuristics.
|
2023-05-26 11:45:27 -04:00
|
|
|
let air_ctx = parse_package_xml(
|
|
|
|
src,
|
|
|
|
DefaultAsg::with_capacity(1024, 2048).into(),
|
|
|
|
report_err,
|
|
|
|
)?;
|
2022-10-19 14:58:05 -04:00
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
match reporter.has_errors() {
|
2023-02-01 13:03:23 -05:00
|
|
|
false => {
|
|
|
|
#[cfg(feature = "wip-asg-derived-xmli")]
|
|
|
|
{
|
2023-05-19 13:02:07 -04:00
|
|
|
let asg = air_ctx.finish();
|
2023-02-21 16:40:36 -05:00
|
|
|
derive_xmli(asg, fout, &escaper)
|
2023-02-01 13:03:23 -05:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "wip-asg-derived-xmli"))]
|
|
|
|
{
|
2023-05-19 13:02:07 -04:00
|
|
|
let _ = air_ctx; // unused_variables
|
2023-02-01 13:03:23 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
true => Err(UnrecoverableError::ErrorsDuringLowering(
|
2022-10-21 15:35:27 -04:00
|
|
|
reporter.error_count(),
|
2022-10-26 11:29:26 -04:00
|
|
|
)),
|
2022-10-19 14:58:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:03:23 -05:00
|
|
|
/// Derive an `xmli` file from the ASG.
|
|
|
|
///
|
|
|
|
/// The `xmli` file is an intermediate file that allows us to incrementally
|
|
|
|
/// transition responsibilities away from the old XSLT-based compiler and
|
|
|
|
/// into TAMER.
|
|
|
|
/// It will represent a program that is derived from the program the user
|
|
|
|
/// originally defined,
|
|
|
|
/// and must be an equivalent program,
|
|
|
|
/// but will look different;
|
|
|
|
/// TAMER reasons about the system using a different paradigm.
|
|
|
|
#[cfg(feature = "wip-asg-derived-xmli")]
|
|
|
|
fn derive_xmli(
|
2023-02-21 23:58:58 -05:00
|
|
|
asg: tamer::asg::Asg,
|
2023-02-01 13:03:23 -05:00
|
|
|
mut fout: impl std::io::Write,
|
2023-02-21 16:40:36 -05:00
|
|
|
escaper: &DefaultEscaper,
|
2023-02-01 13:03:23 -05:00
|
|
|
) -> Result<(), UnrecoverableError> {
|
2023-02-21 16:40:36 -05:00
|
|
|
use tamer::{
|
2023-05-26 23:25:08 -04:00
|
|
|
asg::visit::tree_reconstruction, pipeline, xir::writer::XmlWriter,
|
2023-02-21 16:40:36 -05:00
|
|
|
};
|
|
|
|
|
2023-05-26 23:25:08 -04:00
|
|
|
let src = lowerable(tree_reconstruction(&asg).map(Ok));
|
2023-02-01 13:03:23 -05:00
|
|
|
|
2023-05-26 23:25:08 -04:00
|
|
|
// TODO: Remove bad file?
|
|
|
|
// Let make do it?
|
|
|
|
pipeline::lower_xmli(src, &asg, |st, tok| {
|
|
|
|
tok.write(&mut fout, st, escaper).map_err(Into::into)
|
|
|
|
})
|
2023-02-01 13:03:23 -05:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:18:18 -04:00
|
|
|
/// Entrypoint for the compiler
|
2022-10-26 11:29:26 -04:00
|
|
|
pub fn main() -> Result<(), UnrecoverableError> {
|
2020-04-08 16:18:18 -04:00
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let program = &args[0];
|
|
|
|
let opts = get_opts();
|
tamer: Integrate clippy
This invokes clippy as part of `make check` now, which I had previously
avoided doing (I'll elaborate on that below).
This commit represents the changes needed to resolve all the warnings
presented by clippy. Many changes have been made where I find the lints to
be useful and agreeable, but there are a number of lints, rationalized in
`src/lib.rs`, where I found the lints to be disagreeable. I have provided
rationale, primarily for those wondering why I desire to deviate from the
default lints, though it does feel backward to rationalize why certain lints
ought to be applied (the reverse should be true).
With that said, this did catch some legitimage issues, and it was also
helpful in getting some older code up-to-date with new language additions
that perhaps I used in new code but hadn't gone back and updated old code
for. My goal was to get clippy working without errors so that, in the
future, when others get into TAMER and are still getting used to Rust,
clippy is able to help guide them in the right direction.
One of the reasons I went without clippy for so long (though I admittedly
forgot I wasn't using it for a period of time) was because there were a
number of suggestions that I found disagreeable, and I didn't take the time
to go through them and determine what I wanted to follow. Furthermore, it
was hard to make that judgment when I was new to the language and lacked
the necessary experience to do so.
One thing I would like to comment further on is the use of `format!` with
`expect`, which is also what the diagnostic system convenience methods
do (which clippy does not cover). Because of all the work I've done trying
to understand Rust and looking at disassemblies and seeing what it
optimizes, I falsely assumed that Rust would convert such things into
conditionals in my otherwise-pure code...but apparently that's not the case,
when `format!` is involved.
I noticed that, after making the suggested fix with `get_ident`, Rust
proceeded to then inline it into each call site and then apply further
optimizations. It was also previously invoking the thread lock (for the
interner) unconditionally and invoking the `Display` implementation. That
is not at all what I intended for, despite knowing the eager semantics of
function calls in Rust.
Anyway, possibly more to come on that, I'm just tired of typing and need to
move on. I'll be returning to investigate further diagnostic messages soon.
2023-01-12 10:46:48 -05:00
|
|
|
let usage = opts.usage(&format!("Usage: {program} [OPTIONS] INPUT"));
|
2020-04-08 16:18:18 -04:00
|
|
|
|
|
|
|
match parse_options(opts, args) {
|
2022-10-19 14:58:05 -04:00
|
|
|
Ok(Command::Compile(src_path, _, dest_path)) => {
|
2022-10-25 23:44:53 -04:00
|
|
|
let mut reporter = VisualReporter::new(FsSpanResolver);
|
2020-04-08 16:18:18 -04:00
|
|
|
|
tamer: Integrate clippy
This invokes clippy as part of `make check` now, which I had previously
avoided doing (I'll elaborate on that below).
This commit represents the changes needed to resolve all the warnings
presented by clippy. Many changes have been made where I find the lints to
be useful and agreeable, but there are a number of lints, rationalized in
`src/lib.rs`, where I found the lints to be disagreeable. I have provided
rationale, primarily for those wondering why I desire to deviate from the
default lints, though it does feel backward to rationalize why certain lints
ought to be applied (the reverse should be true).
With that said, this did catch some legitimage issues, and it was also
helpful in getting some older code up-to-date with new language additions
that perhaps I used in new code but hadn't gone back and updated old code
for. My goal was to get clippy working without errors so that, in the
future, when others get into TAMER and are still getting used to Rust,
clippy is able to help guide them in the right direction.
One of the reasons I went without clippy for so long (though I admittedly
forgot I wasn't using it for a period of time) was because there were a
number of suggestions that I found disagreeable, and I didn't take the time
to go through them and determine what I wanted to follow. Furthermore, it
was hard to make that judgment when I was new to the language and lacked
the necessary experience to do so.
One thing I would like to comment further on is the use of `format!` with
`expect`, which is also what the diagnostic system convenience methods
do (which clippy does not cover). Because of all the work I've done trying
to understand Rust and looking at disassemblies and seeing what it
optimizes, I falsely assumed that Rust would convert such things into
conditionals in my otherwise-pure code...but apparently that's not the case,
when `format!` is involved.
I noticed that, after making the suggested fix with `get_ident`, Rust
proceeded to then inline it into each call site and then apply further
optimizations. It was also previously invoking the thread lock (for the
interner) unconditionally and invoking the `Display` implementation. That
is not at all what I intended for, despite knowing the eager semantics of
function calls in Rust.
Anyway, possibly more to come on that, I'm just tired of typing and need to
move on. I'll be returning to investigate further diagnostic messages soon.
2023-01-12 10:46:48 -05:00
|
|
|
compile(&src_path, &dest_path, &mut reporter).map_err(
|
2022-10-26 11:29:26 -04:00
|
|
|
|e: UnrecoverableError| {
|
|
|
|
// Rendering to a string ensures buffering so that we
|
|
|
|
// don't interleave output between processes.
|
2022-04-27 14:58:50 -04:00
|
|
|
let report = reporter.render(&e).to_string();
|
2022-10-19 14:58:05 -04:00
|
|
|
println!(
|
tamer: Integrate clippy
This invokes clippy as part of `make check` now, which I had previously
avoided doing (I'll elaborate on that below).
This commit represents the changes needed to resolve all the warnings
presented by clippy. Many changes have been made where I find the lints to
be useful and agreeable, but there are a number of lints, rationalized in
`src/lib.rs`, where I found the lints to be disagreeable. I have provided
rationale, primarily for those wondering why I desire to deviate from the
default lints, though it does feel backward to rationalize why certain lints
ought to be applied (the reverse should be true).
With that said, this did catch some legitimage issues, and it was also
helpful in getting some older code up-to-date with new language additions
that perhaps I used in new code but hadn't gone back and updated old code
for. My goal was to get clippy working without errors so that, in the
future, when others get into TAMER and are still getting used to Rust,
clippy is able to help guide them in the right direction.
One of the reasons I went without clippy for so long (though I admittedly
forgot I wasn't using it for a period of time) was because there were a
number of suggestions that I found disagreeable, and I didn't take the time
to go through them and determine what I wanted to follow. Furthermore, it
was hard to make that judgment when I was new to the language and lacked
the necessary experience to do so.
One thing I would like to comment further on is the use of `format!` with
`expect`, which is also what the diagnostic system convenience methods
do (which clippy does not cover). Because of all the work I've done trying
to understand Rust and looking at disassemblies and seeing what it
optimizes, I falsely assumed that Rust would convert such things into
conditionals in my otherwise-pure code...but apparently that's not the case,
when `format!` is involved.
I noticed that, after making the suggested fix with `get_ident`, Rust
proceeded to then inline it into each call site and then apply further
optimizations. It was also previously invoking the thread lock (for the
interner) unconditionally and invoking the `Display` implementation. That
is not at all what I intended for, despite knowing the eager semantics of
function calls in Rust.
Anyway, possibly more to come on that, I'm just tired of typing and need to
move on. I'll be returning to investigate further diagnostic messages soon.
2023-01-12 10:46:48 -05:00
|
|
|
"{report}\nfatal: failed to compile `{dest_path}`",
|
2022-10-19 14:58:05 -04:00
|
|
|
);
|
2022-04-13 15:11:29 -04:00
|
|
|
|
|
|
|
std::process::exit(1);
|
2022-10-19 14:58:05 -04:00
|
|
|
},
|
|
|
|
)
|
2020-04-08 16:18:18 -04:00
|
|
|
}
|
|
|
|
Ok(Command::Usage) => {
|
tamer: Integrate clippy
This invokes clippy as part of `make check` now, which I had previously
avoided doing (I'll elaborate on that below).
This commit represents the changes needed to resolve all the warnings
presented by clippy. Many changes have been made where I find the lints to
be useful and agreeable, but there are a number of lints, rationalized in
`src/lib.rs`, where I found the lints to be disagreeable. I have provided
rationale, primarily for those wondering why I desire to deviate from the
default lints, though it does feel backward to rationalize why certain lints
ought to be applied (the reverse should be true).
With that said, this did catch some legitimage issues, and it was also
helpful in getting some older code up-to-date with new language additions
that perhaps I used in new code but hadn't gone back and updated old code
for. My goal was to get clippy working without errors so that, in the
future, when others get into TAMER and are still getting used to Rust,
clippy is able to help guide them in the right direction.
One of the reasons I went without clippy for so long (though I admittedly
forgot I wasn't using it for a period of time) was because there were a
number of suggestions that I found disagreeable, and I didn't take the time
to go through them and determine what I wanted to follow. Furthermore, it
was hard to make that judgment when I was new to the language and lacked
the necessary experience to do so.
One thing I would like to comment further on is the use of `format!` with
`expect`, which is also what the diagnostic system convenience methods
do (which clippy does not cover). Because of all the work I've done trying
to understand Rust and looking at disassemblies and seeing what it
optimizes, I falsely assumed that Rust would convert such things into
conditionals in my otherwise-pure code...but apparently that's not the case,
when `format!` is involved.
I noticed that, after making the suggested fix with `get_ident`, Rust
proceeded to then inline it into each call site and then apply further
optimizations. It was also previously invoking the thread lock (for the
interner) unconditionally and invoking the `Display` implementation. That
is not at all what I intended for, despite knowing the eager semantics of
function calls in Rust.
Anyway, possibly more to come on that, I'm just tired of typing and need to
move on. I'll be returning to investigate further diagnostic messages soon.
2023-01-12 10:46:48 -05:00
|
|
|
println!("{usage}");
|
2020-04-08 16:18:18 -04:00
|
|
|
std::process::exit(exitcode::OK);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
tamer: Integrate clippy
This invokes clippy as part of `make check` now, which I had previously
avoided doing (I'll elaborate on that below).
This commit represents the changes needed to resolve all the warnings
presented by clippy. Many changes have been made where I find the lints to
be useful and agreeable, but there are a number of lints, rationalized in
`src/lib.rs`, where I found the lints to be disagreeable. I have provided
rationale, primarily for those wondering why I desire to deviate from the
default lints, though it does feel backward to rationalize why certain lints
ought to be applied (the reverse should be true).
With that said, this did catch some legitimage issues, and it was also
helpful in getting some older code up-to-date with new language additions
that perhaps I used in new code but hadn't gone back and updated old code
for. My goal was to get clippy working without errors so that, in the
future, when others get into TAMER and are still getting used to Rust,
clippy is able to help guide them in the right direction.
One of the reasons I went without clippy for so long (though I admittedly
forgot I wasn't using it for a period of time) was because there were a
number of suggestions that I found disagreeable, and I didn't take the time
to go through them and determine what I wanted to follow. Furthermore, it
was hard to make that judgment when I was new to the language and lacked
the necessary experience to do so.
One thing I would like to comment further on is the use of `format!` with
`expect`, which is also what the diagnostic system convenience methods
do (which clippy does not cover). Because of all the work I've done trying
to understand Rust and looking at disassemblies and seeing what it
optimizes, I falsely assumed that Rust would convert such things into
conditionals in my otherwise-pure code...but apparently that's not the case,
when `format!` is involved.
I noticed that, after making the suggested fix with `get_ident`, Rust
proceeded to then inline it into each call site and then apply further
optimizations. It was also previously invoking the thread lock (for the
interner) unconditionally and invoking the `Display` implementation. That
is not at all what I intended for, despite knowing the eager semantics of
function calls in Rust.
Anyway, possibly more to come on that, I'm just tired of typing and need to
move on. I'll be returning to investigate further diagnostic messages soon.
2023-01-12 10:46:48 -05:00
|
|
|
eprintln!("{e}");
|
|
|
|
println!("{usage}");
|
2020-04-08 16:18:18 -04:00
|
|
|
std::process::exit(exitcode::USAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get 'Options'
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use getopts::Options;
|
|
|
|
///
|
|
|
|
/// let opts = get_opts();
|
|
|
|
/// ```
|
|
|
|
fn get_opts() -> Options {
|
|
|
|
let mut opts = Options::new();
|
|
|
|
opts.optopt("o", "output", "set output file name", "NAME");
|
|
|
|
opts.optopt("", "emit", "set output type", "xmlo");
|
|
|
|
opts.optflag("h", "help", "print this help menu");
|
|
|
|
|
|
|
|
opts
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Option parser
|
|
|
|
fn parse_options(opts: Options, args: Vec<String>) -> Result<Command, Fail> {
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => {
|
|
|
|
return Err(f);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("h") {
|
|
|
|
return Ok(Command::Usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
let input = match matches.free.len() {
|
|
|
|
0 => return Err(Fail::OptionMissing(String::from("INPUT"))),
|
|
|
|
1 => matches.free[0].clone(),
|
|
|
|
_ => return Err(Fail::UnrecognizedOption(matches.free[1].clone())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let emit = match matches.opt_str("emit") {
|
|
|
|
Some(m) => match &m[..] {
|
|
|
|
"xmlo" => m,
|
|
|
|
_ => {
|
|
|
|
return Err(Fail::ArgumentMissing(String::from("--emit xmlo")))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
return Err(Fail::OptionMissing(String::from("--emit xmlo")));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let output = match matches.opt_str("o") {
|
|
|
|
Some(m) => m,
|
|
|
|
// we check that the extension is "xml" later
|
tamer: Integrate clippy
This invokes clippy as part of `make check` now, which I had previously
avoided doing (I'll elaborate on that below).
This commit represents the changes needed to resolve all the warnings
presented by clippy. Many changes have been made where I find the lints to
be useful and agreeable, but there are a number of lints, rationalized in
`src/lib.rs`, where I found the lints to be disagreeable. I have provided
rationale, primarily for those wondering why I desire to deviate from the
default lints, though it does feel backward to rationalize why certain lints
ought to be applied (the reverse should be true).
With that said, this did catch some legitimage issues, and it was also
helpful in getting some older code up-to-date with new language additions
that perhaps I used in new code but hadn't gone back and updated old code
for. My goal was to get clippy working without errors so that, in the
future, when others get into TAMER and are still getting used to Rust,
clippy is able to help guide them in the right direction.
One of the reasons I went without clippy for so long (though I admittedly
forgot I wasn't using it for a period of time) was because there were a
number of suggestions that I found disagreeable, and I didn't take the time
to go through them and determine what I wanted to follow. Furthermore, it
was hard to make that judgment when I was new to the language and lacked
the necessary experience to do so.
One thing I would like to comment further on is the use of `format!` with
`expect`, which is also what the diagnostic system convenience methods
do (which clippy does not cover). Because of all the work I've done trying
to understand Rust and looking at disassemblies and seeing what it
optimizes, I falsely assumed that Rust would convert such things into
conditionals in my otherwise-pure code...but apparently that's not the case,
when `format!` is involved.
I noticed that, after making the suggested fix with `get_ident`, Rust
proceeded to then inline it into each call site and then apply further
optimizations. It was also previously invoking the thread lock (for the
interner) unconditionally and invoking the `Display` implementation. That
is not at all what I intended for, despite knowing the eager semantics of
function calls in Rust.
Anyway, possibly more to come on that, I'm just tired of typing and need to
move on. I'll be returning to investigate further diagnostic messages soon.
2023-01-12 10:46:48 -05:00
|
|
|
None => format!("{input}o"),
|
2020-04-08 16:18:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Command::Compile(input, emit, output))
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
/// Toplevel `tamec` error representing a failure to complete the requested
|
|
|
|
/// operation successfully.
|
|
|
|
///
|
|
|
|
/// These are errors that will result in aborting execution and exiting with
|
|
|
|
/// a non-zero status.
|
|
|
|
/// Contrast this with [`RecoverableError`],
|
|
|
|
/// which is reported real-time to the user and _does not_ cause the
|
|
|
|
/// program to abort until the end of the compilation unit.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum UnrecoverableError {
|
|
|
|
Io(io::Error),
|
|
|
|
Fmt(fmt::Error),
|
|
|
|
XirWriterError(xir::writer::Error),
|
|
|
|
ErrorsDuringLowering(ErrorCount),
|
2022-12-13 13:46:31 -05:00
|
|
|
FinalizeError(FinalizeError),
|
2022-10-26 11:29:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Number of errors that occurred during this compilation unit.
|
|
|
|
///
|
|
|
|
/// Let's hope that this is large enough for the number of errors you may
|
|
|
|
/// have in your code.
|
|
|
|
type ErrorCount = usize;
|
|
|
|
|
|
|
|
/// An error that occurs during the lowering pipeline that may be recovered
|
|
|
|
/// from to continue parsing and collection of additional errors.
|
2022-04-13 15:11:29 -04:00
|
|
|
///
|
|
|
|
/// This represents the aggregation of all possible errors that can occur
|
2022-10-26 11:29:26 -04:00
|
|
|
/// during lowering.
|
2022-04-13 15:11:29 -04:00
|
|
|
/// This cannot include panics,
|
|
|
|
/// but efforts have been made to reduce panics to situations that
|
|
|
|
/// represent the equivalent of assertions.
|
2022-10-26 11:29:26 -04:00
|
|
|
///
|
|
|
|
/// These errors are distinct from [`UnrecoverableError`],
|
|
|
|
/// which represents the errors that could be returned to the toplevel
|
|
|
|
/// `main`,
|
|
|
|
/// because these errors are intended to be reported to the user _and then
|
|
|
|
/// recovered from_ so that compilation may continue and more errors may
|
|
|
|
/// be collected;
|
|
|
|
/// nobody wants a compiler that reports one error at a time.
|
|
|
|
///
|
|
|
|
/// Note that an recoverable error,
|
|
|
|
/// under a normal compilation strategy,
|
|
|
|
/// will result in an [`UnrecoverableError::ErrorsDuringLowering`] at the
|
|
|
|
/// end of the compilation unit.
|
2022-04-13 15:11:29 -04:00
|
|
|
#[derive(Debug)]
|
2022-10-26 11:29:26 -04:00
|
|
|
pub enum RecoverableError {
|
2022-06-02 10:30:44 -04:00
|
|
|
XirParseError(ParseError<UnknownToken, xir::Error>),
|
tamer: Introduce NIR (accepting only)
This introduces NIR, but only as an accepting grammar; it doesn't yet emit
the NIR IR, beyond TODOs.
This modifies `tamec` to, while copying XIR, also attempt to lower NIR to
produce parser errors, if any. It does not yet fail compilation, as I just
want to be cautious and observe that everything's working properly for a
little while as people use it, before I potentially break builds.
This is the culmination of months of supporting effort. The NIR grammar is
derived from our existing TAME sources internally, which I use for now as a
test case until I introduce test cases directly into TAMER later on (I'd do
it now, if I hadn't spent so much time on this; I'll start introducing tests
as I begin emitting NIR tokens). This is capable of fully parsing our
largest system with >900 packages, as well as `core`.
`tamec`'s lowering is a mess; that'll be cleaned up in future commits. The
same can be said about `tameld`.
NIR's grammar has some initial documentation, but this will improve over
time as well.
The generated docs still need some improvement, too, especially with
generated identifiers; I just want to get this out here for testing.
DEV-7145
2022-08-29 15:28:03 -04:00
|
|
|
XirfParseError(ParseError<XirToken, XirToXirfError>),
|
|
|
|
NirParseError(ParseError<XirfToken<RefinedText>, XirfToNirError>),
|
2022-12-05 16:32:00 -05:00
|
|
|
InterpError(ParseError<Nir, InterpError>),
|
2022-12-13 13:34:52 -05:00
|
|
|
NirToAirError(ParseError<Nir, NirToAirError>),
|
|
|
|
AirAggregateError(ParseError<Air, AsgError>),
|
2022-04-13 15:11:29 -04:00
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl From<io::Error> for UnrecoverableError {
|
2022-04-13 15:11:29 -04:00
|
|
|
fn from(e: io::Error) -> Self {
|
|
|
|
Self::Io(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl From<fmt::Error> for UnrecoverableError {
|
|
|
|
fn from(e: fmt::Error) -> Self {
|
|
|
|
Self::Fmt(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<xir::writer::Error> for UnrecoverableError {
|
|
|
|
fn from(e: xir::writer::Error) -> Self {
|
|
|
|
Self::XirWriterError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 13:46:31 -05:00
|
|
|
impl From<FinalizeError> for UnrecoverableError {
|
|
|
|
fn from(e: FinalizeError) -> Self {
|
|
|
|
Self::FinalizeError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:40:36 -05:00
|
|
|
impl From<Infallible> for UnrecoverableError {
|
|
|
|
fn from(_: Infallible) -> Self {
|
|
|
|
unreachable!("<UnrecoverableError as From<Infallible>>::from")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Token> From<ParseError<T, Infallible>> for UnrecoverableError {
|
|
|
|
fn from(_: ParseError<T, Infallible>) -> Self {
|
|
|
|
unreachable!(
|
|
|
|
"<UnrecoverableError as From<ParseError<T, Infallible>>>::from"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:38:07 -04:00
|
|
|
impl<T: Token> From<ParseError<T, Infallible>> for RecoverableError {
|
|
|
|
fn from(_: ParseError<T, Infallible>) -> Self {
|
|
|
|
unreachable!(
|
|
|
|
"<RecoverableError as From<ParseError<T, Infallible>>>::from"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl From<ParseError<UnknownToken, xir::Error>> for RecoverableError {
|
2022-06-02 10:30:44 -04:00
|
|
|
fn from(e: ParseError<UnknownToken, xir::Error>) -> Self {
|
|
|
|
Self::XirParseError(e)
|
2022-04-13 15:11:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl From<ParseError<XirToken, XirToXirfError>> for RecoverableError {
|
tamer: Introduce NIR (accepting only)
This introduces NIR, but only as an accepting grammar; it doesn't yet emit
the NIR IR, beyond TODOs.
This modifies `tamec` to, while copying XIR, also attempt to lower NIR to
produce parser errors, if any. It does not yet fail compilation, as I just
want to be cautious and observe that everything's working properly for a
little while as people use it, before I potentially break builds.
This is the culmination of months of supporting effort. The NIR grammar is
derived from our existing TAME sources internally, which I use for now as a
test case until I introduce test cases directly into TAMER later on (I'd do
it now, if I hadn't spent so much time on this; I'll start introducing tests
as I begin emitting NIR tokens). This is capable of fully parsing our
largest system with >900 packages, as well as `core`.
`tamec`'s lowering is a mess; that'll be cleaned up in future commits. The
same can be said about `tameld`.
NIR's grammar has some initial documentation, but this will improve over
time as well.
The generated docs still need some improvement, too, especially with
generated identifiers; I just want to get this out here for testing.
DEV-7145
2022-08-29 15:28:03 -04:00
|
|
|
fn from(e: ParseError<XirToken, XirToXirfError>) -> Self {
|
|
|
|
Self::XirfParseError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 10:00:08 -04:00
|
|
|
impl From<ParseError<XirfToken<RefinedText>, XirfToNirError>>
|
|
|
|
for RecoverableError
|
|
|
|
{
|
tamer: Introduce NIR (accepting only)
This introduces NIR, but only as an accepting grammar; it doesn't yet emit
the NIR IR, beyond TODOs.
This modifies `tamec` to, while copying XIR, also attempt to lower NIR to
produce parser errors, if any. It does not yet fail compilation, as I just
want to be cautious and observe that everything's working properly for a
little while as people use it, before I potentially break builds.
This is the culmination of months of supporting effort. The NIR grammar is
derived from our existing TAME sources internally, which I use for now as a
test case until I introduce test cases directly into TAMER later on (I'd do
it now, if I hadn't spent so much time on this; I'll start introducing tests
as I begin emitting NIR tokens). This is capable of fully parsing our
largest system with >900 packages, as well as `core`.
`tamec`'s lowering is a mess; that'll be cleaned up in future commits. The
same can be said about `tameld`.
NIR's grammar has some initial documentation, but this will improve over
time as well.
The generated docs still need some improvement, too, especially with
generated identifiers; I just want to get this out here for testing.
DEV-7145
2022-08-29 15:28:03 -04:00
|
|
|
fn from(e: ParseError<XirfToken<RefinedText>, XirfToNirError>) -> Self {
|
|
|
|
Self::NirParseError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 16:32:00 -05:00
|
|
|
impl From<ParseError<Nir, InterpError>> for RecoverableError {
|
|
|
|
fn from(e: ParseError<Nir, InterpError>) -> Self {
|
|
|
|
Self::InterpError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 13:34:52 -05:00
|
|
|
impl From<ParseError<Nir, NirToAirError>> for RecoverableError {
|
|
|
|
fn from(e: ParseError<Nir, NirToAirError>) -> Self {
|
|
|
|
Self::NirToAirError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseError<Air, AsgError>> for RecoverableError {
|
|
|
|
fn from(e: ParseError<Air, AsgError>) -> Self {
|
|
|
|
Self::AirAggregateError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl Display for UnrecoverableError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-12-13 13:46:31 -05:00
|
|
|
use UnrecoverableError::*;
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
match self {
|
2022-12-13 13:46:31 -05:00
|
|
|
Io(e) => Display::fmt(e, f),
|
|
|
|
Fmt(e) => Display::fmt(e, f),
|
|
|
|
XirWriterError(e) => Display::fmt(e, f),
|
|
|
|
FinalizeError(e) => Display::fmt(e, f),
|
2022-04-13 15:11:29 -04:00
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
// TODO: Use formatter for dynamic "error(s)"
|
2022-12-13 13:46:31 -05:00
|
|
|
ErrorsDuringLowering(err_count) => {
|
2022-10-26 11:29:26 -04:00
|
|
|
write!(f, "aborting due to previous {err_count} error(s)",)
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 15:11:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl Display for RecoverableError {
|
2022-04-13 15:11:29 -04:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-12-13 13:34:52 -05:00
|
|
|
use RecoverableError::*;
|
|
|
|
|
2022-04-13 15:11:29 -04:00
|
|
|
match self {
|
2022-12-13 13:34:52 -05:00
|
|
|
XirParseError(e) => Display::fmt(e, f),
|
|
|
|
XirfParseError(e) => Display::fmt(e, f),
|
|
|
|
NirParseError(e) => Display::fmt(e, f),
|
|
|
|
InterpError(e) => Display::fmt(e, f),
|
|
|
|
NirToAirError(e) => Display::fmt(e, f),
|
|
|
|
AirAggregateError(e) => Display::fmt(e, f),
|
2022-04-13 15:11:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl Error for UnrecoverableError {
|
2022-04-13 15:11:29 -04:00
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
2022-12-13 13:46:31 -05:00
|
|
|
use UnrecoverableError::*;
|
|
|
|
|
2022-04-13 15:11:29 -04:00
|
|
|
match self {
|
2022-12-13 13:46:31 -05:00
|
|
|
Io(e) => Some(e),
|
|
|
|
Fmt(e) => Some(e),
|
|
|
|
XirWriterError(e) => Some(e),
|
|
|
|
ErrorsDuringLowering(_) => None,
|
|
|
|
FinalizeError(e) => Some(e),
|
2022-10-26 11:29:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for RecoverableError {
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
2022-12-13 13:34:52 -05:00
|
|
|
use RecoverableError::*;
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
match self {
|
2022-12-13 13:34:52 -05:00
|
|
|
XirParseError(e) => Some(e),
|
|
|
|
XirfParseError(e) => Some(e),
|
|
|
|
NirParseError(e) => Some(e),
|
|
|
|
InterpError(e) => Some(e),
|
|
|
|
NirToAirError(e) => Some(e),
|
|
|
|
AirAggregateError(e) => Some(e),
|
2022-04-13 15:11:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
impl Diagnostic for UnrecoverableError {
|
|
|
|
fn describe(&self) -> Vec<AnnotatedSpan> {
|
2022-12-13 13:46:31 -05:00
|
|
|
use UnrecoverableError::*;
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
match self {
|
2022-12-13 13:46:31 -05:00
|
|
|
FinalizeError(e) => e.describe(),
|
|
|
|
|
2022-10-26 11:29:26 -04:00
|
|
|
// Fall back to `Display`
|
2022-12-13 13:46:31 -05:00
|
|
|
Io(_) | Fmt(_) | XirWriterError(_) | ErrorsDuringLowering(_) => {
|
|
|
|
vec![]
|
|
|
|
}
|
2022-10-26 11:29:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Diagnostic for RecoverableError {
|
2022-04-13 15:11:29 -04:00
|
|
|
fn describe(&self) -> Vec<AnnotatedSpan> {
|
2022-12-13 13:34:52 -05:00
|
|
|
use RecoverableError::*;
|
|
|
|
|
2022-04-13 15:11:29 -04:00
|
|
|
match self {
|
2022-12-13 13:34:52 -05:00
|
|
|
XirParseError(e) => e.describe(),
|
|
|
|
XirfParseError(e) => e.describe(),
|
|
|
|
NirParseError(e) => e.describe(),
|
|
|
|
InterpError(e) => e.describe(),
|
|
|
|
NirToAirError(e) => e.describe(),
|
|
|
|
AirAggregateError(e) => e.describe(),
|
2022-04-13 15:11:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 16:18:18 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_help() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![String::from("program"), String::from("-h")],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(Command::Usage) => {}
|
|
|
|
_ => panic!("Long help option did not parse"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_help_long() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![String::from("program"), String::from("--help")],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(Command::Usage) => {}
|
|
|
|
_ => panic!("Help option did not parse"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_invalid() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![String::from("program"), String::from("-q")],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Err(Fail::UnrecognizedOption(_)) => {}
|
|
|
|
_ => panic!("Invalid option not caught"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_missing_input() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let result = parse_options(opts, vec![String::from("program")]);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Err(Fail::OptionMissing(message)) => {
|
|
|
|
assert_eq!("INPUT", message);
|
|
|
|
}
|
|
|
|
_ => panic!("Missing input not caught"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_missing_emit() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![String::from("program"), String::from("filename")],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Err(Fail::OptionMissing(message)) => {
|
|
|
|
assert_eq!("--emit xmlo", message);
|
|
|
|
}
|
|
|
|
_ => panic!("Missing emit not caught"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_invalid_emit() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![
|
|
|
|
String::from("program"),
|
|
|
|
String::from("filename.xml"),
|
|
|
|
String::from("--emit"),
|
|
|
|
String::from("foo"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Err(Fail::ArgumentMissing(message)) => {
|
|
|
|
assert_eq!("--emit xmlo", message);
|
|
|
|
}
|
|
|
|
_ => panic!("Invalid emit not caught"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_too_many_args() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![
|
|
|
|
String::from("program"),
|
|
|
|
String::from("foo"),
|
|
|
|
String::from("--emit"),
|
|
|
|
String::from("bar"),
|
|
|
|
String::from("baz"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Err(Fail::UnrecognizedOption(message)) => {
|
|
|
|
assert_eq!("baz", message);
|
|
|
|
}
|
|
|
|
_ => panic!("Extra option not caught"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_valid() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let xmlo = String::from("xmlo");
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![
|
|
|
|
String::from("program"),
|
|
|
|
String::from("foo.xml"),
|
|
|
|
String::from("--emit"),
|
|
|
|
xmlo,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(Command::Compile(infile, xmlo, outfile)) => {
|
|
|
|
assert_eq!("foo.xml", infile);
|
|
|
|
assert_eq!("foo.xmlo", outfile);
|
|
|
|
assert_eq!("xmlo", xmlo);
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_valid_custom_out() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let xmlo = String::from("xmlo");
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![
|
|
|
|
String::from("program"),
|
|
|
|
String::from("foo.xml"),
|
|
|
|
String::from("--emit"),
|
|
|
|
xmlo,
|
|
|
|
String::from("-o"),
|
|
|
|
String::from("foo.xmli"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(Command::Compile(infile, xmlo, outfile)) => {
|
|
|
|
assert_eq!("foo.xml", infile);
|
|
|
|
assert_eq!("foo.xmli", outfile);
|
|
|
|
assert_eq!("xmlo", xmlo);
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_options_valid_custom_out_long() {
|
|
|
|
let opts = get_opts();
|
|
|
|
let xmlo = String::from("xmlo");
|
|
|
|
let result = parse_options(
|
|
|
|
opts,
|
|
|
|
vec![
|
|
|
|
String::from("program"),
|
|
|
|
String::from("foo.xml"),
|
|
|
|
String::from("--emit"),
|
|
|
|
xmlo,
|
|
|
|
String::from("--output"),
|
|
|
|
String::from("foo.xmli"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(Command::Compile(infile, xmlo, outfile)) => {
|
|
|
|
assert_eq!("foo.xml", infile);
|
|
|
|
assert_eq!("foo.xmli", outfile);
|
|
|
|
assert_eq!("xmlo", xmlo);
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|