#[non_exhaustive]pub struct ParseOptions {
pub default_width: Option<Width>,
pub existing_symbols_only: bool,
}Expand description
Options for Context::parse.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.default_width: Option<Width>Width for symbols and literals whose width cannot be inferred. None makes such an
expression an error.
existing_symbols_only: boolOnly resolve symbols that already exist in the context (never create new ones).
Implementations§
Source§impl ParseOptions
impl ParseOptions
Sourcepub fn width(w: Width) -> Self
pub fn width(w: Width) -> Self
Options with a default width.
Examples found in repository?
examples/valeria_rule_audit.rs (line 26)
7fn main() {
8 let strategy = if std::env::args().any(|a| a == "--deobfuscate") {
9 Strategy::deobfuscate()
10 } else {
11 Strategy::standard()
12 };
13 let engine = Engine::builder()
14 .builtin()
15 .strategy(strategy)
16 .build()
17 .unwrap();
18 let mut invalid = false;
19 for line in io::stdin().lock().lines() {
20 let line = line.unwrap();
21 let fields: Vec<_> = line.split('\t').collect();
22 if fields.len() != 4 {
23 panic!("expected id/width/input/reference");
24 }
25 let mut cx = Context::new();
26 let options = ParseOptions::width(Width::new(fields[1].parse().unwrap()).unwrap());
27 let result = (|| -> Result<_, Box<dyn std::error::Error>> {
28 let input = cx.parse(fields[2], &options)?;
29 let reference = cx.parse(fields[3], &options)?;
30 let before = cx.tree_size(input)?;
31 if !agrees(&mut cx, &[input, reference])? {
32 return Ok((
33 "invalid-reference",
34 before,
35 before,
36 cx.tree_size(reference)?,
37 cx.display(input).to_string().replace(['\n', '\t'], " "),
38 cx.display(reference).to_string().replace(['\n', '\t'], " "),
39 ));
40 }
41 let output = engine.simplify(&mut cx, input)?.expr;
42 let want = engine.simplify(&mut cx, reference)?.expr;
43 let after = cx.tree_size(output)?;
44 let target = cx.tree_size(want)?;
45 let status = if !agrees(&mut cx, &[input, reference, output, want])? {
46 "invalid-output"
47 } else if output == want {
48 "covered"
49 } else if after > target {
50 "gap"
51 } else {
52 "different-form"
53 };
54 Ok((
55 status,
56 before,
57 after,
58 target,
59 cx.display(output).to_string().replace(['\n', '\t'], " "),
60 cx.display(want).to_string().replace(['\n', '\t'], " "),
61 ))
62 })();
63 match result {
64 Ok((status, before, after, target, output, want)) => {
65 invalid |= status.starts_with("invalid-");
66 println!(
67 "{}\t{}\t{status}\t{before}\t{after}\t{target}\t{output}\t{want}",
68 fields[0], fields[1]
69 );
70 }
71 Err(error) => {
72 invalid = true;
73 println!(
74 "{}\t{}\terror\t{}",
75 fields[0],
76 fields[1],
77 error.to_string().replace(['\n', '\t'], " ")
78 );
79 }
80 }
81 }
82 if invalid {
83 std::process::exit(1);
84 }
85}More examples
examples/engine_corpus.rs (line 78)
25fn main() {
26 let mut args = std::env::args_os().skip(1);
27 let dir = args.next().expect("directory [--default-strategy]");
28 let default_strategy = match args.next() {
29 None => false,
30 Some(arg) if arg == "--default-strategy" && args.next().is_none() => true,
31 _ => panic!("usage: engine_corpus DIR [--default-strategy]"),
32 };
33 let mut files: Vec<_> = std::fs::read_dir(&dir)
34 .expect("read dir")
35 .filter_map(|e| e.ok().map(|e| e.path()))
36 .collect();
37 files.sort();
38 let solver: Arc<dyn MbaSolver> = Arc::new(NormalFormSolver::new(
39 NfOptions::default()
40 .with_memo(1 << 14)
41 .with_basis_sums(default_strategy),
42 ));
43 let build = |m| {
44 let strategy = Strategy::deobfuscate().with_mba(m);
45 let strategy = if default_strategy {
46 strategy
47 } else {
48 strategy.with_basis_sums(false).with_kept_steps(1 << 12)
49 };
50 Engine::builder()
51 .builtin()
52 .strategy(strategy)
53 .mba_solver(Arc::clone(&solver))
54 .build()
55 .unwrap()
56 };
57 let (stock_engine, forms_engine) = (build(stock()), build(forms()));
58 let mut missing_assumptions = 0usize;
59 for f in files {
60 let mut cx = Context::new();
61 let text = std::fs::read_to_string(&f).unwrap();
62 if text.lines().any(|line| line == "# assumptions: true") {
63 missing_assumptions += 1;
64 }
65 let what = text
66 .lines()
67 .find_map(|l| l.strip_prefix("# engine: "))
68 .unwrap_or("deep")
69 .to_owned();
70 let fuel: u64 = text
71 .lines()
72 .find_map(|l| l.strip_prefix("# fuel: "))
73 .and_then(|v| v.parse().ok())
74 .unwrap_or(1 << 19);
75 let Some(root) = text.split("## root 0\n").nth(1) else {
76 continue;
77 };
78 let e = match cx.parse(root.trim(), &ParseOptions::width(Width::W64)) {
79 Ok(e) => e,
80 Err(err) => {
81 println!("{} parse-error {err}", f.display());
82 continue;
83 }
84 };
85 let engine = if what == "forms" {
86 &forms_engine
87 } else {
88 &stock_engine
89 };
90 let t = std::time::Instant::now();
91 let out = engine.run(
92 &mut cx,
93 &[e],
94 Run::default().with_per_call(Budget::fuel(fuel)),
95 );
96 let ms = t.elapsed().as_secs_f64() * 1e3;
97 let name = f.file_name().unwrap().to_string_lossy();
98 let o = match out {
99 Ok(o) => o,
100 Err(err) => {
101 println!("{name} {what} {ms:.2} error={err}");
102 continue;
103 }
104 };
105 let r = o.roots[0].expr;
106 let Bounded::Exact(nodes) = cx.dag_size(&[r], u32::MAX).expect("a result") else {
107 unreachable!("the arena's node indices fit in u32")
108 };
109 match cx.view(r).expect("a result") {
110 View::Const(v) => println!("{name} {what} {ms:.2} nodes={nodes} constant={v}"),
111 _ => println!("{name} {what} {ms:.2} nodes={nodes} constant=-"),
112 }
113 }
114 if missing_assumptions > 0 {
115 eprintln!("{missing_assumptions} inputs marked as assumed were replayed unconstrained");
116 }
117}Sourcepub fn with_existing_symbols_only(self, yes: bool) -> Self
pub fn with_existing_symbols_only(self, yes: bool) -> Self
Sets existing_symbols_only.
Trait Implementations§
Source§impl Clone for ParseOptions
impl Clone for ParseOptions
Source§impl Debug for ParseOptions
impl Debug for ParseOptions
Auto Trait Implementations§
impl Forget for ParseOptions
impl Freeze for ParseOptions
impl Move for ParseOptions
impl RefUnwindSafe for ParseOptions
impl Send for ParseOptions
impl Sync for ParseOptions
impl Unpin for ParseOptions
impl UnsafeUnpin for ParseOptions
impl UnwindSafe for ParseOptions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more