Skip to content

Drop curly_block #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/parser/event_parser/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn item(p: &mut Parser) {
let item = p.start();
attributes::outer_attributes(p);
visibility(p);
let la = p.raw_lookahead(1);
let la = p.nth(1);
let item_kind = match p.current() {
EXTERN_KW if la == CRATE_KW => {
extern_crate_item(p);
Expand Down Expand Up @@ -171,7 +171,7 @@ fn use_item(p: &mut Parser) {
p.expect(SEMI);

fn use_tree(p: &mut Parser) {
let la = p.raw_lookahead(1);
let la = p.nth(1);
let m = p.start();
match (p.current(), la) {
(STAR, _) => {
Expand Down Expand Up @@ -235,5 +235,21 @@ fn fn_item(p: &mut Parser) {
assert!(p.at(FN_KW));
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);
}
}
28 changes: 4 additions & 24 deletions src/parser/event_parser/grammar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn visibility(p: &mut Parser) {
let vis = p.start();
p.bump();
if p.at(L_PAREN) {
match p.raw_lookahead(1) {
match p.nth(1) {
CRATE_KW | SELF_KW | SUPER_KW | IN_KW => {
p.bump();
if p.bump() == IN_KW {
Expand Down Expand Up @@ -57,7 +57,7 @@ impl<'p> Parser<'p> {
err.complete(self, ERROR);
}

pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool {
fn expect(&mut self, kind: SyntaxKind) -> bool {
if self.at(kind) {
self.bump();
true
Expand All @@ -77,39 +77,23 @@ impl<'p> Parser<'p> {

trait Lookahead: Copy {
fn is_ahead(self, p: &Parser) -> bool;
fn consume(p: &mut Parser);
}

impl Lookahead for SyntaxKind {
fn is_ahead(self, p: &Parser) -> bool {
p.current() == self
}

fn consume(p: &mut Parser) {
p.bump();
}
}

impl Lookahead for [SyntaxKind; 2] {
fn is_ahead(self, p: &Parser) -> bool {
p.current() == self[0] && p.raw_lookahead(1) == self[1]
}

fn consume(p: &mut Parser) {
p.bump();
p.bump();
p.current() == self[0] && p.nth(1) == self[1]
}
}

impl Lookahead for [SyntaxKind; 3] {
fn is_ahead(self, p: &Parser) -> bool {
p.current() == self[0] && p.raw_lookahead(1) == self[1] && p.raw_lookahead(2) == self[2]
}

fn consume(p: &mut Parser) {
p.bump();
p.bump();
p.bump();
p.current() == self[0] && p.nth(1) == self[1] && p.nth(2) == self[2]
}
}

Expand All @@ -121,8 +105,4 @@ impl<'a> Lookahead for AnyOf<'a> {
let curr = p.current();
self.0.iter().any(|&k| k == curr)
}

fn consume(p: &mut Parser) {
p.bump();
}
}
2 changes: 1 addition & 1 deletion src/parser/event_parser/grammar/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn use_path(p: &mut Parser) {
path_segment(p, true);
let mut qual = path.complete(p, PATH);
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);
p.bump();
path_segment(p, false);
Expand Down
52 changes: 6 additions & 46 deletions src/parser/event_parser/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {SyntaxKind, TextUnit, Token};
use super::Event;
use super::super::is_insignificant;
use SyntaxKind::{EOF, ERROR, L_CURLY, R_CURLY, TOMBSTONE};
use SyntaxKind::{EOF, TOMBSTONE};

pub(crate) struct Marker {
pos: u32,
Expand Down Expand Up @@ -106,9 +106,6 @@ pub(crate) struct Parser<'t> {

pos: usize,
events: Vec<Event>,

curly_level: i32,
curly_limit: Option<i32>,
}

impl<'t> Parser<'t> {
Expand All @@ -131,30 +128,14 @@ impl<'t> Parser<'t> {

pos: 0,
events: Vec::new(),
curly_level: 0,
curly_limit: None,
}
}

pub(crate) fn into_events(self) -> Vec<Event> {
assert!(self.curly_limit.is_none());
assert_eq!(self.current(), EOF);
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 {
let m = Marker {
pos: self.events.len() as u32,
Expand All @@ -172,11 +153,8 @@ impl<'t> Parser<'t> {

pub(crate) fn bump(&mut self) -> SyntaxKind {
let kind = self.current();
match kind {
L_CURLY => self.curly_level += 1,
R_CURLY => self.curly_level -= 1,
EOF => return EOF,
_ => (),
if kind == EOF {
return EOF;
}
self.pos += 1;
self.event(Event::Token {
Expand All @@ -186,30 +164,12 @@ impl<'t> Parser<'t> {
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)
}

pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> bool {
let old_level = self.curly_level;
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
pub(crate) fn current(&self) -> SyntaxKind {
self.nth(0)
}

fn event(&mut self, event: Event) {
Expand Down