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