From de67c88651e97bfeb281c187b056fdc9f84fd297 Mon Sep 17 00:00:00 2001 From: jonathan Date: Sun, 24 Mar 2024 18:23:02 +0100 Subject: [PATCH] Cleanup parser_cache_recurse --- prism-compiler/src/coc/env.rs | 2 +- prism-compiler/src/lib.rs | 4 +-- prism-parser/src/core/cache.rs | 39 ++++++++++----------- prism-parser/src/parser/parser_rule_body.rs | 9 ++--- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/prism-compiler/src/coc/env.rs b/prism-compiler/src/coc/env.rs index 5f0bebc0..d55b58bb 100644 --- a/prism-compiler/src/coc/env.rs +++ b/prism-compiler/src/coc/env.rs @@ -40,7 +40,7 @@ impl Env { pub fn len(&self) -> usize { self.0.len() } - + pub fn is_empty(&self) -> bool { self.0.is_empty() } diff --git a/prism-compiler/src/lib.rs b/prism-compiler/src/lib.rs index a6bcf1a5..9b1c205c 100644 --- a/prism-compiler/src/lib.rs +++ b/prism-compiler/src/lib.rs @@ -1,9 +1,9 @@ -use crate::coc::{TcEnv}; +use crate::coc::TcEnv; use lazy_static::lazy_static; use prism_parser::error::error_printer::print_set_error; use prism_parser::grammar::GrammarFile; use prism_parser::parse_grammar; -use prism_parser::parser::parser_instance::{run_parser_rule}; +use prism_parser::parser::parser_instance::run_parser_rule; use prism_parser::rule_action::RuleAction; pub mod coc; diff --git a/prism-parser/src/core/cache.rs b/prism-parser/src/core/cache.rs index da648d47..ac7ddd63 100644 --- a/prism-parser/src/core/cache.rs +++ b/prism-parser/src/core/cache.rs @@ -115,23 +115,20 @@ impl<'grm, 'arn, E: ParseError> ParserCache<'grm, 'arn, E> { pub fn parser_cache_recurse<'a, 'arn: 'a, 'grm: 'arn, E: ParseError>>( sub: &'a impl Parser<'arn, 'grm, &'arn ActionResult<'arn, 'grm>, E>, - id: ( - ByAddress<&'arn [BlockState<'arn, 'grm>]>, - ParserContext, - GrammarStateId, - Vec<(&'grm str, RuleId)>, - ), + block: ByAddress<&'arn [BlockState<'arn, 'grm>]>, + state: GrammarStateId, + params: Vec<(&'grm str, RuleId)>, ) -> impl Parser<'arn, 'grm, &'arn ActionResult<'arn, 'grm>, E> + 'a { - move |pos_start: Pos, state: &mut PCache<'arn, 'grm, E>, context: &ParserContext| { + move |pos_start: Pos, cache: &mut PCache<'arn, 'grm, E>, context: &ParserContext| { //Check if this result is cached let key = CacheKey { pos: pos_start, - block: id.0, - ctx: id.1.clone(), - state: id.2, - params: id.3.clone(), + block, + ctx: context.clone(), + state, + params: params.clone(), }; - if let Some(cached) = state.cache_get(&key) { + if let Some(cached) = cache.cache_get(&key) { return cached.clone(); } @@ -140,8 +137,8 @@ pub fn parser_cache_recurse<'a, 'arn: 'a, 'grm: 'arn, E: ParseError { //Did our rule left-recurse? (Safety: We just inserted it) - if !state.cache_is_read(key.clone()).unwrap() { + if !cache.cache_is_read(key.clone()).unwrap() { //No leftrec, cache and return let res = POk(o, spos, epos, empty, be); - state.cache_insert(key, res.clone()); + cache.cache_insert(key, res.clone()); res } else { //There was leftrec, we need to grow the seed loop { //Insert the current seed into the cache - state.cache_state_revert(cache_state); - state.cache_insert(key.clone(), POk(o, spos, epos, empty, be.clone())); + cache.cache_state_revert(cache_state); + cache.cache_insert(key.clone(), POk(o, spos, epos, empty, be.clone())); //Grow the seed - let new_res = sub.parse(pos_start, state, context); + let new_res = sub.parse(pos_start, cache, context); match new_res { POk(new_o, new_spos, new_epos, new_empty, new_be) if new_epos.cmp(&epos).is_gt() => @@ -195,7 +192,7 @@ pub fn parser_cache_recurse<'a, 'arn: 'a, 'grm: 'arn, E: ParseError { - state.cache_insert(key, res.clone()); + cache.cache_insert(key, res.clone()); res } } diff --git a/prism-parser/src/parser/parser_rule_body.rs b/prism-parser/src/parser/parser_rule_body.rs index e58d0f02..0b5d5f28 100644 --- a/prism-parser/src/parser/parser_rule_body.rs +++ b/prism-parser/src/parser/parser_rule_body.rs @@ -32,12 +32,9 @@ pub fn parser_body_cache_recurse< move |stream: Pos, cache: &mut PCache<'arn, 'grm, E>, context: &ParserContext| { parser_cache_recurse( &parser_body_sub_blocks(rules, bs, rule_args), - ( - ByAddress(bs), - context.clone(), - rules.unique_id(), - rule_args.to_vec(), - ), + ByAddress(bs), + rules.unique_id(), + rule_args.to_vec(), ) .parse(stream, cache, context) }