Skip to content

Commit

Permalink
Partial rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vcfxb committed May 17, 2024
1 parent 7768178 commit f2a6e6d
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 111 deletions.
2 changes: 1 addition & 1 deletion wright/src/bin/wright.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@
// Ok(())
// }

fn main() {
fn main() {
todo!("Wright frontend undergoing overhaul");
}
4 changes: 2 additions & 2 deletions wright/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! The wright programming language crate. This is being re-written from the ground up as of September 2022.
// We want to enforce good stuff by default.
// We want to enforce good stuff by default.
#![deny(missing_copy_implementations, missing_debug_implementations)]
#![deny(rustdoc::broken_intra_doc_links)]
#![warn(missing_docs)]

/// The version of this copy of Wright.
pub const WRIGHT_VERSION: &str = env!("CARGO_PKG_VERSION");

pub mod source_tracking;
pub mod reporting;
pub mod source_tracking;
// pub mod parser;
// pub mod repl;
// pub mod solver;
47 changes: 23 additions & 24 deletions wright/src/reporting.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
//! Reporting for errors, warnings, and everything else.
//!
//! The code in this module is heavily inspired by [codespan-reporting] and [ariadne].
//!
//! Reporting for errors, warnings, and everything else.
//!
//! The code in this module is heavily inspired by [codespan-reporting] and [ariadne].
//!
//! [codespan-reporting]: https://crates.io/crates/codespan-reporting
//! [ariadne]: https://crates.io/crates/ariadne
use std::io;
use self::{owned_string::OwnedString, severity::Severity};
use std::io;
use termcolor::{ColorChoice, StandardStream, StandardStreamLock, WriteColor};

pub mod severity;
pub mod owned_string;
pub mod label;
mod draw;
pub mod label;
pub mod owned_string;
pub mod severity;

/// A diagnostic to help the user to understand details of their interactions with the Wright compiler.
#[derive(Debug)]
pub struct Diagnostic {
/// The severity of this diagnostic, helps determine coloration when shown to the user.
/// The severity of this diagnostic, helps determine coloration when shown to the user.
pub severity: Severity,

/// An optional error code, that identifies this type of diagnostic.
/// An optional error code, that identifies this type of diagnostic.
pub code: Option<OwnedString>,

/// The main message of the diagnostic. This should be short enough to display on one terminal line in most cases.
pub message: OwnedString,
}


impl Diagnostic {
/// Construct a new [Diagnostic].
/// Construct a new [Diagnostic].
/// Use the arguments to fill their corresponding fields,
/// then fill the rest with the following defaults:
/// then fill the rest with the following defaults:
/// - [Diagnostic::code] defaults to [None].
pub fn new<M: Into<OwnedString>>(severity: Severity, message: M) -> Self {
Diagnostic {
severity,
code: None,
message: message.into()
message: message.into(),
}
}

Expand All @@ -52,13 +51,13 @@ impl Diagnostic {
pub fn error<M: Into<OwnedString>>(message: M) -> Self {
Diagnostic::new(Severity::Error, message)
}

/// Construct a new [Diagnostic] using [Severity::Warning].
/// See [Diagnostic::new].
pub fn warning<M: Into<OwnedString>>(message: M) -> Self {
Diagnostic::new(Severity::Warning, message)
}

/// Construct a new [Diagnostic] using [Severity::Info].
/// See [Diagnostic::new].
pub fn info<M: Into<OwnedString>>(message: M) -> Self {
Expand All @@ -71,8 +70,8 @@ impl Diagnostic {
self
}

/// Print this diagnostic to the standard output.
///
/// Print this diagnostic to the standard output.
///
/// Uses [supports_unicode] to determine whether to print unicode characters.
pub fn print(&self, color_choice: ColorChoice) -> io::Result<()> {
// Check if the standard output supports unicode.
Expand All @@ -85,8 +84,8 @@ impl Diagnostic {
self.write(&mut stdout_lock, write_unicode)
}

/// Print this diagnostic to the standard error.
///
/// Print this diagnostic to the standard error.
///
/// Uses [supports_unicode] to determine whether to print unicode characters.
pub fn eprint(&self, color_choice: ColorChoice) -> io::Result<()> {
// Check if the standard error supports unicode.
Expand All @@ -99,16 +98,16 @@ impl Diagnostic {
self.write(&mut stderr_lock, write_unicode)
}

/// Write this [Diagnostic] to the given writer.
///
/// It is suggested to use [supports_unicode] to determine a good value for `write_unicode` when writing to
/// Write this [Diagnostic] to the given writer.
///
/// It is suggested to use [supports_unicode] to determine a good value for `write_unicode` when writing to
/// standard streams. That is what this crate does in functions like [Diagnostic::print].
pub fn write<W: WriteColor>(&self, w: &mut W, write_unicode: bool) -> io::Result<()> {
draw::draw(self, w, write_unicode)
}
}

#[cfg(test)]
#[cfg(test)]
mod tests {
// Drawing tests currently covered in [super::draw].
}
2 changes: 1 addition & 1 deletion wright/src/reporting/label.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! Labels on pieces of source in a [Diagnostic].
//!
//!
//! [Diagnostic]: super::Diagnostic
17 changes: 8 additions & 9 deletions wright/src/reporting/owned_string.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
//! Owned strings are used throughout the error reporting code, since in many cases, our error messages and codes
//! will be static (or at least partially so).
//!
//! will be static (or at least partially so).
//!
//! The code here will ultimately be very similar to a [`Cow<'static, str>`]
//!
//!
//! [`Cow<'static, str>`]: std::borrow::Cow
use derive_more::{From, Display};
use derive_more::{Display, From};

/// An owned string used for error reporting. Since many error messages and codes are static, we use this struct
/// An owned string used for error reporting. Since many error messages and codes are static, we use this struct
/// to provide [`&'static str`]s as an option when a heap allocated string is not necessary.
///
///
/// [`&'static str`]: str
#[derive(Debug, From, Display)]
pub enum OwnedString {
/// A static string reference.
/// A static string reference.
Static(&'static str),

/// An owned [String].
/// An owned [String].
Owned(String),
}


impl AsRef<str> for OwnedString {
fn as_ref(&self) -> &str {
match self {
Expand Down
17 changes: 8 additions & 9 deletions wright/src/reporting/severity.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Modeling and implementation pertaining to the severity of a diagnostic from the compiler/wright system.
//! Modeling and implementation pertaining to the severity of a diagnostic from the compiler/wright system.
use termcolor::Color;
use derive_more::Display;
use termcolor::Color;

/// The severity of a [Diagnostic].
///
///
/// [Diagnostic]: super::Diagnostic
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]
pub enum Severity {
/// A compiler bug. Something internally happened that wasn't supposed to.
/// A compiler bug. Something internally happened that wasn't supposed to.
#[display(fmt = "bug")]
Bug,

Expand All @@ -18,22 +18,21 @@ pub enum Severity {

/// A warning about something non-fatal but not ideal.
#[display(fmt = "warning")]
Warning,
Warning,

/// An info message. Likely rarely used.
/// An info message. Likely rarely used.
#[display(fmt = "info")]
Info,
}


impl Severity {
/// Get the default color to display a diagnostic of this type with if the terminal supports it.
/// Get the default color to display a diagnostic of this type with if the terminal supports it.
pub const fn color(self) -> Color {
match self {
Self::Bug => Color::Magenta,
Self::Error => Color::Red,
Self::Warning => Color::Yellow,
Self::Info => Color::Cyan
Self::Info => Color::Cyan,
}
}
}
27 changes: 15 additions & 12 deletions wright/src/source_tracking.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
//! Types and traits for tracking source code fed to the wright compiler.
use std::sync::{Arc, RwLock};
use self::source::Source;
use std::sync::{Arc, RwLock};

pub mod immutable_string;
pub mod fragment;
pub mod filename;
pub mod fragment;
pub mod immutable_string;
pub mod source;

/// Storage for a list of [Source]s used and reference in compiling a wright project.
/// Storage for a list of [Source]s used and reference in compiling a wright project.
#[derive(Debug)]
pub struct SourceMap {
/// Internally, we use an [Arc] [RwLock] [Vec] to create a concurrent mutable list.
///
///
/// Each source is wrapped in an [Arc] to make them all accessible without needing to use [RwLock::read].
inner: Arc<RwLock<Vec<Arc<Source>>>>
inner: Arc<RwLock<Vec<Arc<Source>>>>,
}

/// A reference to a [Source] in a [SourceMap].
pub type SourceRef = Arc<Source>;

impl SourceMap {
/// Construct a new empty [SourceMap].
/// Construct a new empty [SourceMap].
pub fn new() -> Self {
SourceMap { inner: Arc::new(RwLock::new(Vec::new())) }
SourceMap {
inner: Arc::new(RwLock::new(Vec::new())),
}
}

/// Add a [Source] to this [SourceMap] and get a [SourceRef] to it after it's added.
/// Add a [Source] to this [SourceMap] and get a [SourceRef] to it after it's added.
pub fn add(&self, source: Source) -> SourceRef {
// Put the source in an Arc.
let source: Arc<Source> = Arc::new(source);

// Get a write guard to the internal list.
let mut write_guard = self.inner
// Get a write guard to the internal list.
let mut write_guard = self
.inner
.write()
.expect("Should be able to acquire write guard");

// Push the souce to the internal Vec.
write_guard.push(Arc::clone(&source));
// Drop the write guard -- make sure other functions can access this source map.
drop(write_guard);
// Return the now-Arc'd source.
// Return the now-Arc'd source.
source
}
}
Expand Down
4 changes: 2 additions & 2 deletions wright/src/source_tracking/filename.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Structure and implementation relating to file names used throughout the wright compiler and tooling.
//! Structure and implementation relating to file names used throughout the wright compiler and tooling.
use std::path::PathBuf;
use derive_more::Display;
use std::path::PathBuf;

/// Used to represent different file names used throughout this crate.
#[derive(Debug, Display, Clone)]
Expand Down
Loading

0 comments on commit f2a6e6d

Please sign in to comment.