diff --git a/grammar.ron b/grammar.ron index 8418ed2a55..c475826e4c 100644 --- a/grammar.ron +++ b/grammar.ron @@ -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", ] ) \ No newline at end of file diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index 68b444b698..9b174679c4 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items.rs @@ -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); diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs index 78309aa832..dd1270eae8 100644 --- a/src/parser/event_parser/grammar/mod.rs +++ b/src/parser/event_parser/grammar/mod.rs @@ -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( p: &mut Parser, first: L, diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index b82a4f7f24..6db82051ea 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -9,66 +9,71 @@ pub const TRAIT_KW: SyntaxKind = SyntaxKind(4); 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 @@ static INFOS: [SyntaxInfo; 66] = [ 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 @@ static INFOS: [SyntaxInfo; 66] = [ 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 { "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, } } diff --git a/tests/data/lexer/0011_keywords.rs b/tests/data/lexer/0011_keywords.rs index 1f4a4a74c4..4adb1587e4 100644 --- a/tests/data/lexer/0011_keywords.rs +++ b/tests/data/lexer/0011_keywords.rs @@ -1 +1 @@ -fn use struct trait enum impl true false +fn use struct trait enum impl true false as extern crate diff --git a/tests/data/lexer/0011_keywords.txt b/tests/data/lexer/0011_keywords.txt index eded370847..bf8ceffd49 100644 --- a/tests/data/lexer/0011_keywords.txt +++ b/tests/data/lexer/0011_keywords.txt @@ -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" diff --git a/tests/data/parser/ok/0007_extern_crate.rs b/tests/data/parser/ok/0007_extern_crate.rs new file mode 100644 index 0000000000..3ce3366765 --- /dev/null +++ b/tests/data/parser/ok/0007_extern_crate.rs @@ -0,0 +1,2 @@ +extern crate foo; +extern crate foo as bar; diff --git a/tests/data/parser/ok/0007_extern_crate.txt b/tests/data/parser/ok/0007_extern_crate.txt new file mode 100644 index 0000000000..902c80f7db --- /dev/null +++ b/tests/data/parser/ok/0007_extern_crate.txt @@ -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)