Skip to content

Commit 7a93c29

Browse files
committed
(main) fix: naming and todos and fmt
1 parent d94f09e commit 7a93c29

File tree

16 files changed

+104
-85
lines changed

16 files changed

+104
-85
lines changed

rustfmt.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
imports_layout = "Horizontal"
22
imports_granularity = "Module"
3-
group_imports = "StdExternalCrate"
3+
group_imports = "StdExternalCrate"
4+
format_code_in_doc_comments = true
5+
wrap_comments = true

src/errors/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<T> ops::FromResidual<Result<convert::Infallible, CompileError>> for Res<Opt
9797
#[inline]
9898
fn from_residual(residual: Result<convert::Infallible, CompileError>) -> Self {
9999
match residual {
100-
Ok(_) => panic!(), //TODO: check that it is unreachable
100+
Ok(_) => unreachable!(/* By definition of Infallible */),
101101
Err(err) => Self::from_err(err),
102102
}
103103
}

src/lexer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn lex_char(
7373
state.repr()
7474
)),
7575

76-
/* Static strings and chars*/
76+
/* Static strings and chars */
7777
// open/close
7878
('\'', status @ Char(_), _) => end_current(status, lex_data, location),
7979
('\'', status, _) if !matches!(status, Str(_)) => {

src/lexer/numbers/base/binary.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::errors::location::Location;
44
#[allow(clippy::wildcard_imports)]
55
use crate::lexer::numbers::types::arch_types::*;
66
use crate::lexer::numbers::types::{Number, NumberType, ERR_PREFIX};
7-
use crate::lexer::numbers::ParseResult;
7+
use crate::lexer::numbers::OverParseRes;
88

99
pub fn to_bin_value(
1010
literal: &str,
1111
nb_type: &NumberType,
1212
location: &Location,
13-
) -> ParseResult<Number> {
13+
) -> OverParseRes<Number> {
1414
if literal.chars().all(|ch| matches!(ch, '0' | '1')) {
1515
parse_int_from_radix!(location,
1616
nb_type, literal, "a binary must be an integer", 2, Int Long LongLong UInt ULong ULongLong
@@ -20,6 +20,6 @@ pub fn to_bin_value(
2020
.chars()
2121
.find(|ch| matches!(ch, '0' | '1'))
2222
.expect("Exists according to line above");
23-
ParseResult::from(to_error!(location, "{ERR_PREFIX}a binary constant must only contain '0's and '1's. Found invalid character '{first}'."))
23+
OverParseRes::from(to_error!(location, "{ERR_PREFIX}a binary constant must only contain '0's and '1's. Found invalid character '{first}'."))
2424
}
2525
}

src/lexer/numbers/base/decimal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use crate::errors::location::Location;
66
#[allow(clippy::wildcard_imports)]
77
use crate::lexer::numbers::types::arch_types::*;
88
use crate::lexer::numbers::types::{Number, NumberType, ERR_PREFIX};
9-
use crate::lexer::numbers::ParseResult;
9+
use crate::lexer::numbers::OverParseRes;
1010

1111
macro_rules! parse_number {
1212
($location:ident, $nb_type:ident, $literal:tt, $($int:ident)*, $($float:ident)*) => {
1313
match $nb_type {
14-
NumberType::LongDouble => ParseResult::from(to_error!($location, "{ERR_PREFIX}`long double` not supported yet.")), //TODO: f128 not implemented
14+
NumberType::LongDouble => OverParseRes::from(to_error!($location, "{ERR_PREFIX}`long double` not supported yet.")), //TODO: f128 not implemented
1515
$(NumberType::$int => $crate::lexer::numbers::parse::safe_parse_int!(ERR_PREFIX, $int, $location, $literal.parse::<$int>()).map(|nb| Number::$int(nb)),)*
16-
$(NumberType::$float => ParseResult::from(parse_and_error::<$float>($literal, $location).map(|nb| Number::$float(nb))?),)*
16+
$(NumberType::$float => OverParseRes::from(parse_and_error::<$float>($literal, $location).map(|nb| Number::$float(nb))?),)*
1717
}
1818
};
1919
}
@@ -32,6 +32,6 @@ pub fn to_decimal_value(
3232
literal: &str,
3333
nb_type: &NumberType,
3434
location: &Location,
35-
) -> ParseResult<Number> {
35+
) -> OverParseRes<Number> {
3636
parse_number!(location, nb_type, literal, Int Long LongLong UInt ULong ULongLong, Float Double )
3737
}

src/lexer/numbers/base/hexadecimal.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::errors::location::Location;
44
#[allow(clippy::wildcard_imports)]
55
use crate::lexer::numbers::types::arch_types::*;
66
use crate::lexer::numbers::types::{Number, NumberType, ERR_PREFIX};
7-
use crate::lexer::numbers::ParseResult;
7+
use crate::lexer::numbers::OverParseRes;
88

99
#[derive(Default, Debug)]
1010
struct HexFloatParse {
@@ -99,9 +99,9 @@ macro_rules! parse_hexadecimal_float {
9999
decimal_part += digit_value / exponent_pow;
100100
}
101101
if $float_parse.exponent_neg.unwrap_or(false) {
102-
ParseResult::from(Number::$t((int_part + decimal_part) / exponent))
102+
OverParseRes::from(Number::$t((int_part + decimal_part) / exponent))
103103
} else {
104-
ParseResult::from(Number::$t((int_part + decimal_part) * exponent))
104+
OverParseRes::from(Number::$t((int_part + decimal_part) * exponent))
105105
}
106106
},)*
107107
_ => panic!("Never happens: nb_type is float"),
@@ -158,9 +158,9 @@ pub fn to_hex_value(
158158
literal: &str,
159159
nb_type: &NumberType,
160160
location: &Location,
161-
) -> ParseResult<Number> {
161+
) -> OverParseRes<Number> {
162162
let float_parse = match get_hex_float_state(literal, location) {
163-
Err(err) => return ParseResult::from(err),
163+
Err(err) => return OverParseRes::from(err),
164164
Ok(parsed) => parsed,
165165
};
166166
if nb_type.is_int() {

src/lexer/numbers/base/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ pub mod octal;
55

66
macro_rules! parse_int_from_radix {
77
($location:ident, $nb_type:ident, $literal:tt, $reason:expr, $radix:expr, $($t:ident)*) => {{
8-
use $crate::lexer::numbers::parse::{safe_parse_int, ParseResult};
8+
use $crate::lexer::numbers::parse::{safe_parse_int, OverParseRes};
99
match $nb_type {
10-
_ if !$nb_type.is_int() => ParseResult::Err(to_error!($location, "{ERR_PREFIX}{}, but found a `{}`", $reason, $nb_type)),
10+
_ if !$nb_type.is_int() => OverParseRes::Err(to_error!($location, "{ERR_PREFIX}{}, but found a `{}`", $reason, $nb_type)),
1111
$(NumberType::$t => safe_parse_int!(ERR_PREFIX, $t, $location, $t::from_str_radix($literal, $radix)).map(|nb| Number::$t(nb)),)*
1212
_ => panic!("this is unreachable")
1313
}

src/lexer/numbers/base/octal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::errors::location::Location;
44
#[allow(clippy::wildcard_imports)]
55
use crate::lexer::numbers::types::arch_types::*;
66
use crate::lexer::numbers::types::NumberType;
7-
use crate::lexer::numbers::{Number, ParseResult, ERR_PREFIX};
7+
use crate::lexer::numbers::{Number, OverParseRes, ERR_PREFIX};
88

99
pub fn to_oct_value(
1010
literal: &str,
1111
nb_type: &NumberType,
1212
location: &Location,
13-
) -> ParseResult<Number> {
13+
) -> OverParseRes<Number> {
1414
if literal.chars().all(|ch| matches!(ch, '0'..='7')) {
1515
parse_int_from_radix!(
1616
location,
@@ -21,6 +21,6 @@ pub fn to_oct_value(
2121
.chars()
2222
.find(|ch| matches!(ch, '0'..='7'))
2323
.expect("Exists according to line above");
24-
ParseResult::from(to_error!(location, "{ERR_PREFIX}a octal constant must only contain digits between '0' and '7'. Found invalid character '{first}'."))
24+
OverParseRes::from(to_error!(location, "{ERR_PREFIX}a octal constant must only contain digits between '0' and '7'. Found invalid character '{first}'."))
2525
}
2626
}

src/lexer/numbers/mod.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod types;
44
use core::str;
55

66
use base::{binary, decimal, hexadecimal, octal};
7-
use parse::ParseResult;
7+
use parse::{OverParseRes, ParseRes};
88
#[allow(clippy::wildcard_imports)]
99
use types::arch_types::*;
1010
#[allow(clippy::pub_use)]
@@ -15,7 +15,6 @@ use super::types::lexing_data::LexingData;
1515
use super::types::lexing_state::Ident;
1616
use crate::errors::compile::{to_error, CompileError};
1717
use crate::errors::location::Location;
18-
use crate::errors::result::Res;
1918

2019
pub fn literal_to_number(
2120
lex_data: &mut LexingData,
@@ -34,38 +33,36 @@ pub fn literal_to_number(
3433
};
3534

3635
let mut res = literal_to_number_err(literal.value(), location, lex_data.last_is_minus());
37-
res.edit_errors(|err| err.specify_length(literal.len() - 1));
38-
let (value, errors) = res.into_value();
39-
40-
assert!(
41-
value.is_some() || !errors.iter().all(|x| !x.is_error()),
42-
"//TODO"
43-
);
44-
lex_data.extend_err(errors);
45-
46-
value
36+
res.edit_err(|err| err.specify_length(literal.len() - 1));
37+
match res {
38+
ParseRes::Value(val) => Some(val),
39+
ParseRes::Err(err) => {
40+
lex_data.push_err(err);
41+
None
42+
}
43+
ParseRes::ValueErr(val, err) => {
44+
lex_data.push_err(err);
45+
Some(val)
46+
}
47+
}
4748
}
4849

49-
fn literal_to_number_err(literal: &str, location: &Location, signed: bool) -> Res<Option<Number>> {
50+
fn literal_to_number_err(literal: &str, location: &Location, signed: bool) -> ParseRes<Number> {
5051
let mut nb_type = get_number_type(literal, location)?;
5152
let base = get_base(literal, &nb_type, location)?;
52-
let value = str::from_utf8(
53-
literal
54-
.as_bytes()
55-
.get(base.prefix_size()..literal.len() - nb_type.suffix_size())
56-
.expect("never happens as suffix size + prefix size <= len, as 'x' and 'b' can't be used as suffix"),
57-
)
58-
.expect("never happens: all rust chars are valid utf8");
53+
let value = literal
54+
.get(base.prefix_size()..literal.len() - nb_type.suffix_size())
55+
.expect("never happens as suffix size + prefix size <= len, as 'x' and 'b' can't be used as suffix");
5956

6057
if value.is_empty() {
61-
return Res::from_err(to_error!(
58+
return ParseRes::Err(to_error!(
6259
location,
6360
"{ERR_PREFIX}found no digits between prefix and suffix. Please add at least one digit."
6461
));
6562
}
6663

6764
if let Some(ch) = check_with_base(value, &base) {
68-
return Res::from_err(to_error!(
65+
return ParseRes::Err(to_error!(
6966
location,
7067
"{ERR_PREFIX}found invalid character '{ch}' in {} base.",
7168
base.repr()
@@ -84,7 +81,7 @@ fn literal_to_number_err(literal: &str, location: &Location, signed: bool) -> Re
8481
{
8582
nb_type = new_type;
8683
} else {
87-
return parse_res.ignore_overflow(literal, location).into_res();
84+
return parse_res.ignore_overflow(literal, location);
8885
}
8986
}
9087
}
@@ -142,7 +139,6 @@ fn get_base(
142139
}
143140

144141
fn get_number_type(literal: &str, location: &Location) -> Result<NumberType, CompileError> {
145-
// TODO: automatic conversion to bigger int if too large, whatever the suffix
146142
let is_hex = literal.starts_with("0x");
147143
/* literal characteristics */
148144
let double_or_float = literal.contains('.')

0 commit comments

Comments
 (0)