Skip to content

Commit

Permalink
Fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Oct 2, 2024
1 parent 7caf5c2 commit ae3de6b
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 73 deletions.
15 changes: 7 additions & 8 deletions prism-parser/src/core/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::adaptive::GrammarStateId;
use crate::core::context::ParserContext;
use crate::core::parser::Parser;
use crate::core::pos::Pos;
use crate::core::presult::PResult;
use crate::core::presult::PResult::{PErr, POk};
Expand Down Expand Up @@ -143,13 +142,13 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
let new_res = sub(self, pos_start);
match new_res {
POk(new_o, new_spos, new_epos, new_be)
if new_epos.cmp(&epos).is_gt() =>
{
o = new_o;
spos = new_spos;
epos = new_epos;
be = new_be;
}
if new_epos.cmp(&epos).is_gt() =>
{
o = new_o;
spos = new_spos;
epos = new_epos;
be = new_be;
}
POk(_, _, _, new_be) => {
be = err_combine_opt(be, new_be);
break;
Expand Down
3 changes: 0 additions & 3 deletions prism-parser/src/core/presult.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::core::context::ParserContext;
use crate::core::parser::Parser;
use crate::core::pos::Pos;
use crate::core::presult::PResult::{PErr, POk};
use crate::core::span::Span;
use crate::core::state::ParserState;
use crate::error::{err_combine, err_combine_opt, ParseError};

#[derive(Clone)]
Expand Down
10 changes: 7 additions & 3 deletions prism-parser/src/parser/parser_instance.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::core::adaptive::{AdaptError, GrammarState};
use crate::core::cache::Allocs;
use crate::core::context::ParserContext;
use crate::core::parser::Parser;
use crate::core::pos::Pos;
use crate::core::state::ParserState;
use crate::error::aggregate_error::AggregatedParseError;
use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
use crate::grammar::action_result::ActionResult;
use crate::grammar::GrammarFile;
use crate::parser::parser_rule;
use crate::parser::var_map::VarMap;
use crate::META_GRAMMAR;
use bumpalo::Bump;
Expand Down Expand Up @@ -70,7 +68,13 @@ impl<'arn, 'grm: 'arn, E: ParseError<L = ErrorLabel<'grm>>> ParserInstance<'arn,
.expect("Rule exists")
.as_rule_id()
.expect("Rule is a rule");
let result = self.state.parse_rule(&self.grammar_state, rule, &[], Pos::start(), ParserContext::new());
let result = self.state.parse_rule(
&self.grammar_state,
rule,
&[],
Pos::start(),
ParserContext::new(),
);
let end_pos = result.end_pos();
let result = result
.merge_seq(self.state.parse_end_with_layout(
Expand Down
15 changes: 10 additions & 5 deletions prism-parser/src/parser/parser_layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::adaptive::GrammarState;
use crate::core::context::ParserContext;
use crate::core::parser::Parser;
use crate::core::pos::Pos;
use crate::core::presult::PResult;
use crate::core::presult::PResult::{PErr, POk};
Expand Down Expand Up @@ -36,10 +35,16 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
let pos_before_layout = new_res.end_pos();
// Add in optional error information from sub_res, then require another layout token
let new_res = res.merge_seq_opt(new_res).merge_seq_chain(|pos| {
self.parse_rule(rules, layout, &[], pos, ParserContext {
layout_disabled: true,
..context
})
self.parse_rule(
rules,
layout,
&[],
pos,
ParserContext {
layout_disabled: true,
..context
},
)
});
match new_res {
// We have parsed more layout, we can try again
Expand Down
7 changes: 3 additions & 4 deletions prism-parser/src/parser/parser_rule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::adaptive::{GrammarState, RuleId, RuleState};
use crate::core::context::{ParserContext, PR};
use crate::core::parser::Parser;
use crate::core::context::ParserContext;
use crate::core::pos::Pos;
use crate::core::presult::PResult;
use crate::core::state::ParserState;
Expand All @@ -16,7 +15,7 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
rule: RuleId,
args: &[VarMapValue<'arn, 'grm>],
pos: Pos,
context: ParserContext
context: ParserContext,
) -> PResult<&'arn ActionResult<'arn, 'grm>, E> {
let rule_state: &'arn RuleState<'arn, 'grm> = rules
.get(rule)
Expand All @@ -35,4 +34,4 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
));
res.map(|pr| pr)
}
}
}
127 changes: 82 additions & 45 deletions prism-parser/src/parser/parser_rule_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::core::parser::Parser;
use crate::core::presult::PResult;
use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
Expand All @@ -20,13 +19,13 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
pos: Pos,
context: ParserContext,
) -> PResult<&'arn ActionResult<'arn, 'grm>, E> {
self.parse_cache_recurse(
|state, pos| state.parse_sub_blocks(rules, block_ctx, pos, context),
block_ctx,
rules.unique_id(),
pos,
context
)
self.parse_cache_recurse(
|state, pos| state.parse_sub_blocks(rules, block_ctx, pos, context),
block_ctx,
rules.unique_id(),
pos,
context,
)
}

