cosmic_nom/
lib.rs

1#![allow(warnings)]
2
3use std::ops::{Deref, Range, RangeFrom, RangeTo};
4use std::sync::Arc;
5
6use nom::character::complete::multispace0;
7use nom::combinator::recognize;
8use nom::error::{ErrorKind, ParseError};
9use nom::sequence::delimited;
10use nom::{
11    AsBytes, AsChar, Compare, CompareResult, FindSubstring, IResult, InputIter, InputLength,
12    InputTake, InputTakeAtPosition, Needed, Offset, Slice,
13};
14use nom_locate::LocatedSpan;
15use nom_supreme::error::ErrorTree;
16use serde::{Deserialize, Serialize};
17
18#[cfg(test)]
19mod tests {
20    #[test]
21    fn it_works() {
22        let result = 2 + 2;
23        assert_eq!(result, 4);
24    }
25}
26
27pub trait Span:
28    Clone
29    + ToString
30    + AsBytes
31    + Slice<Range<usize>>
32    + Slice<RangeTo<usize>>
33    + Slice<RangeFrom<usize>>
34    + InputLength
35    + Offset
36    + InputTake
37    + InputIter<Item = char>
38    + InputTakeAtPosition<Item = char>
39    + Compare<&'static str>
40    + FindSubstring<&'static str>
41    + core::fmt::Debug
42where
43    Self: Sized,
44    <Self as InputTakeAtPosition>::Item: AsChar,
45{
46    fn location_offset(&self) -> usize;
47
48    fn location_line(&self) -> u32;
49
50    fn get_column(&self) -> usize;
51
52    fn extra(&self) -> Arc<String>;
53
54    fn len(&self) -> usize;
55
56    fn range(&self) -> Range<usize>;
57
58    fn trace(&self) -> Trace {
59        Trace {
60            range: self.range(),
61            extra: self.extra(),
62        }
63    }
64}
65
66impl<'a> Span for Wrap<LocatedSpan<&'a str, Arc<String>>> {
67    fn location_offset(&self) -> usize {
68        self.input.location_offset()
69    }
70
71    fn get_column(&self) -> usize {
72        self.input.get_column()
73    }
74
75    fn location_line(&self) -> u32 {
76        self.input.location_line()
77    }
78
79    fn extra(&self) -> Arc<String> {
80        self.input.extra.clone()
81    }
82
83    fn len(&self) -> usize {
84        self.input.len()
85    }
86
87    fn range(&self) -> Range<usize> {
88        Range {
89            start: self.location_offset(),
90            end: self.location_offset() + self.len(),
91        }
92    }
93}
94
95// TraceWrap
96#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
97pub struct Tw<W> {
98    pub trace: Trace,
99    pub w: W,
100}
101
102impl<W> Tw<W> {
103    pub fn new<I: Span>(span: I, w: W) -> Self {
104        Self {
105            trace: span.trace(),
106            w,
107        }
108    }
109
110    pub fn unwrap(self) -> W {
111        self.w
112    }
113}
114
115impl<W> ToString for Tw<W>
116where
117    W: ToString,
118{
119    fn to_string(&self) -> String {
120        self.w.to_string()
121    }
122}
123
124impl<W> Deref for Tw<W> {
125    type Target = W;
126
127    fn deref(&self) -> &Self::Target {
128        &self.w
129    }
130}
131
132pub fn tw<I, F, O>(mut f: F) -> impl FnMut(I) -> Res<I, Tw<O>>
133where
134    I: Span,
135    F: FnMut(I) -> Res<I, O>,
136{
137    move |input: I| {
138        let (next, output) = f(input.clone())?;
139
140        let span = input.slice(0..next.len());
141        let tw = Tw::new(span, output);
142
143        Ok((next, tw))
144    }
145}
146
147//pub type OwnedSpan<'a> = LocatedSpan<&'a str, SpanExtra>;
148pub type SpanExtra = Arc<String>;
149
150pub fn new_span<'a>(s: &'a str) -> Wrap<LocatedSpan<&'a str, Arc<String>>> {
151    let extra = Arc::new(s.to_string());
152    let span = LocatedSpan::new_extra(s, extra);
153    Wrap::new(span)
154}
155
156pub fn span_with_extra<'a>(
157    s: &'a str,
158    extra: Arc<String>,
159) -> Wrap<LocatedSpan<&'a str, Arc<String>>> {
160    Wrap::new(LocatedSpan::new_extra(s, extra))
161}
162
163#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
164pub struct Trace {
165    pub range: Range<usize>,
166    pub extra: SpanExtra,
167}
168
169impl Trace {
170    pub fn new(range: Range<usize>, extra: SpanExtra) -> Self {
171        Self { range, extra }
172    }
173
174    pub fn at_offset(offset: usize, extra: SpanExtra) -> Self {
175        Self {
176            range: offset..offset,
177            extra,
178        }
179    }
180
181    pub fn scan<F, I: Span, O>(f: F, input: I) -> Self
182    where
183        F: FnMut(I) -> Res<I, O> + Copy,
184    {
185        let extra = input.extra();
186        let range = input.location_offset()..len(f)(input);
187        Self { range, extra }
188    }
189}
190
191#[derive(Debug, Clone)]
192pub struct SliceStr {
193    location_offset: usize,
194    len: usize,
195    string: Arc<String>,
196}
197
198impl ToString for SliceStr {
199    fn to_string(&self) -> String {
200        self.string
201            .as_str()
202            .slice(self.location_offset..self.location_offset + self.len)
203            .to_string()
204    }
205}
206
207impl SliceStr {
208    pub fn new(string: String) -> Self {
209        Self::from_arc(Arc::new(string))
210    }
211
212    pub fn from_arc(string: Arc<String>) -> Self {
213        Self {
214            len: string.len(),
215            string,
216            location_offset: 0,
217        }
218    }
219
220    pub fn from(string: Arc<String>, location_offset: usize, len: usize) -> Self {
221        Self {
222            string,
223            location_offset,
224            len,
225        }
226    }
227}
228
229impl SliceStr {
230    pub fn as_str(&self) -> &str {
231        &self
232            .string
233            .as_str()
234            .slice(self.location_offset..self.location_offset + self.len)
235    }
236}
237
238impl Deref for SliceStr {
239    type Target = str;
240
241    fn deref(&self) -> &Self::Target {
242        self.as_str()
243    }
244}
245
246impl AsBytes for SliceStr {
247    fn as_bytes(&self) -> &[u8] {
248        println!("AS BYTES: {}", self.string.as_bytes().len());
249        self.string
250            .as_bytes()
251            .slice(self.location_offset..self.location_offset + self.len)
252    }
253}
254
255impl Slice<Range<usize>> for SliceStr {
256    fn slice(&self, range: Range<usize>) -> Self {
257        SliceStr {
258            location_offset: self.location_offset + range.start,
259            len: range.end - range.start,
260            string: self.string.clone(),
261        }
262    }
263}
264
265impl Slice<RangeFrom<usize>> for SliceStr {
266    fn slice(&self, range: RangeFrom<usize>) -> Self {
267        SliceStr {
268            location_offset: self.location_offset + range.start,
269            len: self.len - range.start,
270            string: self.string.clone(),
271        }
272    }
273}
274
275impl Slice<RangeTo<usize>> for SliceStr {
276    fn slice(&self, range: RangeTo<usize>) -> Self {
277        SliceStr {
278            location_offset: self.location_offset,
279            len: range.end,
280            string: self.string.clone(),
281        }
282    }
283}
284
285impl Compare<&str> for SliceStr {
286    fn compare(&self, t: &str) -> CompareResult {
287        self.as_str().compare(t)
288    }
289
290    fn compare_no_case(&self, t: &str) -> CompareResult {
291        self.as_str().compare_no_case(t)
292    }
293}
294
295impl InputLength for SliceStr {
296    fn input_len(&self) -> usize {
297        self.len
298    }
299}
300
301impl Offset for SliceStr {
302    fn offset(&self, second: &Self) -> usize {
303        self.location_offset
304    }
305}
306
307pub struct MyCharIterator {}
308
309pub struct MyChars {
310    index: usize,
311    slice: SliceStr,
312}
313
314impl MyChars {
315    pub fn new(slice: SliceStr) -> Self {
316        Self { index: 0, slice }
317    }
318}
319
320impl Iterator for MyChars {
321    type Item = char;
322
323    fn next(&mut self) -> Option<Self::Item> {
324        let mut chars = self.slice.as_str().chars();
325        let next = chars.nth(self.index);
326        match next {
327            None => None,
328            Some(next) => {
329                self.index = self.index + 1;
330                Some(next)
331            }
332        }
333    }
334}
335
336pub struct CharIterator {
337    index: usize,
338    slice: SliceStr,
339}
340
341impl CharIterator {
342    pub fn new(slice: SliceStr) -> Self {
343        Self { index: 0, slice }
344    }
345}
346
347impl Iterator for CharIterator {
348    type Item = (usize, char);
349
350    fn next(&mut self) -> Option<Self::Item> {
351        let mut chars = self.slice.as_str().chars();
352        let next = chars.nth(self.index);
353        match next {
354            None => None,
355            Some(next) => {
356                //let byte_index = self.index * std::mem::size_of::<char>();
357                let byte_index = self.index;
358                self.index = self.index + 1;
359                Some((byte_index, next))
360            }
361        }
362    }
363}
364
365impl InputIter for SliceStr {
366    type Item = char;
367    type Iter = CharIterator;
368    type IterElem = MyChars;
369
370    #[inline]
371    fn iter_indices(&self) -> Self::Iter {
372        CharIterator::new(self.clone())
373    }
374    #[inline]
375    fn iter_elements(&self) -> Self::IterElem {
376        MyChars::new(self.clone())
377    }
378    #[inline]
379    fn position<P>(&self, predicate: P) -> Option<usize>
380    where
381        P: Fn(Self::Item) -> bool,
382    {
383        self.as_str().position(predicate)
384    }
385
386    #[inline]
387    fn slice_index(&self, count: usize) -> Result<usize, nom::Needed> {
388        self.as_str().slice_index(count)
389    }
390}
391
392impl InputTakeAtPosition for SliceStr {
393    type Item = char;
394
395    fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E>
396    where
397        P: Fn(Self::Item) -> bool,
398    {
399        match self.split_at_position(predicate) {
400            Err(nom::Err::Incomplete(_)) => Ok(self.take_split(self.input_len())),
401            res => res,
402        }
403    }
404
405    fn split_at_position1<P, E: ParseError<Self>>(
406        &self,
407        predicate: P,
408        e: ErrorKind,
409    ) -> IResult<Self, Self, E>
410    where
411        P: Fn(Self::Item) -> bool,
412    {
413        match self.as_str().position(predicate) {
414            Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))),
415            Some(n) => Ok(self.take_split(n)),
416            None => Err(nom::Err::Incomplete(nom::Needed::new(1))),
417        }
418    }
419
420    fn split_at_position_complete<P, E: ParseError<Self>>(
421        &self,
422        predicate: P,
423    ) -> IResult<Self, Self, E>
424    where
425        P: Fn(Self::Item) -> bool,
426    {
427        match self.split_at_position(predicate) {
428            Err(nom::Err::Incomplete(_)) => Ok(self.take_split(self.input_len())),
429            res => res,
430        }
431    }
432
433    fn split_at_position1_complete<P, E: ParseError<Self>>(
434        &self,
435        predicate: P,
436        e: ErrorKind,
437    ) -> IResult<Self, Self, E>
438    where
439        P: Fn(Self::Item) -> bool,
440    {
441        match self.as_str().position(predicate) {
442            Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))),
443            Some(n) => Ok(self.take_split(n)),
444            None => {
445                if self.as_str().input_len() == 0 {
446                    Err(nom::Err::Error(E::from_error_kind(self.clone(), e)))
447                } else {
448                    Ok(self.take_split(self.input_len()))
449                }
450            }
451        }
452    }
453}
454
455impl InputTake for SliceStr {
456    fn take(&self, count: usize) -> Self {
457        self.slice(count..)
458    }
459
460    fn take_split(&self, count: usize) -> (Self, Self) {
461        (self.slice(count..), self.slice(..count))
462    }
463}
464
465impl FindSubstring<&str> for SliceStr {
466    fn find_substring(&self, substr: &str) -> Option<usize> {
467        self.as_str().find_substring(substr)
468    }
469}
470
471#[cfg(test)]
472pub mod test {
473    use nom::Slice;
474
475    use crate::SliceStr;
476
477    #[test]
478    pub fn test() {
479        let s = SliceStr::new("abc123".to_string());
480        assert_eq!(6, s.len());
481
482        let s = s.slice(0..3);
483        assert_eq!(3, s.len());
484        assert_eq!("abc", s.as_str());
485
486        println!("bytes: {}", s.as_bytes().len());
487        println!("chars: {}", s.chars().count());
488
489        let s = SliceStr::new("abc123".to_string());
490        assert_eq!("123", s.slice(3..).as_str());
491        assert_eq!("abc", s.slice(..3).as_str());
492    }
493}
494
495#[derive(Debug, Clone)]
496pub struct Wrap<I>
497where
498    I: Clone
499        + ToString
500        + AsBytes
501        + Slice<Range<usize>>
502        + Slice<RangeTo<usize>>
503        + Slice<RangeFrom<usize>>
504        + InputLength
505        + Offset
506        + InputTake
507        + InputIter<Item = char>
508        + core::fmt::Debug
509        + InputTakeAtPosition<Item = char>,
510{
511    input: I,
512}
513
514impl<I> Wrap<I>
515where
516    I: Clone
517        + ToString
518        + AsBytes
519        + Slice<Range<usize>>
520        + Slice<RangeTo<usize>>
521        + Slice<RangeFrom<usize>>
522        + InputLength
523        + Offset
524        + InputTake
525        + InputIter<Item = char>
526        + core::fmt::Debug
527        + InputTakeAtPosition<Item = char>,
528{
529    pub fn new(input: I) -> Self {
530        Self { input }
531    }
532}
533
534impl<I> Deref for Wrap<I>
535where
536    I: Clone
537        + ToString
538        + AsBytes
539        + Slice<Range<usize>>
540        + Slice<RangeTo<usize>>
541        + Slice<RangeFrom<usize>>
542        + InputLength
543        + Offset
544        + InputTake
545        + InputIter<Item = char>
546        + core::fmt::Debug
547        + InputTakeAtPosition<Item = char>,
548{
549    type Target = I;
550
551    fn deref(&self) -> &Self::Target {
552        &self.input
553    }
554}
555
556impl<I> AsBytes for Wrap<I>
557where
558    I: Clone
559        + ToString
560        + AsBytes
561        + Slice<Range<usize>>
562        + Slice<RangeTo<usize>>
563        + Slice<RangeFrom<usize>>
564        + InputLength
565        + Offset
566        + InputTake
567        + InputIter<Item = char>
568        + core::fmt::Debug
569        + InputTakeAtPosition<Item = char>,
570{
571    fn as_bytes(&self) -> &[u8] {
572        self.input.as_bytes()
573    }
574}
575
576impl<I> Slice<Range<usize>> for Wrap<I>
577where
578    I: Clone
579        + ToString
580        + AsBytes
581        + Slice<Range<usize>>
582        + Slice<RangeTo<usize>>
583        + Slice<RangeFrom<usize>>
584        + InputLength
585        + Offset
586        + InputTake
587        + InputIter<Item = char>
588        + core::fmt::Debug
589        + InputTakeAtPosition<Item = char>,
590{
591    fn slice(&self, range: Range<usize>) -> Self {
592        Self::new(self.input.slice(range))
593    }
594}
595
596impl<I> Slice<RangeFrom<usize>> for Wrap<I>
597where
598    I: Clone
599        + ToString
600        + AsBytes
601        + Slice<Range<usize>>
602        + Slice<RangeTo<usize>>
603        + Slice<RangeFrom<usize>>
604        + InputLength
605        + Offset
606        + InputTake
607        + InputIter<Item = char>
608        + core::fmt::Debug
609        + InputTakeAtPosition<Item = char>,
610{
611    fn slice(&self, range: RangeFrom<usize>) -> Self {
612        Self::new(self.input.slice(range))
613    }
614}
615
616impl<I> Slice<RangeTo<usize>> for Wrap<I>
617where
618    I: Clone
619        + ToString
620        + AsBytes
621        + Slice<Range<usize>>
622        + Slice<RangeTo<usize>>
623        + Slice<RangeFrom<usize>>
624        + InputLength
625        + Offset
626        + InputTake
627        + InputIter<Item = char>
628        + core::fmt::Debug
629        + InputTakeAtPosition<Item = char>,
630{
631    fn slice(&self, range: RangeTo<usize>) -> Self {
632        Self::new(self.input.slice(range))
633    }
634}
635
636impl<'a> Compare<&'static str> for Wrap<LocatedSpan<&'a str, Arc<String>>> {
637    fn compare(&self, t: &str) -> CompareResult {
638        self.input.compare(t)
639    }
640
641    fn compare_no_case(&self, t: &str) -> CompareResult {
642        self.input.compare_no_case(t)
643    }
644}
645
646impl<I> InputLength for Wrap<I>
647where
648    I: Clone
649        + ToString
650        + AsBytes
651        + Slice<Range<usize>>
652        + Slice<RangeTo<usize>>
653        + Slice<RangeFrom<usize>>
654        + InputLength
655        + Offset
656        + InputTake
657        + InputIter<Item = char>
658        + core::fmt::Debug
659        + InputTakeAtPosition<Item = char>,
660{
661    fn input_len(&self) -> usize {
662        self.input.input_len()
663    }
664}
665
666impl<I> Offset for Wrap<I>
667where
668    I: Clone
669        + ToString
670        + AsBytes
671        + Slice<Range<usize>>
672        + Slice<RangeTo<usize>>
673        + Slice<RangeFrom<usize>>
674        + InputLength
675        + Offset
676        + InputTake
677        + InputIter<Item = char>
678        + core::fmt::Debug
679        + InputTakeAtPosition<Item = char>,
680{
681    fn offset(&self, second: &Self) -> usize {
682        self.input.offset(&second.input)
683    }
684}
685
686impl<I> InputIter for Wrap<I>
687where
688    I: Clone
689        + ToString
690        + AsBytes
691        + Slice<Range<usize>>
692        + Slice<RangeTo<usize>>
693        + Slice<RangeFrom<usize>>
694        + InputLength
695        + Offset
696        + InputTake
697        + InputIter<Item = char>
698        + core::fmt::Debug
699        + InputTakeAtPosition<Item = char>,
700{
701    type Item = <I as InputIter>::Item;
702    type Iter = <I as InputIter>::Iter;
703    type IterElem = <I as InputIter>::IterElem;
704
705    fn iter_indices(&self) -> Self::Iter {
706        self.input.iter_indices()
707    }
708
709    fn iter_elements(&self) -> Self::IterElem {
710        self.input.iter_elements()
711    }
712
713    fn position<P>(&self, predicate: P) -> Option<usize>
714    where
715        P: Fn(Self::Item) -> bool,
716    {
717        self.input.position(predicate)
718    }
719
720    fn slice_index(&self, count: usize) -> Result<usize, Needed> {
721        self.input.slice_index(count)
722    }
723}
724
725impl<I> InputTake for Wrap<I>
726where
727    I: Clone
728        + ToString
729        + AsBytes
730        + Slice<Range<usize>>
731        + Slice<RangeTo<usize>>
732        + Slice<RangeFrom<usize>>
733        + InputLength
734        + Offset
735        + InputTake
736        + InputIter<Item = char>
737        + core::fmt::Debug
738        + InputTakeAtPosition<Item = char>,
739{
740    fn take(&self, count: usize) -> Self {
741        Wrap::new(self.input.take(count))
742    }
743
744    fn take_split(&self, count: usize) -> (Self, Self) {
745        let (left, right) = self.input.take_split(count);
746        (Wrap::new(left), Wrap::new(right))
747    }
748}
749
750impl<I> ToString for Wrap<I>
751where
752    I: Clone
753        + ToString
754        + AsBytes
755        + Slice<Range<usize>>
756        + Slice<RangeTo<usize>>
757        + Slice<RangeFrom<usize>>
758        + InputLength
759        + Offset
760        + InputTake
761        + InputIter<Item = char>
762        + core::fmt::Debug
763        + InputTakeAtPosition<Item = char>,
764{
765    fn to_string(&self) -> String {
766        self.input.to_string()
767    }
768}
769
770impl<'a> FindSubstring<&str> for Wrap<LocatedSpan<&'a str, Arc<String>>> {
771    fn find_substring(&self, substr: &str) -> Option<usize> {
772        self.input.find_substring(substr)
773    }
774}
775
776impl<I> InputTakeAtPosition for Wrap<I>
777where
778    I: Clone
779        + ToString
780        + AsBytes
781        + Slice<Range<usize>>
782        + Slice<RangeTo<usize>>
783        + Slice<RangeFrom<usize>>
784        + InputLength
785        + Offset
786        + InputTake
787        + InputIter<Item = char>
788        + core::fmt::Debug
789        + InputTakeAtPosition<Item = char>,
790{
791    type Item = <I as InputIter>::Item;
792
793    fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E>
794    where
795        P: Fn(Self::Item) -> bool,
796    {
797        match self.position(predicate) {
798            Some(n) => Ok(self.take_split(n)),
799            None => Err(nom::Err::Incomplete(Needed::new(1))),
800        }
801    }
802
803    fn split_at_position1<P, E: ParseError<Self>>(
804        &self,
805        predicate: P,
806        e: ErrorKind,
807    ) -> IResult<Self, Self, E>
808    where
809        P: Fn(Self::Item) -> bool,
810    {
811        match self.position(predicate) {
812            Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))),
813            Some(n) => Ok(self.take_split(n)),
814            None => Err(nom::Err::Incomplete(Needed::new(1))),
815        }
816    }
817
818    fn split_at_position_complete<P, E: ParseError<Self>>(
819        &self,
820        predicate: P,
821    ) -> IResult<Self, Self, E>
822    where
823        P: Fn(Self::Item) -> bool,
824    {
825        match self.split_at_position(predicate) {
826            Err(nom::Err::Incomplete(_)) => Ok(self.take_split(self.input_len())),
827            res => res,
828        }
829    }
830
831    fn split_at_position1_complete<P, E: ParseError<Self>>(
832        &self,
833        predicate: P,
834        e: ErrorKind,
835    ) -> IResult<Self, Self, E>
836    where
837        P: Fn(Self::Item) -> bool,
838    {
839        match self.split_at_position1(predicate, e) {
840            Err(nom::Err::Incomplete(_)) => {
841                if self.input_len() == 0 {
842                    Err(nom::Err::Error(E::from_error_kind(self.clone(), e)))
843                } else {
844                    Ok(self.take_split(self.input_len()))
845                }
846            }
847            res => res,
848        }
849    }
850}
851
852pub type Res<I: Span, O> = IResult<I, O, ErrorTree<I>>;
853
854pub fn wrap<I, F, O>(mut f: F) -> impl FnMut(I) -> Res<I, O>
855where
856    I: Span,
857    F: FnMut(I) -> Res<I, O> + Copy,
858{
859    move |input: I| f(input)
860}
861
862pub fn len<I, F, O>(f: F) -> impl FnMut(I) -> usize
863where
864    I: Span,
865    F: FnMut(I) -> Res<I, O> + Copy,
866{
867    move |input: I| match recognize(wrap(f))(input) {
868        Ok((_, span)) => span.len(),
869        Err(_) => 0,
870    }
871}
872
873pub fn trim<I, F, O>(f: F) -> impl FnMut(I) -> Res<I, O>
874where
875    I: Span,
876    F: FnMut(I) -> Res<I, O> + Copy,
877{
878    move |input: I| delimited(multispace0, f, multispace0)(input)
879}