From 7b5ab0b9e6d22560f418dcd1fc5e76c19ad55234 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Mon, 4 Dec 2023 21:34:16 +0800 Subject: [PATCH] Refactor parse module in macro crate (#374) --- macro/src/parse.rs | 94 ++---------------------- macro/src/parse/dialect_operation_set.rs | 38 ++++++++++ macro/src/parse/identifier_list.rs | 26 +++++++ macro/src/parse/pass_set.rs | 38 ++++++++++ 4 files changed, 108 insertions(+), 88 deletions(-) create mode 100644 macro/src/parse/dialect_operation_set.rs create mode 100644 macro/src/parse/identifier_list.rs create mode 100644 macro/src/parse/pass_set.rs diff --git a/macro/src/parse.rs b/macro/src/parse.rs index 0f8cdbae1d..ddf099919a 100644 --- a/macro/src/parse.rs +++ b/macro/src/parse.rs @@ -1,89 +1,7 @@ -use proc_macro2::Ident; -use syn::{ - bracketed, - parse::{Parse, ParseStream}, - punctuated::Punctuated, - LitStr, Result, Token, -}; +mod dialect_operation_set; +mod identifier_list; +mod pass_set; -pub struct IdentifierList { - identifiers: Vec, -} - -impl IdentifierList { - pub fn identifiers(&self) -> &[Ident] { - &self.identifiers - } -} - -impl Parse for IdentifierList { - fn parse(input: ParseStream) -> Result { - Ok(Self { - identifiers: Punctuated::::parse_terminated(input)? - .into_iter() - .collect(), - }) - } -} - -pub struct DialectOperationSet { - dialect: Ident, - identifiers: IdentifierList, -} - -impl DialectOperationSet { - pub const fn dialect(&self) -> &Ident { - &self.dialect - } - - pub fn identifiers(&self) -> &[Ident] { - self.identifiers.identifiers() - } -} - -impl Parse for DialectOperationSet { - fn parse(input: ParseStream) -> Result { - let dialect = Ident::parse(input)?; - ::parse(input)?; - - Ok(Self { - dialect, - identifiers: { - let content; - bracketed!(content in input); - content.parse::()? - }, - }) - } -} - -pub struct PassSet { - prefix: LitStr, - identifiers: IdentifierList, -} - -impl PassSet { - pub const fn prefix(&self) -> &LitStr { - &self.prefix - } - - pub fn identifiers(&self) -> &[Ident] { - self.identifiers.identifiers() - } -} - -impl Parse for PassSet { - fn parse(input: ParseStream) -> Result { - let prefix = input.parse()?; - ::parse(input)?; - - Ok(Self { - prefix, - identifiers: { - let content; - bracketed!(content in input); - content.parse::()? - }, - }) - } -} +pub use dialect_operation_set::DialectOperationSet; +pub use identifier_list::IdentifierList; +pub use pass_set::PassSet; diff --git a/macro/src/parse/dialect_operation_set.rs b/macro/src/parse/dialect_operation_set.rs new file mode 100644 index 0000000000..1c9022f508 --- /dev/null +++ b/macro/src/parse/dialect_operation_set.rs @@ -0,0 +1,38 @@ +use super::IdentifierList; +use proc_macro2::Ident; +use syn::{ + bracketed, + parse::{Parse, ParseStream}, + Result, Token, +}; + +pub struct DialectOperationSet { + dialect: Ident, + identifiers: IdentifierList, +} + +impl DialectOperationSet { + pub const fn dialect(&self) -> &Ident { + &self.dialect + } + + pub fn identifiers(&self) -> &[Ident] { + self.identifiers.identifiers() + } +} + +impl Parse for DialectOperationSet { + fn parse(input: ParseStream) -> Result { + let dialect = Ident::parse(input)?; + ::parse(input)?; + + Ok(Self { + dialect, + identifiers: { + let content; + bracketed!(content in input); + content.parse::()? + }, + }) + } +} diff --git a/macro/src/parse/identifier_list.rs b/macro/src/parse/identifier_list.rs new file mode 100644 index 0000000000..af5a5a0742 --- /dev/null +++ b/macro/src/parse/identifier_list.rs @@ -0,0 +1,26 @@ +use proc_macro2::Ident; +use syn::{ + parse::{Parse, ParseStream}, + punctuated::Punctuated, + Result, Token, +}; + +pub struct IdentifierList { + identifiers: Vec, +} + +impl IdentifierList { + pub fn identifiers(&self) -> &[Ident] { + &self.identifiers + } +} + +impl Parse for IdentifierList { + fn parse(input: ParseStream) -> Result { + Ok(Self { + identifiers: Punctuated::::parse_terminated(input)? + .into_iter() + .collect(), + }) + } +} diff --git a/macro/src/parse/pass_set.rs b/macro/src/parse/pass_set.rs new file mode 100644 index 0000000000..1c646d055f --- /dev/null +++ b/macro/src/parse/pass_set.rs @@ -0,0 +1,38 @@ +use super::IdentifierList; +use proc_macro2::Ident; +use syn::{ + bracketed, + parse::{Parse, ParseStream}, + LitStr, Result, Token, +}; + +pub struct PassSet { + prefix: LitStr, + identifiers: IdentifierList, +} + +impl PassSet { + pub const fn prefix(&self) -> &LitStr { + &self.prefix + } + + pub fn identifiers(&self) -> &[Ident] { + self.identifiers.identifiers() + } +} + +impl Parse for PassSet { + fn parse(input: ParseStream) -> Result { + let prefix = input.parse()?; + ::parse(input)?; + + Ok(Self { + prefix, + identifiers: { + let content; + bracketed!(content in input); + content.parse::()? + }, + }) + } +}