forked from softdevteam/grmtools
-
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.
289: Add a name_span field to Rule. r=ltratt a=ratmice Co-authored-by: matt rice <ratmice@gmail.com>
- Loading branch information
Showing
9 changed files
with
152 additions
and
65 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
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,45 @@ | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A `Span` records what portion of the user's input something (e.g. a lexeme or production) | ||
/// references (i.e. the `Span` doesn't hold a reference / copy of the actual input). | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
pub struct Span { | ||
start: usize, | ||
end: usize, | ||
} | ||
|
||
impl Span { | ||
/// Create a new span starting at byte `start` and ending at byte `end`. | ||
/// | ||
/// # Panics | ||
/// | ||
/// If `end` is less than `start`. | ||
pub fn new(start: usize, end: usize) -> Self { | ||
if end < start { | ||
panic!("Span starts ({}) after it ends ({})!", start, end); | ||
} | ||
Span { start, end } | ||
} | ||
|
||
/// Byte offset of the start of the span. | ||
pub fn start(&self) -> usize { | ||
self.start | ||
} | ||
|
||
/// Byte offset of the end of the span. | ||
pub fn end(&self) -> usize { | ||
self.end | ||
} | ||
|
||
/// Length in bytes of the span. | ||
pub fn len(&self) -> usize { | ||
self.end - self.start | ||
} | ||
|
||
/// Returns `true` if this `Span` covers 0 bytes, or `false` otherwise. | ||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
} |
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
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
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
Oops, something went wrong.