25: Drop curly_block r=matklad a=matklad

closes #13
This commit is contained in:
bors[bot] 2018-01-28 11:34:32 +00:00
commit 4212d28727
4 changed files with 30 additions and 74 deletions

View file

@ -14,7 +14,7 @@ fn item(p: &mut Parser) {
let item = p.start(); let item = p.start();
attributes::outer_attributes(p); attributes::outer_attributes(p);
visibility(p); visibility(p);
let la = p.raw_lookahead(1); let la = p.nth(1);
let item_kind = match p.current() { let item_kind = match p.current() {
EXTERN_KW if la == CRATE_KW => { EXTERN_KW if la == CRATE_KW => {
extern_crate_item(p); extern_crate_item(p);
@ -171,7 +171,7 @@ fn use_item(p: &mut Parser) {
p.expect(SEMI); p.expect(SEMI);
fn use_tree(p: &mut Parser) { fn use_tree(p: &mut Parser) {
let la = p.raw_lookahead(1); let la = p.nth(1);
let m = p.start(); let m = p.start();
match (p.current(), la) { match (p.current(), la) {
(STAR, _) => { (STAR, _) => {
@ -235,5 +235,21 @@ fn fn_item(p: &mut Parser) {
assert!(p.at(FN_KW)); assert!(p.at(FN_KW));
p.bump(); p.bump();
p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN) && p.curly_block(|_| ()); p.expect(IDENT);
if p.at(L_PAREN) {
fn_value_parameters(p);
} else {
p.error().message("expected function arguments").emit();
}
if p.at(L_CURLY) {
p.expect(L_CURLY);
p.expect(R_CURLY);
}
fn fn_value_parameters(p: &mut Parser) {
assert!(p.at(L_PAREN));
p.bump();
p.expect(R_PAREN);
}
} }

View file

@ -20,7 +20,7 @@ fn visibility(p: &mut Parser) {
let vis = p.start(); let vis = p.start();
p.bump(); p.bump();
if p.at(L_PAREN) { if p.at(L_PAREN) {
match p.raw_lookahead(1) { match p.nth(1) {
CRATE_KW | SELF_KW | SUPER_KW | IN_KW => { CRATE_KW | SELF_KW | SUPER_KW | IN_KW => {
p.bump(); p.bump();
if p.bump() == IN_KW { if p.bump() == IN_KW {
@ -57,7 +57,7 @@ impl<'p> Parser<'p> {
err.complete(self, ERROR); err.complete(self, ERROR);
} }
pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool { fn expect(&mut self, kind: SyntaxKind) -> bool {
if self.at(kind) { if self.at(kind) {
self.bump(); self.bump();
true true
@ -77,39 +77,23 @@ impl<'p> Parser<'p> {
trait Lookahead: Copy { trait Lookahead: Copy {
fn is_ahead(self, p: &Parser) -> bool; fn is_ahead(self, p: &Parser) -> bool;
fn consume(p: &mut Parser);
} }
impl Lookahead for SyntaxKind { impl Lookahead for SyntaxKind {
fn is_ahead(self, p: &Parser) -> bool { fn is_ahead(self, p: &Parser) -> bool {
p.current() == self p.current() == self
} }
fn consume(p: &mut Parser) {
p.bump();
}
} }
impl Lookahead for [SyntaxKind; 2] { impl Lookahead for [SyntaxKind; 2] {
fn is_ahead(self, p: &Parser) -> bool { fn is_ahead(self, p: &Parser) -> bool {
p.current() == self[0] && p.raw_lookahead(1) == self[1] p.current() == self[0] && p.nth(1) == self[1]
}
fn consume(p: &mut Parser) {
p.bump();
p.bump();
} }
} }
impl Lookahead for [SyntaxKind; 3] { impl Lookahead for [SyntaxKind; 3] {
fn is_ahead(self, p: &Parser) -> bool { fn is_ahead(self, p: &Parser) -> bool {
p.current() == self[0] && p.raw_lookahead(1) == self[1] && p.raw_lookahead(2) == self[2] p.current() == self[0] && p.nth(1) == self[1] && p.nth(2) == self[2]
}
fn consume(p: &mut Parser) {
p.bump();
p.bump();
p.bump();
} }
} }
@ -121,8 +105,4 @@ impl<'a> Lookahead for AnyOf<'a> {
let curr = p.current(); let curr = p.current();
self.0.iter().any(|&k| k == curr) self.0.iter().any(|&k| k == curr)
} }
fn consume(p: &mut Parser) {
p.bump();
}
} }

View file

@ -12,7 +12,7 @@ pub(crate) fn use_path(p: &mut Parser) {
path_segment(p, true); path_segment(p, true);
let mut qual = path.complete(p, PATH); let mut qual = path.complete(p, PATH);
loop { loop {
if p.at(COLONCOLON) && !items::is_use_tree_start(p.raw_lookahead(1)) { if p.at(COLONCOLON) && !items::is_use_tree_start(p.nth(1)) {
let path = qual.precede(p); let path = qual.precede(p);
p.bump(); p.bump();
path_segment(p, false); path_segment(p, false);

View file

@ -1,7 +1,7 @@
use {SyntaxKind, TextUnit, Token}; use {SyntaxKind, TextUnit, Token};
use super::Event; use super::Event;
use super::super::is_insignificant; use super::super::is_insignificant;
use SyntaxKind::{EOF, ERROR, L_CURLY, R_CURLY, TOMBSTONE}; use SyntaxKind::{EOF, TOMBSTONE};
pub(crate) struct Marker { pub(crate) struct Marker {
pos: u32, pos: u32,
@ -106,9 +106,6 @@ pub(crate) struct Parser<'t> {
pos: usize, pos: usize,
events: Vec<Event>, events: Vec<Event>,
curly_level: i32,
curly_limit: Option<i32>,
} }
impl<'t> Parser<'t> { impl<'t> Parser<'t> {
@ -131,30 +128,14 @@ impl<'t> Parser<'t> {
pos: 0, pos: 0,
events: Vec::new(), events: Vec::new(),
curly_level: 0,
curly_limit: None,
} }
} }
pub(crate) fn into_events(self) -> Vec<Event> { pub(crate) fn into_events(self) -> Vec<Event> {
assert!(self.curly_limit.is_none());
assert_eq!(self.current(), EOF); assert_eq!(self.current(), EOF);
self.events self.events
} }
pub(crate) fn current(&self) -> SyntaxKind {
if self.pos == self.tokens.len() {
return EOF;
}
let token = self.tokens[self.pos];
if let Some(limit) = self.curly_limit {
if limit == self.curly_level && token.kind == R_CURLY {
return EOF;
}
}
token.kind
}
pub(crate) fn start(&mut self) -> Marker { pub(crate) fn start(&mut self) -> Marker {
let m = Marker { let m = Marker {
pos: self.events.len() as u32, pos: self.events.len() as u32,
@ -172,11 +153,8 @@ impl<'t> Parser<'t> {
pub(crate) fn bump(&mut self) -> SyntaxKind { pub(crate) fn bump(&mut self) -> SyntaxKind {
let kind = self.current(); let kind = self.current();
match kind { if kind == EOF {
L_CURLY => self.curly_level += 1, return EOF;
R_CURLY => self.curly_level -= 1,
EOF => return EOF,
_ => (),
} }
self.pos += 1; self.pos += 1;
self.event(Event::Token { self.event(Event::Token {
@ -186,30 +164,12 @@ impl<'t> Parser<'t> {
kind kind
} }
pub(crate) fn raw_lookahead(&self, n: usize) -> SyntaxKind { pub(crate) fn nth(&self, n: usize) -> SyntaxKind {
self.tokens.get(self.pos + n).map(|t| t.kind).unwrap_or(EOF) self.tokens.get(self.pos + n).map(|t| t.kind).unwrap_or(EOF)
} }
pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> bool { pub(crate) fn current(&self) -> SyntaxKind {
let old_level = self.curly_level; self.nth(0)
let old_limit = self.curly_limit;
if !self.expect(L_CURLY) {
return false;
}
self.curly_limit = Some(self.curly_level);
f(self);
assert!(self.curly_level > old_level);
self.curly_limit = old_limit;
if !self.expect(R_CURLY) {
let err = self.start();
while self.curly_level > old_level {
if self.bump() == EOF {
break;
}
}
err.complete(self, ERROR);
}
true
} }
fn event(&mut self, event: Event) { fn event(&mut self, event: Event) {