Skip to content

Commit b91a470

Browse files
committed
Further refinement of error offset calculations
1 parent b6c65c7 commit b91a470

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

src/error/display.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'i> TechniqueError<'i> {
2525
.unwrap_or("?");
2626

2727
let line = i + 1;
28-
let column = j;
28+
let column = j + 1;
2929

3030
let width = line
3131
.to_string()
@@ -38,7 +38,7 @@ impl<'i> TechniqueError<'i> {
3838
3939
{:width$} {}
4040
{:width$} {} {}
41-
{:width$} {} {:>j$}
41+
{:width$} {} {:>column$}
4242
4343
{}
4444
"#,
@@ -68,7 +68,7 @@ impl<'i> TechniqueError<'i> {
6868
let j = calculate_column_number(self.source, self.offset);
6969

7070
let line = i + 1;
71-
let column = j;
71+
let column = j + 1;
7272

7373
format!(
7474
"{}: {}:{}:{} {}",

src/language/quantity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Quantity types and parsing for scientific measurements with uncertainty and units
2-
//!
2+
//!
33
//! A Quantity is an amount, possibly with uncertainty, at the magnitude if
44
//! given, of the units specified.
55
//!

src/parsing/parser.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ decimal point:
470470
42
471471
-123.45
472472
0.001
473-
473+
474474
Values less than 1 must have a leading '0' before the decimal.
475475
"#
476476
.trim_ascii()
@@ -512,7 +512,7 @@ The base must be 10, and the exponent must be an integer.
512512
Symbols used to denote units can contain:
513513
514514
Letters 'A'..'z' kg, Hz, mol
515-
Degrees '°': °C, °F
515+
Degrees '°': °C, °F
516516
Rates '/': m/s, km/h
517517
SI prefix 'μ': μg, μs
518518
@@ -1070,7 +1070,7 @@ impl<'i> Parser<'i> {
10701070
let cap = match re.captures(self.source) {
10711071
Some(c) => c,
10721072
None => {
1073-
let arrow_offset = analyse_malformed_signature(self.source);
1073+
let arrow_offset = analyze_malformed_signature(self.source);
10741074
return Err(ParsingError::InvalidSignature(self.offset + arrow_offset));
10751075
}
10761076
};
@@ -1827,7 +1827,6 @@ impl<'i> Parser<'i> {
18271827
// Parse unit symbol (required) - consume everything remaining
18281828
let symbol = self.read_units_symbol()?;
18291829

1830-
18311830
let quantity = Quantity {
18321831
mantissa,
18331832
uncertainty,
@@ -1838,7 +1837,6 @@ impl<'i> Parser<'i> {
18381837
Ok(Numeric::Scientific(quantity))
18391838
}
18401839

1841-
18421840
fn read_decimal_part(&mut self) -> Result<crate::language::Decimal, ParsingError<'i>> {
18431841
use crate::regex::*;
18441842
let re = regex!(r"^-?[0-9]+(\.[0-9]+)?");
@@ -1911,25 +1909,29 @@ impl<'i> Parser<'i> {
19111909
fn read_units_symbol(&mut self) -> Result<&'i str, ParsingError<'i>> {
19121910
// Scan through each character and find the first invalid one
19131911
let mut valid_end = 0;
1914-
1915-
for (byte_offset, ch) in self.source.char_indices() {
1912+
1913+
for (byte_offset, ch) in self
1914+
.source
1915+
.char_indices()
1916+
{
19161917
if ch.is_whitespace() {
19171918
// Stop at whitespace
19181919
break;
19191920
} else if ch.is_ascii_alphabetic() || ch == '°' || ch == '/' || ch == 'μ' {
19201921
// Valid character
19211922
valid_end = byte_offset + ch.len_utf8();
19221923
} else {
1923-
// Invalid character found - point directly at it
1924-
// Add 1 because we want to point TO the character, not before it
1925-
return Err(ParsingError::InvalidQuantitySymbol(self.offset + byte_offset + 1));
1924+
// Invalid character found - point directly at it
1925+
return Err(ParsingError::InvalidQuantitySymbol(
1926+
self.offset + byte_offset,
1927+
));
19261928
}
19271929
}
1928-
1930+
19291931
if valid_end == 0 {
19301932
return Err(ParsingError::InvalidQuantitySymbol(self.offset));
19311933
}
1932-
1934+
19331935
let symbol = &self.source[..valid_end];
19341936
self.advance(valid_end);
19351937
Ok(symbol)
@@ -2509,25 +2511,21 @@ fn is_signature(content: &str) -> bool {
25092511
re.is_match(content)
25102512
}
25112513

2512-
fn analyse_malformed_signature(content: &str) -> usize {
2513-
let mut offset = 0;
2514-
let mut count = 0;
2514+
fn analyze_malformed_signature(content: &str) -> usize {
2515+
let mut tokens = content.split_ascii_whitespace();
25152516

2516-
for token in content
2517-
.trim_ascii()
2518-
.split_ascii_whitespace()
2519-
{
2520-
if count == 1 {
2521-
// Found second token - point to where arrow should be (between tokens)
2522-
return offset;
2517+
// Skip the first token
2518+
let first_token = tokens.next();
2519+
if first_token.is_none() {
2520+
return 0;
2521+
}
2522+
2523+
// Find the second token
2524+
if let Some(second_token) = tokens.next() {
2525+
// Find where this token starts in the original content
2526+
if let Some(pos) = content.find(second_token) {
2527+
return pos;
25232528
}
2524-
offset += token.len();
2525-
// Skip whitespace to next token
2526-
offset += content[offset..]
2527-
.chars()
2528-
.take_while(|c| c.is_ascii_whitespace())
2529-
.count();
2530-
count += 1;
25312529
}
25322530

25332531
0 // fallback

0 commit comments

Comments
 (0)