From 3cd2b2473b034f290d65e3dc839c0530e55de75b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 28 Jan 2018 14:26:24 +0300 Subject: [PATCH 1/3] Drop curly_block closes #13 --- src/parser/event_parser/grammar/items.rs | 18 +++++++++- src/parser/event_parser/parser.rs | 45 +++--------------------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index 0638e3093f..812e407d14 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items.rs @@ -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); + } } diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index 573e3d73a8..d196638650 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs @@ -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, @@ -106,9 +106,6 @@ pub(crate) struct Parser<'t> { pos: usize, events: Vec, - - curly_level: i32, - curly_limit: Option, } impl<'t> Parser<'t> { @@ -131,13 +128,10 @@ impl<'t> Parser<'t> { pos: 0, events: Vec::new(), - curly_level: 0, - curly_limit: None, } } pub(crate) fn into_events(self) -> Vec { - assert!(self.curly_limit.is_none()); assert_eq!(self.current(), EOF); self.events } @@ -146,13 +140,7 @@ impl<'t> Parser<'t> { 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 + self.tokens[self.pos].kind } pub(crate) fn start(&mut self) -> Marker { @@ -172,11 +160,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 { @@ -190,28 +175,6 @@ impl<'t> Parser<'t> { self.tokens.get(self.pos + n).map(|t| t.kind).unwrap_or(EOF) } - pub(crate) fn curly_block(&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 - } - fn event(&mut self, event: Event) { self.events.push(event) } From 60725def49834bb7dfd46c1a7b84d86f810e1d03 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 28 Jan 2018 14:30:59 +0300 Subject: [PATCH 2/3] Simplify --- src/parser/event_parser/grammar/mod.rs | 22 +--------------------- src/parser/event_parser/parser.rs | 11 ++++------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs index b87f3ca8af..79a4c10d3c 100644 --- a/src/parser/event_parser/grammar/mod.rs +++ b/src/parser/event_parser/grammar/mod.rs @@ -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 @@ -77,40 +77,24 @@ 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(); - } } 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(); - } } #[derive(Clone, Copy)] @@ -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(); - } } diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index d196638650..2507af6bf9 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs @@ -136,13 +136,6 @@ impl<'t> Parser<'t> { self.events } - pub(crate) fn current(&self) -> SyntaxKind { - if self.pos == self.tokens.len() { - return EOF; - } - self.tokens[self.pos].kind - } - pub(crate) fn start(&mut self) -> Marker { let m = Marker { pos: self.events.len() as u32, @@ -175,6 +168,10 @@ impl<'t> Parser<'t> { self.tokens.get(self.pos + n).map(|t| t.kind).unwrap_or(EOF) } + pub(crate) fn current(&self) -> SyntaxKind { + self.raw_lookahead(0) + } + fn event(&mut self, event: Event) { self.events.push(event) } From 2141888782a74de4a655fee585c99163a3e75e5c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 28 Jan 2018 14:33:10 +0300 Subject: [PATCH 3/3] Rename raw_lookahead -> nth --- src/parser/event_parser/grammar/items.rs | 4 ++-- src/parser/event_parser/grammar/mod.rs | 6 +++--- src/parser/event_parser/grammar/paths.rs | 2 +- src/parser/event_parser/parser.rs | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index 812e407d14..7fed5e83b0 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items.rs @@ -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); @@ -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, _) => { diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs index 79a4c10d3c..931193b5fd 100644 --- a/src/parser/event_parser/grammar/mod.rs +++ b/src/parser/event_parser/grammar/mod.rs @@ -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 { @@ -87,13 +87,13 @@ impl Lookahead for SyntaxKind { impl Lookahead for [SyntaxKind; 2] { 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] } } 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] + p.current() == self[0] && p.nth(1) == self[1] && p.nth(2) == self[2] } } diff --git a/src/parser/event_parser/grammar/paths.rs b/src/parser/event_parser/grammar/paths.rs index 4e028073a0..6c8a89f6c6 100644 --- a/src/parser/event_parser/grammar/paths.rs +++ b/src/parser/event_parser/grammar/paths.rs @@ -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); diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index 2507af6bf9..a15d0b6335 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs @@ -164,12 +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 current(&self) -> SyntaxKind { - self.raw_lookahead(0) + self.nth(0) } fn event(&mut self, event: Event) {