use serde::{Deserialize, Serialize};
use crate::Position;
use super::Span;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Match {
token_type: usize,
span: Span,
}
impl Match {
pub fn new(token_type: usize, span: Span) -> Self {
Self { token_type, span }
}
#[inline]
pub fn start(&self) -> usize {
self.span.start
}
#[inline]
pub fn end(&self) -> usize {
self.span.end
}
#[inline]
pub fn span(&self) -> Span {
self.span
}
#[inline]
pub fn range(&self) -> std::ops::Range<usize> {
self.span.range()
}
#[inline]
pub fn len(&self) -> usize {
self.span.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.span.is_empty()
}
#[inline]
pub fn token_type(&self) -> usize {
self.token_type
}
pub fn add_offset(&mut self, offset: usize) {
self.span.start += offset;
self.span.end += offset;
}
}
#[cfg(feature = "regex_automata")]
impl From<regex_automata::Match> for Match {
fn from(value: regex_automata::Match) -> Self {
Self {
token_type: value.pattern().as_usize(),
span: Span::new(value.start(), value.end()),
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct MatchExt {
token_type: usize,
span: Span,
start_position: Position,
end_position: Position,
}
impl MatchExt {
pub(crate) fn new(
token_type: usize,
span: Span,
start_position: Position,
end_position: Position,
) -> Self {
Self {
token_type,
span,
start_position,
end_position,
}
}
#[inline]
pub fn start(&self) -> usize {
self.span.start
}
#[inline]
pub fn end(&self) -> usize {
self.span.end
}
#[inline]
pub fn span(&self) -> Span {
self.span
}
#[inline]
pub fn range(&self) -> std::ops::Range<usize> {
self.span.range()
}
#[inline]
pub fn len(&self) -> usize {
self.span.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.span.is_empty()
}
#[inline]
pub fn token_type(&self) -> usize {
self.token_type
}
#[inline]
pub fn start_position(&self) -> Position {
self.start_position
}
#[inline]
pub fn end_position(&self) -> Position {
self.end_position
}
}