Skip to content

Commit

Permalink
Eliminate itertools
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Aug 6, 2024
1 parent 873d6de commit 966cd06
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 78 deletions.
17 changes: 0 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ clap = { version = "4.5.8", default-features = false, features = ["help", "usage
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
bincode = "1.3"
itertools = "0.13"
ariadne = "0.4"
by_address = "1.2"
rpds = "1.1"
Expand Down
1 change: 0 additions & 1 deletion prism-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ clap.workspace = true
prism-parser.workspace = true
rpds.workspace = true
ariadne.workspace = true
itertools.workspace = true

[dev-dependencies]
test_each_file.workspace = true
2 changes: 1 addition & 1 deletion prism-compiler/src/lang/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::lang::UnionIndex;
use crate::lang::{TcEnv, ValueOrigin};
use ariadne::{Color, Label, Report, ReportKind, Source};
use itertools::Itertools;
use prism_parser::core::span::Span;
use std::io;

Expand Down Expand Up @@ -144,6 +143,7 @@ impl TcEnv {
.queued_beq_free
.iter()
.flat_map(|(i, cs)| cs.iter().map(move |c| format!("{:?} = {:?}", i, c.1 .0)))
.collect::<Vec<_>>()
.join(",");

report.with_message("Constraint creates an infinite type")
Expand Down
2 changes: 1 addition & 1 deletion prism-compiler/src/lang/from_action_result.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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::grammar::action_result::ActionResult;
use prism_parser::parser::var_map::{VarMap, VarMapValue};
use rpds::RedBlackTreeMap;
use std::borrow::Cow;

Expand Down
2 changes: 1 addition & 1 deletion prism-compiler/tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ fn parse_complex() {
let mut env = TcEnv::default();
parse_prism_in_env(input_str, &mut env).unwrap_or_eprint();
}
}
}
2 changes: 1 addition & 1 deletion prism-parser-bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +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::action_result::ActionResult;
use prism_parser::grammar::from_action_result::{parse_grammarfile, parse_rule_action};
use prism_parser::grammar::GrammarFile;
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
1 change: 0 additions & 1 deletion prism-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ authors.workspace = true
license.workspace = true

[dependencies]
itertools.workspace = true
serde.workspace = true
bincode.workspace = true
ariadne.workspace = true
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 @@ -8,8 +8,8 @@ use crate::core::state::PState;
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::grammar::action_result::ActionResult;
use crate::parser::var_map::BlockCtx;
use bumpalo::Bump;
use bumpalo_try::BumpaloExtend;

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::grammar::action_result::ActionResult;
use crate::parser::var_map::VarMap;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};

Expand Down
22 changes: 12 additions & 10 deletions prism-parser/src/error/set_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::core::span::Span;
use crate::error::error_printer::{base_report, ErrorLabel};
use crate::error::ParseError;
use ariadne::{Label, Report};
use itertools::Itertools;
use std::cmp::max;
use std::collections::HashSet;
use std::collections::{BTreeMap, HashSet};

