summaryrefslogtreecommitdiff
path: root/grammar/src/label.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-02-12 12:07:34 +0800
committerJSDurand <mmemmew@gmail.com>2023-02-12 12:07:34 +0800
commit987c84f3454c687cca0efe0d471fcf00e052ecab (patch)
tree04b9cf073a12adfb5d07ae308c3809e88cf4ebd2 /grammar/src/label.rs
parent265ff8f87dc7392fdf701f811eb2bf54d7bc6678 (diff)
Added the functionality of split or clone.
I need more than the ability to clone nodes: I also need to split the nodes. Now this seems to be correctly added.
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> {