summaryrefslogtreecommitdiff
path: root/grammar/src/label.rs
diff options
context:
space:
mode:
Diffstat (limited to 'grammar/src/label.rs')
-rw-r--r--grammar/src/label.rs40
1 files changed, 35 insertions, 5 deletions
diff --git a/grammar/src/label.rs b/grammar/src/label.rs
index 3f89d9a..058baaf 100644
--- a/grammar/src/label.rs
+++ b/grammar/src/label.rs
@@ -12,6 +12,15 @@ pub enum GrammarLabelType {
Rule(usize),
}
+impl Display for GrammarLabelType {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::TNT(tnt) => write!(f, "{tnt}"),
+ Self::Rule(pos) => write!(f, "R({pos})"),
+ }
+ }
+}
+
// Some convenient conversions
impl From<usize> for GrammarLabelType {
@@ -71,11 +80,17 @@ pub struct GrammarLabel {
impl core::fmt::Display for GrammarLabel {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
- // Simply displaying this without the help of a grammar is not
- // of much help, so we just use the debug method to cheat,
- // haha.
-
- write!(f, "{:?}", self)
+ write!(
+ f,
+ "{}, {}, {}",
+ self.label,
+ self.start,
+ if let Some(end) = self.end {
+ format!("{end}")
+ } else {
+ "ε".to_owned()
+ }
+ )
}
}
@@ -89,6 +104,15 @@ impl GrammarLabel {
Self { label, start, end }
}
+ /// Construct a new label with an ending position.
+ #[inline]
+ pub fn new_closed(label: impl Into<GrammarLabelType>, start: usize, end: usize) -> Self {
+ let label = label.into();
+ let end = Some(end);
+
+ Self { label, start, end }
+ }
+
/// Return the end in the input.
#[inline]
pub fn end(&self) -> Option<usize> {
@@ -113,6 +137,12 @@ impl GrammarLabel {
self.end = Some(end);
}
+ /// Remove the ending boundary.
+ #[inline]
+ pub fn open_end(&mut self) {
+ self.end = None;
+ }
+
/// Return a string description with the help of the associated
/// grammar.
pub fn to_string(&self, grammar: &Grammar) -> Result<String, Error> {