about summary refs log tree commit diff
path: root/compiler/rustc_lexer/src/cursor.rs
diff options
context:
space:
mode:
authorDeadbeef <ent3rm4n@gmail.com>2025-04-12 15:53:46 +0000
committerDeadbeef <ent3rm4n@gmail.com>2025-05-05 23:10:08 +0800
commit662182637e100642d1e5de7cb193da837a14ae48 (patch)
tree28e183216387d8d8cd939404a0c2fc01efaf450f /compiler/rustc_lexer/src/cursor.rs
parent0c33fe2c3d3eecadd17a84b110bb067288a64f1c (diff)
downloadrust-662182637e100642d1e5de7cb193da837a14ae48.tar.gz
rust-662182637e100642d1e5de7cb193da837a14ae48.zip
Implement RFC 3503: frontmatters
Supercedes #137193
Diffstat (limited to 'compiler/rustc_lexer/src/cursor.rs')
-rw-r--r--compiler/rustc_lexer/src/cursor.rs14
1 files changed, 13 insertions, 1 deletions
diff --git a/compiler/rustc_lexer/src/cursor.rs b/compiler/rustc_lexer/src/cursor.rs
index e0e3bd0e30b..526693d3de1 100644
--- a/compiler/rustc_lexer/src/cursor.rs
+++ b/compiler/rustc_lexer/src/cursor.rs
@@ -1,5 +1,10 @@
 use std::str::Chars;
 
+pub enum FrontmatterAllowed {
+    Yes,
+    No,
+}
+
 /// Peekable iterator over a char sequence.
 ///
 /// Next characters can be peeked via `first` method,
@@ -8,6 +13,7 @@ pub struct Cursor<'a> {
     len_remaining: usize,
     /// Iterator over chars. Slightly faster than a &str.
     chars: Chars<'a>,
+    pub(crate) frontmatter_allowed: FrontmatterAllowed,
     #[cfg(debug_assertions)]
     prev: char,
 }
@@ -15,10 +21,11 @@ pub struct Cursor<'a> {
 pub(crate) const EOF_CHAR: char = '\0';
 
 impl<'a> Cursor<'a> {
-    pub fn new(input: &'a str) -> Cursor<'a> {
+    pub fn new(input: &'a str, frontmatter_allowed: FrontmatterAllowed) -> Cursor<'a> {
         Cursor {
             len_remaining: input.len(),
             chars: input.chars(),
+            frontmatter_allowed,
             #[cfg(debug_assertions)]
             prev: EOF_CHAR,
         }
@@ -95,6 +102,11 @@ impl<'a> Cursor<'a> {
         Some(c)
     }
 
+    /// Moves to a substring by a number of bytes.
+    pub(crate) fn bump_bytes(&mut self, n: usize) {
+        self.chars = self.as_str()[n..].chars();
+    }
+
     /// Eats symbols while predicate returns true or until the end of file is reached.
     pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
         // It was tried making optimized version of this for eg. line comments, but