macroonz_compiler/token/capture/
fragment.rs1use super::{CapturedFragment, CapturedInput, CapturedTokenTree, SpanHandle};
7
8impl CapturedInput {
9 #[must_use]
11 pub fn fragment(&self) -> CapturedFragment<'_> {
12 CapturedFragment::over(self.trees(), None)
13 }
14}
15
16impl CapturedTokenTree {
17 #[must_use]
19 pub fn group_fragment(
20 &self,
21 expected: super::CapturedDelimiter,
22 ) -> Option<CapturedFragment<'_>> {
23 self.group().and_then(|(delimiter, members)| {
24 (delimiter == expected).then(|| CapturedFragment::over(members, Some(self.span())))
25 })
26 }
27}
28
29impl<'tokens> CapturedFragment<'tokens> {
30 #[must_use]
32 pub(super) const fn over(
33 tokens: &'tokens [CapturedTokenTree],
34 end: Option<SpanHandle>,
35 ) -> Self {
36 Self { tokens, end }
37 }
38
39 #[must_use]
41 pub const fn tokens(self) -> &'tokens [CapturedTokenTree] {
42 self.tokens
43 }
44
45 #[must_use]
47 pub const fn len(self) -> usize {
48 self.tokens.len()
49 }
50
51 #[must_use]
53 pub const fn is_empty(self) -> bool {
54 self.tokens.is_empty()
55 }
56
57 #[must_use]
59 pub fn first_span(self) -> Option<SpanHandle> {
60 self.tokens.first().map(CapturedTokenTree::span)
61 }
62
63 #[must_use]
65 pub fn last_span(self) -> Option<SpanHandle> {
66 self.tokens.last().map(CapturedTokenTree::span)
67 }
68
69 #[must_use]
73 pub const fn enclosing_span(self) -> Option<SpanHandle> {
74 self.end
75 }
76
77 #[must_use]
81 pub fn canonical_bytes(self) -> Vec<u8> {
82 let mut bytes = Vec::new();
83 for token in self.tokens {
84 super::encode::encode_captured(token, &mut bytes);
85 }
86 bytes
87 }
88}