248 lines
6.4 KiB
Rust
248 lines
6.4 KiB
Rust
#![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;
|
|
}
|
|
}
|