-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Thank you, @JulesGuesnon!)
- Loading branch information
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ rust-version = "1.72.0" | |
|
||
[dependencies] | ||
nom = "7.1" | ||
nom-span = "0.1.1" | ||
thiserror = "1.0.50" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! Data types that describe the location of a syntactic element. | ||
|
||
use std::fmt::Display; | ||
|
||
use nom_span::Spanned; | ||
|
||
pub(crate) type Input<'a> = Spanned<&'a str>; | ||
|
||
/// Location within an input stream. | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub struct Location { | ||
line: usize, | ||
col: usize, | ||
} | ||
|
||
impl Location { | ||
/// Return the line number (1-based). | ||
pub fn line(&self) -> usize { | ||
self.line | ||
} | ||
|
||
/// Return the column number (1-based). | ||
pub fn col(&self) -> usize { | ||
self.col | ||
} | ||
} | ||
|
||
impl<'a> From<&Input<'a>> for Location { | ||
fn from(i: &Input<'a>) -> Self { | ||
Self { | ||
line: i.line(), | ||
col: i.col(), | ||
} | ||
} | ||
} | ||
|
||
impl Display for Location { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{line}:{col}", line = self.line, col = self.col) | ||
} | ||
} | ||
|
||
/// Span (start and end location) within an input stream. | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub struct Span { | ||
start: Location, | ||
after_end: Location, | ||
} | ||
|
||
impl Span { | ||
#[allow(dead_code)] // TEMPORARY | ||
pub(crate) fn from_start_and_after_end(start: &'_ Input<'_>, after_end: &'_ Input<'_>) -> Self { | ||
Self { | ||
start: start.into(), | ||
after_end: after_end.into(), | ||
} | ||
} | ||
|
||
/// Returns the starting location of this span. | ||
pub fn start(&self) -> Location { | ||
self.start | ||
} | ||
|
||
/// Returns the location immediately after the end of this span. | ||
pub fn after_end(&self) -> Location { | ||
self.after_end | ||
} | ||
|
||
/// Returns true if the `start` and `after_end` locations are the same. | ||
pub fn is_empty(&self) -> bool { | ||
self.start == self.after_end | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
mod location { | ||
use nom::character::complete::alpha1; | ||
|
||
use crate::input::{Input, Location}; | ||
|
||
#[test] | ||
fn empty_str() { | ||
let i = Input::new("", true); | ||
let l: Location = (&i).into(); | ||
assert_eq!(l.line(), 1); | ||
assert_eq!(l.col(), 1); | ||
} | ||
|
||
#[test] | ||
fn take3() { | ||
let i1 = Input::new("abc456", true); | ||
let (i2, _) = alpha1::<Input, crate::Error>(i1).unwrap(); | ||
let l2: Location = (&i2).into(); | ||
assert_eq!(l2.line(), 1); | ||
assert_eq!(l2.col(), 4); | ||
} | ||
} | ||
|
||
mod span { | ||
use nom::character::complete::alpha1; | ||
|
||
use crate::input::{Input, Span}; | ||
|
||
#[test] | ||
fn empty_str() { | ||
let i = Input::new("", true); | ||
let s = Span::from_start_and_after_end(&i, &i); | ||
|
||
assert_eq!(s.start().line(), 1); | ||
assert_eq!(s.start().col(), 1); | ||
assert_eq!(s.after_end().line(), 1); | ||
assert_eq!(s.after_end().col(), 1); | ||
assert!(s.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn take3() { | ||
let i1 = Input::new("abc456", true); | ||
let (i2, _) = alpha1::<Input, crate::Error>(i1).unwrap(); | ||
let s = Span::from_start_and_after_end(&i1, &i2); | ||
|
||
assert_eq!(s.start().line(), 1); | ||
assert_eq!(s.start().col(), 1); | ||
assert_eq!(s.after_end().line(), 1); | ||
assert_eq!(s.after_end().col(), 4); | ||
assert!(!s.is_empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ | |
|
||
mod blocks; | ||
mod fixtures; | ||
mod input; | ||
mod primitives; | ||
mod strings; |