use std::fmt::Display;
use crate::{Position, Span, internals::position::Positions};
#[derive(Debug, Clone)]
pub struct Match {
pub span: Span,
pub token_type: usize,
pub positions: Option<Positions>,
}
impl Match {
pub fn new(span: Span, token_type: usize) -> Self {
Match {
span,
token_type,
positions: None,
}
}
#[inline]
pub fn with_positions(mut self, positions: Option<Positions>) -> Self {
self.positions = positions;
self
}
}
impl Display for Match {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"[{}..{}] tok {}{}",
self.span.start,
self.span.end,
self.token_type,
if let Some(pos) = self.positions.as_ref() {
format!(" at {pos}")
} else {
String::new()
}
)
}
}
#[derive(Debug, Default)]
pub(crate) struct MatchStart {
pub(crate) byte_index: usize,
pub(crate) position: Option<Position>,
}
impl MatchStart {
pub(crate) fn new(byte_index: usize) -> Self {
MatchStart {
byte_index,
position: None,
}
}
pub(crate) fn with_position(mut self, position: Option<Position>) -> Self {
self.position = position;
self
}
}
#[derive(Debug, Default)]
pub(crate) struct MatchEnd {
pub(crate) byte_index: usize,
pub(crate) position: Option<Position>,
pub(crate) token_type: usize,
pub(crate) priority: usize,
}
impl MatchEnd {
pub(crate) fn new(byte_index: usize, token_type: usize, priority: usize) -> Self {
MatchEnd {
byte_index,
position: None,
token_type,
priority,
}
}
pub(crate) fn with_position(mut self, position: Option<Position>) -> Self {
self.position = position;
self
}
}