Skip to content

Commit

Permalink
Move RuleAction into grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Aug 6, 2024
1 parent 6002e6e commit 17ce490
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 53 deletions.
2 changes: 1 addition & 1 deletion prism-compiler/src/lang/from_action_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::lang::error::TypeError;
use crate::lang::{PartialExpr, TcEnv, UnionIndex, ValueOrigin};
use prism_parser::core::cache::Allocs;
use prism_parser::parser::var_map::{VarMap, VarMapValue};
use prism_parser::rule_action::action_result::ActionResult;
use prism_parser::grammar::action_result::ActionResult;
use rpds::RedBlackTreeMap;
use std::borrow::Cow;

Expand Down
5 changes: 2 additions & 3 deletions prism-parser-bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use bumpalo::Bump;
use prism_parser::core::cache::Allocs;
use prism_parser::error::aggregate_error::ParseResultExt;
use prism_parser::error::set_error::SetError;
use prism_parser::grammar::from_action_result::parse_grammarfile;
use prism_parser::grammar::from_action_result::{parse_grammarfile, parse_rule_action};
use prism_parser::grammar::GrammarFile;
use prism_parser::rule_action::action_result::ActionResult;
use prism_parser::rule_action::from_action_result::parse_rule_action;
use prism_parser::grammar::action_result::ActionResult;
use prism_parser::{parse_grammar, run_parser_rule_here, META_GRAMMAR};
use std::fs::{read, File};

Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/core/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::error::error_printer::ErrorLabel;
use crate::error::error_printer::ErrorLabel::Debug;
use crate::error::{err_combine_opt, ParseError};
use crate::parser::var_map::BlockCtx;
use crate::rule_action::action_result::ActionResult;
use crate::grammar::action_result::ActionResult;
use bumpalo::Bump;

#[derive(Eq, PartialEq, Hash, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/core/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parser::var_map::VarMap;
use crate::rule_action::action_result::ActionResult;
use crate::grammar::action_result::ActionResult;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};

Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/core/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::presult::PResult::{PErr, POk};
use crate::core::state::PState;
use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
use crate::rule_action::action_result::ActionResult;
use crate::grammar::action_result::ActionResult;

const MAX_RECOVERIES: usize = 2;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::core::cache::Allocs;
use crate::core::span::Span;
use crate::parser::var_map::{VarMap, VarMapValue};
use crate::rule_action::action_result::ActionResult;
use crate::rule_action::RuleAction;
use crate::grammar::action_result::ActionResult;
use crate::grammar::rule_action::RuleAction;

