Rewrote generics to associated types
This commit is contained in:
parent
cb743f94d2
commit
732c5a12fc
@ -1,11 +1,11 @@
|
||||
/*use source_stream_0::{CollectedSubstring, Pos, SourceStream};
|
||||
use crate::iterators::StrSourceIterator;
|
||||
use crate::pos::{IndexPosCounter, NewLinePosCounter, PosLineCol};
|
||||
use crate::{_CollectScopeContext, SourceIterator};
|
||||
|
||||
#[test]
|
||||
fn sandbox() {
|
||||
let mut z: StrSourceIterator<'_, '_, usize, IndexPosCounter> =
|
||||
StrSourceIterator::start("12\n34");
|
||||
let mut z = StrSourceIterator::start("12\n34");
|
||||
println!("{}", z.pos());
|
||||
z.next();
|
||||
println!("{} ", z.pos());
|
||||
@ -30,3 +30,4 @@ fn sandbox() {
|
||||
|
||||
return;
|
||||
}
|
||||
*/
|
||||
@ -1,26 +1,69 @@
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Pos};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub trait PosCounter<'pos, C, P: Pos<'pos>> {
|
||||
fn update(&mut self, c: C);
|
||||
pub trait PosCounter<'pos> {
|
||||
type C;
|
||||
type P: Pos<'pos>;
|
||||
|
||||
fn export(&self) -> P;
|
||||
fn update(&mut self, c: Self::C);
|
||||
|
||||
fn export(&self) -> Self::P;
|
||||
|
||||
fn init() -> Self;
|
||||
}
|
||||
pub trait _CollectScopeContext<'source, C> {
|
||||
fn next(&mut self) -> Option<C>;
|
||||
fn current(&self) -> Option<C>;
|
||||
|
||||
pub trait SourceIterator<'source, 'pos> {
|
||||
type C;
|
||||
type P: Pos<'pos>;
|
||||
type CS: CollectedSubstring<'source, C = Self::C>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::C>;
|
||||
fn current(&self) -> Option<Self::C>;
|
||||
fn pos(&self) -> Self::P;
|
||||
|
||||
fn collect(&mut self, scope: &mut impl _SourceIteratorCollect::Lambda<C=Self::C>) -> CollectResult<Self::CS>;
|
||||
}
|
||||
|
||||
pub trait SourceIterator<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C>>:
|
||||
_CollectScopeContext<'source, C>
|
||||
{
|
||||
fn pos(&self) -> P;
|
||||
pub mod _SourceIteratorCollect {
|
||||
use crate::SourceIterator;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
type CollectScopeContext: _CollectScopeContext<'source, C>;
|
||||
pub trait Context<'source> {
|
||||
type C;
|
||||
fn next(&mut self) -> Option<Self::C>;
|
||||
fn current(&self) -> Option<Self::C>;
|
||||
}
|
||||
|
||||
fn collect(
|
||||
&mut self,
|
||||
scope: impl FnOnce(&mut Self::CollectScopeContext) -> bool,
|
||||
) -> CollectResult<CS>;
|
||||
pub trait Lambda {
|
||||
type C;
|
||||
fn collect<'source>(&mut self, src: &mut impl Context<'source, C=Self::C>) -> bool;
|
||||
}
|
||||
|
||||
pub struct DefaultImpl<'source, 'pos, 'iter, I: SourceIterator<'source, 'pos>> {
|
||||
_full: &'iter mut I,
|
||||
__phantom: PhantomData<(&'source (), &'pos ())>,
|
||||
}
|
||||
|
||||
impl<'source, 'pos, 'iter, I: SourceIterator<'source, 'pos>> DefaultImpl<'source, 'pos, 'iter, I> {
|
||||
pub fn wrap(s: &'iter mut I) -> Self {
|
||||
return DefaultImpl {
|
||||
_full: s,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, 'iter, I: SourceIterator<'source, 'pos>> Context<'source>
|
||||
for DefaultImpl<'source, 'pos, 'iter, I>
|
||||
{
|
||||
type C = I::C;
|
||||
|
||||
fn next(&mut self) -> Option<Self::C> {
|
||||
return self._full.next();
|
||||
}
|
||||
|
||||
fn current(&self) -> Option<Self::C> {
|
||||
return self._full.current();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::iterator::{_CollectScopeContext, PosCounter, SourceIterator};
|
||||
use crate::_SourceIteratorCollect;
|
||||
use crate::iterator::{PosCounter, SourceIterator};
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator, Pos};
|
||||
use std::marker::PhantomData;
|
||||
use std::slice::Iter;
|
||||
@ -7,8 +8,9 @@ pub struct ArrayCollectedSubstring<'source, C> {
|
||||
pub slice: &'source [C],
|
||||
}
|
||||
|
||||
impl<'source, C: Copy> CollectedSubstring<'source, C> for ArrayCollectedSubstring<'source, C> {
|
||||
fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C>) -> bool {
|
||||
impl<'source, C: Copy> CollectedSubstring<'source> for ArrayCollectedSubstring<'source, C> {
|
||||
type C = C;
|
||||
fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C = C>) -> bool {
|
||||
let mut kwi;
|
||||
match kw.startComparation(self.slice.len()) {
|
||||
None => return false,
|
||||
@ -25,20 +27,17 @@ impl<'source, C: Copy> CollectedSubstring<'source, C> for ArrayCollectedSubstrin
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ArraySourceIterator<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> {
|
||||
pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos>> {
|
||||
_source: &'source [C],
|
||||
_iter: Iter<'source, C>,
|
||||
_current: Option<&'source C>,
|
||||
_posRaw: usize,
|
||||
_posHighlevel: PC,
|
||||
__phantom1: PhantomData<&'pos ()>,
|
||||
__phantom2: PhantomData<P>,
|
||||
__phantom: PhantomData<&'pos ()>,
|
||||
}
|
||||
|
||||
impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>>
|
||||
ArraySourceIterator<'source, 'pos, C, P, PC>
|
||||
{
|
||||
pub fn start(arr: &'source [C]) -> ArraySourceIterator<'source, 'pos, C, P, PC> {
|
||||
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos>> ArraySourceIterator<'source, 'pos, C, PC> {
|
||||
pub fn start(arr: &'source [C]) -> ArraySourceIterator<'source, 'pos, C, PC> {
|
||||
let mut it = arr.iter();
|
||||
let first = it.next();
|
||||
return ArraySourceIterator {
|
||||
@ -47,15 +46,18 @@ impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>>
|
||||
_current: first,
|
||||
_posRaw: 0,
|
||||
_posHighlevel: PC::init(),
|
||||
__phantom1: PhantomData::default(),
|
||||
__phantom2: PhantomData::default(),
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>>
|
||||
_CollectScopeContext<'source, C> for ArraySourceIterator<'source, 'pos, C, P, PC>
|
||||
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C = C>> SourceIterator<'source, 'pos>
|
||||
for ArraySourceIterator<'source, 'pos, C, PC>
|
||||
{
|
||||
type C = C;
|
||||
type P = PC::P;
|
||||
type CS = ArrayCollectedSubstring<'source, C>;
|
||||
|
||||
fn next(&mut self) -> Option<C> {
|
||||
if let Some(c) = self._current {
|
||||
self._posHighlevel.update(*c);
|
||||
@ -68,21 +70,14 @@ impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>>
|
||||
fn current(&self) -> Option<C> {
|
||||
return self._current.map(|c| *c);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>>
|
||||
SourceIterator<'source, 'pos, C, P, ArrayCollectedSubstring<'source, C>>
|
||||
for ArraySourceIterator<'source, 'pos, C, P, PC>
|
||||
{
|
||||
fn pos(&self) -> P {
|
||||
fn pos(&self) -> Self::P {
|
||||
return self._posHighlevel.export();
|
||||
}
|
||||
|
||||
type CollectScopeContext = ArraySourceIterator<'source, 'pos, C, P, PC>;
|
||||
|
||||
fn collect(
|
||||
&mut self,
|
||||
scope: impl FnOnce(&mut Self::CollectScopeContext) -> bool,
|
||||
scope: &mut impl _SourceIteratorCollect::Lambda<C = C>,
|
||||
) -> CollectResult<ArrayCollectedSubstring<'source, C>> {
|
||||
if self._current.is_none() {
|
||||
return CollectResult::EOF;
|
||||
@ -90,7 +85,7 @@ impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>>
|
||||
|
||||
let startPosRaw = self._posRaw;
|
||||
|
||||
match scope(self) {
|
||||
match scope.collect(&mut _SourceIteratorCollect::DefaultImpl::wrap(self)) {
|
||||
false => return CollectResult::NotMatches,
|
||||
true => {
|
||||
let slice: &'source [C];
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
use crate::iterator::{_CollectScopeContext, PosCounter, SourceIterator};
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator, Pos};
|
||||
use crate::_SourceIteratorCollect;
|
||||
use crate::iterator::{PosCounter, SourceIterator};
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator};
|
||||
use std::marker::PhantomData;
|
||||
use std::str::CharIndices;
|
||||
|
||||
@ -8,8 +9,10 @@ pub struct StrCollectedSubstring<'source> {
|
||||
pub size: usize,
|
||||
}
|
||||
|
||||
impl<'source> CollectedSubstring<'source, char> for StrCollectedSubstring<'source> {
|
||||
fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, char>) -> bool {
|
||||
impl<'source> CollectedSubstring<'source> for StrCollectedSubstring<'source> {
|
||||
type C = char;
|
||||
|
||||
fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C = char>) -> bool {
|
||||
let mut kwi;
|
||||
match kw.startComparation(self.size) {
|
||||
None => return false,
|
||||
@ -26,21 +29,18 @@ impl<'source> CollectedSubstring<'source, char> for StrCollectedSubstring<'sourc
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StrSourceIterator<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> {
|
||||
pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, C = char>> {
|
||||
_source: &'source str,
|
||||
_iter: CharIndices<'source>,
|
||||
_current: Option<char>,
|
||||
_posRaw: usize,
|
||||
_posCodePoints: usize,
|
||||
_posHighlevel: PC,
|
||||
__phantom1: PhantomData<&'pos ()>,
|
||||
__phantom2: PhantomData<P>,
|
||||
__phantom: PhantomData<(&'pos ())>,
|
||||
}
|
||||
|
||||
impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>>
|
||||
StrSourceIterator<'source, 'pos, P, PC>
|
||||
{
|
||||
pub fn start(s: &'source str) -> StrSourceIterator<'source, 'pos, P, PC> {
|
||||
impl<'source, 'pos, PC: PosCounter<'pos, C = char>> StrSourceIterator<'source, 'pos, PC> {
|
||||
pub fn start(s: &'source str) -> Self {
|
||||
let mut it = s.char_indices();
|
||||
let first = it.next();
|
||||
return StrSourceIterator {
|
||||
@ -50,15 +50,18 @@ impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>>
|
||||
_posRaw: first.map_or(s.len(), |(p, _)| p),
|
||||
_posCodePoints: 0,
|
||||
_posHighlevel: PC::init(),
|
||||
__phantom1: PhantomData::default(),
|
||||
__phantom2: PhantomData::default(),
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> _CollectScopeContext<'source, char>
|
||||
for StrSourceIterator<'source, 'pos, P, PC>
|
||||
impl<'source, 'pos, PC: PosCounter<'pos, C = char>> SourceIterator<'source, 'pos>
|
||||
for StrSourceIterator<'source, 'pos, PC>
|
||||
{
|
||||
type C = char;
|
||||
type P = PC::P;
|
||||
type CS = StrCollectedSubstring<'source>;
|
||||
|
||||
fn next(&mut self) -> Option<char> {
|
||||
match self._current {
|
||||
None => return None,
|
||||
@ -85,21 +88,14 @@ impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> _CollectScopeCo
|
||||
fn current(&self) -> Option<char> {
|
||||
return self._current;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>>
|
||||
SourceIterator<'source, 'pos, char, P, StrCollectedSubstring<'source>>
|
||||
for StrSourceIterator<'source, 'pos, P, PC>
|
||||
{
|
||||
fn pos(&self) -> P {
|
||||
fn pos(&self) -> PC::P {
|
||||
return self._posHighlevel.export();
|
||||
}
|
||||
|
||||
type CollectScopeContext = StrSourceIterator<'source, 'pos, P, PC>;
|
||||
|
||||
fn collect(
|
||||
&mut self,
|
||||
scope: impl FnOnce(&mut Self::CollectScopeContext) -> bool,
|
||||
scope: &mut impl _SourceIteratorCollect::Lambda<C = char>,
|
||||
) -> CollectResult<StrCollectedSubstring<'source>> {
|
||||
if self._current.is_none() {
|
||||
return CollectResult::EOF;
|
||||
@ -108,7 +104,7 @@ impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>>
|
||||
let startPosRaw = self._posRaw;
|
||||
let startPosCodePoints = self._posCodePoints;
|
||||
|
||||
match scope(self) {
|
||||
match scope.collect(&mut _SourceIteratorCollect::DefaultImpl::wrap(self)) {
|
||||
false => return CollectResult::NotMatches,
|
||||
true => {
|
||||
let slice: &'source str;
|
||||
|
||||
@ -2,7 +2,7 @@ mod iterator;
|
||||
mod stream;
|
||||
pub use iterator::PosCounter;
|
||||
pub use iterator::SourceIterator;
|
||||
pub use iterator::_CollectScopeContext;
|
||||
pub use iterator::_SourceIteratorCollect;
|
||||
pub use stream::SourceStreamOverIterator;
|
||||
|
||||
pub mod pos;
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
use std::marker::PhantomData;
|
||||
use crate::iterator::PosCounter;
|
||||
|
||||
pub struct IndexPosCounter {
|
||||
pub struct IndexPosCounter<C> {
|
||||
index: usize,
|
||||
__phantom: PhantomData<C>
|
||||
}
|
||||
|
||||
impl<C> PosCounter<'static, C, usize> for IndexPosCounter {
|
||||
impl<C> PosCounter<'static> for IndexPosCounter<C> {
|
||||
type C = C;
|
||||
type P = usize;
|
||||
|
||||
fn update(&mut self, c: C) {
|
||||
self.index += 1;
|
||||
}
|
||||
@ -14,6 +19,6 @@ impl<C> PosCounter<'static, C, usize> for IndexPosCounter {
|
||||
}
|
||||
|
||||
fn init() -> Self {
|
||||
return IndexPosCounter { index: 0 };
|
||||
return IndexPosCounter { index: 0, __phantom: PhantomData::default() };
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use std::marker::PhantomData;
|
||||
use crate::iterator::PosCounter;
|
||||
use source_stream_0::Pos;
|
||||
|
||||
@ -8,13 +9,14 @@ pub struct PosLineCol {
|
||||
|
||||
impl Pos<'static> for PosLineCol {}
|
||||
|
||||
pub struct NewLinePosCounter {
|
||||
pub struct NewLinePosCounter<C> {
|
||||
row: usize,
|
||||
col: usize,
|
||||
__phantom: PhantomData<C>
|
||||
}
|
||||
|
||||
impl NewLinePosCounter {
|
||||
pub fn _update<C: PartialEq>(&mut self, actual: C, expected: C) {
|
||||
impl<C: PartialEq> NewLinePosCounter<C> {
|
||||
pub fn _update(&mut self, actual: C, expected: C) {
|
||||
if actual == expected {
|
||||
self.row += 1;
|
||||
self.col = 0;
|
||||
@ -31,11 +33,14 @@ impl NewLinePosCounter {
|
||||
}
|
||||
|
||||
pub fn _init() -> Self {
|
||||
return NewLinePosCounter { row: 0, col: 0 };
|
||||
return NewLinePosCounter { row: 0, col: 0, __phantom: Default::default() };
|
||||
}
|
||||
}
|
||||
|
||||
impl PosCounter<'static, char, PosLineCol> for NewLinePosCounter {
|
||||
impl PosCounter<'static> for NewLinePosCounter<char> {
|
||||
type C = char;
|
||||
type P = PosLineCol;
|
||||
|
||||
fn update(&mut self, c: char) {
|
||||
self._update(c, '\n')
|
||||
}
|
||||
@ -49,7 +54,10 @@ impl PosCounter<'static, char, PosLineCol> for NewLinePosCounter {
|
||||
}
|
||||
}
|
||||
|
||||
impl PosCounter<'static, u8, PosLineCol> for NewLinePosCounter {
|
||||
impl PosCounter<'static> for NewLinePosCounter<u8> {
|
||||
type C = u8;
|
||||
type P = PosLineCol;
|
||||
|
||||
fn update(&mut self, c: u8) {
|
||||
self._update(c, b'\n')
|
||||
}
|
||||
|
||||
@ -1,12 +1,21 @@
|
||||
use crate::iterator::PosCounter;
|
||||
pub struct NopPosCounter {}
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct NopPosCounter<C> {
|
||||
__phantom: PhantomData<C>,
|
||||
}
|
||||
|
||||
impl<C> PosCounter<'static> for NopPosCounter<C> {
|
||||
type C = C;
|
||||
type P = ();
|
||||
|
||||
impl<C> PosCounter<'static, C, ()> for NopPosCounter {
|
||||
fn update(&mut self, c: C) {}
|
||||
|
||||
fn export(&self) {}
|
||||
|
||||
fn init() -> Self {
|
||||
return NopPosCounter {};
|
||||
return NopPosCounter {
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,54 +1,60 @@
|
||||
use crate::iterator::{_CollectScopeContext, SourceIterator};
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Pos, Predicate, SourceStream};
|
||||
use crate::_SourceIteratorCollect;
|
||||
use crate::iterator::SourceIterator;
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Predicate, SourceStream};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct SourceStreamOverIterator<
|
||||
'source,
|
||||
'pos,
|
||||
C,
|
||||
P: Pos<'pos>,
|
||||
CS: CollectedSubstring<'source, C>,
|
||||
I: SourceIterator<'source, 'pos, C, P, CS>,
|
||||
> {
|
||||
pub struct SourceStreamOverIterator<'source, 'pos, I: SourceIterator<'source, 'pos>> {
|
||||
_iter: I,
|
||||
__phantom1: PhantomData<&'source ()>,
|
||||
__phantom2: PhantomData<&'pos ()>,
|
||||
__phantom3: PhantomData<C>,
|
||||
__phantom4: PhantomData<P>,
|
||||
__phantom5: PhantomData<CS>,
|
||||
__phantom: PhantomData<(&'source (), &'pos ())>,
|
||||
}
|
||||
|
||||
impl<
|
||||
'source,
|
||||
'pos,
|
||||
C,
|
||||
P: Pos<'pos>,
|
||||
CS: CollectedSubstring<'source, C>,
|
||||
I: SourceIterator<'source, 'pos, C, P, CS>,
|
||||
> SourceStreamOverIterator<'source, 'pos, C, P, CS, I>
|
||||
{
|
||||
fn wrap(iter: I) -> SourceStreamOverIterator<'source, 'pos, C, P, CS, I> {
|
||||
impl<'source, 'pos, I: SourceIterator<'source, 'pos>> SourceStreamOverIterator<'source, 'pos, I> {
|
||||
fn wrap(iter: I) -> SourceStreamOverIterator<'source, 'pos, I> {
|
||||
return SourceStreamOverIterator {
|
||||
_iter: iter,
|
||||
__phantom1: PhantomData::default(),
|
||||
__phantom2: PhantomData::default(),
|
||||
__phantom3: PhantomData::default(),
|
||||
__phantom4: PhantomData::default(),
|
||||
__phantom5: PhantomData::default(),
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
'source,
|
||||
'pos,
|
||||
C,
|
||||
P: Pos<'pos>,
|
||||
CS: CollectedSubstring<'source, C>,
|
||||
I: SourceIterator<'source, 'pos, C, P, CS>,
|
||||
> SourceStream<'source, 'pos, C, P, CS> for SourceStreamOverIterator<'source, 'pos, C, P, CS, I>
|
||||
struct CollectFunction<'predicate, C, I: Predicate<C = C>> {
|
||||
predicate: &'predicate mut I,
|
||||
}
|
||||
|
||||
impl<'predicate, C, I: Predicate<C = C>> _SourceIteratorCollect::Lambda for CollectFunction<'predicate, C, I> {
|
||||
type C = C;
|
||||
|
||||
fn collect<'source>(&mut self, src: &mut impl _SourceIteratorCollect::Context<'source, C = Self::C>) -> bool {
|
||||
match src.current() {
|
||||
Some(c) => match self.predicate.check(c) {
|
||||
false => return false,
|
||||
true => {}
|
||||
},
|
||||
None => return false,
|
||||
}
|
||||
|
||||
loop {
|
||||
match src.next() {
|
||||
None => {
|
||||
return true;
|
||||
}
|
||||
Some(c) => match self.predicate.check(c) {
|
||||
true => continue,
|
||||
false => return true,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source, 'pos, I: SourceIterator<'source, 'pos>> SourceStream<'source, 'pos>
|
||||
for SourceStreamOverIterator<'source, 'pos, I>
|
||||
{
|
||||
fn skip(&mut self, predicate: &mut impl Predicate<C>) -> bool {
|
||||
type C = I::C;
|
||||
type P = I::P;
|
||||
type CS = I::CS;
|
||||
|
||||
fn skip(&mut self, predicate: &mut impl Predicate<C = I::C>) -> bool {
|
||||
match self._iter.current() {
|
||||
Some(c) => match predicate.check(c) {
|
||||
false => return false,
|
||||
@ -68,39 +74,19 @@ impl<
|
||||
}
|
||||
}
|
||||
|
||||
fn collect(&mut self, predicate: &mut impl Predicate<C>) -> CollectResult<CS> {
|
||||
return self._iter.collect(|iter| {
|
||||
match iter.current() {
|
||||
Some(c) => match predicate.check(c) {
|
||||
false => return false,
|
||||
true => {}
|
||||
},
|
||||
None => return false,
|
||||
fn collect(&mut self, predicate: &mut impl Predicate<C = I::C>) -> CollectResult<I::CS> {
|
||||
return self._iter.collect(&mut CollectFunction { predicate });
|
||||
}
|
||||
|
||||
loop {
|
||||
match iter.next() {
|
||||
None => {
|
||||
return true;
|
||||
}
|
||||
Some(c) => match predicate.check(c) {
|
||||
true => continue,
|
||||
false => return true,
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn pos(&self) -> P {
|
||||
fn pos(&self) -> I::P {
|
||||
return self._iter.pos();
|
||||
}
|
||||
|
||||
fn currentChar(&self) -> Option<C> {
|
||||
fn currentChar(&self) -> Option<I::C> {
|
||||
return self._iter.current();
|
||||
}
|
||||
|
||||
fn nextChar(&mut self) -> Option<C> {
|
||||
fn nextChar(&mut self) -> Option<I::C> {
|
||||
return self._iter.next();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,17 +1,21 @@
|
||||
use std::slice::Iter;
|
||||
use std::str::Chars;
|
||||
|
||||
impl <'keyword> crate::KeywordComparatorIterator<'keyword, char> for Chars<'keyword> {
|
||||
impl<'keyword> crate::KeywordComparatorIterator<'keyword> for Chars<'keyword> {
|
||||
type C = char;
|
||||
|
||||
fn consume(&mut self, e: char) -> bool {
|
||||
return self.next().is_some_and(|a| a == e);
|
||||
}
|
||||
}
|
||||
|
||||
impl <'keyword> crate::Keyword<'keyword, char> for &'keyword str {
|
||||
impl<'keyword> crate::Keyword<'keyword> for &'keyword str {
|
||||
type C = char;
|
||||
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = char>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
@ -20,11 +24,13 @@ impl <'keyword> crate::Keyword<'keyword, char> for &'keyword str {
|
||||
}
|
||||
}
|
||||
|
||||
impl <'keyword> crate::Keyword<'keyword, char> for &'keyword String {
|
||||
impl<'keyword> crate::Keyword<'keyword> for &'keyword String {
|
||||
type C = char;
|
||||
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = char>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
@ -32,17 +38,20 @@ impl <'keyword> crate::Keyword<'keyword, char> for &'keyword String {
|
||||
return Option::Some(self.chars());
|
||||
}
|
||||
}
|
||||
impl<'keyword, T: PartialEq> crate::KeywordComparatorIterator<'keyword, T> for Iter<'keyword, T> {
|
||||
impl<'keyword, T: PartialEq> crate::KeywordComparatorIterator<'keyword> for Iter<'keyword, T> {
|
||||
type C = T;
|
||||
fn consume(&mut self, e: T) -> bool {
|
||||
return self.next().is_some_and(|a| a.eq(&e));
|
||||
}
|
||||
}
|
||||
|
||||
impl<'keyword, T: PartialEq> crate::Keyword<'keyword, T> for &'keyword [T] {
|
||||
impl<'keyword, T: PartialEq> crate::Keyword<'keyword> for &'keyword [T] {
|
||||
type C = T;
|
||||
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = T>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
@ -51,11 +60,13 @@ impl<'keyword, T: PartialEq> crate::Keyword<'keyword, T> for &'keyword [T] {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'keyword, T: PartialEq, const SZ: usize> crate::Keyword<'keyword, T> for &'keyword [T; SZ] {
|
||||
impl<'keyword, T: PartialEq, const SZ: usize> crate::Keyword<'keyword> for &'keyword [T; SZ] {
|
||||
type C = T;
|
||||
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C=T>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
|
||||
42
src/lib.rs
42
src/lib.rs
@ -1,8 +1,11 @@
|
||||
mod _keyword_impls;
|
||||
mod macros;
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
pub trait Predicate<C> {
|
||||
fn check(&mut self, chr: C) -> bool;
|
||||
mod _keyword_impls;
|
||||
// mod macros;
|
||||
|
||||
pub trait Predicate {
|
||||
type C;
|
||||
fn check(&mut self, chr: Self::C) -> bool;
|
||||
}
|
||||
|
||||
pub trait Pos<'pos> {}
|
||||
@ -10,19 +13,22 @@ pub trait Pos<'pos> {}
|
||||
impl Pos<'static> for () {}
|
||||
impl Pos<'static> for usize {}
|
||||
|
||||
pub trait KeywordComparatorIterator<'keyword, C> {
|
||||
fn consume(&mut self, c: C) -> bool;
|
||||
pub trait KeywordComparatorIterator<'keyword> {
|
||||
type C;
|
||||
fn consume(&mut self, c: Self::C) -> bool;
|
||||
}
|
||||
|
||||
pub trait Keyword<'keyword, C> {
|
||||
pub trait Keyword<'keyword> {
|
||||
type C;
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl KeywordComparatorIterator<'keyword, C>>;
|
||||
) -> Option<impl KeywordComparatorIterator<'keyword, C=Self::C>>;
|
||||
}
|
||||
|
||||
pub trait CollectedSubstring<'source, C> {
|
||||
fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C>) -> bool;
|
||||
pub trait CollectedSubstring<'source> {
|
||||
type C;
|
||||
fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C=Self::C>) -> bool;
|
||||
}
|
||||
|
||||
pub enum CollectResult<T> {
|
||||
@ -31,17 +37,21 @@ pub enum CollectResult<T> {
|
||||
Matches(T),
|
||||
}
|
||||
|
||||
pub trait SourceStream<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C>> {
|
||||
pub trait SourceStream<'source, 'pos> {
|
||||
type C;
|
||||
type P: Pos<'pos>;
|
||||
type CS: CollectedSubstring<'source, C=Self::C>;
|
||||
|
||||
/**
|
||||
* Returns `true` if the end of stream reached.
|
||||
*/
|
||||
fn skip(&mut self, predicate: &mut impl Predicate<C>) -> bool;
|
||||
fn collect(&mut self, predicate: &mut impl Predicate<C>) -> CollectResult<CS>;
|
||||
fn skip(&mut self, predicate: &mut impl Predicate<C=Self::C>) -> bool;
|
||||
fn collect(&mut self, predicate: &mut impl Predicate<C=Self::C>) -> CollectResult<Self::CS>;
|
||||
|
||||
fn pos(&self) -> P;
|
||||
fn pos(&self) -> Self::P;
|
||||
|
||||
fn currentChar(&self) -> Option<C>;
|
||||
fn nextChar(&mut self) -> Option<C>;
|
||||
fn currentChar(&self) -> Option<Self::C>;
|
||||
fn nextChar(&mut self) -> Option<Self::C>;
|
||||
|
||||
fn isEnded(&mut self) -> bool {
|
||||
return self.currentChar().is_none();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user