G: struct flavors

This commit is contained in:
Aleksey Kladov 2018-01-13 22:00:26 +03:00
parent 55891be06a
commit 08f7c69f90
12 changed files with 257 additions and 85 deletions

View file

@ -16,6 +16,7 @@ Grammar(
"self", "self",
"super", "super",
"in", "in",
"where",
], ],
tokens: [ tokens: [
"ERROR", "ERROR",
@ -73,7 +74,8 @@ Grammar(
nodes: [ nodes: [
"FILE", "FILE",
"STRUCT_ITEM", "STRUCT_ITEM",
"STRUCT_FIELD", "NAMED_FIELD",
"POS_FIELD",
"FN_ITEM", "FN_ITEM",
"EXTERN_CRATE_ITEM", "EXTERN_CRATE_ITEM",
"ATTR", "ATTR",

View file

@ -56,8 +56,84 @@ fn item(p: &mut Parser) -> bool {
} }
fn struct_item(p: &mut Parser) { fn struct_item(p: &mut Parser) {
p.expect(IDENT) if !p.expect(IDENT) {
&& p.curly_block(|p| comma_list(p, EOF, struct_field)); return
}
generic_parameters(p);
match p.current() {
WHERE_KW => {
where_clause(p);
match p.current() {
SEMI => {
p.bump();
return
}
L_CURLY => named_fields(p),
_ => { //TODO: special case `(` error message
p.error()
.message("expected `;` or `{`")
.emit();
return
}
}
}
SEMI => {
p.bump();
return
}
L_CURLY => named_fields(p),
L_PAREN => {
tuple_fields(p);
p.expect(SEMI);
},
_ => {
p.error()
.message("expected `;`, `{`, or `(`")
.emit();
return
}
}
}
fn named_fields(p: &mut Parser) {
p.curly_block(|p| comma_list(p, EOF, |p| {
named_field(p);
true
}));
fn named_field(p: &mut Parser) {
node(p, NAMED_FIELD, |p| {
visibility(p);
p.expect(IDENT) && p.expect(COLON) && {
types::type_ref(p);
true
};
})
}
}
fn tuple_fields(p: &mut Parser) {
if !p.expect(L_PAREN) {
return
}
comma_list(p, R_PAREN, |p| {
tuple_field(p);
true
});
p.expect(R_PAREN);
fn tuple_field(p: &mut Parser) {
node(p, POS_FIELD, |p| {
visibility(p);
types::type_ref(p);
})
}
}
fn generic_parameters(_: &mut Parser) {
}
fn where_clause(_: &mut Parser) {
} }
fn extern_crate_item(p: &mut Parser) { fn extern_crate_item(p: &mut Parser) {
@ -133,11 +209,7 @@ fn use_item(p: &mut Parser) {
} }
} }
fn struct_field(p: &mut Parser) -> bool {
node_if(p, IDENT, STRUCT_FIELD, |p| {
p.expect(COLON) && p.expect(IDENT);
})
}
fn fn_item(p: &mut Parser) { fn fn_item(p: &mut Parser) {
p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN) p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN)

View file

@ -6,6 +6,7 @@ use syntax_kinds::*;
mod items; mod items;
mod attributes; mod attributes;
mod expressions; mod expressions;
mod types;
mod paths; mod paths;
pub(crate) fn file(p: &mut Parser) { pub(crate) fn file(p: &mut Parser) {
@ -72,12 +73,21 @@ fn many<F: FnMut(&mut Parser) -> bool>(p: &mut Parser, mut f: F) {
fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) { fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) {
many(p, |p| { many(p, |p| {
if !f(p) || p.current() == end { if p.current() == end {
false return false
}
let pos = p.pos();
f(p);
if p.pos() == pos {
return false
}
if p.current() == end {
p.eat(COMMA);
} else { } else {
p.expect(COMMA); p.expect(COMMA);
true
} }
true
}) })
} }

View file

@ -0,0 +1,5 @@
use super::*;
pub(super) fn type_ref(p: &mut Parser) {
p.expect(IDENT);
}

View file

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

View file

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

View file

@ -29,4 +29,6 @@ WHITESPACE 1 " "
SUPER_KW 5 "super" SUPER_KW 5 "super"
WHITESPACE 1 " " WHITESPACE 1 " "
IN_KW 2 "in" IN_KW 2 "in"
WHITESPACE 1 " "
WHERE_KW 5 "where"
WHITESPACE 1 "\n" WHITESPACE 1 "\n"

View file

@ -5,7 +5,7 @@ FILE@[0; 34)
IDENT@[7; 8) IDENT@[7; 8)
WHITESPACE@[8; 9) WHITESPACE@[8; 9)
L_CURLY@[9; 10) L_CURLY@[9; 10)
STRUCT_FIELD@[10; 26) NAMED_FIELD@[10; 26)
WHITESPACE@[10; 15) WHITESPACE@[10; 15)
IDENT@[15; 16) IDENT@[15; 16)
COLON@[16; 17) COLON@[16; 17)
@ -13,7 +13,7 @@ FILE@[0; 34)
IDENT@[18; 21) IDENT@[18; 21)
WHITESPACE@[21; 26) WHITESPACE@[21; 26)
err: `expected COMMA` err: `expected COMMA`
STRUCT_FIELD@[26; 33) NAMED_FIELD@[26; 33)
IDENT@[26; 27) IDENT@[26; 27)
COLON@[27; 28) COLON@[27; 28)
WHITESPACE@[28; 29) WHITESPACE@[28; 29)

View file

@ -5,14 +5,14 @@ FILE@[0; 40)
IDENT@[7; 8) IDENT@[7; 8)
WHITESPACE@[8; 9) WHITESPACE@[8; 9)
L_CURLY@[9; 10) L_CURLY@[9; 10)
STRUCT_FIELD@[10; 21) NAMED_FIELD@[10; 21)
WHITESPACE@[10; 15) WHITESPACE@[10; 15)
IDENT@[15; 16) IDENT@[15; 16)
COLON@[16; 17) COLON@[16; 17)
WHITESPACE@[17; 18) WHITESPACE@[17; 18)
IDENT@[18; 21) IDENT@[18; 21)
COMMA@[21; 22) COMMA@[21; 22)
STRUCT_FIELD@[22; 36) NAMED_FIELD@[22; 36)
WHITESPACE@[22; 27) WHITESPACE@[22; 27)
IDENT@[27; 28) IDENT@[27; 28)
COLON@[28; 29) COLON@[28; 29)

View file

@ -5,7 +5,7 @@ FILE@[0; 25)
IDENT@[7; 8) IDENT@[7; 8)
WHITESPACE@[8; 9) WHITESPACE@[8; 9)
L_CURLY@[9; 10) L_CURLY@[9; 10)
STRUCT_FIELD@[10; 24) NAMED_FIELD@[10; 24)
WHITESPACE@[10; 15) WHITESPACE@[10; 15)
IDENT@[15; 18) IDENT@[15; 18)
COLON@[18; 19) COLON@[18; 19)

View file

@ -0,0 +1,10 @@
struct A;
struct B {}
struct C();
struct D {
a: u32,
pub b: u32
}
struct E(pub x, y,);

View file

@ -0,0 +1,66 @@
FILE@[0; 97)
STRUCT_ITEM@[0; 10)
STRUCT_KW@[0; 6)
WHITESPACE@[6; 7)
IDENT@[7; 8)
SEMI@[8; 9)
WHITESPACE@[9; 10)
STRUCT_ITEM@[10; 22)
STRUCT_KW@[10; 16)
WHITESPACE@[16; 17)
IDENT@[17; 18)
WHITESPACE@[18; 19)
L_CURLY@[19; 20)
R_CURLY@[20; 21)
WHITESPACE@[21; 22)
STRUCT_ITEM@[22; 35)
STRUCT_KW@[22; 28)
WHITESPACE@[28; 29)
IDENT@[29; 30)
L_PAREN@[30; 31)
R_PAREN@[31; 32)
SEMI@[32; 33)
WHITESPACE@[33; 35)
STRUCT_ITEM@[35; 76)
STRUCT_KW@[35; 41)
WHITESPACE@[41; 42)
IDENT@[42; 43)
WHITESPACE@[43; 44)
L_CURLY@[44; 45)
NAMED_FIELD@[45; 56)
WHITESPACE@[45; 50)
IDENT@[50; 51)
COLON@[51; 52)
WHITESPACE@[52; 53)
IDENT@[53; 56)
COMMA@[56; 57)
NAMED_FIELD@[57; 73)
VISIBILITY@[57; 66)
WHITESPACE@[57; 62)
PUB_KW@[62; 65)
WHITESPACE@[65; 66)
IDENT@[66; 67)
COLON@[67; 68)
WHITESPACE@[68; 69)
IDENT@[69; 72)
WHITESPACE@[72; 73)
R_CURLY@[73; 74)
WHITESPACE@[74; 76)
STRUCT_ITEM@[76; 97)
STRUCT_KW@[76; 82)
WHITESPACE@[82; 83)
IDENT@[83; 84)
L_PAREN@[84; 85)
POS_FIELD@[85; 90)
VISIBILITY@[85; 89)
PUB_KW@[85; 88)
WHITESPACE@[88; 89)
IDENT@[89; 90)
COMMA@[90; 91)
POS_FIELD@[91; 93)
WHITESPACE@[91; 92)
IDENT@[92; 93)
COMMA@[93; 94)
R_PAREN@[94; 95)
SEMI@[95; 96)
WHITESPACE@[96; 97)