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 c48037a commit b9ece47
Showing 1 changed file with 43 additions and 53 deletions.
96 changes: 43 additions & 53 deletions tools/analysis/traceevent/src/cparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,10 +1328,9 @@ where
)
}

fn lexeme_id<I, O, E, P>(inner: P) -> impl nom::Parser<I, O, E>
fn keyword<'a, I, E>(name: &'a str) -> impl nom::Parser<I, I, E> + 'a
where
E: nom::error::ParseError<I>,
P: for<'a> nom::Parser<I, O, E>,
E: nom::error::ParseError<I> + 'a,
I: nom::AsBytes
+ Clone
+ nom::InputTake
Expand All @@ -1340,27 +1339,16 @@ where
+ nom::InputLength
+ nom::InputIter<Item = u8>
+ nom::InputTakeAtPosition
+ for<'a> nom::Compare<&'a str>,
+ for<'b>nom::Compare<&'b str> + 'a,
<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>());
let mut inner = all_consuming(lexeme(tag(name)));
let mut identifier = recognize(identifier());
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)
}
let (input, id) = identifier.parse(input)?;
let (_, x) = inner.parse(id)?;
Ok((input, x))
}
}

Expand Down Expand Up @@ -1527,12 +1515,12 @@ grammar! {

// https://port70.net/~nsz/c/c11/n1570.html#6.7.3p1
rule type_qualifier() -> () {
lexeme_id(alt((
tag("const"),
tag("restrict"),
tag("volatile"),
tag("_Atomic"),
))).map(|_| ())
alt((
keyword("const"),
keyword("restrict"),
keyword("volatile"),
keyword("_Atomic"),
)).map(|_| ())
}

// https://port70.net/~nsz/c/c11/n1570.html#6.7.6
Expand Down Expand Up @@ -1657,11 +1645,11 @@ grammar! {
char('['),
preceded(
delimited(
lexeme_id(opt(tag("static"))),
opt(keyword("static")),
many0(
Self::type_qualifier()
),
lexeme_id(opt(tag("static"))),
opt(keyword("static")),
),
lexeme(opt(Self::assignment_expr())),
),
Expand Down Expand Up @@ -1731,16 +1719,14 @@ grammar! {
context(
"discarded",
many0_count(
lexeme_id(
alt((
tag("extern").map(|_| ()),
tag("static").map(|_| ()),
tag("auto").map(|_| ()),
tag("register").map(|_| ()),
tag("_Thread_local").map(|_| ()),
Self::type_qualifier(),
))
)
alt((
keyword("extern").map(|_| ()),
keyword("static").map(|_| ()),
keyword("auto").map(|_| ()),
keyword("register").map(|_| ()),
keyword("_Thread_local").map(|_| ()),
Self::type_qualifier(),
))
)
)
};
Expand Down Expand Up @@ -1829,17 +1815,17 @@ grammar! {
State::Unknown(DeclSignedness::Unknown) => lexeme(alt((
context(
"struct",
preceded(lexeme_id(tag("struct")), Self::identifier())
preceded(keyword("struct"), Self::identifier())
.map(Type::Struct),
),
context(
"enum",
preceded(lexeme_id(tag("enum")), Self::identifier())
preceded(keyword("enum"), Self::identifier())
.map(|id| Type::Enum(Box::new(Type::Unknown), id)),
),
context(
"union",
preceded(lexeme_id(tag("union")), Self::identifier())
preceded(keyword("union"), Self::identifier())
.map(Type::Union),
),
context(
Expand Down Expand Up @@ -1969,10 +1955,10 @@ grammar! {
// never yield something sensible.
map_res_cut(
tuple((
lexeme_id(alt((
tag("__data_loc"),
tag("__rel_loc"),
))),
alt((
keyword("__data_loc"),
keyword("__rel_loc"),
)),
Self::declaration_specifier(),
// This will be an abstract declarator, i.e. a declarator with
// no identifier (like parameters in a function prototype), as
Expand Down Expand Up @@ -2185,7 +2171,7 @@ grammar! {
),
context("__builtin_expect",
preceded(
lexeme_id(tag("__builtin_expect")),
keyword("__builtin_expect"),
parenthesized(
Self::assignment_expr(),
)
Expand Down Expand Up @@ -2609,15 +2595,15 @@ grammar! {
),
context("sizeof type",
preceded(
lexeme_id(tag("sizeof")),
keyword("sizeof"),
parenthesized(
Self::type_name(),
)
).map(Expr::SizeofType)
),
context("sizeof expr",
preceded(
lexeme_id(tag("sizeof")),
keyword("sizeof"),
Self::unary_expr(),
).map(|e| Expr::SizeofExpr(Box::new(e)))
),
Expand Down Expand Up @@ -2650,7 +2636,7 @@ grammar! {
pair(
Self::grammar_ctx(),
preceded(
lexeme_id(tag("__typeof__")),
keyword("__typeof__"),
cut(parenthesized(
Self::expr(),
))
Expand Down Expand Up @@ -2700,7 +2686,7 @@ grammar! {
tuple((
context(
"char encoding prefix",
lexeme_id(opt(alt((tag("u8"), tag("u"), tag("U"), tag("L"))))),
opt(alt((keyword("u8"), keyword("u"), keyword("U"), keyword("L")))),
),
delimited(
char('\''),
Expand Down Expand Up @@ -3760,11 +3746,15 @@ mod tests {
Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))),
);
test(
b" u64 foo\t [static_foo]",
b"u64 foo [static_bar]",
"foo",
Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))),
Type::Array(
Box::new(u64_typ.clone()),
ArrayKind::Fixed(Err(Box::new(InterpError::CompileError(Box::new(
CompileError::CannotHandleExpr(Expr::EnumConstant(Type::I32, "static_bar".into())),
)))))
)
);

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

0 comments on commit b9ece47

Please sign in to comment.