pub fn apply_action<'arn, 'grm>(
rule: &RuleAction<'arn, 'grm>,
Expand Down
24 changes: 21 additions & 3 deletions prism-parser/src/grammar/from_action_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::core::cache::Allocs;
use crate::grammar::escaped_string::EscapedString;
use crate::grammar::{AnnotatedRuleExpr, Block, GrammarFile, Rule, RuleExpr};
use crate::grammar::{CharClass, RuleAnnotation};
use crate::rule_action::action_result::ActionResult;
use crate::rule_action::action_result::ActionResult::*;
use crate::rule_action::RuleAction;
use crate::grammar::action_result::ActionResult;
use crate::grammar::action_result::ActionResult::*;
use std::borrow::Cow;
use crate::grammar::rule_action::RuleAction;

#[macro_export]
macro_rules! result_match {
Expand Down Expand Up @@ -248,3 +248,21 @@ fn parse_u64(r: &ActionResult<'_, '_>, src: &str) -> Option<u64> {
_ => None,
}
}

pub fn parse_rule_action<'arn, 'grm>(
r: &ActionResult<'_, 'grm>,
src: &'grm str,
allocs: Allocs<'arn>,
) -> Option<RuleAction<'arn, 'grm>> {
Some(match r {
Construct(_, "Construct", b) => RuleAction::Construct(
parse_identifier(&b[0], src).unwrap(),
result_match! {
create allocs.try_alloc_extend(b[1].iter_list().map(|sub| parse_rule_action(sub, src, allocs)))?
}?,
),
Construct(_, "InputLiteral", b) => RuleAction::InputLiteral(parse_string(&b[0], src)?),
Construct(_, "Name", b) => RuleAction::Name(parse_identifier(&b[0], src)?),
_ => return None,
})
}
5 changes: 4 additions & 1 deletion prism-parser/src/grammar/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::grammar::escaped_string::EscapedString;
use crate::grammar::serde_leak::*;
use crate::rule_action::RuleAction;
use serde::{Deserialize, Serialize};
use crate::grammar::rule_action::RuleAction;

pub mod escaped_string;
pub mod from_action_result;
pub mod serde_leak;
pub mod action_result;
pub mod apply_action;
pub mod rule_action;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct GrammarFile<'arn, 'grm> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::grammar::action_result::ActionResult;
use crate::grammar::escaped_string::EscapedString;
use crate::grammar::serde_leak::*;
use crate::rule_action::action_result::ActionResult;

pub mod action_result;
pub mod apply_action;
pub mod from_action_result;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum RuleAction<'arn, 'grm> {
Expand Down
3 changes: 1 addition & 2 deletions prism-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ use crate::core::cache::Allocs;
use crate::error::aggregate_error::AggregatedParseError;
use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
use crate::grammar::from_action_result::parse_rule_action;
use crate::grammar::GrammarFile;
use crate::parser::parser_instance::run_parser_rule;
use crate::parser::var_map::VarMap;
use crate::rule_action::from_action_result::parse_rule_action;

pub mod core;
pub mod error;
pub mod grammar;
pub mod parser;
pub mod rule_action;

pub static META_GRAMMAR: LazyLock<GrammarFile<'static, 'static>> = LazyLock::new(|| {
let meta_grammar = include_bytes!("../resources/bootstrap.bincode");
Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/parser/parser_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::grammar::GrammarFile;
use crate::parser::parser_layout::full_input_layout;
use crate::parser::parser_rule;
use crate::parser::var_map::{VarMap, VarMapValue};
use crate::rule_action::action_result::ActionResult;
use crate::grammar::action_result::ActionResult;
use crate::META_GRAMMAR_STATE;
use bumpalo::Bump;

Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/parser/parser_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
use crate::parser::parser_rule_body::parser_body_cache_recurse;
use crate::parser::var_map::{VarMap, VarMapValue};
use crate::rule_action::action_result::ActionResult;
use crate::grammar::action_result::ActionResult;
use itertools::Itertools;

pub fn parser_rule<'a, 'arn: 'a, 'grm: 'arn, E: ParseError<L = ErrorLabel<'grm>> + 'grm>(
Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/parser/parser_rule_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::grammar::{RuleAnnotation, RuleExpr};
use crate::parser::parser_layout::parser_with_layout;
use crate::parser::parser_rule_expr::parser_expr;
use crate::parser::var_map::{BlockCtx, VarMap};
use crate::rule_action::action_result::ActionResult;
use crate::grammar::action_result::ActionResult;

pub fn parser_body_cache_recurse<
'a,
Expand Down
6 changes: 3 additions & 3 deletions prism-parser/src/parser/parser_rule_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use crate::parser::parser_layout::parser_with_layout;
use crate::parser::parser_rule::parser_rule;
use crate::parser::parser_rule_body::parser_body_cache_recurse;
use crate::parser::var_map::{BlockCtx, CapturedExpr, VarMap, VarMapValue};
use crate::rule_action::action_result::ActionResult;
use crate::rule_action::apply_action::apply_action;
use crate::rule_action::RuleAction;
use crate::grammar::action_result::ActionResult;
use crate::grammar::apply_action::apply_action;
use crate::grammar::rule_action::RuleAction;

pub fn parser_expr<'a, 'arn: 'a, 'grm: 'arn, E: ParseError<L = ErrorLabel<'grm>> + 'grm>(
rules: &'arn GrammarState<'arn, 'grm>,
Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/parser/var_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
use crate::grammar::RuleExpr;
use crate::parser::parser_rule_expr::parser_expr;
use crate::rule_action::action_result::ActionResult;
use crate::grammar::action_result::ActionResult;
use std::fmt::{Debug, Formatter};
use std::iter;
use std::ptr::null;
Expand Down
24 changes: 0 additions & 24 deletions prism-parser/src/rule_action/from_action_result.rs

This file was deleted.

2 changes: 1 addition & 1 deletion prism-parser/tests/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ macro_rules! parse_test {
use prism_parser::core::context::ParserContext;
use std::collections::HashMap;
use prism_parser::error::set_error::SetError;
use prism_parser::rule_action::RuleAction;
use prism_parser::grammar::rule_action::RuleAction;
use itertools::Itertools;
use prism_parser::error::aggregate_error::ParseResultExt;
use bumpalo::Bump;
Expand Down

0 comments on commit 17ce490

Please sign in to comment.