Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Sep 17, 2022
1 parent 1a4f02b commit 59b3ba8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 84 deletions.
176 changes: 93 additions & 83 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ fn identifier_token(input: &[Token]) -> CResult<'_, &[u8]> {
if input[0].kind == TokenKind::Identifier {
Ok((&input[1..], &input[0].raw[..]))
} else {
Err(crate::nom::Err::Error((input, crate::ErrorKind::TypedToken(TokenKind::Identifier)).into()))
Err(crate::nom::Err::Error(
(input, crate::ErrorKind::TypedToken(TokenKind::Identifier)).into(),
))
}
}
}
Expand Down Expand Up @@ -295,7 +297,7 @@ where
}

fn expr_cast(input: (Vec<Vec<u8>>, EvalResult)) -> EvalResult {
EvalResult::Cast(input.0, Box::new(input.1))
EvalResult::Cast(input.0, Box::new(input.1))
}

impl<'a> PRef<'a> {
Expand Down Expand Up @@ -482,10 +484,7 @@ impl<'a> PRef<'a> {

fn expr(self, input: &'_ [Token]) -> CResult<'_, EvalResult> {
alt((
map(
pair(|i| self.cast(i), |i| self.expr(i)),
expr_cast
),
map(pair(|i| self.cast(i), |i| self.expr(i)), expr_cast),
|i| self.numeric_expr(i),
delimited(p("("), |i| self.expr(i), p(")")),
|i| self.concat_str(i),
Expand All @@ -500,81 +499,96 @@ impl<'a> PRef<'a> {
}

fn int_ty(input: &'_ [Token]) -> CResult<'_, Vec<&[u8]>> {
fn int_signedness(input: &'_ [Token]) -> CResult<'_, &[u8]> {
alt((
keyword("unsigned"),
keyword("signed"),
))(input)
}

fn int_longness(input: &'_ [Token]) -> CResult<'_, &[u8]> {
alt((
keyword("short"),
keyword("long"),
))(input)
}

alt((
// [const] [(un)signed] long long [int]
map(
permutation((opt(keyword("const")), opt(int_signedness), keyword("long"), keyword("long"), opt(keyword("int")))),
|(_, s, i1, i2, _)| if let Some(s) = s {
if s == b"signed" {
vec![i1, i2]
} else {
vec![s, i1, i2]
}
} else {
vec![i1, i2]
},
),
// [const] [(un)signed] long/short [int]
map(
permutation((opt(keyword("const")), opt(int_signedness), int_longness, opt(keyword("int")))),
|(_, s, i, _)| if let Some(s) = s {
if s == b"signed" {
vec![i]
} else {
vec![s, i]
}
} else {
vec![i]
},
),
// [const] [(un)signed] char/int
map(
permutation((opt(keyword("const")), opt(int_signedness), alt((keyword("char"), keyword("int"))))),
|(_, s, i)| if let Some(s) = s {
if s == b"signed" && i == b"int" {
vec![i]
} else {
vec![s, i]
}
} else {
vec![i]
},
),
))(input)
fn int_signedness(input: &'_ [Token]) -> CResult<'_, &[u8]> {
alt((keyword("unsigned"), keyword("signed")))(input)
}

fn int_longness(input: &'_ [Token]) -> CResult<'_, &[u8]> {
alt((keyword("short"), keyword("long")))(input)
}

alt((
// [const] [(un)signed] long long [int]
map(
permutation((
opt(keyword("const")),
opt(int_signedness),
keyword("long"),
keyword("long"),
opt(keyword("int")),
)),
|(_, s, i1, i2, _)| {
if let Some(s) = s {
if s == b"signed" {
vec![i1, i2]
} else {
vec![s, i1, i2]
}
} else {
vec![i1, i2]
}
},
),
// [const] [(un)signed] long/short [int]
map(
permutation((
opt(keyword("const")),
opt(int_signedness),
int_longness,
opt(keyword("int")),
)),
|(_, s, i, _)| {
if let Some(s) = s {
if s == b"signed" {
vec![i]
} else {
vec![s, i]
}
} else {
vec![i]
}
},
),
// [const] [(un)signed] char/int
map(
permutation((
opt(keyword("const")),
opt(int_signedness),
alt((keyword("char"), keyword("int"))),
)),
|(_, s, i)| {
if let Some(s) = s {
if s == b"signed" && i == b"int" {
vec![i]
} else {
vec![s, i]
}
} else {
vec![i]
}
},
),
))(input)
}

fn ty(self, input: &'_ [Token]) -> CResult<'_, Vec<Vec<u8>>> {
map(
alt((
// [const] <identifier>
map(
permutation((opt(keyword("const")), identifier_token)),
|(_, id)| vec![id],
),
// [const] bool
map(
permutation((opt(keyword("const")), keyword("bool"))),
|(_, b)| vec![b],
),
Self::int_ty,
)),
|v| v.into_iter().map(|t| t.to_vec()).collect(),
)(input)
.to_cexpr_result()
map(
alt((
// [const] <identifier>
map(
permutation((opt(keyword("const")), identifier_token)),
|(_, id)| vec![id],
),
// [const] bool
map(
permutation((opt(keyword("const")), keyword("bool"))),
|(_, b)| vec![b],
),
Self::int_ty,
)),
|v| v.into_iter().map(|t| t.to_vec()).collect(),
)(input)
.to_cexpr_result()
}

fn macro_definition(self, input: &'_ [Token]) -> CResult<'_, (&'_ [u8], EvalResult)> {
Expand Down Expand Up @@ -696,10 +710,6 @@ pub fn macro_definition(input: &[Token]) -> CResult<'_, (&'_ [u8], EvalResult)>
pub fn fn_macro_declaration(input: &[Token]) -> CResult<'_, (&[u8], Vec<&[u8]>)> {
pair(
identifier_token,
delimited(
p("("),
separated_list0(p(","), identifier_token),
p(")"),
),
delimited(p("("), separated_list0(p(","), identifier_token), p(")")),
)(input)
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

pub mod nom {
//! nom's result types, re-exported.
pub use nom::{error::ErrorKind, error::Error, Err, IResult, Needed};
pub use nom::{error::Error, error::ErrorKind, Err, IResult, Needed};
}
pub mod expr;
pub mod literal;
Expand Down

0 comments on commit 59b3ba8

Please sign in to comment.