Move type parameter parsing to a separate file

This commit is contained in:
Aleksey Kladov 2018-02-02 23:23:39 +03:00
parent 7cdf990c40
commit be20b014d9
4 changed files with 80 additions and 74 deletions

View file

@ -83,76 +83,6 @@ fn item(p: &mut Parser) {
item.complete(p, item_kind);
}
fn type_param_list(p: &mut Parser) {
if !p.at(L_ANGLE) {
return;
}
let m = p.start();
p.bump();
while !p.at(EOF) && !p.at(R_ANGLE) {
match p.current() {
LIFETIME => lifetime_param(p),
IDENT => type_param(p),
_ => p.err_and_bump("expected type parameter"),
}
if !p.at(R_ANGLE) && !p.expect(COMMA) {
break;
}
}
p.expect(R_ANGLE);
m.complete(p, TYPE_PARAM_LIST);
fn lifetime_param(p: &mut Parser) {
assert!(p.at(LIFETIME));
let m = p.start();
p.bump();
if p.eat(COLON) {
while p.at(LIFETIME) {
p.bump();
if !p.eat(PLUS) {
break;
}
}
}
m.complete(p, LIFETIME_PARAM);
}
fn type_param(p: &mut Parser) {
assert!(p.at(IDENT));
let m = p.start();
p.bump();
if p.eat(COLON) {
loop {
let has_paren = p.eat(L_PAREN);
p.eat(QUESTION);
if p.at(FOR_KW) {
//TODO
}
if p.at(LIFETIME) {
p.bump();
} else if paths::is_path_start(p) {
paths::type_path(p);
} else {
break;
}
if has_paren {
p.expect(R_PAREN);
}
if !p.eat(PLUS) {
break;
}
}
}
if p.at(EQ) {
types::type_ref(p)
}
m.complete(p, TYPE_PARAM);
}
}
fn where_clause(_: &mut Parser) {}
fn extern_crate_item(p: &mut Parser) {
assert!(p.at(EXTERN_KW));
p.bump();

View file

@ -7,10 +7,10 @@ pub(super) fn struct_item(p: &mut Parser) {
if !p.expect(IDENT) {
return;
}
type_param_list(p);
type_params::list(p);
match p.current() {
WHERE_KW => {
where_clause(p);
type_params::where_clause(p);
match p.current() {
SEMI => {
p.bump();
@ -44,8 +44,8 @@ pub(super) fn enum_item(p: &mut Parser) {
assert!(p.at(ENUM_KW));
p.bump();
p.expect(IDENT);
type_param_list(p);
where_clause(p);
type_params::list(p);
type_params::where_clause(p);
if p.expect(L_CURLY) {
while !p.at(EOF) && !p.at(R_CURLY) {
let var = p.start();

View file

@ -7,6 +7,7 @@ mod attributes;
mod expressions;
mod types;
mod paths;
mod type_params;
pub(crate) fn file(p: &mut Parser) {
let file = p.start();

View file

@ -0,0 +1,75 @@
use super::*;
pub(super) fn list(p: &mut Parser) {
if !p.at(L_ANGLE) {
return;
}
let m = p.start();
p.bump();
while !p.at(EOF) && !p.at(R_ANGLE) {
match p.current() {
LIFETIME => lifetime_param(p),
IDENT => type_param(p),
_ => p.err_and_bump("expected type parameter"),
}
if !p.at(R_ANGLE) && !p.expect(COMMA) {
break;
}
}
p.expect(R_ANGLE);
m.complete(p, TYPE_PARAM_LIST);
fn lifetime_param(p: &mut Parser) {
assert!(p.at(LIFETIME));
let m = p.start();
p.bump();
if p.eat(COLON) {
while p.at(LIFETIME) {
p.bump();
if !p.eat(PLUS) {
break;
}
}
}
m.complete(p, LIFETIME_PARAM);
}
fn type_param(p: &mut Parser) {
assert!(p.at(IDENT));
let m = p.start();
p.bump();
if p.eat(COLON) {
loop {
let has_paren = p.eat(L_PAREN);
p.eat(QUESTION);
if p.at(FOR_KW) {
//TODO
}
if p.at(LIFETIME) {
p.bump();
} else if paths::is_path_start(p) {
paths::type_path(p);
} else {
break;
}
if has_paren {
p.expect(R_PAREN);
}
if !p.eat(PLUS) {
break;
}
}
}
if p.at(EQ) {
types::type_ref(p)
}
m.complete(p, TYPE_PARAM);
}
}
pub(super) fn where_clause(p: &mut Parser) {
if p.at(WHERE_KW) {
p.bump();
}
}