maciejhirsz / logos

Create ridiculously fast Lexers
https://logos.maciej.codes
Apache License 2.0
2.9k stars 118 forks source link

[Question] Segmented source input? #270

Open hh9527 opened 1 year ago

hh9527 commented 1 year ago

Is it possible to implement lexer over a segement input (E.g. RopeSlice) ?

I found that there is a Source trait in logos, but I have no idea how to implement these methods over RopeSlice.

fn slice(&self, range: Range<usize>) -> Option<&Self::Slice>;
unsafe fn slice_unchecked(&self, range: Range<usize>) -> &Self::Slice;

Those 2 methods require returning a reference, not a reference-like-type.

For RopeSlice, I would expect using these approach:

impl<'a> Source<'a> for RopeSlice<'a> {
    type Slice = RopeSlice<'a>
    fn slice(&self, range: Range<usize>) -> Option<Self::Slice> {}
    unsafe fn slice_unchecked(&self, range: Range<usize>) -> Self::Slice {}
}

But this can not be done with current trait requirement.

maciejhirsz commented 1 year ago

Slices wouldn't be so bad and I could relax the types here, however for the Lexer to work on segmented input, it would need to be able to read Chunks of sequentially allocated memory, which might be tricky with segmented input (in case Lexer wants to quickly match the next 8 bytes, but there are only 4 left in current segment, f.e.).