Improved pos counter
This commit is contained in:
parent
732c5a12fc
commit
41dfb7f432
@ -1,11 +1,11 @@
|
||||
/*use source_stream_0::{CollectedSubstring, Pos, SourceStream};
|
||||
use crate::iterators::StrSourceIterator;
|
||||
use source_stream_0::{CollectedSubstring, Pos, SourceStream};
|
||||
use crate::iterators::{ArraySourceIterator, StrSourceIterator};
|
||||
use crate::pos::{IndexPosCounter, NewLinePosCounter, PosLineCol};
|
||||
use crate::{_CollectScopeContext, SourceIterator};
|
||||
use crate::SourceIterator;
|
||||
|
||||
#[test]
|
||||
fn sandbox() {
|
||||
let mut z = StrSourceIterator::start("12\n34");
|
||||
let mut z = ArraySourceIterator::start(b"12\n34", IndexPosCounter::default());
|
||||
println!("{}", z.pos());
|
||||
z.next();
|
||||
println!("{} ", z.pos());
|
||||
@ -30,4 +30,3 @@ fn sandbox() {
|
||||
|
||||
return;
|
||||
}
|
||||
*/
|
||||
@ -1,15 +1,12 @@
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Pos};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub trait PosCounter<'pos> {
|
||||
type C;
|
||||
pub trait PosCounter<'pos, C> {
|
||||
type P: Pos<'pos>;
|
||||
|
||||
fn update(&mut self, c: Self::C);
|
||||
fn update(&mut self, c: C);
|
||||
|
||||
fn export(&self) -> Self::P;
|
||||
|
||||
fn init() -> Self;
|
||||
}
|
||||
|
||||
pub trait SourceIterator<'source, 'pos> {
|
||||
|
||||
@ -27,7 +27,7 @@ impl<'source, C: Copy> CollectedSubstring<'source> for ArrayCollectedSubstring<'
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos>> {
|
||||
pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> {
|
||||
_source: &'source [C],
|
||||
_iter: Iter<'source, C>,
|
||||
_current: Option<&'source C>,
|
||||
@ -36,8 +36,8 @@ pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos>> {
|
||||
__phantom: PhantomData<&'pos ()>,
|
||||
}
|
||||
|
||||
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos>> ArraySourceIterator<'source, 'pos, C, PC> {
|
||||
pub fn start(arr: &'source [C]) -> ArraySourceIterator<'source, 'pos, C, PC> {
|
||||
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> ArraySourceIterator<'source, 'pos, C, PC> {
|
||||
pub fn start(arr: &'source [C], pos: PC) -> ArraySourceIterator<'source, 'pos, C, PC> {
|
||||
let mut it = arr.iter();
|
||||
let first = it.next();
|
||||
return ArraySourceIterator {
|
||||
@ -45,13 +45,13 @@ impl<'source, 'pos, C: Copy, PC: PosCounter<'pos>> ArraySourceIterator<'source,
|
||||
_iter: it,
|
||||
_current: first,
|
||||
_posRaw: 0,
|
||||
_posHighlevel: PC::init(),
|
||||
_posHighlevel: pos,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C = C>> SourceIterator<'source, 'pos>
|
||||
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> SourceIterator<'source, 'pos>
|
||||
for ArraySourceIterator<'source, 'pos, C, PC>
|
||||
{
|
||||
type C = C;
|
||||
|
||||
@ -29,7 +29,7 @@ impl<'source> CollectedSubstring<'source> for StrCollectedSubstring<'source> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, C = char>> {
|
||||
pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, char>> {
|
||||
_source: &'source str,
|
||||
_iter: CharIndices<'source>,
|
||||
_current: Option<char>,
|
||||
@ -39,8 +39,8 @@ pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, C = char>> {
|
||||
__phantom: PhantomData<(&'pos ())>,
|
||||
}
|
||||
|
||||
impl<'source, 'pos, PC: PosCounter<'pos, C = char>> StrSourceIterator<'source, 'pos, PC> {
|
||||
pub fn start(s: &'source str) -> Self {
|
||||
impl<'source, 'pos, PC: PosCounter<'pos, char>> StrSourceIterator<'source, 'pos, PC> {
|
||||
pub fn start(s: &'source str, pos: PC) -> Self {
|
||||
let mut it = s.char_indices();
|
||||
let first = it.next();
|
||||
return StrSourceIterator {
|
||||
@ -49,13 +49,13 @@ impl<'source, 'pos, PC: PosCounter<'pos, C = char>> StrSourceIterator<'source, '
|
||||
_current: first.map(|(_, c)| c),
|
||||
_posRaw: first.map_or(s.len(), |(p, _)| p),
|
||||
_posCodePoints: 0,
|
||||
_posHighlevel: PC::init(),
|
||||
_posHighlevel: pos,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, PC: PosCounter<'pos, C = char>> SourceIterator<'source, 'pos>
|
||||
impl<'source, 'pos, PC: PosCounter<'pos, char>> SourceIterator<'source, 'pos>
|
||||
for StrSourceIterator<'source, 'pos, PC>
|
||||
{
|
||||
type C = char;
|
||||
|
||||
@ -1,13 +1,16 @@
|
||||
use std::marker::PhantomData;
|
||||
use crate::iterator::PosCounter;
|
||||
|
||||
pub struct IndexPosCounter<C> {
|
||||
pub struct IndexPosCounter {
|
||||
index: usize,
|
||||
__phantom: PhantomData<C>
|
||||
}
|
||||
|
||||
impl<C> PosCounter<'static> for IndexPosCounter<C> {
|
||||
type C = C;
|
||||
impl Default for IndexPosCounter {
|
||||
fn default() -> Self {
|
||||
return IndexPosCounter { index: 0 };
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> PosCounter<'static, C> for IndexPosCounter {
|
||||
type P = usize;
|
||||
|
||||
fn update(&mut self, c: C) {
|
||||
@ -17,8 +20,4 @@ impl<C> PosCounter<'static> for IndexPosCounter<C> {
|
||||
fn export(&self) -> usize {
|
||||
return self.index;
|
||||
}
|
||||
|
||||
fn init() -> Self {
|
||||
return IndexPosCounter { index: 0, __phantom: PhantomData::default() };
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
use std::marker::PhantomData;
|
||||
use crate::iterator::PosCounter;
|
||||
use source_stream_0::Pos;
|
||||
use crate::pos::IndexPosCounter;
|
||||
|
||||
pub struct PosLineCol {
|
||||
pub row: usize,
|
||||
@ -9,14 +10,13 @@ pub struct PosLineCol {
|
||||
|
||||
impl Pos<'static> for PosLineCol {}
|
||||
|
||||
pub struct NewLinePosCounter<C> {
|
||||
pub struct NewLinePosCounter {
|
||||
row: usize,
|
||||
col: usize,
|
||||
__phantom: PhantomData<C>
|
||||
}
|
||||
|
||||
impl<C: PartialEq> NewLinePosCounter<C> {
|
||||
pub fn _update(&mut self, actual: C, expected: C) {
|
||||
impl NewLinePosCounter {
|
||||
pub fn _update<C: PartialEq>(&mut self, actual: C, expected: C) {
|
||||
if actual == expected {
|
||||
self.row += 1;
|
||||
self.col = 0;
|
||||
@ -31,14 +31,15 @@ impl<C: PartialEq> NewLinePosCounter<C> {
|
||||
col: self.col,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn _init() -> Self {
|
||||
return NewLinePosCounter { row: 0, col: 0, __phantom: Default::default() };
|
||||
impl Default for NewLinePosCounter {
|
||||
fn default() -> Self {
|
||||
return NewLinePosCounter { row: 0, col: 0 };
|
||||
}
|
||||
}
|
||||
|
||||
impl PosCounter<'static> for NewLinePosCounter<char> {
|
||||
type C = char;
|
||||
impl PosCounter<'static, char> for NewLinePosCounter {
|
||||
type P = PosLineCol;
|
||||
|
||||
fn update(&mut self, c: char) {
|
||||
@ -49,13 +50,9 @@ impl PosCounter<'static> for NewLinePosCounter<char> {
|
||||
self._export()
|
||||
}
|
||||
|
||||
fn init() -> Self {
|
||||
return NewLinePosCounter::_init();
|
||||
}
|
||||
}
|
||||
|
||||
impl PosCounter<'static> for NewLinePosCounter<u8> {
|
||||
type C = u8;
|
||||
impl PosCounter<'static, u8> for NewLinePosCounter {
|
||||
type P = PosLineCol;
|
||||
|
||||
fn update(&mut self, c: u8) {
|
||||
@ -66,7 +63,4 @@ impl PosCounter<'static> for NewLinePosCounter<u8> {
|
||||
self._export()
|
||||
}
|
||||
|
||||
fn init() -> Self {
|
||||
return NewLinePosCounter::_init();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,21 +1,17 @@
|
||||
use crate::iterator::PosCounter;
|
||||
use std::marker::PhantomData;
|
||||
pub struct NopPosCounter {}
|
||||
|
||||
pub struct NopPosCounter<C> {
|
||||
__phantom: PhantomData<C>,
|
||||
impl Default for NopPosCounter {
|
||||
fn default() -> Self {
|
||||
return NopPosCounter {};
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> PosCounter<'static> for NopPosCounter<C> {
|
||||
type C = C;
|
||||
impl<C> PosCounter<'static, C> for NopPosCounter {
|
||||
type P = ();
|
||||
|
||||
fn update(&mut self, c: C) {}
|
||||
|
||||
fn export(&self) {}
|
||||
|
||||
fn init() -> Self {
|
||||
return NopPosCounter {
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user