Skip to content

Commit 1d6640a

Browse files
committed
(blocks) fix: successive blocks
1 parent ba56e6f commit 1d6640a

File tree

21 files changed

+264
-137
lines changed

21 files changed

+264
-137
lines changed

src/errors/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn display_errors(errors: Vec<CompileError>, files: &[(String, &str)], err_t
1616
.expect("Never happens: File of error doesn't exist");
1717
let code_line = code_lines
1818
.get(line_nb - 1)
19-
.expect("Never happens: given line of file that doesn't exist");
19+
.unwrap_or_else(|| panic!("Never happens: given line of file that doesn't exist: {filename}:{line_nb}:{column_nb}"));
2020
eprintln!("\n{filename}:{line_nb}:{column_nb}: {err_type} {err_lvl}: {message}");
2121
eprintln!("{line_nb:5} | {code_line}");
2222
eprintln!("{}^{}", " ".repeat(8 + column_nb - 1), "~".repeat(length));

src/errors/location.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl Location {
2020

2121
pub(crate) fn incr_line(&mut self) {
2222
self.line += 1;
23+
self.col = 1;
2324
}
2425

2526
pub(crate) fn into_error(self, msg: String) -> CompileError {
@@ -33,11 +34,6 @@ impl Location {
3334
}
3435
}
3536

36-
pub(crate) fn new_line(&mut self) {
37-
self.line += 1;
38-
self.col = 1;
39-
}
40-
4137
pub(crate) fn to_error(&self, msg: String) -> CompileError {
4238
CompileError::from((self.to_owned(), msg, ErrorLevel::Error))
4339
}

src/lexer/lex_content.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub fn lex_file(content: &str, location: &mut Location) -> Res<Vec<Token>> {
157157

158158
for line in content.lines() {
159159
lex_line(line, location, &mut lex_data, &mut lex_status);
160-
location.new_line();
160+
location.incr_line();
161161
}
162162

163163
Res::from((lex_data.take_tokens(), lex_data.take_errors()))
@@ -190,7 +190,6 @@ fn lex_line(
190190
break;
191191
}
192192
}
193-
location.incr_line();
194193
end_current(lex_status, lex_data, location);
195194
if line.trim_end().ends_with('\\') {
196195
if line.ends_with(char::is_whitespace) {

src/lexer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod api {
33

44
pub use super::lex_content::lex_file;
55
pub use super::numbers::api::Number;
6+
pub use super::types::lexing_data::display_tokens;
67
pub use super::types::tokens_types::{Symbol, Token, TokenValue};
78
}
89
mod end_state;

src/lexer/numbers/base/hexadecimal.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ fn get_hex_float_state(literal: &str, location: &Location) -> Result<HexFloatPar
123123
_ if float_parse.state == HexFloatParseState::Exponent => return Err(location.to_error(format!("{ERR_PREFIX}invalid character for exponent. Expected an ascii digit, but found '{ch}'"))),
124124
_ if ch.is_ascii_hexdigit() => float_parse.push(ch),
125125
'.' if float_parse.state == HexFloatParseState::Int => float_parse.state = HexFloatParseState::Decimal,
126-
'.' if float_parse.state == HexFloatParseState::Decimal => return Err(location.to_error(format!("{ERR_PREFIX}maximum one '.' in number constant, but 2 were found."))),
127-
'.' if float_parse.state == HexFloatParseState::Exponent => return Err(location.to_error(format!("{ERR_PREFIX}exponent must be an integer, but found a period."))),
128-
'p' | 'P' if float_parse.state == HexFloatParseState::Exponent => return Err(location.to_error(format!("{ERR_PREFIX}maximum one 'p' in number constant, but 2 were found."))),
126+
'.' if float_parse.state == HexFloatParseState::Decimal => return Err(location.to_error(format!("{ERR_PREFIX}maximum one '.' in number constant, but 2 were found."))),
127+
'.' if float_parse.state == HexFloatParseState::Exponent => return Err(location.to_error(format!("{ERR_PREFIX}exponent must be an integer, but found a period."))),
128+
'p' | 'P' if float_parse.state == HexFloatParseState::Exponent => return Err(location.to_error(format!("{ERR_PREFIX}maximum one 'p' in number constant, but 2 were found."))),
129129
'p' | 'P' => float_parse.state = HexFloatParseState::Exponent,
130-
_ => return Err(location.to_error(format!("{ERR_PREFIX}invalid character '{ch}' found in number constant"))),
131-
};
130+
_ => return Err(location.to_error(format!("{ERR_PREFIX}invalid character '{ch}' found in number constant"))),
131+
};
132132
}
133133
Ok(float_parse)
134134
}

