Skip to content

Commit

Permalink
avoid the "boolean trap" in SpanTracker::end_with_auto_semi
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Herman committed May 3, 2015
1 parent 08fd8a0 commit 28653c3
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ impl Follows for Token {
}
}

#[derive(Eq, PartialEq)]
enum Newline {
Required,
Optional
}

struct SpanTracker {
start: Posn
}
Expand All @@ -81,7 +87,7 @@ impl SpanTracker {
Tracked { value: value, location: Some(Span { start: self.start, end: parser.posn() }) }
}

fn end_with_auto_semi<I, T, F>(&self, parser: &mut Parser<I>, require_newline: bool, cons: F)
fn end_with_auto_semi<I, T, F>(&self, parser: &mut Parser<I>, newline: Newline, cons: F)
-> Parse<Tracked<T>>
where I: Iterator<Item=char>,
F: FnOnce(Semi) -> T
Expand All @@ -102,8 +108,8 @@ impl SpanTracker {
location: Some(Span { start: self.start, end: before })
})
}
&Token { newline, .. } => {
if require_newline && !newline {
&Token { newline: found_newline, .. } => {
if newline == Newline::Required && !found_newline {
let token = try!(parser.read());
return Err(ParseError::FailedASI(token));
}
Expand Down Expand Up @@ -265,7 +271,7 @@ impl<I> Parser<I>
let span = self.start();
self.reread(TokenData::Reserved(Word::Var));
let dtors = try!(self.var_declaration_list());
Ok(try!(span.end_with_auto_semi(self, true, move |semi| StmtData::Var(dtors, semi))))
Ok(try!(span.end_with_auto_semi(self, Newline::Required, move |semi| StmtData::Var(dtors, semi))))
}

fn var_declaration_list(&mut self) -> Parse<Vec<VarDtor>> {
Expand Down Expand Up @@ -326,7 +332,7 @@ impl<I> Parser<I>
let body = Box::new(try!(self.statement()));
try!(self.expect(TokenData::Reserved(Word::While)));
let test = try!(self.paren_expression());
Ok(try!(span.end_with_auto_semi(self, false, move |semi| {
Ok(try!(span.end_with_auto_semi(self, Newline::Optional, move |semi| {
StmtData::DoWhile(body, test, semi)
})))
}
Expand Down

0 comments on commit 28653c3

Please sign in to comment.