Skip to content

Commit 4212d28

Browse files
committed
Merge #25
25: Drop curly_block r=matklad a=matklad closes #13
2 parents 7a6fa65 + 2141888 commit 4212d28

File tree

4 files changed

+30
-74
lines changed

4 files changed

+30
-74
lines changed

src/parser/event_parser/grammar/items.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn item(p: &mut Parser) {
1414
let item = p.start();
1515
attributes::outer_attributes(p);
1616
visibility(p);
17-
let la = p.raw_lookahead(1);
17+
let la = p.nth(1);
1818
let item_kind = match p.current() {
1919
EXTERN_KW if la == CRATE_KW => {
2020
extern_crate_item(p);
@@ -171,7 +171,7 @@ fn use_item(p: &mut Parser) {
171171
p.expect(SEMI);
172172

173173
fn use_tree(p: &mut Parser) {
174-
let la = p.raw_lookahead(1);
174+
let la = p.nth(1);
175175
let m = p.start();
176176
match (p.current(), la) {
177177
(STAR, _) => {
@@ -235,5 +235,21 @@ fn fn_item(p: &mut Parser) {
235235
assert!(p.at(FN_KW));
236236
p.bump();
237237

238-
p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN) && p.curly_block(|_| ());
238+
p.expect(IDENT);
239+
if p.at(L_PAREN) {
240+
fn_value_parameters(p);
241+
} else {
242+
p.error().message("expected function arguments").emit();
243+
}
244+
245+
if p.at(L_CURLY) {
246+
p.expect(L_CURLY);
247+
p.expect(R_CURLY);
248+
}
249+
250+
fn fn_value_parameters(p: &mut Parser) {
251+
assert!(p.at(L_PAREN));
252+
p.bump();
253+
p.expect(R_PAREN);
254+
}
239255
}

src/parser/event_parser/grammar/mod.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn visibility(p: &mut Parser) {
2020
let vis = p.start();
2121
p.bump();
2222
if p.at(L_PAREN) {
23-
match p.raw_lookahead(1) {
23+
match p.nth(1) {
2424
CRATE_KW | SELF_KW | SUPER_KW | IN_KW => {
2525
p.bump();
2626
if p.bump() == IN_KW {
@@ -57,7 +57,7 @@ impl<'p> Parser<'p> {
5757
err.complete(self, ERROR);
5858
}
5959

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

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

8382
impl Lookahead for SyntaxKind {
8483
fn is_ahead(self, p: &Parser) -> bool {
8584
p.current() == self
8685
}
87-
88-
fn consume(p: &mut Parser) {
89-
p.bump();
90-
}
9186
}
9287

9388
impl Lookahead for [SyntaxKind; 2] {
9489
fn is_ahead(self, p: &Parser) -> bool {
95-
p.current() == self[0] && p.raw_lookahead(1) == self[1]
96-
}
97-
98-
fn consume(p: &mut Parser) {
99-
p.bump();
100-
p.bump();
90+
p.current() == self[0] && p.nth(1) == self[1]
10191
}
10292
}
10393

10494
impl Lookahead for [SyntaxKind; 3] {
10595
fn is_ahead(self, p: &Parser) -> bool {
106-
p.current() == self[0] && p.raw_lookahead(1) == self[1] && p.raw_lookahead(2) == self[2]
107-
}
108-
109-
fn consume(p: &mut Parser) {
110-
p.bump();
111-
p.bump();
112-
p.bump();
96+
p.current() == self[0] && p.nth(1) == self[1] && p.nth(2) == self[2]
11397
}
11498
}
11599

@@ -121,8 +105,4 @@ impl<'a> Lookahead for AnyOf<'a> {
121105
let curr = p.current();
122106
self.0.iter().any(|&k| k == curr)
123107
}
124-
125-
fn consume(p: &mut Parser) {
126-
p.bump();
127-
}
128108
}

src/parser/event_parser/grammar/paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) fn use_path(p: &mut Parser) {
1212
path_segment(p, true);
1313
let mut qual = path.complete(p, PATH);
1414
loop {
15-
if p.at(COLONCOLON) && !items::is_use_tree_start(p.raw_lookahead(1)) {
15+
if p.at(COLONCOLON) && !items::is_use_tree_start(p.nth(1)) {
1616
let path = qual.precede(p);
1717
p.bump();
1818
path_segment(p, false);

src/parser/event_parser/parser.rs

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use {SyntaxKind, TextUnit, Token};
22
use super::Event;
33
use super::super::is_insignificant;
4-
use SyntaxKind::{EOF, ERROR, L_CURLY, R_CURLY, TOMBSTONE};
4+
use SyntaxKind::{EOF, TOMBSTONE};
55

66
pub(crate) struct Marker {
77
pos: u32,
@@ -106,9 +106,6 @@ pub(crate) struct Parser<'t> {
106106

107107
pos: usize,
108108
events: Vec<Event>,
109-
110-
curly_level: i32,
111-
curly_limit: Option<i32>,
112109
}
113110

114111
impl<'t> Parser<'t> {
@@ -131,30 +128,14 @@ impl<'t> Parser<'t> {
131128

132129
pos: 0,
133130
events: Vec::new(),
134-
curly_level: 0,
135-
curly_limit: None,
136131
}
137132
}
138133

139134
pub(crate) fn into_events(self) -> Vec<Event> {
140-
assert!(self.curly_limit.is_none());
141135
assert_eq!(self.current(), EOF);
142136
self.events
143137
}
144138

145-
pub(crate) fn current(&self) -> SyntaxKind {
146-
if self.pos == self.tokens.len() {
147-
return EOF;
148-
}
149-
let token = self.tokens[self.pos];
150-
if let Some(limit) = self.curly_limit {
151-
if limit == self.curly_level && token.kind == R_CURLY {
152-
return EOF;
153-
}
154-
}
155-
token.kind
156-
}
157-
158139
pub(crate) fn start(&mut self) -> Marker {
159140
let m = Marker {
160141
pos: self.events.len() as u32,
@@ -172,11 +153,8 @@ impl<'t> Parser<'t> {
172153

173154
pub(crate) fn bump(&mut self) -> SyntaxKind {
174155
let kind = self.current();
175-
match kind {
176-
L_CURLY => self.curly_level += 1,
177-
R_CURLY => self.curly_level -= 1,
178-
EOF => return EOF,
179-
_ => (),
156+
if kind == EOF {
157+
return EOF;
180158
}
181159
self.pos += 1;
182160
self.event(Event::Token {
@@ -186,30 +164,12 @@ impl<'t> Parser<'t> {
186164
kind
187165
}
188166

189-
pub(crate) fn raw_lookahead(&self, n: usize) -> SyntaxKind {
167+
pub(crate) fn nth(&self, n: usize) -> SyntaxKind {
190168
self.tokens.get(self.pos + n).map(|t| t.kind).unwrap_or(EOF)
191169
}
192170

193-
pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> bool {
194-
let old_level = self.curly_level;
195-
let old_limit = self.curly_limit;
196-
if !self.expect(L_CURLY) {
197-
return false;
198-
}
199-
self.curly_limit = Some(self.curly_level);
200-
f(self);
201-
assert!(self.curly_level > old_level);
202-
self.curly_limit = old_limit;
203-
if !self.expect(R_CURLY) {
204-
let err = self.start();
205-
while self.curly_level > old_level {
206-
if self.bump() == EOF {
207-
break;
208-
}
209-
}
210-
err.complete(self, ERROR);
211-
}
212-
true
171+
pub(crate) fn current(&self) -> SyntaxKind {
172+
self.nth(0)
213173
}
214174

215175
fn event(&mut self, event: Event) {

0 commit comments

Comments
 (0)