MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / scan_fstring_body

Method scan_fstring_body

compiler/src/modules/lexer/scan.rs:202–256  ·  view source on GitHub ↗
(&mut self, quote: u8, triple: bool, body_start: usize)

Source from the content-addressed store, hash-verified

200 }
201
202 fn scan_fstring_body(&mut self, quote: u8, triple: bool, body_start: usize) {
203 let ql: usize = if triple { 3 } else { 1 };
204 let mut pos = self.pos;
205 while pos < self.src.len() {
206 let closes = if triple {
207 pos + 2 < self.src.len()
208 && self.src[pos] == quote
209 && self.src[pos + 1] == quote
210 && self.src[pos + 2] == quote
211 } else {
212 self.src[pos] == quote
213 };
214 if closes {
215 self.pending.push((TokenType::FstringEnd, self.line, pos, pos + ql));
216 // Pushed after End so LIFO pop yields Middle first.
217 if pos > self.pos {
218 self.pending.push((TokenType::FstringMiddle, self.line, body_start, pos));
219 }
220 self.pos = pos + ql;
221 return;
222 }
223 match self.src[pos] {
224 b'\\' => pos = (pos + 2).min(self.src.len()),
225 b'{' if self.src.get(pos + 1) == Some(&b'{') => {
226 pos += 2;
227 }
228 b'}' if self.src.get(pos + 1) == Some(&b'}') => {
229 pos += 2;
230 }
231 b'{' => {
232 if self.fstring_stack.len() >= MAX_FSTRING_DEPTH {
233 self.report(pos, pos + 1, "f-string nesting depth exceeds maximum (200)");
234 self.pos = pos + 1;
235 return;
236 }
237 self.pending.push((TokenType::Lbrace, self.line, pos, pos + 1));
238 if pos > self.pos {
239 self.pending.push((TokenType::FstringMiddle, self.line, body_start, pos));
240 }
241 self.fstring_stack.push((quote, triple, pos + 1, self.nesting));
242 self.pos = pos + 1;
243 return;
244 }
245 b'\n' => { self.line += 1; pos += 1; }
246 _ => pos += 1,
247 }
248 }
249 /* Synthesise End on EOF so the parser sees a balanced structure, then report the unterminated literal. */
250 self.report(self.pos, pos, "unterminated f-string literal");
251 self.pending.push((TokenType::FstringEnd, self.line, pos, pos));
252 if pos > self.pos {
253 self.pending.push((TokenType::FstringMiddle, self.line, body_start, pos));
254 }
255 self.pos = pos;
256 }
257
258 // Newlines: emit Newline/Indent/Dedent, or NL inside brackets.
259

Callers 2

start_fstringMethod · 0.80
close_braceMethod · 0.80

Calls 4

pushMethod · 0.80
reportMethod · 0.80
lenMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected