G: extern crate

This commit is contained in:
Aleksey Kladov 2018-01-09 00:06:42 +03:00
parent 0cf2d6afee
commit 7f8ca07d86
8 changed files with 122 additions and 62 deletions

View File

@ -8,6 +8,9 @@ Grammar(
"impl",
"true",
"false",
"as",
"extern",
"crate",
],
tokens: [
"ERROR",
@ -67,8 +70,10 @@ Grammar(
"STRUCT_ITEM",
"STRUCT_FIELD",
"FN_ITEM",
"EXTERN_CRATE_ITEM",
"ATTR",
"META_ITEM",
"LITERAL",
"ALIAS",
]
)

View File

@ -11,7 +11,7 @@ pub(super) fn mod_items(p: &mut Parser) {
fn item_first(p: &Parser) -> bool {
match p.current() {
STRUCT_KW | FN_KW => true,
STRUCT_KW | FN_KW | EXTERN_KW => true,
_ => false,
}
}
@ -41,7 +41,8 @@ fn item(p: &mut Parser) -> bool {
// || 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, STRUCT_KW, STRUCT_ITEM, struct_item)
node_if(p, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item)
|| node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item)
|| node_if(p, FN_KW, FN_ITEM, fn_item)
}
@ -50,6 +51,10 @@ fn struct_item(p: &mut Parser) {
&& p.curly_block(|p| comma_list(p, EOF, struct_field));
}
fn extern_crate_item(p: &mut Parser) {
p.expect(IDENT) && alias(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

@ -18,6 +18,13 @@ pub(crate) fn file(p: &mut Parser) {
fn visibility(_: &mut Parser) {
}
fn alias(p: &mut Parser) -> bool {
node_if(p, AS_KW, ALIAS, |p| {
p.expect(IDENT);
});
true //FIXME: return false if three are errors
}
fn node_if<F: FnOnce(&mut Parser), L: Lookahead>(
p: &mut Parser,
first: L,

View File

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

View File

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

View File

@ -13,4 +13,10 @@ WHITESPACE 1 " "
TRUE_KW 4 "true"
WHITESPACE 1 " "
FALSE_KW 5 "false"
WHITESPACE 1 " "
AS_KW 2 "as"
WHITESPACE 1 " "
EXTERN_KW 6 "extern"
WHITESPACE 1 " "
CRATE_KW 5 "crate"
WHITESPACE 1 "\n"

View File

@ -0,0 +1,2 @@
extern crate foo;
extern crate foo as bar;

View File

@ -0,0 +1,22 @@
FILE@[0; 43)
EXTERN_CRATE_ITEM@[0; 18)
EXTERN_KW@[0; 6)
WHITESPACE@[6; 7)
CRATE_KW@[7; 12)
WHITESPACE@[12; 13)
IDENT@[13; 16)
SEMI@[16; 17)
WHITESPACE@[17; 18)
EXTERN_CRATE_ITEM@[18; 43)
EXTERN_KW@[18; 24)
WHITESPACE@[24; 25)
CRATE_KW@[25; 30)
WHITESPACE@[30; 31)
IDENT@[31; 34)
ALIAS@[34; 41)
WHITESPACE@[34; 35)
AS_KW@[35; 37)
WHITESPACE@[37; 38)
IDENT@[38; 41)
SEMI@[41; 42)
WHITESPACE@[42; 43)