Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas-raillard-arm committed Jan 2, 2024
1 parent d300a7e commit c48037a
Showing 1 changed file with 72 additions and 20 deletions.
92 changes: 72 additions & 20 deletions tools/analysis/traceevent/src/cparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,42 @@ where
)
}

fn lexeme_id<I, O, E, P>(inner: P) -> impl nom::Parser<I, O, E>
where
E: nom::error::ParseError<I>,
P: for<'a> nom::Parser<I, O, E>,
I: nom::AsBytes
+ Clone
+ nom::InputTake
+ nom::Offset
+ nom::Slice<core::ops::RangeTo<usize>>
+ nom::InputLength
+ nom::InputIter<Item = u8>
+ nom::InputTakeAtPosition
+ for<'a> nom::Compare<&'a str>,
<I as nom::InputTakeAtPosition>::Item: Clone + nom::AsChar,
E: FromExternalError<I, CParseError> + nom::error::ParseError<I>,
{
let mut inner = lexeme(inner);
let mut identifier = recognize(identifier::<I, E>());
move |input: I| {
let input2 = input.clone();
match identifier.parse(input) {
Ok((input, id)) => {
// If we found an identifier, the parser is expected to consume it entirely, so we
// don't accidentally just match a prefix of the identifier.
match all_consuming(|input| inner.parse(input)).parse(id) {
Ok((_, x)) => Ok((input, x)),
Err(err) => Err(err)
}
}
// If we could not recognize an identifier, we just run the parser on unmodified input.
// By construction, this cannot start with an identifier.
Err(_) => inner.parse(input2)
}
}
}

fn escape_sequence<I, E>() -> impl nom::Parser<I, u8, E>
where
I: Clone
Expand Down Expand Up @@ -1491,7 +1527,7 @@ grammar! {

// https://port70.net/~nsz/c/c11/n1570.html#6.7.3p1
rule type_qualifier() -> () {
lexeme(alt((
lexeme_id(alt((
tag("const"),
tag("restrict"),
tag("volatile"),
Expand Down Expand Up @@ -1621,11 +1657,11 @@ grammar! {
char('['),
preceded(
delimited(
lexeme(opt(tag("static"))),
lexeme_id(opt(tag("static"))),
many0(
Self::type_qualifier()
),
lexeme(opt(tag("static"))),
lexeme_id(opt(tag("static"))),
),
lexeme(opt(Self::assignment_expr())),
),
Expand Down Expand Up @@ -1694,14 +1730,18 @@ grammar! {
let discard_parser = || {
context(
"discarded",
many0_count(lexeme(alt((
tag("extern").map(|_| ()),
tag("static").map(|_| ()),
tag("auto").map(|_| ()),
tag("register").map(|_| ()),
tag("_Thread_local").map(|_| ()),
Self::type_qualifier(),
)))),
many0_count(
lexeme_id(
alt((
tag("extern").map(|_| ()),
tag("static").map(|_| ()),
tag("auto").map(|_| ()),
tag("register").map(|_| ()),
tag("_Thread_local").map(|_| ()),
Self::type_qualifier(),
))
)
)
)
};

Expand Down Expand Up @@ -1789,17 +1829,17 @@ grammar! {
State::Unknown(DeclSignedness::Unknown) => lexeme(alt((
context(
"struct",
preceded(lexeme(tag("struct")), Self::identifier())
preceded(lexeme_id(tag("struct")), Self::identifier())
.map(Type::Struct),
),
context(
"enum",
preceded(lexeme(tag("enum")), Self::identifier())
preceded(lexeme_id(tag("enum")), Self::identifier())
.map(|id| Type::Enum(Box::new(Type::Unknown), id)),
),
context(
"union",
preceded(lexeme(tag("union")), Self::identifier())
preceded(lexeme_id(tag("union")), Self::identifier())
.map(Type::Union),
),
context(
Expand Down Expand Up @@ -1929,7 +1969,7 @@ grammar! {
// never yield something sensible.
map_res_cut(
tuple((
lexeme(alt((
lexeme_id(alt((
tag("__data_loc"),
tag("__rel_loc"),
))),
Expand Down Expand Up @@ -2145,7 +2185,7 @@ grammar! {
),
context("__builtin_expect",
preceded(
lexeme(tag("__builtin_expect")),
lexeme_id(tag("__builtin_expect")),
parenthesized(
Self::assignment_expr(),
)
Expand Down Expand Up @@ -2569,15 +2609,15 @@ grammar! {
),
context("sizeof type",
preceded(
lexeme(tag("sizeof")),
lexeme_id(tag("sizeof")),
parenthesized(
Self::type_name(),
)
).map(Expr::SizeofType)
),
context("sizeof expr",
preceded(
lexeme(tag("sizeof")),
lexeme_id(tag("sizeof")),
Self::unary_expr(),
).map(|e| Expr::SizeofExpr(Box::new(e)))
),
Expand Down Expand Up @@ -2610,7 +2650,7 @@ grammar! {
pair(
Self::grammar_ctx(),
preceded(
lexeme(tag("__typeof__")),
lexeme_id(tag("__typeof__")),
cut(parenthesized(
Self::expr(),
))
Expand Down Expand Up @@ -2660,7 +2700,7 @@ grammar! {
tuple((
context(
"char encoding prefix",
lexeme(opt(alt((tag("u8"), tag("u"), tag("U"), tag("L"))))),
lexeme_id(opt(alt((tag("u8"), tag("u"), tag("U"), tag("L"))))),
),
delimited(
char('\''),
Expand Down Expand Up @@ -3696,6 +3736,8 @@ mod tests {
Type::Pointer(Box::new(Type::Struct("callback_head".into()))),
);

test(b"u64 *static_foo", "static_foo", Type::Pointer(Box::new(u64_typ.clone())));

// Arrays
test(
b" u64 foo\t []",
Expand All @@ -3712,6 +3754,16 @@ mod tests {
"foo",
Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))),
);
test(
b" u64 foo\t [static 124]",
"foo",
Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))),
);
test(
b" u64 foo\t [static_foo]",
"foo",
Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))),
);

test(
b" u64 (*foo) [1]",
Expand Down

0 comments on commit c48037a

Please sign in to comment.