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
206
207
|
use super::errors::{ErrorKind, ParserError};
use super::{core::Parser, core::Result, slice::Slice};
use crate::ast;
#[derive(Debug, PartialEq)]
enum TextElementTermination {
LineFeed,
CRLF,
PlaceableStart,
EOF,
}
// This enum tracks the placement of the text element in the pattern, which is needed for
// dedentation logic.
#[derive(Debug, PartialEq)]
enum TextElementPosition {
InitialLineStart,
LineStart,
Continuation,
}
// This enum allows us to mark pointers in the source which will later become text elements
// but without slicing them out of the source string. This makes the indentation adjustments
// cheaper since they'll happen on the pointers, rather than extracted slices.
#[derive(Debug)]
enum PatternElementPlaceholders<S> {
Placeable(ast::Expression<S>),
// (start, end, indent, position)
TextElement(usize, usize, usize, TextElementPosition),
}
// This enum tracks whether the text element is blank or not.
// This is important to identify text elements which should not be taken into account
// when calculating common indent.
#[derive(Debug, PartialEq)]
enum TextElementType {
Blank,
NonBlank,
}
impl<'s, S> Parser<S>
where
S: Slice<'s>,
{
pub(super) fn get_pattern(&mut self) -> Result<Option<ast::Pattern<S>>> {
let mut elements = vec![];
let mut last_non_blank = None;
let mut common_indent = None;
self.skip_blank_inline();
let mut text_element_role = if self.skip_eol() {
self.skip_blank_block();
TextElementPosition::LineStart
} else {
TextElementPosition::InitialLineStart
};
while self.ptr < self.length {
if self.take_byte_if(b'{') {
if text_element_role == TextElementPosition::LineStart {
common_indent = Some(0);
}
let exp = self.get_placeable()?;
last_non_blank = Some(elements.len());
elements.push(PatternElementPlaceholders::Placeable(exp));
text_element_role = TextElementPosition::Continuation;
} else {
let slice_start = self.ptr;
let mut indent = 0;
if text_element_role == TextElementPosition::LineStart {
indent = self.skip_blank_inline();
if let Some(b) = get_current_byte!(self) {
if indent == 0 {
if b != &b'\r' && b != &b'\n' {
break;
}
} else if !Self::is_byte_pattern_continuation(*b) {
self.ptr = slice_start;
break;
}
} else {
break;
}
}
let (start, end, text_element_type, termination_reason) = self.get_text_slice()?;
if start != end {
if text_element_role == TextElementPosition::LineStart
&& text_element_type == TextElementType::NonBlank
{
if let Some(common) = common_indent {
if indent < common {
common_indent = Some(indent);
}
} else {
common_indent = Some(indent);
}
}
if text_element_role != TextElementPosition::LineStart
|| text_element_type == TextElementType::NonBlank
|| termination_reason == TextElementTermination::LineFeed
{
if text_element_type == TextElementType::NonBlank {
last_non_blank = Some(elements.len());
}
elements.push(PatternElementPlaceholders::TextElement(
slice_start,
end,
indent,
text_element_role,
));
}
}
text_element_role = match termination_reason {
TextElementTermination::LineFeed => TextElementPosition::LineStart,
TextElementTermination::CRLF => TextElementPosition::LineStart,
TextElementTermination::PlaceableStart => TextElementPosition::Continuation,
TextElementTermination::EOF => TextElementPosition::Continuation,
};
}
}
if let Some(last_non_blank) = last_non_blank {
let elements = elements
.into_iter()
.take(last_non_blank + 1)
.enumerate()
.map(|(i, elem)| match elem {
PatternElementPlaceholders::Placeable(expression) => {
ast::PatternElement::Placeable { expression }
}
PatternElementPlaceholders::TextElement(start, end, indent, role) => {
let start = if role == TextElementPosition::LineStart {
common_indent.map_or_else(
|| start + indent,
|common_indent| start + std::cmp::min(indent, common_indent),
)
} else {
start
};
let mut value = self.source.slice(start..end);
if last_non_blank == i {
value.trim();
}
ast::PatternElement::TextElement { value }
}
})
.collect();
return Ok(Some(ast::Pattern { elements }));
}
Ok(None)
}
fn get_text_slice(
&mut self,
) -> Result<(usize, usize, TextElementType, TextElementTermination)> {
let start_pos = self.ptr;
let mut text_element_type = TextElementType::Blank;
while let Some(b) = get_current_byte!(self) {
match b {
b' ' => self.ptr += 1,
b'\n' => {
self.ptr += 1;
return Ok((
start_pos,
self.ptr,
text_element_type,
TextElementTermination::LineFeed,
));
}
b'\r' if self.is_byte_at(b'\n', self.ptr + 1) => {
self.ptr += 1;
return Ok((
start_pos,
self.ptr - 1,
text_element_type,
TextElementTermination::CRLF,
));
}
b'{' => {
return Ok((
start_pos,
self.ptr,
text_element_type,
TextElementTermination::PlaceableStart,
));
}
b'}' => {
return error!(ErrorKind::UnbalancedClosingBrace, self.ptr);
}
_ => {
text_element_type = TextElementType::NonBlank;
self.ptr += 1
}
}
}
Ok((
start_pos,
self.ptr,
text_element_type,
TextElementTermination::EOF,
))
}
}
|