Skip to content

Commit

Permalink
The magic part moved into nom_locate.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj committed Oct 20, 2020
1 parent 586bcf5 commit 5380596
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spectest = ["yaml-rust", "deunicode", "hrx-get", "regex"]
[dependencies]
lazy_static = "1.0"
nom = "5.0.0"
nom_locate = "2.0.0"
nom_locate = { git = "https://github.com/fflorent/nom_locate" }
num-bigint = { version = "0.3.0", default-features = false, features = ["std"] }
num-integer = "0.1.42"
num-rational = { version = "0.3.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'a> From<ParseError<'a>> for Error {
fn from(err: ParseError) -> Self {
Error::ParseError {
msg: format!("Parse error: {:?}", err.err),
pos: SourcePos::magic_pos(err.span),
pos: err.span.into(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! That said, this implementation has reached a version where I find it
//! usable for my personal projects, and the number of working tests are
//! improving.
//#![forbid(unsafe_code)]
#![forbid(unsafe_code)]
use std::path::Path;

pub mod css;
Expand Down
6 changes: 1 addition & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,7 @@ fn import2(input: Span) -> IResult<Span, Item> {
),
),
|(position, import, args)| {
Item::Import(
import,
args.unwrap_or(Value::Null),
SourcePos::magic_pos(position),
)
Item::Import(import, args.unwrap_or(Value::Null), position.into())
},
)(input)
}
Expand Down
30 changes: 5 additions & 25 deletions src/parser/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,12 @@ pub struct SourcePos {
pub file: SourceName,
}

impl SourcePos {
pub fn magic_pos(span: Span) -> Self {
use std::slice;

let self_bytes = span.fragment();
let self_ptr = self_bytes.as_ptr();
let offset = span.get_column() - 1;
let the_line = unsafe {
assert!(
offset <= isize::max_value() as usize,
"offset is too big"
);
let orig_input_ptr = self_ptr.offset(-(offset as isize));
slice::from_raw_parts(
orig_input_ptr,
offset + span.fragment().len(),
)
};
let the_line = the_line
.split(|c| *c == b'\n')
.next()
.and_then(|s| from_utf8(s).ok())
.unwrap_or("<<failed to display line>>");

impl From<Span<'_>> for SourcePos {
fn from(span: Span) -> Self {
SourcePos {
line: the_line.to_string(),
line: from_utf8(span.get_line_beginning())
.unwrap_or("<<failed to display line>>")
.to_string(),
line_no: span.location_line(),
line_pos: span.get_utf8_column(),
file: span.extra.clone(),
Expand Down

0 comments on commit 5380596

Please sign in to comment.