forked from ethereum/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes ethereum#432
- Loading branch information
Showing
46 changed files
with
544 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,41 @@ | ||
//! Semantic errors. | ||
|
||
use ansi_term::Color::Red; | ||
use fe_common::diagnostics::Diagnostic; | ||
use fe_parser::node::Span; | ||
|
||
/// Error to be returned from APIs that should reject duplicate definitions | ||
#[derive(Debug)] | ||
pub struct AlreadyDefined; | ||
|
||
/// Error to be returned when otherwise no meaningful information can be returned | ||
#[derive(Debug)] | ||
pub struct FatalError; | ||
|
||
/// Error indicating that a value can not move between memory and storage | ||
#[derive(Debug)] | ||
pub struct CannotMove; | ||
|
||
/// Error indicating that a value has the wrong type | ||
#[derive(Debug)] | ||
pub struct AnalyzerError { | ||
pub diagnostics: Vec<Diagnostic>, | ||
pub classic: Option<SemanticError>, | ||
} | ||
pub struct TypeError; | ||
|
||
/// Errors for things that may arise in a valid Fe AST. | ||
/// Errors that can result from indexing | ||
#[derive(Debug, PartialEq)] | ||
pub enum ErrorKind { | ||
pub enum IndexingError { | ||
WrongIndexType, | ||
NotSubscriptable, | ||
SignedExponentNotAllowed, | ||
TypeError, | ||
Fatal, | ||
} | ||
|
||
/// Errors that can result from a binary operation | ||
#[derive(Debug, PartialEq)] | ||
pub struct SemanticError { | ||
pub kind: ErrorKind, | ||
/// A sequence of nested spans containing the error's origin in the source | ||
/// code. | ||
pub context: Vec<Span>, | ||
pub enum BinaryOperationError { | ||
TypesNotEqual, | ||
TypesNotNumeric, | ||
RightTooLarge, | ||
RightIsSigned, | ||
NotEqualAndUnsigned, | ||
} | ||
|
||
impl SemanticError { | ||
pub fn fatal() -> Self { | ||
SemanticError { | ||
kind: ErrorKind::Fatal, | ||
context: vec![], | ||
} | ||
} | ||
|
||
/// Create a new error with kind `NotSubscriptable` | ||
pub fn not_subscriptable() -> Self { | ||
SemanticError { | ||
kind: ErrorKind::NotSubscriptable, | ||
context: vec![], | ||
} | ||
} | ||
|
||
/// Create a new error with kind `SignedExponentNotAllowed` | ||
pub fn signed_exponent_not_allowed() -> Self { | ||
SemanticError { | ||
kind: ErrorKind::SignedExponentNotAllowed, | ||
context: vec![], | ||
} | ||
} | ||
|
||
/// Create a new error with kind `TypeError` | ||
pub fn type_error() -> Self { | ||
SemanticError { | ||
kind: ErrorKind::TypeError, | ||
context: vec![], | ||
} | ||
} | ||
|
||
/// Maps the error to a new error that contains the given span in its | ||
/// context. | ||
pub fn with_context(mut self, span: Span) -> Self { | ||
self.context.push(span); | ||
self | ||
} | ||
|
||
/// Formats the error using the source code. | ||
/// | ||
/// The string will contain the error kind, line number, and surrounding | ||
/// code. | ||
pub fn format_user(&self, src: &str) -> String { | ||
let line = if let Some(span) = self.context.first() { | ||
src[..span.start].lines().count() | ||
} else { | ||
0 | ||
}; | ||
|
||
let context = match (self.context.get(0), self.context.get(1)) { | ||
(Some(inner), Some(outer)) => { | ||
let first_part = src[outer.start..inner.start].to_string(); | ||
let middle_part = Red.paint(&src[inner.start..inner.end]).to_string(); | ||
let last_part = src[inner.end..outer.end].to_string(); | ||
|
||
format!("{}{}{}", first_part, middle_part, last_part) | ||
} | ||
(Some(span), None) => src[span.start..span.end].to_string(), | ||
(_, _) => "no error context available".to_string(), | ||
}; | ||
|
||
format!("{:?} on line {}\n{}", self.kind, line, context) | ||
} | ||
#[derive(Debug)] | ||
pub struct AnalyzerError { | ||
pub diagnostics: Vec<Diagnostic>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.