fn parse_sub_blocks(
Expand All @@ -35,21 +34,32 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
(block_state, rule_args): BlockCtx<'arn, 'grm>,
pos: Pos,
context: ParserContext,
)-> PResult<&'arn ActionResult<'arn, 'grm>, E>{
match block_state {
[] => unreachable!(),
[b] => self.parse_sub_constructors(rules, (block_state, rule_args), b.constructors, pos, context),
[b, brest @ ..] => {
// Parse current
let res =
self.parse_sub_constructors(rules, (block_state, rule_args), b.constructors, pos, context);
) -> PResult<&'arn ActionResult<'arn, 'grm>, E> {
match block_state {
[] => unreachable!(),
[b] => self.parse_sub_constructors(
rules,
(block_state, rule_args),
b.constructors,
pos,
context,
),
[b, brest @ ..] => {
// Parse current
let res = self.parse_sub_constructors(
rules,
(block_state, rule_args),
b.constructors,
pos,
context,
);

// Parse next with recursion check
res.merge_choice_chain(|| {
self.parse_rule_block(rules, (brest, rule_args), pos, context)
})
}
// Parse next with recursion check
res.merge_choice_chain(|| {
self.parse_rule_block(rules, (brest, rule_args), pos, context)
})
}
}
}

fn parse_sub_constructors(
Expand All @@ -59,7 +69,7 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
es: &'arn [Constructor<'arn, 'grm>],
pos: Pos,
context: ParserContext,
)-> PResult<&'arn ActionResult<'arn, 'grm>, E>{
) -> PResult<&'arn ActionResult<'arn, 'grm>, E> {
match es {
[] => PResult::new_err(E::new(pos.span_to(pos)), pos),
[(crate::grammar::AnnotatedRuleExpr(annots, expr), rule_ctx), rest @ ..] => {
Expand All @@ -68,11 +78,25 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
let vars: VarMap<'arn, 'grm> =
VarMap::from_iter(rule_args_iter.chain(rule_ctx), self.alloc);

let res =
self.parse_sub_annotations(rules, (block_state, rule_args), annots, expr, vars, pos, context)
.merge_choice_chain(|| {
self.parse_sub_constructors(rules, (block_state, rule_args), rest, pos, context)
});
let res = self
.parse_sub_annotations(
rules,
(block_state, rule_args),
annots,
expr,
vars,
pos,
context,
)
.merge_choice_chain(|| {
self.parse_sub_constructors(
rules,
(block_state, rule_args),
rest,
pos,
context,
)
});
res
}
}
Expand All @@ -86,11 +110,12 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
expr: &'arn RuleExpr<'arn, 'grm>,
vars: VarMap<'arn, 'grm>,
pos: Pos,
context: ParserContext
)-> PResult<&'arn ActionResult<'arn, 'grm>, E>{
context: ParserContext,
) -> PResult<&'arn ActionResult<'arn, 'grm>, E> {
match annots {
[RuleAnnotation::Error(err_label), rest @ ..] => {
let mut res = self.parse_sub_annotations(rules, block_state, rest, expr, vars, pos, context);
let mut res =
self.parse_sub_annotations(rules, block_state, rest, expr, vars, pos, context);
res.add_label_explicit(ErrorLabel::Explicit(
pos.span_to(res.end_pos().next(self.input).0),
*err_label,
Expand All @@ -101,7 +126,12 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
rules,
vars,
|state, pos| {
state.parse_sub_annotations(rules, block_state, rest, expr, vars,
state.parse_sub_annotations(
rules,
block_state,
rest,
expr,
vars,
pos,
ParserContext {
layout_disabled: true,
Expand All @@ -112,27 +142,34 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
pos,
context,
),
[RuleAnnotation::EnableLayout, rest @ ..] => {
self.parse_sub_annotations(rules, block_state, rest, expr, vars,
pos,
ParserContext {
layout_disabled: false,
..context
},
)
}
[RuleAnnotation::DisableRecovery | RuleAnnotation::EnableRecovery, rest @ ..] => {
self.parse_sub_annotations(rules, block_state, rest, expr, vars,
[RuleAnnotation::EnableLayout, rest @ ..] => self.parse_sub_annotations(
rules,
block_state,
rest,
expr,
vars,
pos,
ParserContext {
layout_disabled: false,
..context
},
),
[RuleAnnotation::DisableRecovery | RuleAnnotation::EnableRecovery, rest @ ..] => self
.parse_sub_annotations(
rules,
block_state,
rest,
expr,
vars,
pos,
ParserContext {
recovery_disabled: true,
..context
},
)
}
),
&[] => self
.parse_expr(rules, block_state, expr, vars, pos, context)
.map(|pr| pr.rtrn),
}
}
}
}
10 changes: 6 additions & 4 deletions prism-parser/src/parser/parser_rule_expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::adaptive::GrammarState;
use crate::core::context::{ParserContext, PR};
use crate::core::parser::Parser;
use crate::core::pos::Pos;
use crate::core::presult::PResult;
use crate::core::state::ParserState;
Expand Down Expand Up @@ -243,9 +242,11 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
PR::with_rtrn(self.alloc.alloc(ActionResult::Value(span)))
})
}
RuleExpr::This => self.parse_rule_block(rules, block_ctx, pos, context)
RuleExpr::This => self
.parse_rule_block(rules, block_ctx, pos, context)
.map(PR::with_rtrn),
RuleExpr::Next => self.parse_rule_block(rules, (&block_ctx.0[1..], block_ctx.1), pos, context)
RuleExpr::Next => self
.parse_rule_block(rules, (&block_ctx.0[1..], block_ctx.1), pos, context)
.map(PR::with_rtrn),
RuleExpr::PosLookahead(sub) => self
.parse_expr(rules, block_ctx, sub, vars, pos, context)
Expand Down Expand Up @@ -310,7 +311,8 @@ impl<'arn, 'grm, E: ParseError<L = ErrorLabel<'grm>>> ParserState<'arn, 'grm, E>
.expect("Adaptation rule exists");

// Parse body
let mut res = self.parse_rule(rules, rule, &[], pos, context)
let mut res = self
.parse_rule(rules, rule, &[], pos, context)
.map(PR::with_rtrn);
res.add_label_implicit(ErrorLabel::Debug(pos.span_to(pos), "adaptation"));
res
Expand Down
1 change: 0 additions & 1 deletion prism-parser/src/parser/var_map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::core::adaptive::{BlockState, GrammarState, RuleId};
use crate::core::cache::Allocs;
use crate::core::context::ParserContext;
use crate::core::parser::Parser;
use crate::core::pos::Pos;
use crate::core::state::ParserState;
use crate::error::error_printer::ErrorLabel;
Expand Down

0 comments on commit ae3de6b

Please sign in to comment.