mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
commit
4212d28727
4 changed files with 30 additions and 74 deletions
|
@ -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, _) => {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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,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]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<Event>,
|
||||
|
||||
curly_level: i32,
|
||||
curly_limit: Option<i32>,
|
||||
}
|
||||
|
||||
impl<'t> Parser<'t> {
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue