Skip to content

Commit

Permalink
Merge pull request #30 from ahuoguo/print-full-ast
Browse files Browse the repository at this point in the history
print full ast
  • Loading branch information
ejrgilbert authored Jun 3, 2024
2 parents 4c9ab94 + cf655f8 commit eb8aec5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
35 changes: 14 additions & 21 deletions src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ErrorGen {
}

pub fn add_error(&mut self, error: WhammError) {
let fatal = error.fatal.clone();
let fatal = error.fatal;
self.errors.push(error);
self.inc_errors();

Expand Down Expand Up @@ -215,13 +215,9 @@ impl ErrorGen {
// See code the following code for why we can do this:
// https://github.com/pest-parser/pest/blob/master/pest/src/error.rs#L612
let mut lines = orig_msg.lines();
if let Some(line) = lines.rfind(|line| {
lines.rfind(|line| {
line.as_bytes()[0].is_ascii_digit()
}) {
Some(line.to_string())
} else {
None
}
}).map(|line| line.to_string())
} else {
None
};
Expand Down Expand Up @@ -285,13 +281,10 @@ pub struct CodeLocation {
}
impl CodeLocation {
pub fn is_span(&self) -> bool {
match self.line_col {
LineColLocation::Span(..) => true,
_ => false
}
matches!(self.line_col, LineColLocation::Span(..))
}
pub fn lines_are_defined(&self) -> bool {
!self.line_str.is_none()
self.line_str.is_some()
}

// report this error to the console, including color highlighting
Expand Down Expand Up @@ -333,7 +326,7 @@ impl CodeLocation {
}
}

fn define_lines(&mut self, script: &String) {
fn define_lines(&mut self, script: &str) {
match &self.line_col {
LineColLocation::Pos((line_no, ..)) => {
if let Some(script_line) = script.lines().nth(line_no - 1) {
Expand All @@ -353,7 +346,7 @@ impl CodeLocation {
}
}

fn print_numbered_line(&self, l: &usize, line: &String, s: &String, buffer: &mut Buffer) {
fn print_numbered_line(&self, l: &usize, line: &String, s: &str, buffer: &mut Buffer) {
let w = s.len();
blue(false, format!("{l:w$} | "), buffer);
white(false, format!("{line}\n"), buffer);
Expand Down Expand Up @@ -381,7 +374,7 @@ impl CodeLocation {
fn underline(&self, start_col: &usize) -> String {
let mut underline = String::new();

let mut start_col = start_col.clone();
let mut start_col = *start_col;
let end = match &self.line_col {
LineColLocation::Span(_, (_, mut end)) => {
let inverted_cols = start_col > end;
Expand Down Expand Up @@ -415,10 +408,10 @@ impl CodeLocation {
INFO_UNDERLINE_CHAR
};

underline.push(u_char.clone());
if end - &start_col > 1 {
for _ in 2..(&end - &start_col) {
underline.push(u_char.clone());
underline.push(u_char);
if end - start_col > 1 {
for _ in 2..(end - start_col) {
underline.push(u_char);
}
underline.push(u_char);
}
Expand Down Expand Up @@ -447,7 +440,7 @@ pub struct WhammError {
}
impl WhammError {
pub fn is_fatal(&self) -> bool {
self.fatal.clone()
self.fatal
}

/// report this error to the console, including color highlighting
Expand Down Expand Up @@ -637,7 +630,7 @@ impl ErrorType {
let non_separated = f(&rules[l - 1]);
let separated = rules
.iter()
.take(l.clone() - 1)
.take(l - 1)
.map(f)
.collect::<Vec<_>>()
.join(", ");
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cli::{Cmd, WhammCli};
use crate::parser::whamm_parser::*;
use crate::behavior::builder_visitor::*;
use crate::generator::emitters::{Emitter, WasmRewritingEmitter};
use crate::generator::init_generator::{InitGenerator};
use crate::generator::instr_generator::{InstrGenerator};
use crate::generator::init_generator::InitGenerator;
use crate::generator::instr_generator::InstrGenerator;
use crate::common::error::ErrorGen;

mod cli;
Expand Down
21 changes: 10 additions & 11 deletions src/parser/print_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,8 @@ impl WhammVisitor<String> for AsStrVisitor {
self.increase_indent();
s += &format!("{} {}: ", self.get_indent(), name);

s += &format!("(");
for probe in probes.iter() {
self.visit_probe(probe);
}
s += &format!("({}", NL);
s += &probes.iter().map(|probe| self.visit_probe(probe)).collect::<String>();
s += &format!("){}", NL);
self.decrease_indent();
}
Expand Down Expand Up @@ -273,7 +271,12 @@ impl WhammVisitor<String> for AsStrVisitor {
s += &format!("{} `predicate`:{}", self.get_indent(), NL);
self.increase_indent();
match &probe.predicate {
Some(pred) => s += &format!("{} / {} /{}", self.get_indent(), self.visit_expr(pred), NL),
Some(pred) => {
let expr = self.visit_expr(pred);
expr.split("&&").for_each(|line| {
s += &format!("{}&& {}{}", self.get_indent(), line, NL)
});
},
None => s += &format!("{} / None /{}", self.get_indent(), NL)
}
self.decrease_indent();
Expand Down Expand Up @@ -369,9 +372,7 @@ impl WhammVisitor<String> for AsStrVisitor {
s += &format!("{}(", self.visit_expr(fn_target));
match args {
Some(args) => {
for arg in args {
s += &format!("{}, ", self.visit_expr(arg));
}
s += &args.iter().map(|arg| self.visit_expr(arg)).collect::<Vec<String>>().join(", ");
},
_ => {}
}
Expand Down Expand Up @@ -445,9 +446,7 @@ impl WhammVisitor<String> for AsStrVisitor {
Value::Tuple {ty: _ty, vals} => {
let mut s = "".to_string();
s += &format!("(");
for v in vals.iter() {
s += &format!("{}, ", self.visit_expr(v));
}
s += &vals.iter().map(|v| self.visit_expr(v)).collect::<Vec<String>>().join(", ");
s += &format!(")");
s
}
Expand Down
17 changes: 11 additions & 6 deletions src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const VALID_SCRIPTS: &'static [&'static str] = &[

// Function calls
r#"
wasm::call:alt / strpaircmp((arg2, arg3), "record") / {
wasm::call:alt / strcmp((arg2, arg3), "record") / {
new_target_fn_name = "redirect_to_fault_injector";
}
"#,
Expand All @@ -56,19 +56,24 @@ wasm::call:alt /
target_fn_type == "import" &&
target_imp_module == "ic0" &&
target_imp_name == "call_new" &&
strpaircmp((arg0, arg1), "bookings") &&
strpaircmp((arg2, arg3), "record")
strcmp((arg0, arg1), "bookings") &&
strcmp((arg2, arg3), "record")
/ {
new_target_fn_name = "redirect_to_fault_injector";
}
"#,

// Statements
// Statements (either assignment or function call)
r#"
wasm:bytecode:br:before {
i = 0;
}
"#,
r#"
wasm:bytecode:br:before {
call_new();
}
"#,

// Comments
r#"
Expand Down Expand Up @@ -228,8 +233,8 @@ wasm::call:alt /
target_fn_type == "import" &&
target_imp_module == "ic0" &&
target_imp_name == "call_new" &&
strpaircmp((arg0, arg1), "bookings") &&
strpaircmp((arg2, arg3), "record")
strcmp((arg0, arg1), "bookings") &&
strcmp((arg2, arg3), "record")
/ {
new_target_fn_name = "redirect_to_fault_injector";
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/whamm_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pest::error::{Error, LineColLocation};
use pest::Parser;
use pest::iterators::{Pair, Pairs};

use log::{trace};
use log::trace;
use crate::common::error::{ErrorGen, WhammError};
use crate::parser::types::{DataType, Script, Whamm, Expr, Statement, Value, Location, ProbeSpec, SpecPart};

Expand Down
4 changes: 2 additions & 2 deletions src/verifier/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ wasm::call:alt /
target_fn_type == "import" &&
target_imp_module == "ic0" &&
target_imp_name == "call_new" &&
strpaircmp((arg0, arg1), "bookings") &&
strpaircmp((arg2, arg3), "record")
strcmp((arg0, arg1), "bookings") &&
strcmp((arg2, arg3), "record")
/ {
new_target_fn_name = "redirect_to_fault_injector";
}
Expand Down

0 comments on commit eb8aec5

Please sign in to comment.