pub struct Cursor<'a> { /* private fields */ }
Implementations§
Source§impl<'a> Cursor<'a>
impl<'a> Cursor<'a>
pub fn new(slice: &'a str) -> Cursor<'a>
pub fn is_empty(&self) -> bool
pub fn peek0(&self) -> Option<char>
pub fn peek_n(&self, n: usize) -> Option<&'a str>
Sourcepub fn consume(&mut self, target: char) -> bool
pub fn consume(&mut self, target: char) -> bool
Examples found in repository?
examples/tokenizer.rs (line 30)
13 fn parse(stream: &mut ParseStream) -> syntax_rs::Result<Self> {
14 fn to_u32(chars: &str) -> syntax_rs::Result<u32> {
15 if chars.len() == 0 {
16 return Err("Expected integer.");
17 }
18 let mut number = 0;
19
20 // We don't need to do .chars() here because we are only dealing with numbers.
21 for c in chars.as_bytes() {
22 let digit: u32 = *c as u32 - 0x30;
23 number *= 10;
24 number += digit;
25 }
26 Ok(number)
27 }
28
29 stream.try_parse(|stream| {
30 let negative = stream.cur().consume('-');
31 let mut value = to_u32(stream.cur().advance_while(|c| c.is_digit(10)))? as i64;
32 if negative {
33 value *= -1;
34 }
35
36 Ok(LitInt { value })
37 })
38 }
Sourcepub fn advance(&mut self) -> Option<char>
pub fn advance(&mut self) -> Option<char>
Examples found in repository?
examples/tokenizer.rs (line 159)
154 fn parse(stream: &mut ParseStream) -> syntax_rs::Result<Self> {
155 stream.try_parse(|stream| {
156 Ok(
157 match stream
158 .cur()
159 .advance()
160 .ok_or("Expected `+`, `-`, `*` or `/`.")?
161 {
162 '+' => Punctuation::Plus,
163 '-' => Punctuation::Minus,
164 '*' => Punctuation::Star,
165 '/' => Punctuation::Slash,
166 _ => return Err("Expected `+`, `-`, `*` or `/`."),
167 },
168 )
169 })
170 }
pub fn advance_n(&mut self, n: usize) -> Option<&'a str>
Sourcepub fn advance_while(&mut self, pred: impl FnMut(char) -> bool) -> &'a str
pub fn advance_while(&mut self, pred: impl FnMut(char) -> bool) -> &'a str
Examples found in repository?
examples/tokenizer.rs (line 31)
13 fn parse(stream: &mut ParseStream) -> syntax_rs::Result<Self> {
14 fn to_u32(chars: &str) -> syntax_rs::Result<u32> {
15 if chars.len() == 0 {
16 return Err("Expected integer.");
17 }
18 let mut number = 0;
19
20 // We don't need to do .chars() here because we are only dealing with numbers.
21 for c in chars.as_bytes() {
22 let digit: u32 = *c as u32 - 0x30;
23 number *= 10;
24 number += digit;
25 }
26 Ok(number)
27 }
28
29 stream.try_parse(|stream| {
30 let negative = stream.cur().consume('-');
31 let mut value = to_u32(stream.cur().advance_while(|c| c.is_digit(10)))? as i64;
32 if negative {
33 value *= -1;
34 }
35
36 Ok(LitInt { value })
37 })
38 }
39}
40
41#[derive(Debug)]
42struct LitStr {
43 val: String,
44}
45
46impl Parse for LitStr {
47 fn parse(stream: &mut ParseStream) -> syntax_rs::Result<Self> {
48 stream.parse::<Quote>()?;
49
50 let inside = stream.try_parse(|stream| {
51 Ok(LitStr {
52 val: String::from(stream.cur().advance_while(|c| c != '\"')),
53 })
54 });
55
56 stream.parse::<Quote>()?;
57 inside
58 }
59}
60
61#[derive(Debug)]
62enum Literal {
63 Int(LitInt),
64 String(LitStr),
65}
66
67impl Parse for Literal {
68 fn parse(stream: &mut ParseStream) -> syntax_rs::Result<Self> {
69 if let Ok(lit_int) = stream.parse::<LitInt>() {
70 Ok(Literal::Int(lit_int))
71 } else if let Ok(lit_str) = stream.parse::<LitStr>() {
72 Ok(Literal::String(lit_str))
73 } else {
74 Err("Expected integer or str.")
75 }
76 }
77}
78
79simple_tok_spanned!(Quote, "\"");
80
81#[derive(Debug)]
82struct Ident {
83 string: String,
84 span: Span,
85}
86
87impl Spanned for Ident {
88 fn span(&self) -> Span {
89 self.span
90 }
91
92 fn span_ref_mut(&mut self) -> &mut Span {
93 &mut self.span
94 }
95}
96
97impl Parse for Ident {
98 fn parse(stream: &mut ParseStream) -> syntax_rs::Result<Self> {
99 stream.try_parse(|stream| {
100 let snap = stream.snapshot();
101 let mut first_c = false;
102 let slice = stream.cur().advance_while(|c| {
103 if first_c {
104 first_c = false;
105 UnicodeXID::is_xid_start(c)
106 } else {
107 UnicodeXID::is_xid_continue(c)
108 }
109 });
110 if slice.is_empty() {
111 Err("Expected identifier.")
112 } else {
113 Ok(Ident {
114 string: String::from(slice),
115 span: stream.since(snap),
116 })
117 }
118 })
119 }
pub fn index(&self) -> usize
pub fn iter<'c>(&'c self) -> Iter<'a, 'c> ⓘ
Trait Implementations§
impl<'a> Copy for Cursor<'a>
Auto Trait Implementations§
impl<'a> Freeze for Cursor<'a>
impl<'a> RefUnwindSafe for Cursor<'a>
impl<'a> Send for Cursor<'a>
impl<'a> Sync for Cursor<'a>
impl<'a> Unpin for Cursor<'a>
impl<'a> UnwindSafe for Cursor<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more