Splitted stream converter to several files

This commit is contained in:
Andrew Golovashevich 2025-11-19 02:02:05 +03:00
parent b6bef111c1
commit 7e582faa3b
8 changed files with 276 additions and 248 deletions

View File

@ -1,247 +0,0 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use crate::{
CollectedSubstring, Keyword, KeywordComparatorIterator, Predicate, SourceStream,
};
use crate::pos::Pos;
use std::marker::PhantomData;
pub trait StreamConverter_Char<C: Copy> {
type WC: Copy;
fn convertChar(&self, c: C) -> Self::WC;
}
pub trait StreamConverter_Pos<'pos, P: Pos<'pos>> {
type WP: Pos<'pos>;
fn convertPos(&self, p: P) -> Self::WP;
}
pub trait StreamConverter_Substring<'source, C: Copy, CS: CollectedSubstring<'source, C = C>>:
StreamConverter_Char<C>
{
type WCS: CollectedSubstring<'source, C = Self::WC>;
fn convertSubstring(&self, wcs: CS) -> Self::WCS;
}
pub trait StreamConverter<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
>:
StreamConverter_Char<C> + StreamConverter_Pos<'pos, P> + StreamConverter_Substring<'source, C, CS>
{
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter_Char<C>
+ StreamConverter_Pos<'pos, P>
+ StreamConverter_Substring<'source, C, CS>,
> StreamConverter<'source, 'pos, C, P, CS> for W
{
}
struct PredicateConverter<
'predicate,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: Predicate<W::WC>,
> {
_orig: &'predicate mut I,
_converter: &'converter W,
__phantom: PhantomData<C>,
}
impl<'predicate, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Predicate<W::WC>>
PredicateConverter<'predicate, 'converter, C, W, I>
{
fn wrap(pred: &'predicate mut I, converter: &'converter W) -> Self {
return PredicateConverter {
_orig: pred,
_converter: converter,
__phantom: PhantomData::default(),
};
}
}
impl<'predicate, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Predicate<W::WC>> Predicate<C>
for PredicateConverter<'predicate, 'converter, C, W, I>
{
fn check(&mut self, chr: C) -> bool {
return self._orig.check(self._converter.convertChar(chr));
}
}
pub struct ConvertedSourceStream<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> {
_src: I,
_converter: W,
__phantom: PhantomData<(&'source (), &'pos (), W)>,
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> ConvertedSourceStream<'source, 'pos, C, P, CS, W, I>
{
pub fn convert(stream: I, converter: W) -> Self {
return ConvertedSourceStream {
_src: stream,
_converter: converter,
__phantom: PhantomData::default(),
};
}
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> SourceStream<'source, 'pos> for ConvertedSourceStream<'source, 'pos, C, P, CS, W, I>
{
type C = W::WC;
type P = W::WP;
type CS = W::WCS;
fn skipNext(&mut self, predicate: &mut impl Predicate<Self::C>) -> Option<Self::C> {
return self
._src
.skipNext(&mut PredicateConverter::wrap(predicate, &self._converter))
.map(|c| self._converter.convertChar(c));
}
fn collect(&mut self, predicate: &mut impl Predicate<Self::C>) -> (Self::CS, Option<Self::C>) {
let (cs, wrongChar) = self
._src
.collect(&mut PredicateConverter::wrap(predicate, &self._converter));
return (
self._converter.convertSubstring(cs),
wrongChar.map(|c| self._converter.convertChar(c)),
);
}
fn pos(&self) -> W::WP {
return self._converter.convertPos(self._src.pos());
}
fn nextChar(&mut self) -> Option<W::WC> {
return self._src.nextChar().map(|c| self._converter.convertChar(c));
}
}
pub struct KeywordDeconverted<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: Keyword<W::WC>,
> {
_orig: &'keyword I,
_converter: &'converter W,
__phantom: PhantomData<C>,
}
impl<'keyword, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Keyword<W::WC>>
KeywordDeconverted<'keyword, 'converter, C, W, I>
{
pub fn unwrap(kw: &'keyword I, converter: &'converter W) -> Self {
return KeywordDeconverted {
_orig: kw,
_converter: converter,
__phantom: PhantomData::default(),
};
}
}
impl<'keyword, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Keyword<W::WC>> Keyword<C>
for KeywordDeconverted<'keyword, 'converter, C, W, I>
{
fn startComparation<'self_>(
&'self_ self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'self_, C>> {
return self
._orig
.startComparation(expectedLen)
.map(|it| KeywordComparatorConverter {
_orig: it,
_converter: self._converter,
__phantom: PhantomData::default(),
});
}
}
struct KeywordComparatorConverter<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
> {
_orig: I,
_converter: &'converter W,
__phantom: PhantomData<(&'keyword (), C)>,
}
impl<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
> KeywordComparatorIterator<'keyword, C>
for KeywordComparatorConverter<'keyword, 'converter, C, W, I>
{
fn consume(&mut self, c: C) -> bool {
self._orig.consume(self._converter.convertChar(c))
}
}
pub trait StreamConverter_Char_Noop<C: Copy> {}
pub trait StreamConverter_Pos_Noop<'pos, P: Pos<'pos>> {}
pub trait StreamConverter_Substring_Noop<'source, C: Copy, CS: CollectedSubstring<'source, C = C>>:
StreamConverter_Char<C>
{
}
impl<C: Copy, W: StreamConverter_Char_Noop<C>> StreamConverter_Char<C> for W {
type WC = C;
fn convertChar(&self, c: C) -> Self::WC {
return c;
}
}
impl<'pos, P: Pos<'pos>, W: StreamConverter_Pos_Noop<'pos, P>> StreamConverter_Pos<'pos, P> for W {
type WP = P;
fn convertPos(&self, p: P) -> Self::WP {
return p;
}
}

View File

@ -0,0 +1,43 @@
use crate::CollectedSubstring;
use crate::pos::Pos;
pub trait StreamConverter_Char<C: Copy> {
type WC: Copy;
fn convertChar(&self, c: C) -> Self::WC;
}
pub trait StreamConverter_Pos<'pos, P: Pos<'pos>> {
type WP: Pos<'pos>;
fn convertPos(&self, p: P) -> Self::WP;
}
pub trait StreamConverter_Substring<'source, C: Copy, CS: CollectedSubstring<'source, C = C>>:
StreamConverter_Char<C>
{
type WCS: CollectedSubstring<'source, C = Self::WC>;
fn convertSubstring(&self, wcs: CS) -> Self::WCS;
}
pub trait StreamConverter<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
>:
StreamConverter_Char<C> + StreamConverter_Pos<'pos, P> + StreamConverter_Substring<'source, C, CS>
{
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter_Char<C>
+ StreamConverter_Pos<'pos, P>
+ StreamConverter_Substring<'source, C, CS>,
> StreamConverter<'source, 'pos, C, P, CS> for W
{
}

71
src/converter/keyword.rs Normal file
View File

@ -0,0 +1,71 @@
use crate::converter::StreamConverter_Char;
use crate::{Keyword, KeywordComparatorIterator};
use std::marker::PhantomData;
struct KeywordComparatorConverter<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
> {
_orig: I,
_converter: &'converter W,
__phantom: PhantomData<(&'keyword (), C)>,
}
impl<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
> KeywordComparatorIterator<'keyword, C>
for KeywordComparatorConverter<'keyword, 'converter, C, W, I>
{
fn consume(&mut self, c: C) -> bool {
self._orig.consume(self._converter.convertChar(c))
}
}
pub struct KeywordDeconverted<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: Keyword<W::WC>,
> {
_orig: &'keyword I,
_converter: &'converter W,
__phantom: PhantomData<C>,
}
impl<'keyword, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Keyword<W::WC>>
KeywordDeconverted<'keyword, 'converter, C, W, I>
{
pub fn deconvert(kw: &'keyword I, converter: &'converter W) -> Self {
return KeywordDeconverted {
_orig: kw,
_converter: converter,
__phantom: PhantomData::default(),
};
}
}
impl<'keyword, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Keyword<W::WC>> Keyword<C>
for KeywordDeconverted<'keyword, 'converter, C, W, I>
{
fn startComparation<'self_>(
&'self_ self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'self_, C>> {
return self
._orig
.startComparation(expectedLen)
.map(|it| KeywordComparatorConverter {
_orig: it,
_converter: self._converter,
__phantom: PhantomData::default(),
});
}
}

19
src/converter/mod.rs Normal file
View File

@ -0,0 +1,19 @@
mod converters;
mod keyword;
mod noop;
mod predicate;
mod stream;
pub use converters::StreamConverter;
pub use converters::StreamConverter_Char;
pub use converters::StreamConverter_Pos;
pub use converters::StreamConverter_Substring;
pub use noop::StreamConverter_Char_Noop;
pub use noop::StreamConverter_Pos_Noop;
pub(crate) use predicate::PredicateConverter;
pub use keyword::KeywordDeconverted;
pub use stream::ConvertedSourceStream;

29
src/converter/noop.rs Normal file
View File

@ -0,0 +1,29 @@
use crate::converter::{StreamConverter_Char, StreamConverter_Pos};
use crate::pos::Pos;
pub trait StreamConverter_Char_Noop<C: Copy> {}
pub trait StreamConverter_Pos_Noop<'pos, P: Pos<'pos>> {}
/*
pub trait StreamConverter_Substring_Noop<'source, C: Copy, CS: CollectedSubstring<'source, C = C>>:
StreamConverter_Char<C>
{
}
*/
impl<C: Copy, W: StreamConverter_Char_Noop<C>> StreamConverter_Char<C> for W {
type WC = C;
fn convertChar(&self, c: C) -> Self::WC {
return c;
}
}
impl<'pos, P: Pos<'pos>, W: StreamConverter_Pos_Noop<'pos, P>> StreamConverter_Pos<'pos, P> for W {
type WP = P;
fn convertPos(&self, p: P) -> Self::WP {
return p;
}
}

View File

@ -0,0 +1,35 @@
use std::marker::PhantomData;
use crate::converter::StreamConverter_Char;
use crate::Predicate;
pub(crate) struct PredicateConverter<
'predicate,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: Predicate<W::WC>,
> {
_orig: &'predicate mut I,
_converter: &'converter W,
__phantom: PhantomData<C>,
}
impl<'predicate, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Predicate<W::WC>>
PredicateConverter<'predicate, 'converter, C, W, I>
{
pub(crate) fn wrap(pred: &'predicate mut I, converter: &'converter W) -> Self {
return PredicateConverter {
_orig: pred,
_converter: converter,
__phantom: PhantomData::default(),
};
}
}
impl<'predicate, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Predicate<W::WC>> Predicate<C>
for PredicateConverter<'predicate, 'converter, C, W, I>
{
fn check(&mut self, chr: C) -> bool {
return self._orig.check(self._converter.convertChar(chr));
}
}

78
src/converter/stream.rs Normal file
View File

@ -0,0 +1,78 @@
use std::marker::PhantomData;
use crate::{CollectedSubstring, Predicate, SourceStream};
use crate::converter::{PredicateConverter, StreamConverter};
use crate::pos::Pos;
pub struct ConvertedSourceStream<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> {
_src: I,
_converter: W,
__phantom: PhantomData<(&'source (), &'pos (), W)>,
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> ConvertedSourceStream<'source, 'pos, C, P, CS, W, I>
{
pub fn convert(stream: I, converter: W) -> Self {
return ConvertedSourceStream {
_src: stream,
_converter: converter,
__phantom: PhantomData::default(),
};
}
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> SourceStream<'source, 'pos> for ConvertedSourceStream<'source, 'pos, C, P, CS, W, I>
{
type C = W::WC;
type P = W::WP;
type CS = W::WCS;
fn skipNext(&mut self, predicate: &mut impl Predicate<Self::C>) -> Option<Self::C> {
return self
._src
.skipNext(&mut PredicateConverter::wrap(predicate, &self._converter))
.map(|c| self._converter.convertChar(c));
}
fn collect(&mut self, predicate: &mut impl Predicate<Self::C>) -> (Self::CS, Option<Self::C>) {
let (cs, wrongChar) = self
._src
.collect(&mut PredicateConverter::wrap(predicate, &self._converter));
return (
self._converter.convertSubstring(cs),
wrongChar.map(|c| self._converter.convertChar(c)),
);
}
fn pos(&self) -> W::WP {
return self._converter.convertPos(self._src.pos());
}
fn nextChar(&mut self) -> Option<W::WC> {
return self._src.nextChar().map(|c| self._converter.convertChar(c));
}
}

View File

@ -4,8 +4,8 @@ mod _keyword_impls;
mod macros;
mod default_streams;
mod ascii;
mod converter;
pub mod pos;
pub mod converter;
use pos::Pos;