Skip to content

Move use #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 2 additions & 141 deletions src/parser/event_parser/grammar/items/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;

mod structs;
mod use_item;

pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
attributes::inner_attributes(p);
Expand All @@ -20,7 +21,7 @@ fn item(p: &mut Parser) {
let la = p.nth(1);
let item_kind = match p.current() {
USE_KW => {
use_item(p);
use_item::use_item(p);
USE_ITEM
}
EXTERN_KW if la == CRATE_KW => {
Expand Down Expand Up @@ -82,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();
Expand Down Expand Up @@ -179,76 +110,6 @@ fn extern_block(p: &mut Parser) {
p.expect(R_CURLY);
}

pub(super) fn is_use_tree_start(kind: SyntaxKind) -> bool {
kind == STAR || kind == L_CURLY
}

fn use_item(p: &mut Parser) {
assert!(p.at(USE_KW));
p.bump();

use_tree(p);
p.expect(SEMI);

fn use_tree(p: &mut Parser) {
let la = p.nth(1);
let m = p.start();
match (p.current(), la) {
(STAR, _) => p.bump(),
(COLONCOLON, STAR) => {
p.bump();
p.bump();
}
(L_CURLY, _) | (COLONCOLON, L_CURLY) => {
if p.at(COLONCOLON) {
p.bump();
}
nested_trees(p);
}
_ if paths::is_path_start(p) => {
paths::use_path(p);
match p.current() {
AS_KW => {
alias(p);
}
COLONCOLON => {
p.bump();
match p.current() {
STAR => {
p.bump();
}
L_CURLY => nested_trees(p),
_ => {
// is this unreachable?
p.error().message("expected `{` or `*`").emit();
}
}
}
_ => (),
}
}
_ => {
m.abandon(p);
p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super`, `indent`");
return;
}
}
m.complete(p, USE_TREE);
}

fn nested_trees(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
use_tree(p);
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);
}
}

fn abi(p: &mut Parser) {
assert!(p.at(EXTERN_KW));
let abi = p.start();
Expand Down
8 changes: 4 additions & 4 deletions src/parser/event_parser/grammar/items/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
66 changes: 66 additions & 0 deletions src/parser/event_parser/grammar/items/use_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use super::*;

pub(super) fn use_item(p: &mut Parser) {
assert!(p.at(USE_KW));
p.bump();
use_tree(p);
p.expect(SEMI);
}

fn use_tree(p: &mut Parser) {
let la = p.nth(1);
let m = p.start();
match (p.current(), la) {
(STAR, _) => p.bump(),
(COLONCOLON, STAR) => {
p.bump();
p.bump();
}
(L_CURLY, _) | (COLONCOLON, L_CURLY) => {
if p.at(COLONCOLON) {
p.bump();
}
nested_trees(p);
}
_ if paths::is_path_start(p) => {
paths::use_path(p);
match p.current() {
AS_KW => {
alias(p);
}
COLONCOLON => {
p.bump();
match p.current() {
STAR => {
p.bump();
}
L_CURLY => nested_trees(p),
_ => {
// is this unreachable?
p.error().message("expected `{` or `*`").emit();
}
}
}
_ => (),
}
}
_ => {
m.abandon(p);
p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super`, `indent`");
return;
}
}
m.complete(p, USE_TREE);
}

fn nested_trees(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
use_tree(p);
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);
}
1 change: 1 addition & 0 deletions src/parser/event_parser/grammar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion src/parser/event_parser/grammar/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ fn 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.nth(1)) {
let use_tree = match p.nth(1) {
STAR | L_CURLY => true,
_ => false,
};
if p.at(COLONCOLON) && !use_tree {
let path = qual.precede(p);
p.bump();
path_segment(p, false);
Expand Down
75 changes: 75 additions & 0 deletions src/parser/event_parser/grammar/type_params.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}