Skip to content

Commit

Permalink
Apparently mut self wasn't needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhoo committed Feb 20, 2020
1 parent a524f53 commit 506ee2b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 47 deletions.
79 changes: 37 additions & 42 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ macro_rules! typed_token (
});
);

#[allow(dead_code)]
fn any_token(input: &[Token]) -> CResult<'_, &Token> {
if input.is_empty() {
let res: CResult<'_, &Token> = Err(crate::nom::Err::Incomplete(Needed::Size(1)));
Expand Down Expand Up @@ -303,26 +304,25 @@ impl<'a> PRef<'a> {
use nom::combinator::map_opt;
use nom::sequence::{delimited, pair};
alt((
delimited(p("("), self.numeric_expr, p(")")),
numeric(self.literal),
numeric(self.identifier),
delimited(p("("), |i| self.numeric_expr(i), p(")")),
numeric(|i| self.literal(i)),
numeric(|i| self.identifier(i)),
map_opt(
pair(one_of_punctuation(&["+", "-", "~"][..]), self.unary),
pair(one_of_punctuation(&["+", "-", "~"][..]), |i| self.unary(i)),
unary_op,
),
))(input)
}

fn mul_div_rem<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn mul_div_rem<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::pair;
let (input, acc) = self.unary(input)?;
fold_many0(
pair(
complete(one_of_punctuation(&["*", "/", "%"][..])),
self.unary,
),
pair(complete(one_of_punctuation(&["*", "/", "%"][..])), |i| {
self.unary(i)
}),
acc,
|mut acc, (op, val): (&[u8], EvalResult)| {
match op[0] as char {
Expand All @@ -336,16 +336,15 @@ impl<'a> PRef<'a> {
)(input)
}

fn add_sub<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn add_sub<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::pair;
let (input, acc) = self.mul_div_rem(input)?;
fold_many0(
pair(
complete(one_of_punctuation(&["+", "-"][..])),
self.mul_div_rem,
),
pair(complete(one_of_punctuation(&["+", "-"][..])), |i| {
self.mul_div_rem(i)
}),
acc,
|mut acc, (op, val): (&[u8], EvalResult)| {
match op[0] as char {
Expand All @@ -358,16 +357,15 @@ impl<'a> PRef<'a> {
)(input)
}

fn shl_shr<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn shl_shr<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::pair;
let (input, acc) = self.add_sub(input)?;
numeric(fold_many0(
pair(
complete(one_of_punctuation(&["<<", ">>"][..])),
self.add_sub,
),
pair(complete(one_of_punctuation(&["<<", ">>"][..])), |i| {
self.add_sub(i)
}),
acc,
|mut acc, (op, val): (&[u8], EvalResult)| {
match op {
Expand All @@ -380,13 +378,13 @@ impl<'a> PRef<'a> {
))(input)
}

fn and<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn and<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::preceded;
let (input, acc) = self.shl_shr(input)?;
numeric(fold_many0(
preceded(complete(p("&")), self.shl_shr),
preceded(complete(p("&")), |i| self.shl_shr(i)),
acc,
|mut acc, val: EvalResult| {
acc &= &val;
Expand All @@ -395,13 +393,13 @@ impl<'a> PRef<'a> {
))(input)
}

fn xor<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn xor<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::preceded;
let (input, acc) = self.and(input)?;
numeric(fold_many0(
preceded(complete(p("^")), self.and),
preceded(complete(p("^")), |i| self.and(i)),
acc,
|mut acc, val: EvalResult| {
acc ^= &val;
Expand All @@ -410,13 +408,13 @@ impl<'a> PRef<'a> {
))(input)
}

fn or<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn or<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::preceded;
let (input, acc) = self.xor(input)?;
numeric(fold_many0(
preceded(complete(p("|")), self.xor),
preceded(complete(p("|")), |i| self.xor(i)),
acc,
|mut acc, val: EvalResult| {
acc |= &val;
Expand All @@ -426,7 +424,7 @@ impl<'a> PRef<'a> {
}

#[inline(always)]
fn numeric_expr<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn numeric_expr<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
self.or(input)
}
}
Expand Down Expand Up @@ -479,24 +477,24 @@ impl<'a> PRef<'a> {
}
}

fn string<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, Vec<u8>> {
fn string<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, Vec<u8>> {
use nom::branch::alt;
use nom::combinator::map_opt;
alt((
map_opt(self.literal, EvalResult::as_str),
map_opt(self.identifier, EvalResult::as_str),
map_opt(|i| self.literal(i), EvalResult::as_str),
map_opt(|i| self.identifier(i), EvalResult::as_str),
))(input)
.to_cexpr_result()
}

// "string1" "string2" etc...
fn concat_str<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn concat_str<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::combinator::map;
use nom::multi::many0;
use nom::sequence::pair;
map(
pair(self.string, many0(complete(self.string))),
pair(|i| self.string(i), many0(complete(|i| self.string(i)))),
|(first, v)| {
Vec::into_iter(v)
.fold(first, |mut s, elem| {
Expand All @@ -509,25 +507,22 @@ impl<'a> PRef<'a> {
.to_cexpr_result()
}

fn expr<'t>(&'_ mut self, input: &'t [Token]) -> CResult<'t, EvalResult> {
fn expr<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::branch::alt;
use nom::sequence::delimited;
alt((
self.numeric_expr,
delimited(p("("), self.expr, p(")")),
self.concat_str,
self.literal,
self.identifier,
|i| self.numeric_expr(i),
delimited(p("("), |i| self.expr(i), p(")")),
|i| self.concat_str(i),
|i| self.literal(i),
|i| self.identifier(i),
))(input)
.to_cexpr_result()
}

fn macro_definition<'t>(
&'_ mut self,
input: &'t [Token],
) -> CResult<'t, (&'t [u8], EvalResult)> {
fn macro_definition<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, (&'t [u8], EvalResult)> {
use nom::sequence::pair;
pair(typed_token!(Identifier), self.expr)(input)
pair(typed_token!(Identifier), |i| self.expr(i))(input)
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ fn file_visit_macros<F: FnMut(Vec<u8>, Vec<Token>)>(
let tu = {
let index = clang_createIndex(true as _, false as _);
let cfile = ffi::CString::new(file).unwrap();
let mut tu = mem::uninitialized();
let mut tu = mem::MaybeUninit::uninit();
assert!(
clang_parseTranslationUnit2(
index,
Expand All @@ -245,12 +245,12 @@ fn file_visit_macros<F: FnMut(Vec<u8>, Vec<Token>)>(
ptr::null_mut(),
0,
CXTranslationUnit_DetailedPreprocessingRecord,
&mut tu
&mut *tu.as_mut_ptr()
) == CXError_Success,
"Failure reading test case {}",
file
);
tu
tu.assume_init()
};
visit_children(clang_getTranslationUnitCursor(tu), |cur, _parent| {
if cur.kind == CXCursor_MacroDefinition {
Expand Down Expand Up @@ -308,8 +308,8 @@ fn fix_bug_9069() -> bool {
token_sets[0] != token_sets[1]
}

use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
use std::sync::{Once, ONCE_INIT};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Once;

static CHECK_FIX: Once = Once::new();
static FIX: AtomicBool = AtomicBool::new(false);
Expand Down

0 comments on commit 506ee2b

Please sign in to comment.