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