Move use item parsing to a separate file

This commit is contained in:
Aleksey Kladov 2018-02-02 22:21:06 +03:00
parent a1fcead680
commit 7cdf990c40
3 changed files with 73 additions and 72 deletions

View File

@ -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);
@ -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 => {
@ -179,76 +180,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();

View File

@ -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);
}

View File

@ -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);