-
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathcursor.rs
More file actions
205 lines (179 loc) · 6.2 KB
/
cursor.rs
File metadata and controls
205 lines (179 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! Boa's lexer cursor that manages the input byte stream.
use crate::source::{ReadChar, UTF8Input};
use boa_ast::{LinearPosition, Position, PositionGroup, SourceText};
use std::io::{self, Error};
/// Cursor over the source code.
#[derive(Debug)]
pub(super) struct Cursor<R> {
iter: R,
pos: Position,
module: bool,
strict: bool,
peeked: [Option<u32>; 4],
source_collector: SourceText,
}
impl<R> Cursor<R> {
/// Gets the current position of the cursor in the source code.
#[inline]
pub(super) fn pos_group(&self) -> PositionGroup {
PositionGroup::new(self.pos, self.linear_pos())
}
/// Gets the current position of the cursor in the source code.
#[inline]
pub(super) const fn pos(&self) -> Position {
self.pos
}
/// Gets the current linear position of the cursor in the source code.
#[inline]
pub(super) fn linear_pos(&self) -> LinearPosition {
self.source_collector.cur_linear_position()
}
pub(super) fn take_source(&mut self) -> SourceText {
let replace_with = SourceText::with_capacity(0);
std::mem::replace(&mut self.source_collector, replace_with)
}
/// Advances the position to the next column.
fn next_column(&mut self) {
let current_line = self.pos.line_number();
let next_column = self.pos.column_number() + 1;
self.pos = Position::new(current_line, next_column);
}
/// Advances the position to the next line.
fn next_line(&mut self) {
let next_line = self.pos.line_number() + 1;
self.pos = Position::new(next_line, 1);
}
/// Returns if strict mode is currently active.
pub(super) const fn strict(&self) -> bool {
self.strict
}
/// Sets the current strict mode.
pub(super) fn set_strict(&mut self, strict: bool) {
self.strict = strict;
}
/// Returns if the module mode is currently active.
pub(super) const fn module(&self) -> bool {
self.module
}
/// Sets the current goal symbol to module.
pub(super) fn set_module(&mut self, module: bool) {
self.module = module;
self.strict = module;
}
}
impl<R: ReadChar> Cursor<R> {
/// Creates a new Lexer cursor.
pub(super) fn new(inner: R) -> Self {
Self {
iter: inner,
pos: Position::new(1, 1),
strict: false,
module: false,
peeked: [None; 4],
source_collector: SourceText::default(),
}
}
/// Peeks the next n bytes, the maximum number of peeked bytes is 4 (n <= 4).
pub(super) fn peek_n(&mut self, n: u8) -> Result<&[Option<u32>; 4], Error> {
let peeked = self.peeked.iter().filter(|c| c.is_some()).count();
let needs_peek = n as usize - peeked;
for i in 0..needs_peek {
let next = self.iter.next_char()?;
self.peeked[i + peeked] = next;
}
Ok(&self.peeked)
}
/// Peeks the next UTF-8 character in u32 code point.
pub(super) fn peek_char(&mut self) -> Result<Option<u32>, Error> {
if let Some(c) = self.peeked[0] {
return Ok(Some(c));
}
let next = self.iter.next_char()?;
self.peeked[0] = next;
Ok(next)
}
pub(super) fn next_if(&mut self, c: u32) -> io::Result<bool> {
if self.peek_char()? == Some(c) {
self.next_char()?;
Ok(true)
} else {
Ok(false)
}
}
/// Applies the predicate to the next character and returns the result.
/// Returns false if the next character is not a valid ascii or there is no next character.
/// Otherwise returns the result from the predicate on the ascii in char
///
/// The buffer is not incremented.
pub(super) fn next_is_ascii_pred<F>(&mut self, pred: &F) -> io::Result<bool>
where
F: Fn(char) -> bool,
{
Ok(match self.peek_char()? {
Some(byte) if (0..=0x7F).contains(&byte) =>
{
#[allow(clippy::cast_possible_truncation)]
pred(char::from(byte as u8))
}
Some(_) | None => false,
})
}
/// Fills a mutable slice up to the ends while characters are alphabetic. Returns
/// the number of characters read, or `N+1` if the buffer was filled but there were
/// still characters after.
pub(super) fn take_array_alphabetic<const N: usize>(
&mut self,
arr: &mut [u32; N],
) -> io::Result<usize> {
for (i, out) in arr.iter_mut().enumerate() {
match self.peek_char()? {
// A..Z | a..z
Some(0x41..=0x5A | 0x61..=0x7A) => {
*out = self.next_char()?.expect("Already checked.");
}
_ => return Ok(i),
}
}
// Check the next character and return N+1 if it's alphabetic.
match self.peek_char() {
// A..Z | a..z
Ok(Some(0x41..=0x5A | 0x61..=0x7A)) => Ok(N + 1),
_ => Ok(N),
}
}
/// Retrieves the next UTF-8 character.
pub(crate) fn next_char(&mut self) -> Result<Option<u32>, Error> {
let ch = if let Some(c) = self.peeked[0] {
self.peeked[0] = None;
self.peeked.rotate_left(1);
Some(c)
} else {
self.iter.next_char()?
};
if let Some(ch) = ch {
self.source_collector.collect_code_point(ch);
}
match ch {
Some(0xD) => {
// Try to take a newline if it's next, for windows "\r\n" newlines
// Otherwise, treat as a Mac OS9 bare '\r' newline
if self.peek_char()? == Some(0xA) {
self.peeked[0] = None;
self.peeked.rotate_left(1);
self.source_collector.collect_code_point(0xA);
}
self.next_line();
}
// '\n' | '\u{2028}' | '\u{2029}'
Some(0xA | 0x2028 | 0x2029) => self.next_line(),
Some(_) => self.next_column(),
_ => {}
}
Ok(ch)
}
}
impl<'a> From<&'a [u8]> for Cursor<UTF8Input<&'a [u8]>> {
fn from(input: &'a [u8]) -> Self {
Self::new(UTF8Input::new(input))
}
}