Skip to content

Commit a04f694

Browse files
committed
refactor(pc): Introduce ParserErrorTrait trait
Introducing a `ParserErrorTrait` trait that describes an error. An important change is that that trait now determines if the error is fatal or not, which changes the error returned in `ParseResult` (the fatal part was specified as a boolean in a tuple, now that is gone). Regarding naming, settling on this naming: - soft errors, errors that are not fatal and just indicate a miss - fatal errors, unrecoverable errors Renamed the `ParseError` struct to `ParserError` and the old enum is now called `ParserErrorKind`.
1 parent 3f5d822 commit a04f694

89 files changed

Lines changed: 844 additions & 765 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

rusty_linter/src/core/error.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rusty_common::{AtPos, HasPos, Positioned};
2-
use rusty_parser::ParseError;
2+
use rusty_parser::{ParserError, ParserErrorKind};
33
use rusty_variant::VariantError;
44

55
#[derive(Clone, Debug, PartialEq)]
@@ -26,7 +26,7 @@ pub enum LintError {
2626
TypeNotDefined,
2727
VariableRequired,
2828
WrongNumberOfDimensions,
29-
ParserError(ParseError),
29+
ParserError(ParserErrorKind),
3030

3131
// custom
3232
NotFiniteNumber,
@@ -44,13 +44,13 @@ impl From<VariantError> for LintError {
4444
}
4545
}
4646

