G: mod item

This commit is contained in:
Aleksey Kladov 2018-01-09 22:35:55 +03:00
parent 7f8ca07d86
commit 1544e89c49
8 changed files with 180 additions and 69 deletions

View file

@ -11,6 +11,7 @@ Grammar(
"as", "as",
"extern", "extern",
"crate", "crate",
"mod",
], ],
tokens: [ tokens: [
"ERROR", "ERROR",
@ -73,6 +74,7 @@ Grammar(
"EXTERN_CRATE_ITEM", "EXTERN_CRATE_ITEM",
"ATTR", "ATTR",
"META_ITEM", "META_ITEM",
"MOD_ITEM",
"LITERAL", "LITERAL",
"ALIAS", "ALIAS",
] ]

View file

@ -1,9 +1,10 @@
use super::*; use super::*;
pub(super) fn mod_items(p: &mut Parser) { pub(super) fn mod_contents(p: &mut Parser) {
attributes::inner_attributes(p);
many(p, |p| { many(p, |p| {
skip_to_first( skip_to_first(
p, item_first, mod_item, p, item_first, mod_contents_item,
"expected item", "expected item",
) )
}); });
@ -11,12 +12,12 @@ pub(super) fn mod_items(p: &mut Parser) {
fn item_first(p: &Parser) -> bool { fn item_first(p: &Parser) -> bool {
match p.current() { match p.current() {
STRUCT_KW | FN_KW | EXTERN_KW => true, STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW => true,
_ => false, _ => false,
} }
} }
fn mod_item(p: &mut Parser) { fn mod_contents_item(p: &mut Parser) {
if item(p) { if item(p) {
if p.current() == SEMI { if p.current() == SEMI {
node(p, ERROR, |p| { node(p, ERROR, |p| {
@ -39,9 +40,9 @@ fn item(p: &mut Parser) -> bool {
// || node_if(p, CONST_KW, CONST_ITEM, const_item) or const FN! // || node_if(p, CONST_KW, CONST_ITEM, const_item) or const FN!
// || unsafe trait, impl // || unsafe trait, impl
// || node_if(p, FN_KW, FN_ITEM, fn_item) // || node_if(p, FN_KW, FN_ITEM, fn_item)
// || node_if(p, MOD_KW, MOD_ITEM, mod_item)
// || node_if(p, TYPE_KW, TYPE_ITEM, type_item) // || 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, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item)
|| node_if(p, MOD_KW, MOD_ITEM, mod_item)
|| node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item) || node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item)
|| node_if(p, FN_KW, FN_ITEM, fn_item) || node_if(p, FN_KW, FN_ITEM, fn_item)
} }
@ -55,6 +56,16 @@ fn extern_crate_item(p: &mut Parser) {
p.expect(IDENT) && alias(p) && p.expect(SEMI); p.expect(IDENT) && alias(p) && p.expect(SEMI);
} }
fn mod_item(p: &mut Parser) {
if !p.expect(IDENT) {
return;
}
if p.eat(SEMI) {
return;
}
p.curly_block(mod_contents);
}
fn struct_field(p: &mut Parser) -> bool { fn struct_field(p: &mut Parser) -> bool {
node_if(p, IDENT, STRUCT_FIELD, |p| { node_if(p, IDENT, STRUCT_FIELD, |p| {
p.expect(COLON) && p.expect(IDENT); p.expect(COLON) && p.expect(IDENT);

View file

@ -10,8 +10,7 @@ mod expressions;
pub(crate) fn file(p: &mut Parser) { pub(crate) fn file(p: &mut Parser) {
node(p, FILE, |p| { node(p, FILE, |p| {
p.optional(SHEBANG); p.optional(SHEBANG);
attributes::inner_attributes(p); items::mod_contents(p);
items::mod_items(p);
}) })
} }

View file

@ -12,68 +12,70 @@ pub const FALSE_KW: SyntaxKind = SyntaxKind(7);
pub const AS_KW: SyntaxKind = SyntaxKind(8); pub const AS_KW: SyntaxKind = SyntaxKind(8);
pub const EXTERN_KW: SyntaxKind = SyntaxKind(9); pub const EXTERN_KW: SyntaxKind = SyntaxKind(9);
pub const CRATE_KW: SyntaxKind = SyntaxKind(10); pub const CRATE_KW: SyntaxKind = SyntaxKind(10);
pub const ERROR: SyntaxKind = SyntaxKind(11); pub const MOD_KW: SyntaxKind = SyntaxKind(11);
pub const IDENT: SyntaxKind = SyntaxKind(12); pub const ERROR: SyntaxKind = SyntaxKind(12);
pub const UNDERSCORE: SyntaxKind = SyntaxKind(13); pub const IDENT: SyntaxKind = SyntaxKind(13);
pub const WHITESPACE: SyntaxKind = SyntaxKind(14); pub const UNDERSCORE: SyntaxKind = SyntaxKind(14);
pub const INT_NUMBER: SyntaxKind = SyntaxKind(15); pub const WHITESPACE: SyntaxKind = SyntaxKind(15);
pub const FLOAT_NUMBER: SyntaxKind = SyntaxKind(16); pub const INT_NUMBER: SyntaxKind = SyntaxKind(16);
pub const SEMI: SyntaxKind = SyntaxKind(17); pub const FLOAT_NUMBER: SyntaxKind = SyntaxKind(17);
pub const COMMA: SyntaxKind = SyntaxKind(18); pub const SEMI: SyntaxKind = SyntaxKind(18);
pub const DOT: SyntaxKind = SyntaxKind(19); pub const COMMA: SyntaxKind = SyntaxKind(19);
pub const DOTDOT: SyntaxKind = SyntaxKind(20); pub const DOT: SyntaxKind = SyntaxKind(20);
pub const DOTDOTDOT: SyntaxKind = SyntaxKind(21); pub const DOTDOT: SyntaxKind = SyntaxKind(21);
pub const DOTDOTEQ: SyntaxKind = SyntaxKind(22); pub const DOTDOTDOT: SyntaxKind = SyntaxKind(22);
pub const L_PAREN: SyntaxKind = SyntaxKind(23); pub const DOTDOTEQ: SyntaxKind = SyntaxKind(23);
pub const R_PAREN: SyntaxKind = SyntaxKind(24); pub const L_PAREN: SyntaxKind = SyntaxKind(24);
pub const L_CURLY: SyntaxKind = SyntaxKind(25); pub const R_PAREN: SyntaxKind = SyntaxKind(25);
pub const R_CURLY: SyntaxKind = SyntaxKind(26); pub const L_CURLY: SyntaxKind = SyntaxKind(26);
pub const L_BRACK: SyntaxKind = SyntaxKind(27); pub const R_CURLY: SyntaxKind = SyntaxKind(27);
pub const R_BRACK: SyntaxKind = SyntaxKind(28); pub const L_BRACK: SyntaxKind = SyntaxKind(28);
pub const L_ANGLE: SyntaxKind = SyntaxKind(29); pub const R_BRACK: SyntaxKind = SyntaxKind(29);
pub const R_ANGLE: SyntaxKind = SyntaxKind(30); pub const L_ANGLE: SyntaxKind = SyntaxKind(30);
pub const AT: SyntaxKind = SyntaxKind(31); pub const R_ANGLE: SyntaxKind = SyntaxKind(31);
pub const POUND: SyntaxKind = SyntaxKind(32); pub const AT: SyntaxKind = SyntaxKind(32);
pub const TILDE: SyntaxKind = SyntaxKind(33); pub const POUND: SyntaxKind = SyntaxKind(33);
pub const QUESTION: SyntaxKind = SyntaxKind(34); pub const TILDE: SyntaxKind = SyntaxKind(34);
pub const COLON: SyntaxKind = SyntaxKind(35); pub const QUESTION: SyntaxKind = SyntaxKind(35);
pub const COLONCOLON: SyntaxKind = SyntaxKind(36); pub const COLON: SyntaxKind = SyntaxKind(36);
pub const DOLLAR: SyntaxKind = SyntaxKind(37); pub const COLONCOLON: SyntaxKind = SyntaxKind(37);
pub const EQ: SyntaxKind = SyntaxKind(38); pub const DOLLAR: SyntaxKind = SyntaxKind(38);
pub const EQEQ: SyntaxKind = SyntaxKind(39); pub const EQ: SyntaxKind = SyntaxKind(39);
pub const FAT_ARROW: SyntaxKind = SyntaxKind(40); pub const EQEQ: SyntaxKind = SyntaxKind(40);
pub const NEQ: SyntaxKind = SyntaxKind(41); pub const FAT_ARROW: SyntaxKind = SyntaxKind(41);
pub const EXCL: SyntaxKind = SyntaxKind(42); pub const NEQ: SyntaxKind = SyntaxKind(42);
pub const LIFETIME: SyntaxKind = SyntaxKind(43); pub const EXCL: SyntaxKind = SyntaxKind(43);
pub const CHAR: SyntaxKind = SyntaxKind(44); pub const LIFETIME: SyntaxKind = SyntaxKind(44);
pub const BYTE: SyntaxKind = SyntaxKind(45); pub const CHAR: SyntaxKind = SyntaxKind(45);
pub const STRING: SyntaxKind = SyntaxKind(46); pub const BYTE: SyntaxKind = SyntaxKind(46);
pub const RAW_STRING: SyntaxKind = SyntaxKind(47); pub const STRING: SyntaxKind = SyntaxKind(47);
pub const BYTE_STRING: SyntaxKind = SyntaxKind(48); pub const RAW_STRING: SyntaxKind = SyntaxKind(48);
pub const RAW_BYTE_STRING: SyntaxKind = SyntaxKind(49); pub const BYTE_STRING: SyntaxKind = SyntaxKind(49);
pub const PLUS: SyntaxKind = SyntaxKind(50); pub const RAW_BYTE_STRING: SyntaxKind = SyntaxKind(50);
pub const MINUS: SyntaxKind = SyntaxKind(51); pub const PLUS: SyntaxKind = SyntaxKind(51);
pub const STAR: SyntaxKind = SyntaxKind(52); pub const MINUS: SyntaxKind = SyntaxKind(52);
pub const SLASH: SyntaxKind = SyntaxKind(53); pub const STAR: SyntaxKind = SyntaxKind(53);
pub const CARET: SyntaxKind = SyntaxKind(54); pub const SLASH: SyntaxKind = SyntaxKind(54);
pub const PERCENT: SyntaxKind = SyntaxKind(55); pub const CARET: SyntaxKind = SyntaxKind(55);
pub const AMPERSAND: SyntaxKind = SyntaxKind(56); pub const PERCENT: SyntaxKind = SyntaxKind(56);
pub const PIPE: SyntaxKind = SyntaxKind(57); pub const AMPERSAND: SyntaxKind = SyntaxKind(57);
pub const THIN_ARROW: SyntaxKind = SyntaxKind(58); pub const PIPE: SyntaxKind = SyntaxKind(58);
pub const COMMENT: SyntaxKind = SyntaxKind(59); pub const THIN_ARROW: SyntaxKind = SyntaxKind(59);
pub const DOC_COMMENT: SyntaxKind = SyntaxKind(60); pub const COMMENT: SyntaxKind = SyntaxKind(60);
pub const SHEBANG: SyntaxKind = SyntaxKind(61); pub const DOC_COMMENT: SyntaxKind = SyntaxKind(61);
pub const FILE: SyntaxKind = SyntaxKind(62); pub const SHEBANG: SyntaxKind = SyntaxKind(62);
pub const STRUCT_ITEM: SyntaxKind = SyntaxKind(63); pub const FILE: SyntaxKind = SyntaxKind(63);
pub const STRUCT_FIELD: SyntaxKind = SyntaxKind(64); pub const STRUCT_ITEM: SyntaxKind = SyntaxKind(64);
pub const FN_ITEM: SyntaxKind = SyntaxKind(65); pub const STRUCT_FIELD: SyntaxKind = SyntaxKind(65);
pub const EXTERN_CRATE_ITEM: SyntaxKind = SyntaxKind(66); pub const FN_ITEM: SyntaxKind = SyntaxKind(66);
pub const ATTR: SyntaxKind = SyntaxKind(67); pub const EXTERN_CRATE_ITEM: SyntaxKind = SyntaxKind(67);
pub const META_ITEM: SyntaxKind = SyntaxKind(68); pub const ATTR: SyntaxKind = SyntaxKind(68);
pub const LITERAL: SyntaxKind = SyntaxKind(69); pub const META_ITEM: SyntaxKind = SyntaxKind(69);
pub const ALIAS: SyntaxKind = SyntaxKind(70); pub const MOD_ITEM: SyntaxKind = SyntaxKind(70);
pub const LITERAL: SyntaxKind = SyntaxKind(71);
pub const ALIAS: SyntaxKind = SyntaxKind(72);
static INFOS: [SyntaxInfo; 71] = [ static INFOS: [SyntaxInfo; 73] = [
SyntaxInfo { name: "USE_KW" }, SyntaxInfo { name: "USE_KW" },
SyntaxInfo { name: "FN_KW" }, SyntaxInfo { name: "FN_KW" },
SyntaxInfo { name: "STRUCT_KW" }, SyntaxInfo { name: "STRUCT_KW" },
@ -85,6 +87,7 @@ static INFOS: [SyntaxInfo; 71] = [
SyntaxInfo { name: "AS_KW" }, SyntaxInfo { name: "AS_KW" },
SyntaxInfo { name: "EXTERN_KW" }, SyntaxInfo { name: "EXTERN_KW" },
SyntaxInfo { name: "CRATE_KW" }, SyntaxInfo { name: "CRATE_KW" },
SyntaxInfo { name: "MOD_KW" },
SyntaxInfo { name: "ERROR" }, SyntaxInfo { name: "ERROR" },
SyntaxInfo { name: "IDENT" }, SyntaxInfo { name: "IDENT" },
SyntaxInfo { name: "UNDERSCORE" }, SyntaxInfo { name: "UNDERSCORE" },
@ -143,6 +146,7 @@ static INFOS: [SyntaxInfo; 71] = [
SyntaxInfo { name: "EXTERN_CRATE_ITEM" }, SyntaxInfo { name: "EXTERN_CRATE_ITEM" },
SyntaxInfo { name: "ATTR" }, SyntaxInfo { name: "ATTR" },
SyntaxInfo { name: "META_ITEM" }, SyntaxInfo { name: "META_ITEM" },
SyntaxInfo { name: "MOD_ITEM" },
SyntaxInfo { name: "LITERAL" }, SyntaxInfo { name: "LITERAL" },
SyntaxInfo { name: "ALIAS" }, SyntaxInfo { name: "ALIAS" },
]; ];
@ -164,6 +168,7 @@ pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {
"as" => Some(AS_KW), "as" => Some(AS_KW),
"extern" => Some(EXTERN_KW), "extern" => Some(EXTERN_KW),
"crate" => Some(CRATE_KW), "crate" => Some(CRATE_KW),
"mod" => Some(MOD_KW),
_ => None, _ => None,
} }
} }

View file

@ -1 +1 @@
fn use struct trait enum impl true false as extern crate fn use struct trait enum impl true false as extern crate mod

View file

@ -19,4 +19,6 @@ WHITESPACE 1 " "
EXTERN_KW 6 "extern" EXTERN_KW 6 "extern"
WHITESPACE 1 " " WHITESPACE 1 " "
CRATE_KW 5 "crate" CRATE_KW 5 "crate"
WHITESPACE 1 " "
MOD_KW 3 "mod"
WHITESPACE 1 "\n" WHITESPACE 1 "\n"

View file

@ -0,0 +1,17 @@
mod a;
mod b {
}
mod c {
fn foo() {
}
struct S {}
}
mod d {
#![attr]
mod e;
mod f {
}
}

View file

@ -0,0 +1,75 @@
FILE@[0; 118)
MOD_ITEM@[0; 8)
MOD_KW@[0; 3)
WHITESPACE@[3; 4)
IDENT@[4; 5)
SEMI@[5; 6)
WHITESPACE@[6; 8)
MOD_ITEM@[8; 19)
MOD_KW@[8; 11)
WHITESPACE@[11; 12)
IDENT@[12; 13)
WHITESPACE@[13; 14)
L_CURLY@[14; 15)
WHITESPACE@[15; 16)
R_CURLY@[16; 17)
WHITESPACE@[17; 19)
MOD_ITEM@[19; 67)
MOD_KW@[19; 22)
WHITESPACE@[22; 23)
IDENT@[23; 24)
WHITESPACE@[24; 25)
L_CURLY@[25; 26)
FN_ITEM@[26; 52)
WHITESPACE@[26; 31)
FN_KW@[31; 33)
WHITESPACE@[33; 34)
IDENT@[34; 37)
L_PAREN@[37; 38)
R_PAREN@[38; 39)
WHITESPACE@[39; 40)
L_CURLY@[40; 41)
WHITESPACE@[41; 46)
R_CURLY@[46; 47)
WHITESPACE@[47; 52)
STRUCT_ITEM@[52; 64)
STRUCT_KW@[52; 58)
WHITESPACE@[58; 59)
IDENT@[59; 60)
WHITESPACE@[60; 61)
L_CURLY@[61; 62)
R_CURLY@[62; 63)
WHITESPACE@[63; 64)
R_CURLY@[64; 65)
WHITESPACE@[65; 67)
MOD_ITEM@[67; 118)
MOD_KW@[67; 70)
WHITESPACE@[70; 71)
IDENT@[71; 72)
WHITESPACE@[72; 73)
L_CURLY@[73; 74)
ATTR@[74; 92)
WHITESPACE@[74; 79)
POUND@[79; 80)
EXCL@[80; 81)
L_BRACK@[81; 82)
META_ITEM@[82; 86)
IDENT@[82; 86)
R_BRACK@[86; 87)
WHITESPACE@[87; 92)
MOD_ITEM@[92; 103)
MOD_KW@[92; 95)
WHITESPACE@[95; 96)
IDENT@[96; 97)
SEMI@[97; 98)
WHITESPACE@[98; 103)
MOD_ITEM@[103; 117)
MOD_KW@[103; 106)
WHITESPACE@[106; 107)
IDENT@[107; 108)
WHITESPACE@[108; 109)
L_CURLY@[109; 110)
WHITESPACE@[110; 115)
R_CURLY@[115; 116)
WHITESPACE@[116; 117)
R_CURLY@[117; 118)