Skip to content

Commit b0d00f1

Browse files
committed
Begin restructuring to collect errors
1 parent 46c75bc commit b0d00f1

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,17 @@ fn main() {
160160
std::process::exit(1);
161161
}
162162
};
163-
let technique = match parsing::parse(&filename, &content) {
164-
Ok(document) => document,
165-
Err(error) => {
163+
let result = parsing::parser::parse_with_recovery(&filename, &content);
164+
if result.has_errors() {
165+
for error in &result.errors {
166166
eprintln!(
167167
"{}",
168-
problem::full_parsing_error(&error, &filename, &content, &Terminal)
168+
problem::full_parsing_error(error, &filename, &content, &Terminal)
169169
);
170-
std::process::exit(1);
171170
}
172-
};
171+
std::process::exit(1);
172+
}
173+
let technique = result.document;
173174
// TODO continue with validation of the returned technique
174175

175176
eprintln!("{}", "ok".bright_green());

src/parsing/parser.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@ use std::path::Path;
33
use crate::language::*;
44
use crate::regex::*;
55

6+
#[derive(Debug, PartialEq, Eq)]
7+
pub struct ParseResult<'i> {
8+
pub document: Document<'i>,
9+
pub errors: Vec<ParsingError<'i>>,
10+
}
11+
12+
impl<'i> ParseResult<'i> {
13+
pub fn has_errors(&self) -> bool {
14+
!self
15+
.errors
16+
.is_empty()
17+
}
18+
}
19+
20+
pub fn parse_with_recovery<'i>(path: &'i Path, content: &'i str) -> ParseResult<'i> {
21+
let mut input = Parser::new();
22+
input.filename(path);
23+
input.initialize(content);
24+
25+
input.parse_collecting_errors()
26+
}
27+
628
pub fn parse_via_taking<'i>(
729
path: &'i Path,
830
content: &'i str,
@@ -87,6 +109,7 @@ pub struct Parser<'i> {
87109
original: &'i str,
88110
source: &'i str,
89111
offset: usize,
112+
problems: Vec<ParsingError<'i>>,
90113
}
91114

92115
impl<'i> Parser<'i> {
@@ -96,6 +119,7 @@ impl<'i> Parser<'i> {
96119
original: "",
97120
source: "",
98121
offset: 0,
122+
problems: Vec::new(),
99123
}
100124
}
101125

@@ -106,6 +130,8 @@ impl<'i> Parser<'i> {
106130
self.original = content;
107131
self.source = content;
108132
self.offset = 0;
133+
self.problems
134+
.clear();
109135
}
110136

111137
fn advance(&mut self, width: usize) {
@@ -499,6 +525,7 @@ impl<'i> Parser<'i> {
499525
original: self.original,
500526
source: content,
501527
offset: indent + self.offset,
528+
problems: Vec::new(),
502529
};
503530

504531
// and return

0 commit comments

Comments
 (0)