Skip to content

Commit 4166643

Browse files
committed
refactor(pc): Pass context by reference
Propagate context by reference in `set_context`. This removes the need for cloning e.g. in `AndParser`. Added `MapCtxParser` to transform the context into a different type. Removed the projection function from `ThenWithParser` (migrated to using `MapCtxParser`).
1 parent 62f62ae commit 4166643

38 files changed

Lines changed: 140 additions & 89 deletions

rusty_parser/src/core/for_loop.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ fn parse_for_step_p() -> impl Parser<
4343
Error = ParserError,
4444
> {
4545
parse_for_p().then_with_in_context(
46-
opt_step_p(),
47-
|(_, _, upper)| upper.is_parenthesis(),
46+
opt_step_p().map_ctx(|(_, _, upper): &(_, _, ExpressionPos)| upper.is_parenthesis()),
4847
|(n, l, u), opt_step| (n, l, u, opt_step),
4948
)
5049
}

rusty_parser/src/core/sub_call.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub fn sub_call_or_assignment_p() -> impl Parser<StringView, Output = Statement,
4747
}
4848
})
4949
.flatten(),
50-
|x| x.clone(),
5150
KeepRightCombiner,
5251
)
5352
}

rusty_parser/src/core/var_name.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ where
123123
{
124124
name_with_opt_array(opt_array_parser_factory())
125125
.then_with_in_context(
126-
var_type_parser(extended_type_parser_factory()),
127-
|(name, _array)| (name.qualifier(), !name.as_bare_name().contains('.')),
126+
var_type_parser(extended_type_parser_factory()).map_ctx(
127+
|(name, _array): &(Name, _)| (name.qualifier(), !name.as_bare_name().contains('.')),
128+
),
128129
TupleCombiner,
129130
)
130131
.map(|((name, array), var_type)| create_typed_name(name, array, var_type))

rusty_parser/src/expr/binary_expression.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use crate::{ExpressionPos, ExpressionPosTrait, ExpressionTrait, Keyword, Operato
1313
pub(super) fn parser() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
1414
non_bin_expr()
1515
.then_with_in_context(
16-
second_parser(),
17-
|first| first.is_parenthesis(),
16+
second_parser().map_ctx(ExpressionTrait::is_parenthesis),
1817
TupleCombiner,
1918
)
2019
.map(|(l, r)| match r {
@@ -31,7 +30,10 @@ fn second_parser() -> impl Parser<
3130
Error = ParserError,
3231
> {
3332
operator()
34-
.then_with_in_context(expr_after_binary_operator(), is_keyword_op, TupleCombiner)
33+
.then_with_in_context(
34+
expr_after_binary_operator().map_ctx(is_keyword_op),
35+
TupleCombiner,
36+
)
3537
.to_option()
3638
}
3739

rusty_parser/src/expr/parsers.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ fn followed_by_ws(
7070
parser: impl Parser<StringView, Output = ExpressionPos, Error = ParserError>,
7171
) -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
7272
parser.then_with_in_context(
73-
conditionally_opt_whitespace(),
74-
|e| e.is_parenthesis(),
73+
conditionally_opt_whitespace().map_ctx(ExpressionTrait::is_parenthesis),
7574
KeepLeftCombiner,
7675
)
7776
}
@@ -124,8 +123,8 @@ pub fn expr_ws_followed_by(
124123
expr_parser.then_with_in_context(
125124
conditionally_opt_whitespace()
126125
.to_fatal()
127-
.and_keep_right(other_parser.to_fatal().no_context()),
128-
|e| e.is_parenthesis(),
126+
.and_keep_right(other_parser.to_fatal().no_context())
127+
.map_ctx(ExpressionTrait::is_parenthesis),
129128
KeepLeftCombiner,
130129
)
131130
}
@@ -138,8 +137,7 @@ pub fn expr_keyword_opt_expr(
138137
keyword: Keyword,
139138
) -> impl Parser<StringView, Output = (ExpressionPos, Option<ExpressionPos>), Error = ParserError> {
140139
expression_pos_p().then_with_in_context(
141-
opt_keyword_expr(keyword),
142-
ExpressionTrait::is_parenthesis,
140+
opt_keyword_expr(keyword).map_ctx(ExpressionTrait::is_parenthesis),
143141
TupleCombiner,
144142
)
145143
}

rusty_parser/src/expr/property.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::{BareName, Expression, ExpressionPos, ExpressionType, Name, NameAsTok
1717
pub fn parser() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
1818
base_expr_pos_p()
1919
.then_with_in_context(
20-
ctx_dot_property(),
21-
|first_expr_pos| is_qualified(&first_expr_pos.element),
20+
ctx_dot_property()
21+
.map_ctx(|first_expr_pos: &ExpressionPos| is_qualified(&first_expr_pos.element)),
2222
TupleCombiner,
2323
)
2424
.and_then(|(first_expr_pos, properties)| {

rusty_parser/src/pc_specific/keyword.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ where
108108
}
109109
}
110110

111-
fn set_context(&mut self, ctx: C) {
111+
fn set_context(&mut self, ctx: &C) {
112112
self.parser.set_context(ctx);
113113
}
114114
}

rusty_parser/src/pc_specific/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ where
9090
}
9191
}
9292

93-
fn set_context(&mut self, ctx: ()) {
93+
fn set_context(&mut self, ctx: &()) {
9494
self.parser.set_context(ctx);
9595
}
9696
}

rusty_parser/src/pc_specific/with_pos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ where
3939
self.parser.parse(tokenizer).map(|x| x.at_pos(pos))
4040
}
4141

42-
fn set_context(&mut self, ctx: C) {
42+
fn set_context(&mut self, ctx: &C) {
4343
self.parser.set_context(ctx)
4444
}
4545
}

rusty_parser/src/tokens/any_token_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ where
196196
self.parse_token(input)
197197
}
198198

199-
fn set_context(&mut self, ctx: C) {
199+
fn set_context(&mut self, ctx: &C) {
200200
self.parser.set_context(ctx)
201201
}
202202
}

0 commit comments

Comments
 (0)