@@ -555,254 +555,7 @@ use std::ptr::NonNull;
555
555
556
556
#[allow(clippy::wildcard_imports)]
557
557
use ruby_prism_sys::*;
558
-
559
- /// A range in the source file.
560
- pub struct Location<'pr> {{
561
- parser: NonNull<pm_parser_t>,
562
- pub(crate) start: *const u8,
563
- pub(crate) end: *const u8,
564
- marker: PhantomData<&'pr [u8]>
565
- }}
566
-
567
- impl<'pr> Location<'pr> {{
568
- /// Returns a byte slice for the range.
569
- #[must_use]
570
- pub fn as_slice(&self) -> &'pr [u8] {{
571
- unsafe {{
572
- let len = usize::try_from(self.end.offset_from(self.start)).expect("end should point to memory after start");
573
- std::slice::from_raw_parts(self.start, len)
574
- }}
575
- }}
576
-
577
- /// Return a Location from the given `pm_location_t`.
578
- #[must_use]
579
- pub(crate) const fn new(parser: NonNull<pm_parser_t>, loc: &'pr pm_location_t) -> Location<'pr> {{
580
- Location {{ parser, start: loc.start, end: loc.end, marker: PhantomData }}
581
- }}
582
-
583
- /// Return a Location starting at self and ending at the end of other.
584
- /// Returns None if both locations did not originate from the same parser,
585
- /// or if self starts after other.
586
- #[must_use]
587
- pub fn join(&self, other: &Location<'pr>) -> Option<Location<'pr>> {{
588
- if self.parser != other.parser || self.start > other.start {{
589
- None
590
- }} else {{
591
- Some(Location {{ parser: self.parser, start: self.start, end: other.end, marker: PhantomData }})
592
- }}
593
- }}
594
-
595
- /// Return the start offset from the beginning of the parsed source.
596
- #[must_use]
597
- pub fn start_offset(&self) -> usize {{
598
- unsafe {{
599
- let parser_start = (*self.parser.as_ptr()).start;
600
- usize::try_from(self.start.offset_from(parser_start)).expect("start should point to memory after the parser's start")
601
- }}
602
- }}
603
-
604
- /// Return the end offset from the beginning of the parsed source.
605
- #[must_use]
606
- pub fn end_offset(&self) -> usize {{
607
- unsafe {{
608
- let parser_start = (*self.parser.as_ptr()).start;
609
- usize::try_from(self.end.offset_from(parser_start)).expect("end should point to memory after the parser's start")
610
- }}
611
- }}
612
- }}
613
-
614
- impl std::fmt::Debug for Location<'_> {{
615
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
616
- let slice: &[u8] = self.as_slice();
617
-
618
- let mut visible = String::new();
619
- visible.push('"');
620
-
621
- for &byte in slice {{
622
- let part: Vec<u8> = std::ascii::escape_default(byte).collect();
623
- visible.push_str(std::str::from_utf8(&part).unwrap());
624
- }}
625
-
626
- visible.push('"');
627
- write!(f, "{{visible}}")
628
- }}
629
- }}
630
-
631
- /// An iterator over the nodes in a list.
632
- pub struct NodeListIter<'pr> {{
633
- parser: NonNull<pm_parser_t>,
634
- pointer: NonNull<pm_node_list>,
635
- index: usize,
636
- marker: PhantomData<&'pr mut pm_node_list>
637
- }}
638
-
639
- impl<'pr> Iterator for NodeListIter<'pr> {{
640
- type Item = Node<'pr>;
641
-
642
- fn next(&mut self) -> Option<Self::Item> {{
643
- if self.index >= unsafe {{ self.pointer.as_ref().size }} {{
644
- None
645
- }} else {{
646
- let node: *mut pm_node_t = unsafe {{ *(self.pointer.as_ref().nodes.add(self.index)) }};
647
- self.index += 1;
648
- Some(Node::new(self.parser, node))
649
- }}
650
- }}
651
- }}
652
-
653
- /// A list of nodes.
654
- pub struct NodeList<'pr> {{
655
- parser: NonNull<pm_parser_t>,
656
- pointer: NonNull<pm_node_list>,
657
- marker: PhantomData<&'pr mut pm_node_list>
658
- }}
659
-
660
- impl<'pr> NodeList<'pr> {{
661
- /// Returns an iterator over the nodes.
662
- #[must_use]
663
- pub fn iter(&self) -> NodeListIter<'pr> {{
664
- NodeListIter {{
665
- parser: self.parser,
666
- pointer: self.pointer,
667
- index: 0,
668
- marker: PhantomData
669
- }}
670
- }}
671
- }}
672
-
673
- impl std::fmt::Debug for NodeList<'_> {{
674
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
675
- write!(f, "{{:?}}", self.iter().collect::<Vec<_>>())
676
- }}
677
- }}
678
-
679
- /// A handle for a constant ID.
680
- pub struct ConstantId<'pr> {{
681
- parser: NonNull<pm_parser_t>,
682
- id: pm_constant_id_t,
683
- marker: PhantomData<&'pr mut pm_constant_id_t>
684
- }}
685
-
686
- impl<'pr> ConstantId<'pr> {{
687
- fn new(parser: NonNull<pm_parser_t>, id: pm_constant_id_t) -> Self {{
688
- ConstantId {{ parser, id, marker: PhantomData }}
689
- }}
690
-
691
- /// Returns a byte slice for the constant ID.
692
- ///
693
- /// # Panics
694
- ///
695
- /// Panics if the constant ID is not found in the constant pool.
696
- #[must_use]
697
- pub fn as_slice(&self) -> &'pr [u8] {{
698
- unsafe {{
699
- let pool = &(*self.parser.as_ptr()).constant_pool;
700
- let constant = &(*pool.constants.add((self.id - 1).try_into().unwrap()));
701
- std::slice::from_raw_parts(constant.start, constant.length)
702
- }}
703
- }}
704
- }}
705
-
706
- impl std::fmt::Debug for ConstantId<'_> {{
707
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
708
- write!(f, "{{:?}}", self.id)
709
- }}
710
- }}
711
-
712
- /// An iterator over the constants in a list.
713
- pub struct ConstantListIter<'pr> {{
714
- parser: NonNull<pm_parser_t>,
715
- pointer: NonNull<pm_constant_id_list_t>,
716
- index: usize,
717
- marker: PhantomData<&'pr mut pm_constant_id_list_t>
718
- }}
719
-
720
- impl<'pr> Iterator for ConstantListIter<'pr> {{
721
- type Item = ConstantId<'pr>;
722
-
723
- fn next(&mut self) -> Option<Self::Item> {{
724
- if self.index >= unsafe {{ self.pointer.as_ref().size }} {{
725
- None
726
- }} else {{
727
- let constant_id: pm_constant_id_t = unsafe {{ *(self.pointer.as_ref().ids.add(self.index)) }};
728
- self.index += 1;
729
- Some(ConstantId::new(self.parser, constant_id))
730
- }}
731
- }}
732
- }}
733
-
734
- /// A list of constants.
735
- pub struct ConstantList<'pr> {{
736
- /// The raw pointer to the parser where this list came from.
737
- parser: NonNull<pm_parser_t>,
738
-
739
- /// The raw pointer to the list allocated by prism.
740
- pointer: NonNull<pm_constant_id_list_t>,
741
-
742
- /// The marker to indicate the lifetime of the pointer.
743
- marker: PhantomData<&'pr mut pm_constant_id_list_t>
744
- }}
745
-
746
- impl<'pr> ConstantList<'pr> {{
747
- /// Returns an iterator over the constants in the list.
748
- #[must_use]
749
- pub fn iter(&self) -> ConstantListIter<'pr> {{
750
- ConstantListIter {{
751
- parser: self.parser,
752
- pointer: self.pointer,
753
- index: 0,
754
- marker: PhantomData
755
- }}
756
- }}
757
- }}
758
-
759
- impl std::fmt::Debug for ConstantList<'_> {{
760
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
761
- write!(f, "{{:?}}", self.iter().collect::<Vec<_>>())
762
- }}
763
- }}
764
-
765
- /// A handle for an arbitarily-sized integer.
766
- pub struct Integer<'pr> {{
767
- /// The raw pointer to the integer allocated by prism.
768
- pointer: *const pm_integer_t,
769
-
770
- /// The marker to indicate the lifetime of the pointer.
771
- marker: PhantomData<&'pr mut pm_constant_id_t>
772
- }}
773
-
774
- impl<'pr> Integer<'pr> {{
775
- fn new(pointer: *const pm_integer_t) -> Self {{
776
- Integer {{ pointer, marker: PhantomData }}
777
- }}
778
- }}
779
-
780
- impl std::fmt::Debug for Integer<'_> {{
781
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
782
- write!(f, "{{:?}}", self.pointer)
783
- }}
784
- }}
785
-
786
- impl TryInto<i32> for Integer<'_> {{
787
- type Error = ();
788
-
789
- fn try_into(self) -> Result<i32, Self::Error> {{
790
- let negative = unsafe {{ (*self.pointer).negative }};
791
- let length = unsafe {{ (*self.pointer).length }};
792
-
793
- if length == 0 {{
794
- i32::try_from(unsafe {{ (*self.pointer).value }}).map_or(Err(()), |value|
795
- if negative {{
796
- Ok(-value)
797
- }} else {{
798
- Ok(value)
799
- }}
800
- )
801
- }} else {{
802
- Err(())
803
- }}
804
- }}
805
- }}
558
+ use crate::{{ConstantId, ConstantList, Integer, Location, NodeList}};
806
559
"#
807
560
) ?;
808
561
0 commit comments