190 lines
5.0 KiB
Rust
190 lines
5.0 KiB
Rust
#![allow(non_camel_case_types)]
|
|
#![allow(non_snake_case)]
|
|
|
|
use source_stream_0::{
|
|
CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator, Pos, Predicate,
|
|
SourceStream,
|
|
};
|
|
use std::marker::PhantomData;
|
|
|
|
pub trait SourceStreamConverter_Char<C> {
|
|
type WC;
|
|
fn wrapChar(c: C) -> Self::WC;
|
|
}
|
|
|
|
pub trait StreamConverter_Pos<'pos, P: Pos<'pos>> {
|
|
type WP: Pos<'pos>;
|
|
fn wrapPos(p: P) -> Self::WP;
|
|
}
|
|
|
|
pub trait StreamConverter_Substring<'source, C, CS: CollectedSubstring<'source, C = C>>:
|
|
SourceStreamConverter_Char<C>
|
|
{
|
|
type WCS: CollectedSubstring<'source, C = Self::WC>;
|
|
fn wrapSubstring(wcs: CS) -> Self::WCS;
|
|
}
|
|
|
|
pub trait StreamConverter<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C = C>>:
|
|
SourceStreamConverter_Char<C>
|
|
+ StreamConverter_Pos<'pos, P>
|
|
+ StreamConverter_Substring<'source, C, CS>
|
|
{
|
|
}
|
|
|
|
struct PredicateWrapper<
|
|
'predicate,
|
|
'converter,
|
|
C,
|
|
W: SourceStreamConverter_Char<C>,
|
|
I: Predicate<W::WC>,
|
|
> {
|
|
_orig: &'predicate mut I,
|
|
_converter: &'converter W,
|
|
__phantom: PhantomData<C>,
|
|
}
|
|
|
|
impl<'predicate, 'converter, C, W: SourceStreamConverter_Char<C>, I: Predicate<W::WC>>
|
|
PredicateWrapper<'predicate, 'converter, C, W, I>
|
|
{
|
|
fn wrap(pred: &'predicate mut I, converter: &'converter W) -> Self {
|
|
return PredicateWrapper {
|
|
_orig: pred,
|
|
_converter: converter,
|
|
__phantom: PhantomData::default(),
|
|
};
|
|
}
|
|
}
|
|
|
|
impl<'predicate, 'converter, C, W: SourceStreamConverter_Char<C>, I: Predicate<W::WC>> Predicate<C>
|
|
for PredicateWrapper<'predicate, 'converter, C, W, I>
|
|
{
|
|
fn check(&mut self, chr: C) -> bool {
|
|
return self._orig.check(W::wrapChar(chr));
|
|
}
|
|
}
|
|
|
|
pub struct SourceStreamWrapper<
|
|
'source,
|
|
'pos,
|
|
C,
|
|
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,
|
|
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 SourceStreamWrapper<'source, 'pos, C, P, CS, W, I>
|
|
{
|
|
type C = W::WC;
|
|
type P = W::WP;
|
|
type CS = W::WCS;
|
|
|
|
fn skip(&mut self, predicate: &mut impl Predicate<W::WC>) -> bool {
|
|
self._src
|
|
.skip(&mut PredicateWrapper::wrap(predicate, &self._converter))
|
|
}
|
|
|
|
fn collect(&mut self, predicate: &mut impl Predicate<W::WC>) -> CollectResult<W::WCS> {
|
|
match self
|
|
._src
|
|
.collect(&mut PredicateWrapper::wrap(predicate, &self._converter))
|
|
{
|
|
CollectResult::EOF => return CollectResult::EOF,
|
|
CollectResult::NotMatches => return CollectResult::NotMatches,
|
|
CollectResult::Matches(cs) => return CollectResult::Matches(W::wrapSubstring(cs)),
|
|
}
|
|
}
|
|
|
|
fn pos(&self) -> W::WP {
|
|
return W::wrapPos(self._src.pos());
|
|
}
|
|
|
|
fn currentChar(&self) -> Option<W::WC> {
|
|
return self._src.currentChar().map(W::wrapChar);
|
|
}
|
|
|
|
fn nextChar(&mut self) -> Option<W::WC> {
|
|
return self._src.nextChar().map(W::wrapChar);
|
|
}
|
|
}
|
|
|
|
pub struct KeywordUnwrapper<
|
|
'keyword,
|
|
'converter,
|
|
C,
|
|
W: SourceStreamConverter_Char<C>,
|
|
I: Keyword<W::WC>,
|
|
> {
|
|
_orig: &'keyword I,
|
|
_converter: &'converter W,
|
|
__phantom: PhantomData<C>,
|
|
}
|
|
|
|
impl<'keyword, 'converter, C, W: SourceStreamConverter_Char<C>, I: Keyword<W::WC>>
|
|
KeywordUnwrapper<'keyword, 'converter, C, W, I>
|
|
{
|
|
fn unwrap(kw: &'keyword I, converter: &'converter W) -> Self {
|
|
return KeywordUnwrapper {
|
|
_orig: kw,
|
|
_converter: converter,
|
|
__phantom: PhantomData::default(),
|
|
};
|
|
}
|
|
}
|
|
|
|
impl<'keyword, 'converter, C, W: SourceStreamConverter_Char<C>, I: Keyword<W::WC>> Keyword<C>
|
|
for KeywordUnwrapper<'keyword, 'converter, C, W, I>
|
|
{
|
|
fn startComparation<'self_>(
|
|
&'self_ self,
|
|
expectedLen: usize,
|
|
) -> Option<impl KeywordComparatorIterator<'self_, C>> {
|
|
return self
|
|
._orig
|
|
.startComparation(expectedLen)
|
|
.map(|it| KeywordComparatorWrapper {
|
|
_orig: it,
|
|
_converter: self._converter,
|
|
__phantom: PhantomData::default(),
|
|
});
|
|
}
|
|
}
|
|
|
|
struct KeywordComparatorWrapper<
|
|
'keyword,
|
|
'converter,
|
|
C,
|
|
W: SourceStreamConverter_Char<C>,
|
|
I: KeywordComparatorIterator<'keyword, W::WC>,
|
|
> {
|
|
_orig: I,
|
|
_converter: &'converter W,
|
|
__phantom: PhantomData<(&'keyword(), C)>,
|
|
}
|
|
|
|
impl<
|
|
'keyword,
|
|
'converter,
|
|
C,
|
|
W: SourceStreamConverter_Char<C>,
|
|
I: KeywordComparatorIterator<'keyword, W::WC>,
|
|
> KeywordComparatorIterator<'keyword, C>
|
|
for KeywordComparatorWrapper<'keyword, 'converter, C, W, I>
|
|
{
|
|
fn consume(&mut self, c: C) -> bool {
|
|
todo!()
|
|
}
|
|
}
|