mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
Boolean results
This commit is contained in:
parent
d0900b3ca7
commit
31a07da88d
4 changed files with 73 additions and 90 deletions
|
@ -1,20 +1,17 @@
|
||||||
use super::parser::Parser;
|
use super::parser::Parser;
|
||||||
|
use {SyntaxKind};
|
||||||
use syntax_kinds::*;
|
use syntax_kinds::*;
|
||||||
|
|
||||||
// Items //
|
// Items //
|
||||||
|
|
||||||
pub fn file(p: &mut Parser) {
|
pub fn file(p: &mut Parser) {
|
||||||
p.start(FILE);
|
node(p, FILE, |p| {
|
||||||
shebang(p);
|
shebang(p);
|
||||||
inner_attributes(p);
|
inner_attributes(p);
|
||||||
mod_items(p);
|
many(p, |p| skip_to_first(p, item_first, item));
|
||||||
p.finish();
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type Result = ::std::result::Result<(), ()>;
|
|
||||||
const OK: Result = Ok(());
|
|
||||||
const ERR: Result = Err(());
|
|
||||||
|
|
||||||
fn shebang(_: &mut Parser) {
|
fn shebang(_: &mut Parser) {
|
||||||
//TODO
|
//TODO
|
||||||
|
@ -24,88 +21,86 @@ fn inner_attributes(_: &mut Parser) {
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mod_items(p: &mut Parser) {
|
fn item_first(p: &Parser) -> bool {
|
||||||
loop {
|
match p.current() {
|
||||||
skip_until_item(p);
|
Some(STRUCT_KW) => true,
|
||||||
if p.is_eof() {
|
_ => false,
|
||||||
return;
|
|
||||||
}
|
|
||||||
if item(p).is_err() {
|
|
||||||
skip_one_token(p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn item(p: &mut Parser) -> Result {
|
fn item(p: &mut Parser) {
|
||||||
outer_attributes(p)?;
|
outer_attributes(p);
|
||||||
visibility(p)?;
|
visibility(p);
|
||||||
if p.current_is(STRUCT_KW) {
|
node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item);
|
||||||
p.start(STRUCT_ITEM);
|
|
||||||
p.bump();
|
|
||||||
let _ = struct_item(p);
|
|
||||||
p.finish();
|
|
||||||
return OK;
|
|
||||||
}
|
|
||||||
ERR
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_item(p: &mut Parser) -> Result {
|
fn struct_item(p: &mut Parser) {
|
||||||
p.expect(IDENT)?;
|
p.expect(IDENT)
|
||||||
p.curly_block(|p| {
|
&& p.curly_block(|p| comma_list(p, struct_field));
|
||||||
comma_list(p, struct_field)
|
}
|
||||||
|
|
||||||
|
fn struct_field(p: &mut Parser) -> bool {
|
||||||
|
node_if(p, IDENT, STRUCT_FIELD, |p| {
|
||||||
|
p.expect(COLON) && p.expect(IDENT);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_field(p: &mut Parser) -> Result {
|
|
||||||
if !p.current_is(IDENT) {
|
|
||||||
return ERR;
|
|
||||||
}
|
|
||||||
p.start(STRUCT_FIELD);
|
|
||||||
p.bump();
|
|
||||||
ignore_errors(|| {
|
|
||||||
p.expect(COLON)?;
|
|
||||||
p.expect(IDENT)?;
|
|
||||||
OK
|
|
||||||
});
|
|
||||||
p.finish();
|
|
||||||
OK
|
|
||||||
}
|
|
||||||
|
|
||||||
// Paths, types, attributes, and stuff //
|
// Paths, types, attributes, and stuff //
|
||||||
|
|
||||||
fn outer_attributes(_: &mut Parser) -> Result {
|
fn outer_attributes(_: &mut Parser) {
|
||||||
OK
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visibility(_: &mut Parser) -> Result {
|
fn visibility(_: &mut Parser) {
|
||||||
OK
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expressions //
|
// Expressions //
|
||||||
|
|
||||||
// Error recovery and high-order utils //
|
// Error recovery and high-order utils //
|
||||||
|
|
||||||
fn skip_until_item(_: &mut Parser) {
|
fn node_if<F: FnOnce(&mut Parser)>(p: &mut Parser, first: SyntaxKind, node_kind: SyntaxKind, rest: F) -> bool {
|
||||||
//TODO
|
p.current_is(first) && { node(p, node_kind, |p| { p.bump(); rest(p); }); true }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn skip_one_token(p: &mut Parser) {
|
fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) {
|
||||||
p.start(ERROR);
|
p.start(node_kind);
|
||||||
p.bump().unwrap();
|
rest(p);
|
||||||
p.finish();
|
p.finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ignore_errors<F: FnOnce() -> Result>(f: F) {
|
fn many<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
|
||||||
drop(f());
|
while f(p) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn comma_list<F: Fn(&mut Parser) -> Result>(p: &mut Parser, element: F) {
|
fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
|
||||||
|
many(p, |p| {
|
||||||
|
f(p);
|
||||||
|
p.expect(COMMA)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn skip_to_first<C, F>(p: &mut Parser, cond: C, f: F) -> bool
|
||||||
|
where
|
||||||
|
C: Fn(&Parser) -> bool,
|
||||||
|
F: FnOnce(&mut Parser),
|
||||||
|
{
|
||||||
loop {
|
loop {
|
||||||
if element(p).is_err() {
|
if cond(p) {
|
||||||
return
|
f(p);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if p.expect(COMMA).is_err() {
|
if p.bump().is_none() {
|
||||||
return
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'p> Parser<'p> {
|
||||||
|
fn current_is(&self, kind: SyntaxKind) -> bool {
|
||||||
|
self.current() == Some(kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool {
|
||||||
|
self.current_is(kind) && { self.bump(); true }
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -60,10 +60,6 @@ impl<'t> Parser<'t> {
|
||||||
Some(self.raw_tokens[idx].kind)
|
Some(self.raw_tokens[idx].kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn current_is(&self, kind: SyntaxKind) -> bool {
|
|
||||||
self.current() == Some(kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn bump(&mut self) -> Option<SyntaxKind> {
|
pub(crate) fn bump(&mut self) -> Option<SyntaxKind> {
|
||||||
let kind = self.current()?;
|
let kind = self.current()?;
|
||||||
match kind {
|
match kind {
|
||||||
|
@ -76,31 +72,23 @@ impl<'t> Parser<'t> {
|
||||||
Some(kind)
|
Some(kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expect(&mut self, kind: SyntaxKind) -> Result<(), ()> {
|
pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> bool {
|
||||||
if kind == self.current().ok_or(())? {
|
|
||||||
self.bump();
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> Result<(), ()> {
|
|
||||||
let level = self.curly_level;
|
let level = self.curly_level;
|
||||||
self.expect(L_CURLY)?;
|
if !self.expect(L_CURLY) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
f(self);
|
f(self);
|
||||||
assert!(self.curly_level > level);
|
assert!(self.curly_level > level);
|
||||||
if self.expect(R_CURLY).is_ok() {
|
if !self.expect(R_CURLY) {
|
||||||
return Ok(());
|
self.start(ERROR);
|
||||||
}
|
while self.curly_level > level {
|
||||||
self.start(ERROR);
|
if self.bump().is_none() {
|
||||||
while self.curly_level > level {
|
break;
|
||||||
if self.bump().is_none() {
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
self.finish();
|
||||||
}
|
}
|
||||||
self.finish();
|
true
|
||||||
Ok(()) //???
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event(&mut self, event: Event) {
|
fn event(&mut self, event: Event) {
|
||||||
|
|
|
@ -12,4 +12,4 @@ FILE@[0; 25)
|
||||||
WHITESPACE@[19; 20)
|
WHITESPACE@[19; 20)
|
||||||
IDENT@[20; 23)
|
IDENT@[20; 23)
|
||||||
WHITESPACE@[23; 24)
|
WHITESPACE@[23; 24)
|
||||||
R_CURLY@[24; 25)
|
R_CURLY@[24; 25)
|
||||||
|
|
|
@ -49,7 +49,7 @@ fn parser_test_case(path: &Path) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if expected.trim() == actual.trim() {
|
if expected.trim() == actual.trim() {
|
||||||
panic!("Whitespace difference!")
|
panic!("Whitespace difference! {}", path.display())
|
||||||
}
|
}
|
||||||
assert_diff!(expected, actual, "\n", 0)
|
assert_diff!(expected, actual, "\n", 0)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue