@@ -5,62 +5,104 @@ use crate::token::TokenKind;
55use alloc:: { rc:: Rc , string:: String } ;
66use core:: fmt:: { self , Display , Formatter , Result } ;
77
8+ /// Kind of error encountered during compilation.
89#[ derive( Clone , Eq , PartialEq , Hash , Debug ) ]
910pub enum Kind {
11+ /// Invalid token, with the given offending character.
1012 InvalidToken ( char ) ,
13+ /// Invalid constant value.
1114 InvalidConstant ,
15+ /// Constant value exceeded the capacity of a 32-bit `int`.
1216 ConstantTooLarge ,
17+ /// Unknown type encountered, with the given token.
1318 UnknownType ( TokenKind ) ,
19+ /// Expected a type but none was found.
1420 ExpectedType ,
21+ /// Expected an identifier but the given token was found instead (or `None` on EOF).
1522 ExpectedIdentifier ( Option < TokenKind > ) ,
23+ /// Expected a specific token but another given token was found instead (or `None` on EOF).
1624 ExpectedToken ( TokenKind , Option < TokenKind > ) ,
25+ /// Expected an expression but the given token was found instead (or `None` on EOF).
1726 ExpectedExpression ( Option < TokenKind > ) ,
27+ /// Encountered a redefinition of an identifier.
1828 Redefinition ( Rc < String > ) ,
29+ /// Attempted to use an undeclared identifier.
1930 Undeclared ( Rc < String > ) ,
31+ /// Conflicting type definitions for the same identifier.
2032 ConflictingTypes ( Rc < String > ) ,
33+ /// Invalid left-hand value in an assignment.
2134 InvalidLvalue ,
35+ /// `break` outside of a loop or `switch` statement.
2236 BreakOutsideLoopOrSwitch ,
37+ /// `continue` outside of a loop.
2338 ContinueOutsideLoop ,
39+ /// Attempted to call an undefined function.
2440 UndefinedFunction ( Rc < String > ) ,
41+ /// Expression expected to be constant but wasn't.
2542 NonConstantExpression ,
43+ /// Expression expected to be an integer but wasn't.
2644 NonIntegerExpression ,
45+ /// Attempted to assign to a `const` variable.
2746 CannotAssignToConst ( Rc < String > ) ,
47+ /// Duplicate `case` value in a `switch` statement.
2848 DuplicateCase ,
49+ /// Invalid preprocessor directive.
2950 InvalidDirective ( Rc < String > ) ,
51+ /// String literal was not properly terminated.
3052 UnterminatedString ,
53+ /// Invalid character.
3154 InvalidCharacter ( char ) ,
55+ /// Invalid escape sequence in a string literal.
3256 InvalidEscapeSequence ,
57+ /// Expected a string literal but the given token was found instead (or `None` on EOF).
3358 ExpectedString ( Option < TokenKind > ) ,
59+ /// Error emitted by the [`wat`](https://crates.io/crates/wat) crate when compiling the WAT source to binary.
60+ ///
61+ /// Note: Likely indicates a bug in this compiler!
3462 AssemblerError ( String ) ,
3563}
3664
65+ /// Severity level of an error.
3766#[ derive( Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Hash , Debug ) ]
3867pub enum Severity {
68+ /// A warning that does not halt compilation.
69+ ///
70+ /// Note: currently, no warnings are emitted.
3971 Warning ,
72+ /// An error that halts compilation.
4073 Error ,
4174}
4275
76+ /// Compilation error or warning, including its location and context.
4377#[ derive( Clone , Eq , PartialEq , Hash , Debug ) ]
4478pub struct Error {
79+ /// The line number in the C source where the error occurred.
4580 pub ( crate ) line_number : usize ,
81+ /// The column number in the C source where the error occurred.
4682 pub ( crate ) column : usize ,
83+ /// The kind of error encountered.
4784 pub ( crate ) kind : Kind ,
85+ /// The severity of the error.
4886 pub ( crate ) severity : Severity ,
4987}
5088
5189impl Error {
90+ /// Returns the line number in the C source where the error occurred.
5291 pub fn line_number ( & self ) -> usize {
5392 self . line_number
5493 }
5594
95+ /// Returns the column number in the C source where the error occurred.
5696 pub fn column ( & self ) -> usize {
5797 self . column
5898 }
5999
100+ /// Returns the error kind.
60101 pub fn kind ( & self ) -> & Kind {
61102 & self . kind
62103 }
63104
105+ /// Returns the error severity level.
64106 pub fn severity ( & self ) -> Severity {
65107 self . severity
66108 }
0 commit comments