Skip to content

Commit

Permalink
fix: make output deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin-Yeung committed Nov 11, 2024
1 parent 3dca608 commit f61a4c7
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,13 @@ mod test {
let mut seeded_rng = rand::rngs::StdRng::seed_from_u64(42);
insta::assert_snapshot!(gen.generate("S", &mut seeded_rng));
}

#[test]
fn test_typed_set_algebra() {
let text = include_str!("../examples/set-algebra-typed.bnfgen");
let grammar = RawGrammar::parse(text).unwrap().to_checked().unwrap();
let gen = Generator { grammar };
let mut seeded_rng = rand::rngs::StdRng::seed_from_u64(42);
insta::assert_snapshot!(gen.generate("Program", &mut seeded_rng));
}
}
10 changes: 7 additions & 3 deletions src/grammar/checked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::grammar::production::WeightedProduction;
use crate::grammar::state::State;
use crate::grammar::symbol::Ty::Untyped;
use crate::grammar::symbol::{NonTerminal, SymbolKind, Ty};
use indexmap::IndexMap;
use rand::prelude::SliceRandom;
use rand::Rng;
use std::collections::HashMap;
use std::rc::Rc;

#[derive(Debug)]
pub struct CheckedGrammar {
pub(crate) rules: HashMap<NonTerminal, WeightedProduction>,
pub(crate) rules: IndexMap<NonTerminal, WeightedProduction>,
}

pub enum ReduceOutput {
Expand Down Expand Up @@ -37,7 +37,11 @@ impl CheckedGrammar {
.filter(|k| k.name == s.name)
.collect::<Vec<_>>();
self.rules
.get(candidates.choose(state.rng()).unwrap())
.get(
*candidates
.choose(state.rng())
.expect("No candidates available"),
)
.unwrap_or_else(|| panic!("Fail to find rule of {:?}", s))
.choose_by_state(state)
}
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::grammar::graph::GrammarGraph;
use crate::grammar::rule::Rule;
use crate::grammar::symbol::SymbolKind;
use crate::lexer;
use crate::span::Span;
use crate::utils::convert_parse_error;
use indexmap::IndexMap;
use petgraph::graph::{DiGraph, NodeIndex};
use std::collections::{HashMap, HashSet};

Expand All @@ -26,7 +26,7 @@ impl RawGrammar {
pub fn to_checked(self) -> crate::error::Result<CheckedGrammar> {
self.check_undefined()?.check_duplicate()?.check_repeats()?;

let mut rules = HashMap::new();
let mut rules = IndexMap::new();
for rule in self.rules {
rules.insert(rule.lhs, rule.production);
}
Expand Down
1 change: 1 addition & 0 deletions src/grammar/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::regex::Regex;
use crate::span::Span;
use std::borrow::Borrow;

Check warning on line 3 in src/grammar/symbol.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `std::borrow::Borrow`

warning: unused import: `std::borrow::Borrow` --> src/grammar/symbol.rs:3:5 | 3 | use std::borrow::Borrow; | ^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
use std::hash::Hash;
use std::rc::Rc;

Expand Down
16 changes: 8 additions & 8 deletions src/snapshots/bnfgen__generator__test__typed_generator.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
source: src/generator.rs
expression: "gen.generate(\"S\", &mut seeded_rng)"
---
true & false & true & false & false
1 + 3 + 1 + 1 + 3
3
true
true
true
1 + 3 + 2 + 2 + 3
true & false & true & true & false
false
1
2
1
true
3
true
false
2
false
2
2
106 changes: 106 additions & 0 deletions src/snapshots/bnfgen__generator__test__typed_set_algebra.snap

Large diffs are not rendered by default.

0 comments on commit f61a4c7

Please sign in to comment.