G: allow self & super in paths

This commit is contained in:
Aleksey Kladov 2018-01-13 11:55:03 +03:00
parent f31d858607
commit 29b2e0adcc
10 changed files with 156 additions and 72 deletions

View file

@ -15,6 +15,7 @@ Grammar(
"pub",
"self",
"super",
"in",
],
tokens: [
"ERROR",

View file

@ -1,7 +1,7 @@
use super::*;
pub(crate) fn use_path(p: &mut Parser) {
if !AnyOf(&[IDENT, COLONCOLON]).is_ahead(p) {
if !AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p) {
return;
}
let mut prev = p.mark();
@ -23,6 +23,15 @@ fn path_segment(p: &mut Parser, first: bool) {
if first {
p.eat(COLONCOLON);
}
p.expect(IDENT);
match p.current() {
IDENT | SELF_KW | SUPER_KW => {
p.bump();
},
_ => {
p.error()
.message("expected identifier")
.emit();
}
};
})
}

View file

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

View file

@ -1,2 +1 @@
fn use struct trait enum impl true false as extern crate mod pub self super
fn use struct trait enum impl true false as extern crate mod pub self super in

View file

@ -27,4 +27,6 @@ WHITESPACE 1 " "
SELF_KW 4 "self"
WHITESPACE 1 " "
SUPER_KW 5 "super"
WHITESPACE 2 "\n\n"
WHITESPACE 1 " "
IN_KW 2 "in"
WHITESPACE 1 "\n"

View file

@ -0,0 +1 @@
use foo::92;

View file

@ -0,0 +1,16 @@
FILE@[0; 12)
USE_ITEM@[0; 9)
USE_KW@[0; 3)
PATH@[3; 9)
PATH@[3; 7)
PATH_SEGMENT@[3; 7)
WHITESPACE@[3; 4)
IDENT@[4; 7)
COLONCOLON@[7; 9)
PATH_SEGMENT@[9; 9)
err: `expected identifier`
err: `expected SEMI`
ERROR@[9; 12)
err: `expected item`
INT_NUMBER@[9; 11)
SEMI@[11; 12)

View file

@ -58,7 +58,7 @@ FILE@[0; 98)
VISIBILITY@[66; 88)
PUB_KW@[66; 69)
L_PAREN@[69; 70)
IDENT@[70; 72)
IN_KW@[70; 72)
PATH@[72; 86)
PATH@[72; 81)
PATH@[72; 76)

View file

@ -0,0 +1,3 @@
use self::foo;
use super::super::bar;
use ::self::a::super::bar;

View file

@ -0,0 +1,50 @@
FILE@[0; 65)
USE_ITEM@[0; 15)
USE_KW@[0; 3)
PATH@[3; 13)
PATH@[3; 8)
PATH_SEGMENT@[3; 8)
WHITESPACE@[3; 4)
SELF_KW@[4; 8)
COLONCOLON@[8; 10)
PATH_SEGMENT@[10; 13)
IDENT@[10; 13)
SEMI@[13; 14)
WHITESPACE@[14; 15)
USE_ITEM@[15; 38)
USE_KW@[15; 18)
PATH@[18; 36)
PATH@[18; 31)
PATH@[18; 24)
PATH_SEGMENT@[18; 24)
WHITESPACE@[18; 19)
SUPER_KW@[19; 24)
COLONCOLON@[24; 26)
PATH_SEGMENT@[26; 31)
SUPER_KW@[26; 31)
COLONCOLON@[31; 33)
PATH_SEGMENT@[33; 36)
IDENT@[33; 36)
SEMI@[36; 37)
WHITESPACE@[37; 38)
USE_ITEM@[38; 65)
USE_KW@[38; 41)
PATH@[41; 63)
PATH@[41; 58)
PATH@[41; 51)
PATH@[41; 48)
PATH_SEGMENT@[41; 48)
WHITESPACE@[41; 42)
COLONCOLON@[42; 44)
SELF_KW@[44; 48)
COLONCOLON@[48; 50)
PATH_SEGMENT@[50; 51)
IDENT@[50; 51)
COLONCOLON@[51; 53)
PATH_SEGMENT@[53; 58)
SUPER_KW@[53; 58)
COLONCOLON@[58; 60)
PATH_SEGMENT@[60; 63)
IDENT@[60; 63)
SEMI@[63; 64)
WHITESPACE@[64; 65)