From 08f7c69f90bac772c69b3bf34877f3d9a845c541 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Jan 2018 22:00:26 +0300 Subject: G: struct flavors --- src/parser/event_parser/grammar/items.rs | 86 +++++++++++++++++-- src/parser/event_parser/grammar/mod.rs | 16 +++- src/parser/event_parser/grammar/types.rs | 5 ++ src/syntax_kinds.rs | 141 ++++++++++++++++--------------- 4 files changed, 170 insertions(+), 78 deletions(-) create mode 100644 src/parser/event_parser/grammar/types.rs (limited to 'src') diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index 2f64111ab..a14f6de77 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items.rs @@ -56,8 +56,84 @@ fn item(p: &mut Parser) -> bool { } fn struct_item(p: &mut Parser) { - p.expect(IDENT) - && p.curly_block(|p| comma_list(p, EOF, struct_field)); + if !p.expect(IDENT) { + 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) { @@ -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) { p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN) diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs index 1c57e0cb4..881bb3ef3 100644 --- a/src/parser/event_parser/grammar/mod.rs +++ b/src/parser/event_parser/grammar/mod.rs @@ -6,6 +6,7 @@ use syntax_kinds::*; mod items; mod attributes; mod expressions; +mod types; mod paths; pub(crate) fn file(p: &mut Parser) { @@ -72,12 +73,21 @@ fn many bool>(p: &mut Parser, mut f: F) { fn comma_list bool>(p: &mut Parser, end: SyntaxKind, f: F) { many(p, |p| { - if !f(p) || p.current() == end { - false + if p.current() == end { + return false + } + let pos = p.pos(); + f(p); + if p.pos() == pos { + return false + } + + if p.current() == end { + p.eat(COMMA); } else { p.expect(COMMA); - true } + true }) } diff --git a/src/parser/event_parser/grammar/types.rs b/src/parser/event_parser/grammar/types.rs new file mode 100644 index 000000000..c431643d7 --- /dev/null +++ b/src/parser/event_parser/grammar/types.rs @@ -0,0 +1,5 @@ +use super::*; + +pub(super) fn type_ref(p: &mut Parser) { + p.expect(IDENT); +} \ No newline at end of file diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index 8bc63a210..5ab8b934b 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -17,74 +17,76 @@ pub const PUB_KW: SyntaxKind = SyntaxKind(12); pub const SELF_KW: SyntaxKind = SyntaxKind(13); pub const SUPER_KW: SyntaxKind = SyntaxKind(14); 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 USE_TREE: SyntaxKind = SyntaxKind(76); -pub const PATH: SyntaxKind = SyntaxKind(77); -pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(78); -pub const LITERAL: SyntaxKind = SyntaxKind(79); -pub const ALIAS: SyntaxKind = SyntaxKind(80); -pub const VISIBILITY: SyntaxKind = SyntaxKind(81); +pub const WHERE_KW: SyntaxKind = SyntaxKind(16); +pub const ERROR: SyntaxKind = SyntaxKind(17); +pub const IDENT: SyntaxKind = SyntaxKind(18); +pub const UNDERSCORE: SyntaxKind = SyntaxKind(19); +pub const WHITESPACE: SyntaxKind = SyntaxKind(20); +pub const INT_NUMBER: SyntaxKind = SyntaxKind(21); +pub const FLOAT_NUMBER: SyntaxKind = SyntaxKind(22); +pub const SEMI: SyntaxKind = SyntaxKind(23); +pub const COMMA: SyntaxKind = SyntaxKind(24); +pub const DOT: SyntaxKind = SyntaxKind(25); +pub const DOTDOT: SyntaxKind = SyntaxKind(26); +pub const DOTDOTDOT: SyntaxKind = SyntaxKind(27); +pub const DOTDOTEQ: SyntaxKind = SyntaxKind(28); +pub const L_PAREN: SyntaxKind = SyntaxKind(29); +pub const R_PAREN: SyntaxKind = SyntaxKind(30); +pub const L_CURLY: SyntaxKind = SyntaxKind(31); +pub const R_CURLY: SyntaxKind = SyntaxKind(32); +pub const L_BRACK: SyntaxKind = SyntaxKind(33); +pub const R_BRACK: SyntaxKind = SyntaxKind(34); +pub const L_ANGLE: SyntaxKind = SyntaxKind(35); +pub const R_ANGLE: SyntaxKind = SyntaxKind(36); +pub const AT: SyntaxKind = SyntaxKind(37); +pub const POUND: SyntaxKind = SyntaxKind(38); +pub const TILDE: SyntaxKind = SyntaxKind(39); +pub const QUESTION: SyntaxKind = SyntaxKind(40); +pub const COLON: SyntaxKind = SyntaxKind(41); +pub const COLONCOLON: SyntaxKind = SyntaxKind(42); +pub const DOLLAR: SyntaxKind = SyntaxKind(43); +pub const EQ: SyntaxKind = SyntaxKind(44); +pub const EQEQ: SyntaxKind = SyntaxKind(45); +pub const FAT_ARROW: SyntaxKind = SyntaxKind(46); +pub const NEQ: SyntaxKind = SyntaxKind(47); +pub const EXCL: SyntaxKind = SyntaxKind(48); +pub const LIFETIME: SyntaxKind = SyntaxKind(49); +pub const CHAR: SyntaxKind = SyntaxKind(50); +pub const BYTE: SyntaxKind = SyntaxKind(51); +pub const STRING: SyntaxKind = SyntaxKind(52); +pub const RAW_STRING: SyntaxKind = SyntaxKind(53); +pub const BYTE_STRING: SyntaxKind = SyntaxKind(54); +pub const RAW_BYTE_STRING: SyntaxKind = SyntaxKind(55); +pub const PLUS: SyntaxKind = SyntaxKind(56); +pub const MINUS: SyntaxKind = SyntaxKind(57); +pub const STAR: SyntaxKind = SyntaxKind(58); +pub const SLASH: SyntaxKind = SyntaxKind(59); +pub const CARET: SyntaxKind = SyntaxKind(60); +pub const PERCENT: SyntaxKind = SyntaxKind(61); +pub const AMPERSAND: SyntaxKind = SyntaxKind(62); +pub const PIPE: SyntaxKind = SyntaxKind(63); +pub const THIN_ARROW: SyntaxKind = SyntaxKind(64); +pub const COMMENT: SyntaxKind = SyntaxKind(65); +pub const DOC_COMMENT: SyntaxKind = SyntaxKind(66); +pub const SHEBANG: SyntaxKind = SyntaxKind(67); +pub const FILE: SyntaxKind = SyntaxKind(68); +pub const STRUCT_ITEM: SyntaxKind = SyntaxKind(69); +pub const NAMED_FIELD: SyntaxKind = SyntaxKind(70); +pub const POS_FIELD: SyntaxKind = SyntaxKind(71); +pub const FN_ITEM: SyntaxKind = SyntaxKind(72); +pub const EXTERN_CRATE_ITEM: SyntaxKind = SyntaxKind(73); +pub const ATTR: SyntaxKind = SyntaxKind(74); +pub const META_ITEM: SyntaxKind = SyntaxKind(75); +pub const MOD_ITEM: SyntaxKind = SyntaxKind(76); +pub const USE_ITEM: SyntaxKind = SyntaxKind(77); +pub const USE_TREE: SyntaxKind = SyntaxKind(78); +pub const PATH: SyntaxKind = SyntaxKind(79); +pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(80); +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: "FN_KW" }, SyntaxInfo { name: "STRUCT_KW" }, @@ -101,6 +103,7 @@ static INFOS: [SyntaxInfo; 82] = [ SyntaxInfo { name: "SELF_KW" }, SyntaxInfo { name: "SUPER_KW" }, SyntaxInfo { name: "IN_KW" }, + SyntaxInfo { name: "WHERE_KW" }, SyntaxInfo { name: "ERROR" }, SyntaxInfo { name: "IDENT" }, SyntaxInfo { name: "UNDERSCORE" }, @@ -154,7 +157,8 @@ static INFOS: [SyntaxInfo; 82] = [ SyntaxInfo { name: "SHEBANG" }, SyntaxInfo { name: "FILE" }, SyntaxInfo { name: "STRUCT_ITEM" }, - SyntaxInfo { name: "STRUCT_FIELD" }, + SyntaxInfo { name: "NAMED_FIELD" }, + SyntaxInfo { name: "POS_FIELD" }, SyntaxInfo { name: "FN_ITEM" }, SyntaxInfo { name: "EXTERN_CRATE_ITEM" }, SyntaxInfo { name: "ATTR" }, @@ -191,6 +195,7 @@ pub(crate) fn ident_to_keyword(ident: &str) -> Option { "self" => Some(SELF_KW), "super" => Some(SUPER_KW), "in" => Some(IN_KW), + "where" => Some(WHERE_KW), _ => None, } } -- cgit v1.2.3