G: simplest use items

This commit is contained in:
Aleksey Kladov 2018-01-09 23:32:18 +03:00
parent 1544e89c49
commit 5ea7e5fb7a
7 changed files with 55 additions and 11 deletions

View file

@ -75,6 +75,9 @@ Grammar(
"ATTR",
"META_ITEM",
"MOD_ITEM",
"USE_ITEM",
"PATH",
"PATH_SEGMENT",
"LITERAL",
"ALIAS",
]

View file

@ -12,7 +12,7 @@ pub(super) fn mod_contents(p: &mut Parser) {
fn item_first(p: &Parser) -> bool {
match p.current() {
STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW => true,
STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW | USE_KW => true,
_ => false,
}
}
@ -43,6 +43,7 @@ fn item(p: &mut Parser) -> bool {
// || node_if(p, TYPE_KW, TYPE_ITEM, type_item)
node_if(p, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item)
|| node_if(p, MOD_KW, MOD_ITEM, mod_item)
|| node_if(p, USE_KW, USE_ITEM, use_item)
|| node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item)
|| node_if(p, FN_KW, FN_ITEM, fn_item)
}
@ -66,6 +67,11 @@ fn mod_item(p: &mut Parser) {
p.curly_block(mod_contents);
}
fn use_item(p: &mut Parser) {
paths::use_path(p);
p.expect(SEMI);
}
fn struct_field(p: &mut Parser) -> bool {
node_if(p, IDENT, STRUCT_FIELD, |p| {
p.expect(COLON) && p.expect(IDENT);

View file

@ -6,10 +6,11 @@ use syntax_kinds::*;
mod items;
mod attributes;
mod expressions;
mod paths;
pub(crate) fn file(p: &mut Parser) {
node(p, FILE, |p| {
p.optional(SHEBANG);
p.eat(SHEBANG);
items::mod_contents(p);
})
}
@ -99,12 +100,6 @@ impl<'p> Parser<'p> {
}
}
fn optional(&mut self, kind: SyntaxKind) {
if self.current() == kind {
self.bump();
}
}
fn eat(&mut self, kind: SyntaxKind) -> bool {
self.current() == kind && { self.bump(); true }
}

View file

@ -0,0 +1,15 @@
use super::*;
pub(crate) fn use_path(p: &mut Parser) {
if !AnyOf(&[IDENT, COLONCOLON]).is_ahead(p) {
return;
}
node(p, PATH, |p| {
p.eat(COLONCOLON);
path_segment(p);
})
}
fn path_segment(p: &mut Parser) -> bool {
node_if(p, IDENT, PATH_SEGMENT, |p| ())
}

View file

@ -72,10 +72,13 @@ pub const EXTERN_CRATE_ITEM: SyntaxKind = SyntaxKind(67);
pub const ATTR: SyntaxKind = SyntaxKind(68);
pub const META_ITEM: SyntaxKind = SyntaxKind(69);
pub const MOD_ITEM: SyntaxKind = SyntaxKind(70);
pub const LITERAL: SyntaxKind = SyntaxKind(71);
pub const ALIAS: SyntaxKind = SyntaxKind(72);
pub const USE_ITEM: SyntaxKind = SyntaxKind(71);
pub const PATH: SyntaxKind = SyntaxKind(72);
pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(73);
pub const LITERAL: SyntaxKind = SyntaxKind(74);
pub const ALIAS: SyntaxKind = SyntaxKind(75);
static INFOS: [SyntaxInfo; 73] = [
static INFOS: [SyntaxInfo; 76] = [
SyntaxInfo { name: "USE_KW" },
SyntaxInfo { name: "FN_KW" },
SyntaxInfo { name: "STRUCT_KW" },
@ -147,6 +150,9 @@ static INFOS: [SyntaxInfo; 73] = [
SyntaxInfo { name: "ATTR" },
SyntaxInfo { name: "META_ITEM" },
SyntaxInfo { name: "MOD_ITEM" },
SyntaxInfo { name: "USE_ITEM" },
SyntaxInfo { name: "PATH" },
SyntaxInfo { name: "PATH_SEGMENT" },
SyntaxInfo { name: "LITERAL" },
SyntaxInfo { name: "ALIAS" },
];

View file

@ -0,0 +1,2 @@
use foo;
use ::bar;

View file

@ -0,0 +1,17 @@
FILE@[0; 19)
USE_ITEM@[0; 9)
USE_KW@[0; 3)
PATH@[3; 7)
PATH_SEGMENT@[3; 7)
WHITESPACE@[3; 4)
IDENT@[4; 7)
SEMI@[7; 8)
WHITESPACE@[8; 9)
USE_ITEM@[9; 19)
USE_KW@[9; 12)
PATH@[12; 18)
WHITESPACE@[12; 13)
COLONCOLON@[13; 15)
PATH_SEGMENT@[15; 18)
IDENT@[15; 18)
SEMI@[18; 19)