Skip to content

Commit

Permalink
feat: simplify grammar, grab more metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma-s1n committed May 29, 2024
1 parent d862269 commit 63c3736
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/strace/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ mod tests {
Expression::Integer(IntegerExpression {
value: IntegerExpressionValue::BinaryOr(vec![
IntegerExpressionValue::NamedConst("S_IFDIR".to_owned()),
IntegerExpressionValue::NamedConst("0755".to_owned())
IntegerExpressionValue::Literal(0o755)
]),
metadata: None,
}),
Expand Down
25 changes: 19 additions & 6 deletions src/strace/parser/peg.pest
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pid = { ASCII_DIGIT+ }

rel_ts = { ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }

name = { (ASCII_ALPHA_LOWER | ASCII_DIGIT | "_")+ }
name = { symbol_name }

arguments = { "(" ~ (expression ~ (", " ~ expression)*)? ~ ")" }

Expand All @@ -18,31 +18,44 @@ ret_val = { int ~ (" " ~ ANY*)? }
// Subrules

expression = {
#macro = macro |
#int = int |
#struct = struct |
#buf = buffer |
#macro = macro |
#set = set |
#array = array
}

symbol_name = { (ASCII_ALPHA | ASCII_DIGIT | "_")+ }
comment = { " /* " ~ (!" */" ~ ANY)+ ~ " */" }

int = {
#or = or |
#lit = literal_int |
#named = named_constant
}
literal_int = { #hex = literal_int_hex | #dec = literal_int_dec }
literal_int = {
#oct = literal_int_oct |
#hex = literal_int_hex |
#dec = literal_int_dec
}
or = { named_constant ~ ("|" ~ int)+ }
named_constant = { (ASCII_ALPHA_UPPER | ASCII_DIGIT | "_")+ }
named_constant = { symbol_name ~ int_metadata? }

literal_int_oct = { "0" ~ ASCII_OCT_DIGIT+ }
literal_int_hex = { "0x" ~ ASCII_HEX_DIGIT+ }
literal_int_dec = { literal_int_dec_val ~ int_metadata? }
literal_int_dec = {
literal_int_dec_val ~
(
#mdata = int_metadata |
#com = comment
)?
}
literal_int_dec_val = { "-"? ~ ASCII_DIGIT+ }
int_metadata = { "<" ~ buffer_byte+ ~ ">" }

struct = { "{" ~ (struct_member ~ (", " ~ struct_member)*)? ~ "}" }
struct_member = { symbol_name ~ "=" ~ expression }
symbol_name = { (ASCII_ALPHA | ASCII_DIGIT | "_")+ }

buffer = {
"@"? ~
Expand Down
23 changes: 18 additions & 5 deletions src/strace/parser/peg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl From<Pair<'_, Rule>> for IntegerExpression {
Some("lit") => {
let pair = pair_descend(pair, 1).unwrap();
let (val, metadata) = match pair.as_node_tag() {
Some("oct") => (i128::from_str_radix(pair.as_str(), 8).unwrap(), None),
Some("hex") => (
pair.as_str()
.strip_prefix("0x")
Expand All @@ -125,7 +126,14 @@ impl From<Pair<'_, Rule>> for IntegerExpression {
Some("dec") => {
let mut children = pair.into_inner();
let val_pair = children.next().unwrap();
let metadata_pair = children.next();
let mut metadata_pair = children.next();
// TODO use Option::take_if if it gets stable
if metadata_pair
.as_ref()
.is_some_and(|p| p.as_node_tag() == Some("com"))
{
metadata_pair = None;
}
(
val_pair.as_str().parse().unwrap(),
metadata_pair.map(|p| BufferExpression::from(p).value),
Expand All @@ -138,10 +146,15 @@ impl From<Pair<'_, Rule>> for IntegerExpression {
metadata,
}
}
Some("named") => IntegerExpression {
value: IntegerExpressionValue::NamedConst(pair.as_str().to_owned()),
metadata: None,
},
Some("named") => {
let mut children = pair.into_inner();
let val_pair = children.next().unwrap();
let metadata_pair = children.next();
IntegerExpression {
value: IntegerExpressionValue::NamedConst(val_pair.as_str().to_owned()),
metadata: metadata_pair.map(|p| BufferExpression::from(p).value),
}
}
Some("or") => {
let mut children = pair.into_inner();
let mut or_elems = Vec::with_capacity(children.len());
Expand Down

0 comments on commit 63c3736

Please sign in to comment.