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 f727ec5
Showing 1 changed file with 61 additions and 20 deletions.
81 changes: 61 additions & 20 deletions tools/analysis/traceevent/src/cparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,31 @@ 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 = all_consuming(inner);
let mut identifier = recognize(lexeme(identifier()));
move |input: I| {
let (input, id) = identifier.parse(input)?;
let (_, x) = inner.parse(id)?;
Ok((input, x))
}
}

fn escape_sequence<I, E>() -> impl nom::Parser<I, u8, E>
where
I: Clone
Expand Down Expand Up @@ -1491,7 +1516,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 +1646,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 +1719,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 +1818,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 +1958,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 +2174,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 +2598,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 +2639,7 @@ grammar! {
pair(
Self::grammar_ctx(),
preceded(
lexeme(tag("__typeof__")),
lexeme_id(tag("__typeof__")),
cut(parenthesized(
Self::expr(),
))
Expand Down Expand Up @@ -2660,7 +2689,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 +3725,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 +3743,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 f727ec5

Please sign in to comment.