-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
indentation.rs
146 lines (121 loc) · 4.44 KB
/
indentation.rs
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
use static_assertions::assert_eq_size;
use std::cmp::Ordering;
use std::fmt::Debug;
/// The column index of an indentation.
///
/// A space increments the column by one. A tab adds up to 2 (if tab size is 2) indices, but just one
/// if the column isn't even.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default)]
pub(super) struct Column(u32);
impl Column {
pub(super) const fn new(column: u32) -> Self {
Self(column)
}
}
/// The number of characters in an indentation. Each character accounts for 1.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default)]
pub(super) struct Character(u32);
impl Character {
pub(super) const fn new(characters: u32) -> Self {
Self(characters)
}
}
/// The [Indentation](https://docs.python.org/3/reference/lexical_analysis.html#indentation) of a logical line.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub(super) struct Indentation {
column: Column,
character: Character,
}
impl Indentation {
const TAB_SIZE: u32 = 2;
pub(super) const fn root() -> Self {
Self {
column: Column::new(0),
character: Character::new(0),
}
}
#[cfg(test)]
pub(super) const fn new(column: Column, character: Character) -> Self {
Self { column, character }
}
#[must_use]
pub(super) fn add_space(self) -> Self {
Self {
character: Character(self.character.0 + 1),
column: Column(self.column.0 + 1),
}
}
#[must_use]
pub(super) fn add_tab(self) -> Self {
Self {
character: Character(self.character.0 + 1),
// Compute the column index:
// * Adds `TAB_SIZE` if `column` is a multiple of `TAB_SIZE`
// * Rounds `column` up to the next multiple of `TAB_SIZE` otherwise.
// https://github.com/python/cpython/blob/2cf99026d6320f38937257da1ab014fc873a11a6/Parser/tokenizer.c#L1818
column: Column((self.column.0 / Self::TAB_SIZE + 1) * Self::TAB_SIZE),
}
}
pub(super) fn try_compare(self, other: Indentation) -> Result<Ordering, UnexpectedIndentation> {
let column_ordering = self.column.cmp(&other.column);
let character_ordering = self.character.cmp(&other.character);
if column_ordering == character_ordering {
Ok(column_ordering)
} else {
Err(UnexpectedIndentation)
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub(super) struct UnexpectedIndentation;
// The indentations stack is used to keep track of the current indentation level
// [See Indentation](docs.python.org/3/reference/lexical_analysis.html#indentation).
#[derive(Debug, Clone, Default)]
pub(super) struct Indentations {
stack: Vec<Indentation>,
}
impl Indentations {
pub(super) fn indent(&mut self, indent: Indentation) {
debug_assert_eq!(self.current().try_compare(indent), Ok(Ordering::Less));
self.stack.push(indent);
}
/// Dedent one level to eventually reach `new_indentation`.
///
/// Returns `Err` if the `new_indentation` is greater than the new current indentation level.
pub(super) fn dedent_one(
&mut self,
new_indentation: Indentation,
) -> Result<Option<Indentation>, UnexpectedIndentation> {
let previous = self.dedent();
match new_indentation.try_compare(*self.current())? {
Ordering::Less | Ordering::Equal => Ok(previous),
// ```python
// if True:
// pass
// pass <- The indentation is greater than the expected indent of 0.
// ```
Ordering::Greater => Err(UnexpectedIndentation),
}
}
pub(super) fn dedent(&mut self) -> Option<Indentation> {
self.stack.pop()
}
pub(super) fn current(&self) -> &Indentation {
static ROOT: Indentation = Indentation::root();
self.stack.last().unwrap_or(&ROOT)
}
}
assert_eq_size!(Indentation, u64);
#[cfg(test)]
mod tests {
use super::{Character, Column, Indentation};
use std::cmp::Ordering;
#[test]
fn indentation_try_compare() {
let tab = Indentation::new(Column::new(8), Character::new(1));
assert_eq!(tab.try_compare(tab), Ok(Ordering::Equal));
let two_tabs = Indentation::new(Column::new(16), Character::new(2));
assert_eq!(two_tabs.try_compare(tab), Ok(Ordering::Greater));
assert_eq!(tab.try_compare(two_tabs), Ok(Ordering::Less));
}
}