Skip to content

Commit 471b2a2

Browse files
committed
User ParseResult to simplify take_block_lines() result
1 parent d681afe commit 471b2a2

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

src/parsing/parser.rs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::language::*;
44
use crate::regex::*;
55

66
#[derive(Debug, PartialEq, Eq)]
7-
struct ParseResult<'i> {
8-
pub document: Document<'i>,
7+
struct ParseResult<'i, T> {
8+
pub value: T,
99
pub errors: Vec<ParsingError<'i>>,
1010
}
1111

@@ -29,7 +29,7 @@ pub fn parse_with_recovery<'i>(
2929
.errors
3030
.is_empty()
3131
{
32-
Ok(result.document)
32+
Ok(result.value)
3333
} else {
3434
Err(result.errors)
3535
}
@@ -156,7 +156,7 @@ impl<'i> Parser<'i> {
156156
}
157157
}
158158

159-
fn parse_collecting_errors(&mut self) -> ParseResult<'i> {
159+
fn parse_collecting_errors(&mut self) -> ParseResult<'i, Document<'i>> {
160160
// Clear any existing errors
161161
self.problems
162162
.clear();
@@ -212,21 +212,11 @@ impl<'i> Parser<'i> {
212212
}
213213
break;
214214
} else if is_procedure_declaration(self.source) {
215-
match self
216-
.take_block_lines(
217-
is_procedure_declaration,
218-
|line| is_section(line) || potential_procedure_declaration(line),
219-
|inner| {
220-
let result = inner.read_procedure();
221-
let errors = std::mem::take(&mut inner.problems);
222-
Ok((result, errors))
223-
},
224-
)
225-
.and_then(|(result, errors)| {
226-
self.problems
227-
.extend(errors);
228-
result
229-
}) {
215+
match self.take_block_lines(
216+
is_procedure_declaration,
217+
|line| is_section(line) || potential_procedure_declaration(line),
218+
|inner| inner.read_procedure(),
219+
) {
230220
Ok(mut procedure) => {
231221
// Check if there are sections following this procedure
232222
while !self.is_finished() {
@@ -276,21 +266,11 @@ impl<'i> Parser<'i> {
276266
// It might be that we've encountered a malformed procedure
277267
// declaration, so we try parsing it anyway to get a more
278268
// specific error message.
279-
match self
280-
.take_block_lines(
281-
|_| true, // Accept the line regardless
282-
|line| is_section(line) || potential_procedure_declaration(line),
283-
|inner| {
284-
let result = inner.read_procedure();
285-
let errors = std::mem::take(&mut inner.problems);
286-
Ok((result, errors))
287-
},
288-
)
289-
.and_then(|(result, errors)| {
290-
self.problems
291-
.extend(errors);
292-
result
293-
}) {
269+
match self.take_block_lines(
270+
|_| true, // Accept the line regardless
271+
|line| is_section(line) || potential_procedure_declaration(line),
272+
|inner| inner.read_procedure(),
273+
) {
294274
Ok(procedure) => {
295275
procedures.push(procedure);
296276
}
@@ -316,7 +296,10 @@ impl<'i> Parser<'i> {
316296

317297
let document = Document { header, body };
318298
let errors = std::mem::take(&mut self.problems);
319-
ParseResult { document, errors }
299+
ParseResult {
300+
value: document,
301+
errors,
302+
}
320303
}
321304

322305
/// consume up to but not including newline (or end), then take newline
@@ -377,13 +360,17 @@ impl<'i> Parser<'i> {
377360
let mut parser = self.subparser(0, block);
378361

379362
// Pass to closure for processing
380-
let result = function(&mut parser)?;
363+
let result = function(&mut parser);
364+
365+
// Merge any errors from the subparser into the parent
366+
self.problems
367+
.extend(parser.problems);
381368

382369
// Advance parser state
383370
self.source = &self.source[i..];
384371
self.offset += i;
385372

386-
Ok(result)
373+
result
387374
}
388375

389376
fn take_block_chars<A, F>(
@@ -4744,11 +4731,11 @@ echo test
47444731
0
47454732
);
47464733
assert!(result
4747-
.document
4734+
.value
47484735
.header
47494736
.is_some());
47504737
assert!(result
4751-
.document
4738+
.value
47524739
.body
47534740
.is_some());
47544741

@@ -4766,13 +4753,13 @@ echo test
47664753
.iter()
47674754
.any(|e| matches!(e, ParsingError::InvalidHeader(_))));
47684755
assert!(result
4769-
.document
4756+
.value
47704757
.header
47714758
.is_none());
47724759

47734760
// Test that the method returns ParseResult instead of Result
47744761
input.initialize("some content");
4775-
let _result: ParseResult = input.parse_collecting_errors();
4762+
let _result: ParseResult<Document> = input.parse_collecting_errors();
47764763
// If this compiles, the method signature is correct
47774764
}
47784765

0 commit comments

Comments
 (0)