47-
impl From<ParseError> for LintError {
48-
fn from(e: ParseError) -> Self {
49-
match e {
50-
ParseError::NextWithoutFor => Self::NextWithoutFor,
51-
ParseError::Overflow => Self::Overflow,
52-
ParseError::ElementNotDefined => Self::ElementNotDefined,
53-
_ => Self::ParserError(e),
47+
impl From<ParserError> for LintError {
48+
fn from(e: ParserError) -> Self {
49+
match e.kind() {
50+
ParserErrorKind::NextWithoutFor => Self::NextWithoutFor,
51+
ParserErrorKind::Overflow => Self::Overflow,
52+
ParserErrorKind::ElementNotDefined => Self::ElementNotDefined,
53+
_ => Self::ParserError(e.to_kind()),
5454
}
5555
}
5656
}

rusty_parser/src/built_ins/close.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ use crate::input::RcStringView;
44
use crate::pc_specific::*;
55
use crate::tokens::comma_ws;
66
use crate::{
7-
BuiltInSub, ParseError, file_handle_as_expression_pos_p, guarded_file_handle_or_expression_p, *
7+
BuiltInSub, ParserError, file_handle_as_expression_pos_p, guarded_file_handle_or_expression_p, *
88
};
99

1010
// <result> ::= <CLOSE> | <CLOSE><file_handles>
1111
// file_handles ::= <first_file_handle> | <first_file_handle> <opt-ws> "," <opt-ws> <next_file_handles>
1212
// next_file_handles ::= <file_handle> | <file_handle> <opt-ws> "," <opt-ws> <next_file_handles>
1313
// first_file_handle ::= "(" <file_handle> ")" | <ws> <file_handle>
1414
// file_handle ::= "#" <digits> | <expr>
15-
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
15+
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
1616
seq2(
1717
keyword(Keyword::Close),
1818
file_handles(),
1919
|_, file_handles| Statement::built_in_sub_call(BuiltInSub::Close, file_handles),
2020
)
2121
}
2222

23-
fn file_handles() -> impl Parser<RcStringView, Output = Expressions, Error = ParseError> {
23+
fn file_handles() -> impl Parser<RcStringView, Output = Expressions, Error = ParserError> {
2424
guarded_file_handle_or_expression_p()
2525
.map(|first| vec![first])
2626
.and(
@@ -33,7 +33,7 @@ fn file_handles() -> impl Parser<RcStringView, Output = Expressions, Error = Par
3333
}
3434

3535
fn file_handle_or_expression_p()
36-
-> impl Parser<RcStringView, Output = ExpressionPos, Error = ParseError> {
36+
-> impl Parser<RcStringView, Output = ExpressionPos, Error = ParserError> {
3737
OrParser::new(vec![
3838
Box::new(file_handle_as_expression_pos_p()),
3939
Box::new(expression_pos_p()),
@@ -44,7 +44,6 @@ fn file_handle_or_expression_p()
4444
mod tests {
4545
use rusty_common::*;
4646

47-
use crate::error::ParseError;
4847
use crate::test_utils::*;
4948
use crate::{BuiltInSub, assert_parser_err, *};
5049

@@ -114,15 +113,15 @@ mod tests {
114113
#[test]
115114
fn test_one_file_number_with_hash_no_leading_space() {
116115
let input = "CLOSE#1";
117-
assert_parser_err!(input, ParseError::expected("end-of-statement"), 1, 7);
116+
assert_parser_err!(input, ParserErrorKind::expected("end-of-statement"), 1, 7);
118117
}
119118

120119
#[test]
121120
fn test_one_file_number_with_hash_parenthesis_leading_space() {
122121
let input = "CLOSE (#1)";
123122
assert_parser_err!(
124123
input,
125-
ParseError::expected("expression inside parenthesis"),
124+
ParserErrorKind::expected("expression inside parenthesis"),
126125
1,
127126
8
128127
);
@@ -133,7 +132,7 @@ mod tests {
133132
let input = "CLOSE(#1)";
134133
assert_parser_err!(
135134
input,
136-
ParseError::expected("expression inside parenthesis"),
135+
ParserErrorKind::expected("expression inside parenthesis"),
137136
1,
138137
7
139138
);

rusty_parser/src/built_ins/color.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ use rusty_pc::*;
22

33
use crate::built_ins::common::parse_built_in_sub_with_opt_args;
44
use crate::input::RcStringView;
5-
use crate::{BuiltInSub, ParseError, *};
5+
use crate::{BuiltInSub, ParserError, *};
66

7-
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
7+
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
88
parse_built_in_sub_with_opt_args(Keyword::Color, BuiltInSub::Color)
99
}
1010

1111
#[cfg(test)]
1212
mod tests {
13-
use crate::error::ParseError;
1413
use crate::test_utils::*;
15-
use crate::{BuiltInSub, Statement, assert_parser_err, parse};
14+
use crate::{BuiltInSub, ParserErrorKind, Statement, assert_parser_err, parse};
1615

1716
#[test]
1817
fn parse_foreground_only() {
@@ -60,6 +59,6 @@ mod tests {
6059
#[test]
6160
fn parse_no_args() {
6261
let input = "COLOR";
63-
assert_parser_err!(input, ParseError::expected("whitespace"), 1, 6);
62+
assert_parser_err!(input, ParserErrorKind::expected("whitespace"), 1, 6);
6463
}
6564
}

rusty_parser/src/built_ins/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use rusty_pc::*;
44
use crate::input::RcStringView;
55
use crate::pc_specific::*;
66
use crate::tokens::comma_ws;
7-
use crate::{BuiltInSub, ParseError, *};
7+
use crate::{BuiltInSub, ParserError, *};
88

99
/// Parses built-in subs with optional arguments.
1010
/// Used only by `COLOR` and `LOCATE`.
1111
pub fn parse_built_in_sub_with_opt_args(
1212
k: Keyword,
1313
built_in_sub: BuiltInSub,
14-
) -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
14+
) -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
1515
keyword_ws_p(k)
1616
.and_keep_right(csv_allow_missing())
1717
.map(move |opt_args| {
@@ -41,15 +41,15 @@ fn map_opt_args_to_flags(args: Vec<Option<ExpressionPos>>) -> Expressions {
4141

4242
/// Comma separated list of items, allowing items to be missing between commas.
4343
pub fn csv_allow_missing()
44-
-> impl Parser<RcStringView, Output = Vec<Option<ExpressionPos>>, Error = ParseError> {
44+
-> impl Parser<RcStringView, Output = Vec<Option<ExpressionPos>>, Error = ParserError> {
4545
expression_pos_p()
4646
.delimited_by_allow_missing(comma_ws(), trailing_comma_error())
4747
.or_default()
4848
}
4949

5050
/// Used in `INPUT` and `LINE INPUT`, parsing an optional file number.
5151
pub fn opt_file_handle_comma_p()
52-
-> impl Parser<RcStringView, Output = Option<Positioned<FileHandle>>, Error = ParseError> {
52+
-> impl Parser<RcStringView, Output = Option<Positioned<FileHandle>>, Error = ParserError> {
5353
seq2(file_handle_p(), comma_ws(), |l, _| l).to_option()
5454
}
5555

rusty_parser/src/built_ins/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use rusty_pc::*;
22

33
use crate::input::RcStringView;
44
use crate::pc_specific::*;
5-
use crate::{BuiltInSub, ParseError, *};
5+
use crate::{BuiltInSub, ParserError, *};
66

7-
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
7+
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
88
seq2(
99
keyword(Keyword::Data),
1010
csv_expressions_first_guarded().or_default(),

rusty_parser/src/built_ins/def_seg.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use rusty_pc::*;
33
use crate::input::RcStringView;
44
use crate::pc_specific::*;
55
use crate::tokens::equal_sign_ws;
6-
use crate::{BuiltInSub, ParseError, *};
6+
use crate::{BuiltInSub, ParserError, *};
77

88
// DEF SEG(=expr)?
9-
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
9+
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
1010
seq2(
1111
keyword_pair(Keyword::Def, Keyword::Seg),
1212
equal_sign_and_expression().to_option(),
@@ -17,7 +17,7 @@ pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseErr
1717
}
1818

1919
fn equal_sign_and_expression()
20-
-> impl Parser<RcStringView, Output = ExpressionPos, Error = ParseError> {
20+
-> impl Parser<RcStringView, Output = ExpressionPos, Error = ParserError> {
2121
equal_sign_ws().and_keep_right(expression_pos_p().or_expected("Expression after equal sign"))
2222
}
2323

rusty_parser/src/built_ins/field.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use rusty_pc::*;
44
use crate::input::RcStringView;
55
use crate::pc_specific::*;
66
use crate::tokens::comma_ws;
7-
use crate::{BuiltInSub, ParseError, *};
7+
use crate::{BuiltInSub, ParserError, *};
88

99
/// Example: FIELD #1, 10 AS FirstName$, 20 AS LastName$
10-
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
10+
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
1111
seq4(
1212
keyword_ws_p(Keyword::Field),
1313
file_handle_p().or_expected("file-number"),
@@ -19,8 +19,8 @@ pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseErr
1919
)
2020
}
2121

22-
fn field_item_p() -> impl Parser<RcStringView, Output = (ExpressionPos, NamePos), Error = ParseError>
23-
{
22+
fn field_item_p()
23+
-> impl Parser<RcStringView, Output = (ExpressionPos, NamePos), Error = ParserError> {
2424
seq3(
2525
expr_pos_ws_p(),
2626
keyword_ws_p(Keyword::As),

rusty_parser/src/built_ins/get.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ use rusty_pc::*;
33
use crate::input::RcStringView;
44
use crate::pc_specific::*;
55
use crate::tokens::comma_ws;
6-
use crate::{BuiltInSub, ParseError, *};
6+
use crate::{BuiltInSub, ParserError, *};
77

8-
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
8+
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
99
parse_get_or_put(Keyword::Get, BuiltInSub::Get)
1010
}
1111

1212
pub fn parse_get_or_put(
1313
k: Keyword,
1414
built_in_sub: BuiltInSub,
15-
) -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
15+
) -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
1616
seq4(
1717
keyword_ws_p(k),
1818
file_handle_p().or_expected("file-number"),

rusty_parser/src/built_ins/input.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use rusty_pc::*;
33
use crate::built_ins::common::{encode_opt_file_handle_arg, opt_file_handle_comma_p};
44
use crate::input::RcStringView;
55
use crate::pc_specific::*;
6-
use crate::{BuiltInSub, ParseError, *};
6+
use crate::{BuiltInSub, ParserError, *};
77

88
// INPUT variable-list
99
// INPUT #file-number%, variable-list
10-
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseError> {
10+
pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParserError> {
1111
seq3(
1212
keyword_ws_p(Keyword::Input),
1313
opt_file_handle_comma_p(),
@@ -22,7 +22,6 @@ pub fn parse() -> impl Parser<RcStringView, Output = Statement, Error = ParseErr
2222

2323
#[cfg(test)]
2424
mod tests {
25-
use crate::error::ParseError;
2625
use crate::test_utils::*;
2726
use crate::{BuiltInSub, assert_built_in_sub_call, assert_parser_err, *};
2827

@@ -52,13 +51,13 @@ mod tests {
5251
#[test]
5352
fn test_no_whitespace_ignoring_after_input() {
5453
let input = "INPUT";
55-
assert_parser_err!(input, ParseError::expected("whitespace"));
54+
assert_parser_err!(input, ParserErrorKind::expected("whitespace"));
5655
}
5756

5857
#[test]
5958
fn test_no_variable() {
6059
let input = "INPUT ";
61-
assert_parser_err!(input, ParseError::expected("#file-number or variable"));
60+
assert_parser_err!(input, ParserErrorKind::expected("#file-number or variable"));
6261
}
6362

6463
#[test]

rusty_parser/src/built_ins/len.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use rusty_pc::*;
22

33
use crate::input::RcStringView;
44
use crate::pc_specific::*;
5-
use crate::{BuiltInFunction, ParseError, *};
5+
use crate::{BuiltInFunction, ParserError, *};
66

7-
pub fn parse() -> impl Parser<RcStringView, Output = Expression, Error = ParseError> {
7+
pub fn parse() -> impl Parser<RcStringView, Output = Expression, Error = ParserError> {
88
keyword(Keyword::Len)
99
.and_keep_right(in_parenthesis_csv_expressions_non_opt("variable"))
1010
.map(|v| Expression::BuiltInFunctionCall(BuiltInFunction::Len, v))

0 commit comments

Comments
 (0)