risingwave_sqlparser/parser_v2/
impl_.rs1use winnow::stream::{Checkpoint, Offset, SliceLen, Stream, StreamIsPartial, UpdateSlice};
14
15use crate::parser::Parser;
16use crate::tokenizer::TokenWithLocation;
17
18#[derive(Copy, Clone, Debug)]
19pub struct CheckpointWrapper<'a>(Checkpoint<&'a [TokenWithLocation], &'a [TokenWithLocation]>);
20
21impl<'a> Offset<CheckpointWrapper<'a>> for CheckpointWrapper<'a> {
22 #[inline(always)]
23 fn offset_from(&self, start: &Self) -> usize {
24 self.0.offset_from(&start.0)
25 }
26}
27
28impl std::fmt::Debug for Parser<'_> {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 if let Some(token) = self.0.first() {
32 write!(f, "{}", token.token)?;
33 }
34 for token in self.0.iter().skip(1) {
35 write!(f, " {}", token.token)?;
36 }
37 Ok(())
38 }
39}
40
41impl<'a> Offset<Parser<'a>> for Parser<'a> {
42 #[inline(always)]
43 fn offset_from(&self, start: &Self) -> usize {
44 self.0.offset_from(&start.0)
45 }
46}
47
48impl<'a> Offset<CheckpointWrapper<'a>> for Parser<'a> {
49 #[inline(always)]
50 fn offset_from(&self, start: &CheckpointWrapper<'a>) -> usize {
51 self.0.offset_from(&start.0)
52 }
53}
54
55impl SliceLen for Parser<'_> {
56 #[inline(always)]
57 fn slice_len(&self) -> usize {
58 self.0.len()
59 }
60}
61
62impl<'a> StreamIsPartial for Parser<'a> {
63 type PartialState = <&'a [TokenWithLocation] as StreamIsPartial>::PartialState;
64
65 #[inline(always)]
66 fn complete(&mut self) -> Self::PartialState {
67 self.0.complete()
68 }
69
70 #[inline(always)]
71 fn restore_partial(&mut self, state: Self::PartialState) {
72 self.0.restore_partial(state)
73 }
74
75 #[inline(always)]
76 fn is_partial_supported() -> bool {
77 <&'a [TokenWithLocation] as StreamIsPartial>::is_partial_supported()
78 }
79}
80
81impl<'a> Stream for Parser<'a> {
82 type Checkpoint = CheckpointWrapper<'a>;
83 type IterOffsets = <&'a [TokenWithLocation] as Stream>::IterOffsets;
84 type Slice = Parser<'a>;
85 type Token = <&'a [TokenWithLocation] as Stream>::Token;
86
87 #[inline(always)]
88 fn iter_offsets(&self) -> Self::IterOffsets {
89 self.0.iter_offsets()
90 }
91
92 #[inline(always)]
93 fn eof_offset(&self) -> usize {
94 self.0.eof_offset()
95 }
96
97 #[inline(always)]
98 fn next_token(&mut self) -> Option<Self::Token> {
99 self.0.next_token()
100 }
101
102 #[inline(always)]
103 fn offset_for<P>(&self, predicate: P) -> Option<usize>
104 where
105 P: Fn(Self::Token) -> bool,
106 {
107 self.0.offset_for(predicate)
108 }
109
110 #[inline(always)]
111 fn offset_at(&self, tokens: usize) -> Result<usize, winnow::error::Needed> {
112 self.0.offset_at(tokens)
113 }
114
115 #[inline(always)]
116 fn next_slice(&mut self, offset: usize) -> Self::Slice {
117 Parser(self.0.next_slice(offset))
118 }
119
120 #[inline(always)]
121 fn checkpoint(&self) -> Self::Checkpoint {
122 CheckpointWrapper(self.0.checkpoint())
123 }
124
125 #[inline(always)]
126 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
127 self.0.reset(&checkpoint.0)
128 }
129
130 #[inline(always)]
131 fn raw(&self) -> &dyn std::fmt::Debug {
132 self
134 }
135
136 fn peek_token(&self) -> Option<Self::Token> {
137 self.0.peek_token()
138 }
139
140 fn peek_slice(&self, offset: usize) -> Self::Slice {
141 Parser(self.0.peek_slice(offset))
142 }
143}
144
145impl UpdateSlice for Parser<'_> {
146 #[inline(always)]
147 fn update_slice(self, inner: Self::Slice) -> Self {
148 Parser(self.0.update_slice(inner.0))
149 }
150}