Drop more high-order stuff

This commit is contained in:
Aleksey Kladov 2018-01-20 21:49:58 +03:00
parent 111743d82c
commit be60d5aa66
5 changed files with 100 additions and 93 deletions

View file

@ -1,5 +1,6 @@
use super::*;
#[derive(PartialEq, Eq)]
enum AttrKind {
Inner, Outer
}
@ -14,18 +15,27 @@ pub(super) fn outer_attributes(p: &mut Parser) {
fn attribute(p: &mut Parser, kind: AttrKind) -> bool {
fn attr_tail(p: &mut Parser) {
meta_item(p) && p.expect(R_BRACK);
}
match kind {
AttrKind::Inner => node_if(p, [POUND, EXCL, L_BRACK], ATTR, attr_tail),
AttrKind::Outer => node_if(p, [POUND, L_BRACK], ATTR, attr_tail),
if p.at(POUND) {
if kind == AttrKind::Inner && p.raw_lookahead(1) != EXCL {
return false;
}
p.start(ATTR);
p.bump();
if kind == AttrKind::Inner {
p.bump();
}
p.expect(L_BRACK) && meta_item(p) && p.expect(R_BRACK);
p.finish();
true
} else {
false
}
}
fn meta_item(p: &mut Parser) -> bool {
node_if(p, IDENT, META_ITEM, |p| {
if p.at(IDENT) {
p.start(META_ITEM);
p.bump();
if p.eat(EQ) {
if !expressions::literal(p) {
p.error()
@ -36,7 +46,12 @@ fn meta_item(p: &mut Parser) -> bool {
comma_list(p, R_PAREN, meta_item_inner);
p.expect(R_PAREN);
}
})
p.finish();
true
} else {
false
}
}
fn meta_item_inner(p: &mut Parser) -> bool {

View file

@ -1,11 +1,16 @@
use super::*;
pub(super) fn literal(p: &mut Parser) -> bool {
let literals = [
TRUE_KW, FALSE_KW,
INT_NUMBER, FLOAT_NUMBER,
BYTE, CHAR,
STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING,
];
node_if(p, AnyOf(&literals), LITERAL, |_| ())
}
match p.current() {
TRUE_KW | FALSE_KW |
INT_NUMBER | FLOAT_NUMBER |
BYTE | CHAR |
STRING | RAW_STRING | BYTE_STRING | RAW_BYTE_STRING => {
p.start(LITERAL);
p.bump();
p.finish();
true
}
_ => false
}
}

View file

@ -7,7 +7,7 @@ pub(super) fn mod_contents(p: &mut Parser) {
}
}
fn item(p: &mut Parser){
fn item(p: &mut Parser) {
let attrs_start = p.mark();
attributes::outer_attributes(p);
visibility(p);
@ -51,7 +51,7 @@ fn struct_item(p: &mut Parser) {
fn struct_inner(p: &mut Parser) {
if !p.expect(IDENT) {
p.finish();
return
return;
}
generic_parameters(p);
match p.current() {
@ -60,31 +60,31 @@ fn struct_item(p: &mut Parser) {
match p.current() {
SEMI => {
p.bump();
return
return;
}
L_CURLY => named_fields(p),
_ => { //TODO: special case `(` error message
p.error()
.message("expected `;` or `{`")
.emit();
return
return;
}
}
}
SEMI => {
p.bump();
return
return;
}
L_CURLY => named_fields(p),
L_PAREN => {
tuple_fields(p);
p.expect(SEMI);
},
}
_ => {
p.error()
.message("expected `;`, `{`, or `(`")
.emit();
return
return;
}
}
}
@ -108,7 +108,7 @@ fn named_fields(p: &mut Parser) {
fn tuple_fields(p: &mut Parser) {
if !p.expect(L_PAREN) {
return
return;
}
comma_list(p, R_PAREN, |p| {
tuple_field(p);
@ -124,11 +124,9 @@ fn tuple_fields(p: &mut Parser) {
}
}
fn generic_parameters(_: &mut Parser) {
}
fn generic_parameters(_: &mut Parser) {}
fn where_clause(_: &mut Parser) {
}
fn where_clause(_: &mut Parser) {}
fn extern_crate_item(p: &mut Parser) {
p.start(EXTERN_CRATE_ITEM);
@ -168,24 +166,29 @@ fn use_item(p: &mut Parser) {
p.expect(SEMI);
p.finish();
fn use_tree(p: &mut Parser) -> bool{
if node_if(p, STAR, USE_TREE, |_| ()) {
return true
}
if node_if(p, [COLONCOLON, STAR], USE_TREE, |_| ()) {
return true
}
if [COLONCOLON, L_CURLY].is_ahead(p) || L_CURLY.is_ahead(p) {
node(p, USE_TREE, |p| {
p.eat(COLONCOLON);
fn use_tree(p: &mut Parser) -> bool {
let la = p.raw_lookahead(1);
match (p.current(), la) {
(STAR, _) => {
p.start(USE_TREE);
p.bump();
}
(COLONCOLON, STAR) => {
p.start(USE_TREE);
p.bump();
p.bump();
}
(L_CURLY, _) | (COLONCOLON, L_CURLY) => {
p.start(USE_TREE);
if p.at(COLONCOLON) {
p.bump();
}
p.curly_block(|p| {
comma_list(p, EOF, use_tree);
});
});
return true;
}
if paths::is_path_start(p) {
node(p, USE_TREE, |p| {
}
_ if paths::is_path_start(p) => {
p.start(USE_TREE);
paths::use_path(p);
match p.current() {
AS_KW => {
@ -212,15 +215,15 @@ fn use_item(p: &mut Parser) {
}
_ => (),
}
});
return true;
}
_ => return false,
}
false
p.finish();
return true;
}
}
fn fn_item(p: &mut Parser) {
p.start(FN_ITEM);

View file

@ -46,21 +46,6 @@ fn alias(p: &mut Parser) -> bool {
true //FIXME: return false if three are errors
}
fn node_if<F: FnOnce(&mut Parser), L: Lookahead>(
p: &mut Parser,
first: L,
node_kind: SyntaxKind,
rest: F
) -> bool {
first.is_ahead(p) && { node(p, node_kind, |p| { L::consume(p); rest(p); }); true }
}
fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) {
p.start(node_kind);
rest(p);
p.finish();
}
fn repeat<F: FnMut(&mut Parser) -> bool>(p: &mut Parser, mut f: F) {
loop {
let pos = p.pos();

View file

@ -1,6 +1,6 @@
use super::*;
pub (crate) fn is_path_start(p: &Parser) -> bool {
pub(crate) fn is_path_start(p: &Parser) -> bool {
AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p)
}
@ -9,39 +9,38 @@ pub(crate) fn use_path(p: &mut Parser) {
return;
}
let mut prev = p.mark();
node(p, PATH, |p| {
path_segment(p, true);
});
repeat(p, |p| {
p.start(PATH);
path_segment(p, true);
p.finish();
loop {
let curr = p.mark();
if p.current() == COLONCOLON && !items::is_use_tree_start(p.raw_lookahead(1)) {
node(p, PATH, |p| {
p.bump();
path_segment(p, false);
p.forward_parent(prev, curr);
prev = curr;
});
true
if p.at(COLONCOLON) && !items::is_use_tree_start(p.raw_lookahead(1)) {
p.start(PATH);
p.bump();
path_segment(p, false);
p.forward_parent(prev, curr);
prev = curr;
p.finish();
} else {
false
break;
}
});
}
}
fn path_segment(p: &mut Parser, first: bool) {
node(p, PATH_SEGMENT, |p| {
if first {
p.eat(COLONCOLON);
p.start(PATH_SEGMENT);
if first {
p.eat(COLONCOLON);
}
match p.current() {
IDENT | SELF_KW | SUPER_KW => {
p.bump();
}
match p.current() {
IDENT | SELF_KW | SUPER_KW => {
p.bump();
},
_ => {
p.error()
.message("expected identifier")
.emit();
}
};
})
}
_ => {
p.error()
.message("expected identifier")
.emit();
}
};
p.finish();
}