diff --git a/src/strace/parser/mod.rs b/src/strace/parser/mod.rs index 510423c..949e72d 100644 --- a/src/strace/parser/mod.rs +++ b/src/strace/parser/mod.rs @@ -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, }), diff --git a/src/strace/parser/peg.pest b/src/strace/parser/peg.pest index 1ef6113..cb62a7e 100644 --- a/src/strace/parser/peg.pest +++ b/src/strace/parser/peg.pest @@ -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)*)? ~ ")" } @@ -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 = { "@"? ~ diff --git a/src/strace/parser/peg.rs b/src/strace/parser/peg.rs index d1d0d56..e1da3d2 100644 --- a/src/strace/parser/peg.rs +++ b/src/strace/parser/peg.rs @@ -115,6 +115,7 @@ impl From> 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") @@ -125,7 +126,14 @@ impl From> 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), @@ -138,10 +146,15 @@ impl From> 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());