src/lexer/numbers/from_literal.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,20 @@ fn get_number_type(literal: &str, location: &Location) -> Result<NumberType, Com
7070
for ch in chars {
7171
match ch {
7272
'u' | 'U' if unsigned => {
73-
return Err(location.to_error("found 2 'u' characters.".to_owned()))
73+
return Err(location.to_error("found 2 'u' characters.".to_owned()));
7474
}
7575
'u' | 'U' => unsigned = true,
7676
'l' | 'L' if l_count == 2 => {
7777
return Err(location
78-
.to_error("found 3 'l' characters, but max is 2 (`long long`).".to_owned()))
78+
.to_error("found 3 'l' characters, but max is 2 (`long long`).".to_owned()));
7979
}
8080
'l' | 'L' => l_count += 1,
8181
'f' | 'F' if is_hex && !double_or_float => break,
8282
'f' | 'F' => float = true,
8383
'i' | 'I' => {
84-
return Err(location.to_error("imaginary constants are a GCC extension.".to_owned()))
84+
return Err(
85+
location.to_error("imaginary constants are a GCC extension.".to_owned())
86+
);
8587
}
8688
_ => break,
8789
}

src/lexer/numbers/types.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ macro_rules! define_nb_types {
3434
$($t($t),)*
3535
}
3636

37-
#[derive(Clone)]
3837
pub enum NumberType {
3938
$($t,)*
4039
}

src/lexer/types/escape_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Debug, PartialEq, Eq, Clone)]
1+
#[derive(Debug, PartialEq, Eq)]
22
pub enum EscapeSequence {
33
Hexadecimal(String),
44
Octal(String),

src/lexer/types/lexing_data.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ impl LexingData {
5252
mem::take(&mut self.tokens)
5353
}
5454
}
55+
56+
#[must_use]
57+
#[inline]
58+
pub fn display_tokens(tokens: &[Token]) -> String {
59+
format!(
60+
"[{}]",
61+
tokens
62+
.iter()
63+
.map(|x| format!("{x}"))
64+
.collect::<Vec<_>>()
65+
.join(", ")
66+
)
67+
}

src/lexer/types/lexing_state.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ impl SymbolStatus {
169169
} else if self.third == NULL {
170170
self.third = value;
171171
} else {
172-
panic!("This is not meant to happen. Called try_operator on none empty self, and no operator was returned. LexingData: {self:?}");
172+
panic!(
173+
"This is not meant to happen. Called try_operator on none empty self, and no operator was returned. LexingData: {self:?}"
174+
);
173175
}
174176
op
175177
}
@@ -229,24 +231,26 @@ impl SymbolStatus {
229231

230232
if let Some((nb_consumed, _)) = &result {
231233
match *nb_consumed {
232-
0 => (), // two consecutive litterals
233-
1 => {
234-
self.first = self.second;
235-
self.second = self.third;
236-
self.third = NULL;
237-
}
238-
2 => {
239-
self.first = self.third;
240-
self.second = NULL;
241-
self.third = NULL;
242-
}
243-
3 => {
244-
self.first = NULL;
245-
self.second = NULL;
246-
self.third = NULL;
247-
}
248-
_ => panic!("his is not meant to happen. nb_consumed is defined only be having values of 0, 1, 2 or 3, not {nb_consumed}"),
249-
};
234+
0 => (), // two consecutive litterals
235+
1 => {
236+
self.first = self.second;
237+
self.second = self.third;
238+
self.third = NULL;
239+
}
240+
2 => {
241+
self.first = self.third;
242+
self.second = NULL;
243+
self.third = NULL;
244+
}
245+
3 => {
246+
self.first = NULL;
247+
self.second = NULL;
248+
self.third = NULL;
249+
}
250+
_ => panic!(
251+
"his is not meant to happen. nb_consumed is defined only be having values of 0, 1, 2 or 3, not {nb_consumed}"
252+
),
253+
};
250254
};
251255
result
252256
}

0 commit comments

Comments
 (0)