Skip to content

Commit 12c2050

Browse files
committed
refactor(pc): Merge SetContext trait into Parser
It helps with using hidden types (`impl Parser`).
1 parent cd12fd7 commit 12c2050

37 files changed

Lines changed: 122 additions & 187 deletions

rusty_parser/src/core/dim_name.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ mod type_definition {
166166
use crate::{ParserError, *};
167167

168168
pub fn extended_type()
169-
-> impl Parser<StringView, VarNameCtx, Output = DimType, Error = ParserError>
170-
+ SetContext<VarNameCtx> {
169+
-> impl Parser<StringView, VarNameCtx, Output = DimType, Error = ParserError> {
171170
ctx_parser::<StringView, VarNameCtx, ParserError>()
172171
.map(|(_opt_q, allow_user_defined)| {
173172
let mut parsers: Vec<

rusty_parser/src/core/expression.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,7 @@ pub mod property {
937937
/// If it was qualified, we return Ok(None) without trying to parse,
938938
/// because qualified names can't have properties.
939939
fn ctx_dot_property()
940-
-> impl Parser<StringView, bool, Output = Option<NameAsTokens>, Error = ParserError>
941-
+ SetContext<bool> {
940+
-> impl Parser<StringView, bool, Output = Option<NameAsTokens>, Error = ParserError> {
942941
ctx_parser()
943942
.and_then(|was_first_expr_qualified| {
944943
if was_first_expr_qualified {
@@ -1031,7 +1030,7 @@ mod binary_expression {
10311030
bool,
10321031
Output = Option<(Positioned<Operator>, ExpressionPos)>,
10331032
Error = ParserError,
1034-
> + SetContext<bool> {
1033+
> {
10351034
operator()
10361035
.then_with_in_context(third_parser(), is_keyword_op, TupleCombiner)
10371036
.to_option()
@@ -1041,8 +1040,7 @@ mod binary_expression {
10411040
op.element == Operator::And || op.element == Operator::Or || op.element == Operator::Modulo
10421041
}
10431042

1044-
fn third_parser()
1045-
-> impl Parser<StringView, bool, Output = ExpressionPos, Error = ParserError> + SetContext<bool>
1043+
fn third_parser() -> impl Parser<StringView, bool, Output = ExpressionPos, Error = ParserError>
10461044
{
10471045
IifParser::new(
10481046
guard::parser().to_fatal(),
@@ -1077,8 +1075,7 @@ mod binary_expression {
10771075
/// parenthesis. If that is the case, leading whitespace is not required for
10781076
/// keyword based operators.
10791077
fn operator()
1080-
-> impl Parser<StringView, bool, Output = Positioned<Operator>, Error = ParserError>
1081-
+ SetContext<bool> {
1078+
-> impl Parser<StringView, bool, Output = Positioned<Operator>, Error = ParserError> {
10821079
IifParser::new(
10831080
// no whitespace needed
10841081
opt_and_keep_right(whitespace_ignoring(), operator_p()),

rusty_parser/src/core/opt_second_expression.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rusty_pc::and::TupleCombiner;
2-
use rusty_pc::{IifParser, Parser, ParserErrorTrait, SetContext};
2+
use rusty_pc::{IifParser, Parser, ParserErrorTrait};
33

44
use crate::core::expression::ws_expr_pos_p;
55
use crate::error::ParserError;
@@ -33,8 +33,7 @@ where
3333
// first_parser AND [ cond_ws(is_first_paren) KEYWORD !AND! ws_expr ]
3434
fn parse_second(
3535
k: Keyword,
36-
) -> impl Parser<StringView, bool, Output = Option<ExpressionPos>, Error = ParserError> + SetContext<bool>
37-
{
36+
) -> impl Parser<StringView, bool, Output = Option<ExpressionPos>, Error = ParserError> {
3837
// the left side needs the context
3938
ws_keyword(k)
4039
.and_keep_right(
@@ -45,7 +44,7 @@ fn parse_second(
4544
.to_option()
4645
}
4746

48-
fn ws_keyword(k: Keyword) -> impl Parser<StringView, bool, Error = ParserError> + SetContext<bool> {
47+
fn ws_keyword(k: Keyword) -> impl Parser<StringView, bool, Error = ParserError> {
4948
// the left side has the context
5049
conditionally_opt_whitespace().and_tuple(
5150
// but the right side does not
@@ -69,7 +68,7 @@ fn err(keyword: Keyword) -> ParserError {
6968
/// * `(1 + 2)AND` no whitespace is required before `AND`
7069
/// * `1 + 2AND` the lack of whitespace before `AND` is an error
7170
pub(super) fn conditionally_opt_whitespace()
72-
-> impl Parser<StringView, bool, Output = (), Error = ParserError> + SetContext<bool> {
71+
-> impl Parser<StringView, bool, Output = (), Error = ParserError> {
7372
IifParser::new(
7473
// allow none
7574
whitespace_ignoring().to_option().map_to_unit(),

rusty_parser/src/core/param_name.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use rusty_common::Positioned;
22
use rusty_pc::*;
33

4-
use crate::core::{VarNameCtx, var_name};
4+
use crate::core::{VarNameCtx, user_defined_type, var_name};
55
use crate::input::StringView;
66
use crate::pc_specific::*;
77
use crate::tokens::{any_symbol_of, any_token_of};
8-
use crate::{Keyword, ParserError, *};
8+
use crate::{
9+
BareNamePos, BuiltInStyle, ExpressionType, HasExpressionType, Keyword, ParserError, TypeQualifier, TypedName, VarType
10+
};
911

1012
pub type Parameter = TypedName<ParamType>;
1113
pub type ParameterPos = Positioned<Parameter>;
@@ -94,9 +96,7 @@ fn array_indicator() -> impl Parser<StringView, Output = Option<(Token, Token)>,
9496
seq2(any_symbol_of!('('), any_symbol_of!(')'), |l, r| (l, r)).to_option()
9597
}
9698

97-
fn extended_type()
98-
-> impl Parser<StringView, VarNameCtx, Output = ParamType, Error = ParserError> + SetContext<VarNameCtx>
99-
{
99+
fn extended_type() -> impl Parser<StringView, VarNameCtx, Output = ParamType, Error = ParserError> {
100100
ctx_parser()
101101
.map(|(_, allow_user_defined)| {
102102
if allow_user_defined {

rusty_parser/src/core/print.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ fn print_args_parser() -> impl Parser<StringView, Output = Vec<PrintArg>, Error
160160
)
161161
}
162162

163-
fn print_arg_parser()
164-
-> impl Parser<StringView, bool, Output = PrintArg, Error = ParserError> + SetContext<bool> {
163+
fn print_arg_parser() -> impl Parser<StringView, bool, Output = PrintArg, Error = ParserError> {
165164
IifParser::new(delimiter_print_arg(), any_print_arg())
166165
}
167166

rusty_parser/src/core/statements.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn zero_or_more_statements_p(
4242
fn one_statement_p(
4343
exit_keywords: &[Keyword],
4444
custom_err: Option<ParserError>,
45-
) -> impl Parser<StringView, bool, Output = StatementPos, Error = ParserError> + SetContext<bool> {
45+
) -> impl Parser<StringView, bool, Output = StatementPos, Error = ParserError> {
4646
one_statement_or_exit_keyword_p(exit_keywords, custom_err).and_then(
4747
|statement_or_exit_keyword| match statement_or_exit_keyword {
4848
// we parsed a statement, return it
@@ -60,8 +60,7 @@ fn one_statement_p(
6060
fn one_statement_or_exit_keyword_p(
6161
exit_keywords: &[Keyword],
6262
custom_err: Option<ParserError>,
63-
) -> impl Parser<StringView, bool, Output = StatementOrExitKeyword, Error = ParserError> + SetContext<bool>
64-
{
63+
) -> impl Parser<StringView, bool, Output = StatementOrExitKeyword, Error = ParserError> {
6564
ThenWithLeftParser::new(
6665
// must parse the separator
6766
ctx_demand_separator_p(),
@@ -86,8 +85,7 @@ fn is_comment(statement_or_exit_keyword: &StatementOrExitKeyword) -> bool {
8685

8786
/// A statement separator that is aware if the previously parsed statement
8887
/// was a comment or not.
89-
fn ctx_demand_separator_p()
90-
-> impl Parser<StringView, bool, Output = (), Error = ParserError> + SetContext<bool> {
88+
fn ctx_demand_separator_p() -> impl Parser<StringView, bool, Output = (), Error = ParserError> {
9189
// TODO consolidate the two separate separator functions, they are almost never used elsewhere
9290
IifParser::new(
9391
// last statement was comment

rusty_parser/src/core/var_name.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::core::name::{bare_name_without_dots, name_p};
55
use crate::input::StringView;
66
use crate::pc_specific::*;
77
use crate::tokens::whitespace_ignoring;
8-
use crate::{ParserError, *};
8+
use crate::{
9+
ArrayDimensions, AsBareName, BareName, BareNamePos, DimType, ExpressionType, HasExpressionType, Keyword, Name, ParamType, ParserError, ToBareName, TypeQualifier
10+
};
911

1012
/// A variable name with a type.
1113
///
@@ -118,9 +120,7 @@ where
118120
A: Fn() -> AP,
119121
AP: Parser<StringView, Error = ParserError> + 'static,
120122
B: Fn() -> BP + 'static,
121-
BP: Parser<StringView, VarNameCtx, Output = T, Error = ParserError>
122-
+ SetContext<VarNameCtx>
123-
+ 'static,
123+
BP: Parser<StringView, VarNameCtx, Output = T, Error = ParserError> + 'static,
124124
{
125125
name_with_opt_array(opt_array_parser_factory())
126126
.then_with_in_context(
@@ -133,18 +133,15 @@ where
133133

134134
fn var_type_parser<T, BP>(
135135
extended_type_parser: BP,
136-
) -> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError> + SetContext<VarNameCtx>
136+
) -> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError>
137137
where
138138
T: Default + VarType,
139-
BP: Parser<StringView, VarNameCtx, Output = T, Error = ParserError>
140-
+ SetContext<VarNameCtx>
141-
+ 'static,
139+
BP: Parser<StringView, VarNameCtx, Output = T, Error = ParserError> + 'static,
142140
{
143141
qualified().or(extended(extended_type_parser)).or(bare())
144142
}
145143

146-
fn qualified<T>()
147-
-> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError> + SetContext<VarNameCtx>
144+
fn qualified<T>() -> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError>
148145
where
149146
T: Default + VarType,
150147
{
@@ -174,19 +171,18 @@ where
174171

175172
fn extended<T, BP>(
176173
extended_type_parser: BP,
177-
) -> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError> + SetContext<VarNameCtx>
174+
) -> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError>
178175
where
179176
T: Default + VarType,
180-
BP: Parser<StringView, VarNameCtx, Output = T, Error = ParserError> + SetContext<VarNameCtx>,
177+
BP: Parser<StringView, VarNameCtx, Output = T, Error = ParserError>,
181178
{
182179
let extended_type_parser = extended_type_parser.to_fatal();
183180
as_clause()
184181
.no_context()
185182
.and_keep_right(extended_type_parser)
186183
}
187184

188-
fn bare<T>()
189-
-> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError> + SetContext<VarNameCtx>
185+
fn bare<T>() -> impl Parser<StringView, VarNameCtx, Output = T, Error = ParserError>
190186
where
191187
T: Default + VarType,
192188
{

rusty_parser/src/pc_specific/keyword.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,7 @@ where
107107
Err(err) => Err(err),
108108
}
109109
}
110-
}
111110

112-
impl<C, P> SetContext<C> for KeywordParser<P>
113-
where
114-
P: SetContext<C>,
115-
{
116111
fn set_context(&mut self, ctx: C) {
117112
self.parser.set_context(ctx);
118113
}

rusty_parser/src/pc_specific/logging.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[cfg(debug_assertions)]
2-
use rusty_pc::{Parser, SetContext};
2+
use rusty_pc::Parser;
33

44
use crate::error::ParserError;
55
use crate::input::StringView;
@@ -89,13 +89,8 @@ where
8989
}
9090
}
9191
}
92-
}
9392

94-
impl<P> SetContext<()> for LoggingParser<P>
95-
where
96-
P: SetContext<()>,
97-
{
98-
fn set_context(&mut self, _ctx: ()) {
99-
self.parser.set_context(_ctx);
93+
fn set_context(&mut self, ctx: ()) {
94+
self.parser.set_context(ctx);
10095
}
10196
}

rusty_parser/src/pc_specific/with_pos.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rusty_common::{AtPos, HasPos, Positioned};
2-
use rusty_pc::{InputTrait, Parser, SetContext};
2+
use rusty_pc::{InputTrait, Parser};
33

44
pub trait WithPos<I, C>: Parser<I, C>
55
where
@@ -38,12 +38,7 @@ where
3838
let pos = tokenizer.pos();
3939
self.parser.parse(tokenizer).map(|x| x.at_pos(pos))
4040
}
41-
}
4241

43-
impl<C, P> SetContext<C> for WithPosMapper<P>
44-
where
45-
P: SetContext<C>,
46-
{
4742
fn set_context(&mut self, ctx: C) {
4843
self.parser.set_context(ctx)
4944
}

0 commit comments

Comments
 (0)