From 50a02eb3593591a02677e1b56f24d7ff0459b9d0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 17:06:49 +0200 Subject: Rename ra_parser -> parser --- crates/parser/src/grammar/items/adt.rs | 178 +++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 crates/parser/src/grammar/items/adt.rs (limited to 'crates/parser/src/grammar/items/adt.rs') diff --git a/crates/parser/src/grammar/items/adt.rs b/crates/parser/src/grammar/items/adt.rs new file mode 100644 index 000000000..addfb59d4 --- /dev/null +++ b/crates/parser/src/grammar/items/adt.rs @@ -0,0 +1,178 @@ +//! FIXME: write short doc here + +use super::*; + +pub(super) fn struct_def(p: &mut Parser, m: Marker) { + assert!(p.at(T![struct])); + p.bump(T![struct]); + struct_or_union(p, m, T![struct], STRUCT); +} + +pub(super) fn union_def(p: &mut Parser, m: Marker) { + assert!(p.at_contextual_kw("union")); + p.bump_remap(T![union]); + struct_or_union(p, m, T![union], UNION); +} + +fn struct_or_union(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { + name_r(p, ITEM_RECOVERY_SET); + type_params::opt_type_param_list(p); + match p.current() { + T![where] => { + type_params::opt_where_clause(p); + match p.current() { + T![;] => { + p.bump(T![;]); + } + T!['{'] => record_field_def_list(p), + _ => { + //FIXME: special case `(` error message + p.error("expected `;` or `{`"); + } + } + } + T![;] if kw == T![struct] => { + p.bump(T![;]); + } + T!['{'] => record_field_def_list(p), + T!['('] if kw == T![struct] => { + tuple_field_def_list(p); + // test tuple_struct_where + // struct Test(T) where T: Clone; + // struct Test(T); + type_params::opt_where_clause(p); + p.expect(T![;]); + } + _ if kw == T![struct] => { + p.error("expected `;`, `{`, or `(`"); + } + _ => { + p.error("expected `{`"); + } + } + m.complete(p, def); +} + +pub(super) fn enum_def(p: &mut Parser, m: Marker) { + assert!(p.at(T![enum])); + p.bump(T![enum]); + name_r(p, ITEM_RECOVERY_SET); + type_params::opt_type_param_list(p); + type_params::opt_where_clause(p); + if p.at(T!['{']) { + enum_variant_list(p); + } else { + p.error("expected `{`") + } + m.complete(p, ENUM); +} + +pub(crate) fn enum_variant_list(p: &mut Parser) { + assert!(p.at(T!['{'])); + let m = p.start(); + p.bump(T!['{']); + while !p.at(EOF) && !p.at(T!['}']) { + if p.at(T!['{']) { + error_block(p, "expected enum variant"); + continue; + } + let var = p.start(); + attributes::outer_attributes(p); + if p.at(IDENT) { + name(p); + match p.current() { + T!['{'] => record_field_def_list(p), + T!['('] => tuple_field_def_list(p), + _ => (), + } + + // test variant_discriminant + // enum E { X(i32) = 10 } + if p.eat(T![=]) { + expressions::expr(p); + } + var.complete(p, VARIANT); + } else { + var.abandon(p); + p.err_and_bump("expected enum variant"); + } + if !p.at(T!['}']) { + p.expect(T![,]); + } + } + p.expect(T!['}']); + m.complete(p, VARIANT_LIST); +} + +pub(crate) fn record_field_def_list(p: &mut Parser) { + assert!(p.at(T!['{'])); + let m = p.start(); + p.bump(T!['{']); + while !p.at(T!['}']) && !p.at(EOF) { + if p.at(T!['{']) { + error_block(p, "expected field"); + continue; + } + record_field_def(p); + if !p.at(T!['}']) { + p.expect(T![,]); + } + } + p.expect(T!['}']); + m.complete(p, RECORD_FIELD_LIST); + + fn record_field_def(p: &mut Parser) { + let m = p.start(); + // test record_field_attrs + // struct S { + // #[serde(with = "url_serde")] + // pub uri: Uri, + // } + attributes::outer_attributes(p); + opt_visibility(p); + if p.at(IDENT) { + name(p); + p.expect(T![:]); + types::type_(p); + m.complete(p, RECORD_FIELD); + } else { + m.abandon(p); + p.err_and_bump("expected field declaration"); + } + } +} + +fn tuple_field_def_list(p: &mut Parser) { + assert!(p.at(T!['('])); + let m = p.start(); + if !p.expect(T!['(']) { + return; + } + while !p.at(T![')']) && !p.at(EOF) { + let m = p.start(); + // test tuple_field_attrs + // struct S ( + // #[serde(with = "url_serde")] + // pub Uri, + // ); + // + // enum S { + // Uri(#[serde(with = "url_serde")] Uri), + // } + attributes::outer_attributes(p); + opt_visibility(p); + if !p.at_ts(types::TYPE_FIRST) { + p.error("expected a type"); + m.complete(p, ERROR); + break; + } + types::type_(p); + m.complete(p, TUPLE_FIELD); + + if !p.at(T![')']) { + p.expect(T![,]); + } + } + p.expect(T![')']); + m.complete(p, TUPLE_FIELD_LIST); +} -- cgit v1.2.3 From 6bc2633c90cedad057c5201d1ab7f67b57247004 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 17:58:35 +0200 Subject: Align parser names with grammar --- crates/parser/src/grammar/items/adt.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'crates/parser/src/grammar/items/adt.rs') diff --git a/crates/parser/src/grammar/items/adt.rs b/crates/parser/src/grammar/items/adt.rs index addfb59d4..67c0c5697 100644 --- a/crates/parser/src/grammar/items/adt.rs +++ b/crates/parser/src/grammar/items/adt.rs @@ -2,13 +2,13 @@ use super::*; -pub(super) fn struct_def(p: &mut Parser, m: Marker) { +pub(super) fn strukt(p: &mut Parser, m: Marker) { assert!(p.at(T![struct])); p.bump(T![struct]); struct_or_union(p, m, T![struct], STRUCT); } -pub(super) fn union_def(p: &mut Parser, m: Marker) { +pub(super) fn union(p: &mut Parser, m: Marker) { assert!(p.at_contextual_kw("union")); p.bump_remap(T![union]); struct_or_union(p, m, T![union], UNION); @@ -16,7 +16,7 @@ pub(super) fn union_def(p: &mut Parser, m: Marker) { fn struct_or_union(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { name_r(p, ITEM_RECOVERY_SET); - type_params::opt_type_param_list(p); + type_params::opt_generic_param_list(p); match p.current() { T![where] => { type_params::opt_where_clause(p); @@ -24,7 +24,7 @@ fn struct_or_union(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { T![;] => { p.bump(T![;]); } - T!['{'] => record_field_def_list(p), + T!['{'] => record_field_list(p), _ => { //FIXME: special case `(` error message p.error("expected `;` or `{`"); @@ -34,9 +34,9 @@ fn struct_or_union(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { T![;] if kw == T![struct] => { p.bump(T![;]); } - T!['{'] => record_field_def_list(p), + T!['{'] => record_field_list(p), T!['('] if kw == T![struct] => { - tuple_field_def_list(p); + tuple_field_list(p); // test tuple_struct_where // struct Test(T) where T: Clone; // struct Test(T); @@ -53,21 +53,21 @@ fn struct_or_union(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { m.complete(p, def); } -pub(super) fn enum_def(p: &mut Parser, m: Marker) { +pub(super) fn enum_(p: &mut Parser, m: Marker) { assert!(p.at(T![enum])); p.bump(T![enum]); name_r(p, ITEM_RECOVERY_SET); - type_params::opt_type_param_list(p); + type_params::opt_generic_param_list(p); type_params::opt_where_clause(p); if p.at(T!['{']) { - enum_variant_list(p); + variant_list(p); } else { p.error("expected `{`") } m.complete(p, ENUM); } -pub(crate) fn enum_variant_list(p: &mut Parser) { +pub(crate) fn variant_list(p: &mut Parser) { assert!(p.at(T!['{'])); let m = p.start(); p.bump(T!['{']); @@ -77,12 +77,12 @@ pub(crate) fn enum_variant_list(p: &mut Parser) { continue; } let var = p.start(); - attributes::outer_attributes(p); + attributes::outer_attrs(p); if p.at(IDENT) { name(p); match p.current() { - T!['{'] => record_field_def_list(p), - T!['('] => tuple_field_def_list(p), + T!['{'] => record_field_list(p), + T!['('] => tuple_field_list(p), _ => (), } @@ -104,7 +104,7 @@ pub(crate) fn enum_variant_list(p: &mut Parser) { m.complete(p, VARIANT_LIST); } -pub(crate) fn record_field_def_list(p: &mut Parser) { +pub(crate) fn record_field_list(p: &mut Parser) { assert!(p.at(T!['{'])); let m = p.start(); p.bump(T!['{']); @@ -128,7 +128,7 @@ pub(crate) fn record_field_def_list(p: &mut Parser) { // #[serde(with = "url_serde")] // pub uri: Uri, // } - attributes::outer_attributes(p); + attributes::outer_attrs(p); opt_visibility(p); if p.at(IDENT) { name(p); @@ -142,7 +142,7 @@ pub(crate) fn record_field_def_list(p: &mut Parser) { } } -fn tuple_field_def_list(p: &mut Parser) { +fn tuple_field_list(p: &mut Parser) { assert!(p.at(T!['('])); let m = p.start(); if !p.expect(T!['(']) { @@ -159,7 +159,7 @@ fn tuple_field_def_list(p: &mut Parser) { // enum S { // Uri(#[serde(with = "url_serde")] Uri), // } - attributes::outer_attributes(p); + attributes::outer_attrs(p); opt_visibility(p); if !p.at_ts(types::TYPE_FIRST) { p.error("expected a type"); -- cgit v1.2.3