/// Set error keeps track of the set of labels at the furthest position.
#[derive(Clone)]
Expand Down Expand Up @@ -60,19 +59,22 @@ impl<'grm> ParseError for SetError<'grm> {
fn report(&self, enable_debug: bool) -> Report<'static, Span> {
let mut report = base_report(self.span);

let mut labels_map: BTreeMap<Pos, Vec<_>> = BTreeMap::new();
for l in self.labels.iter().filter(|l| enable_debug || !l.is_debug()) {
labels_map.entry(l.span().start).or_default().push(l);
}

//Add labels
for (start, labels) in self
.labels
.iter()
.filter(|l| enable_debug || !l.is_debug())
.into_group_map_by(|l| l.span().start)
.into_iter()
{
for (start, labels) in labels_map {
report = report.with_label(
Label::new(start.span_to(start))
.with_message(format!(
"Tried parsing {}",
labels.into_iter().format(" / ")
labels
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" / ")
))
.with_order(-(<Pos as Into<usize>>::into(start) as i32)),
);
Expand Down
14 changes: 10 additions & 4 deletions prism-parser/src/error/tree_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::core::span::Span;
use crate::error::error_printer::{base_report, ErrorLabel};
use crate::error::ParseError;
use ariadne::{Label, Report, ReportBuilder};
use itertools::Itertools;
use std::cmp::max;
use std::hash::Hash;
use std::mem;
Expand All @@ -30,7 +29,11 @@ impl<L: Eq + Hash + Clone> ErrorTree<L> {
}

pub fn into_paths(&self) -> Vec<Vec<&L>> {
let mut subs = self.1.iter().map(|t| t.into_paths()).concat();
let mut subs = self
.1
.iter()
.flat_map(|t| t.into_paths())
.collect::<Vec<_>>();
if let Some(l) = &self.0 {
if subs.is_empty() {
subs.push(vec![l]);
Expand Down Expand Up @@ -99,15 +102,18 @@ impl<'grm> ParseError for TreeError<'grm> {
let path = path
.iter()
.filter(|l| enable_debug || !l.is_debug())
.collect_vec();
.collect::<Vec<_>>();
if path.is_empty() {
continue;
}
let label = &path[0];

report = report.with_label(
Label::new(label.span())
.with_message(format!("{}", path.iter().format(" <- ")))
.with_message(path.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" <- ").to_string())
.with_order(-(<Pos as Into<usize>>::into(label.span().start) as i32)),
);
}
Expand Down
11 changes: 8 additions & 3 deletions prism-parser/src/grammar/action_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::core::span::Span;
use crate::grammar::escaped_string::EscapedString;
use crate::grammar::serde_leak::*;
use crate::parser::var_map::VarMap;
use itertools::Itertools;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -37,13 +36,19 @@ impl<'arn, 'grm> ActionResult<'arn, 'grm> {
ActionResult::Construct(_, "Cons" | "Nil", _) => {
format!(
"[{}]",
self.iter_list().map(|e| e.to_string(src)).format(", ")
self.iter_list()
.map(|e| e.to_string(src))
.collect::<Vec<String>>()
.join(", ")
)
}
ActionResult::Construct(_, c, es) => format!(
"{}({})",
c,
es.iter().map(|e| e.to_string(src)).format(", ")
es.iter()
.map(|e| e.to_string(src))
.collect::<Vec<String>>()
.join(", ")
),
ActionResult::Guid(r) => format!("Guid({r})"),
ActionResult::RuleId(rule) => format!("Rule({rule})"),
Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/grammar/apply_action.rs
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::grammar::action_result::ActionResult;
use crate::grammar::rule_action::RuleAction;
use crate::parser::var_map::{VarMap, VarMapValue};

pub fn apply_action<'arn, 'grm>(
rule: &RuleAction<'arn, 'grm>,
Expand Down
38 changes: 22 additions & 16 deletions prism-parser/src/grammar/escaped_string.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use std::str::{Chars, FromStr};

#[derive(Debug, Copy, Clone, Hash, Serialize, Deserialize, Eq, PartialEq)]
pub struct EscapedString<'grm>(&'grm str);
Expand All @@ -21,20 +20,7 @@ impl<'grm> EscapedString<'grm> {
}

pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
self.0.chars().batching(|it| {
let c = it.next()?;
if c != '\\' {
return Some(c);
}
Some(match it.next()? {
'n' => '\n',
'r' => '\r',
'\\' => '\\',
'"' => '"',
'\'' => '\'',
_ => panic!("Invalid escape sequence"),
})
})
EscapedStringIter(self.0.chars())
}

pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
Expand All @@ -47,3 +33,23 @@ impl Display for EscapedString<'_> {
write!(f, "{}", self.0)
}
}

struct EscapedStringIter<'grm>(Chars<'grm>);

impl<'grm> Iterator for EscapedStringIter<'grm> {
type Item = char;

fn next(&mut self) -> Option<Self::Item> {
Some(match self.0.next()? {
'\\' => match self.0.next()? {
'n' => '\n',
'r' => '\r',
'\\' => '\\',
'"' => '"',
'\'' => '\'',
_ => panic!("Invalid escape sequence"),
},
c => c,
})
}
}
6 changes: 3 additions & 3 deletions prism-parser/src/grammar/from_action_result.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::core::cache::Allocs;
use crate::grammar::action_result::ActionResult;
use crate::grammar::action_result::ActionResult::*;
use crate::grammar::escaped_string::EscapedString;
use crate::grammar::rule_action::RuleAction;
use crate::grammar::{AnnotatedRuleExpr, Block, GrammarFile, Rule, RuleExpr};
use crate::grammar::{CharClass, RuleAnnotation};
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
8 changes: 4 additions & 4 deletions prism-parser/src/grammar/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::grammar::escaped_string::EscapedString;
use crate::grammar::rule_action::RuleAction;
use crate::grammar::serde_leak::*;
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 escaped_string;
pub mod from_action_result;
pub mod rule_action;
pub mod serde_leak;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct GrammarFile<'arn, 'grm> {
Expand Down
2 changes: 1 addition & 1 deletion prism-parser/src/grammar/rule_action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::grammar::action_result::ActionResult;
use crate::grammar::escaped_string::EscapedString;
use crate::grammar::serde_leak::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum RuleAction<'arn, 'grm> {
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 @@ -7,11 +7,11 @@ use crate::core::state::{PState, 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_layout::full_input_layout;
use crate::parser::parser_rule;
use crate::parser::var_map::{VarMap, VarMapValue};
use crate::grammar::action_result::ActionResult;
use crate::META_GRAMMAR_STATE;
use bumpalo::Bump;

Expand Down
6 changes: 3 additions & 3 deletions prism-parser/src/parser/parser_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use crate::core::pos::Pos;
use crate::core::state::PState;
use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
use crate::grammar::action_result::ActionResult;
use crate::parser::parser_rule_body::parser_body_cache_recurse;
use crate::parser::var_map::{VarMap, VarMapValue};
use crate::grammar::action_result::ActionResult;
use itertools::Itertools;

pub fn parser_rule<'a, 'arn: 'a, 'grm: 'arn, E: ParseError<L = ErrorLabel<'grm>> + 'grm>(
rules: &'arn GrammarState<'arn, 'grm>,
Expand All @@ -20,8 +19,9 @@ pub fn parser_rule<'a, 'arn: 'a, 'grm: 'arn, E: ParseError<L = ErrorLabel<'grm>>
.get(rule)
.unwrap_or_else(|| panic!("Rule not found: {rule}"));

assert_eq!(rule_state.args.len(), args.len());
let rule_args = VarMap::from_iter(
rule_state.args.iter().cloned().zip_eq(args.iter().cloned()),
rule_state.args.iter().cloned().zip(args.iter().cloned()),
state.alloc,
);

Expand Down
Loading

0 comments on commit 966cd06

Please sign in to comment.