Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parse module in macro crate #374

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 6 additions & 88 deletions macro/src/parse.rs
Original file line number Diff line number Diff line change
@@ -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<Ident>,
}

impl IdentifierList {
pub fn identifiers(&self) -> &[Ident] {
&self.identifiers
}
}

impl Parse for IdentifierList {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
identifiers: Punctuated::<Ident, Token![,]>::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<Self> {
let dialect = Ident::parse(input)?;
<Token![,]>::parse(input)?;

Ok(Self {
dialect,
identifiers: {
let content;
bracketed!(content in input);
content.parse::<IdentifierList>()?
},
})
}
}

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<Self> {
let prefix = input.parse()?;
<Token![,]>::parse(input)?;

Ok(Self {
prefix,
identifiers: {
let content;
bracketed!(content in input);
content.parse::<IdentifierList>()?
},
})
}
}
pub use dialect_operation_set::DialectOperationSet;
pub use identifier_list::IdentifierList;
pub use pass_set::PassSet;
38 changes: 38 additions & 0 deletions macro/src/parse/dialect_operation_set.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
let dialect = Ident::parse(input)?;
<Token![,]>::parse(input)?;

Ok(Self {
dialect,
identifiers: {
let content;
bracketed!(content in input);
content.parse::<IdentifierList>()?
},
})
}
}
26 changes: 26 additions & 0 deletions macro/src/parse/identifier_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use proc_macro2::Ident;
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
Result, Token,
};

pub struct IdentifierList {
identifiers: Vec<Ident>,
}

impl IdentifierList {
pub fn identifiers(&self) -> &[Ident] {
&self.identifiers
}
}

impl Parse for IdentifierList {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
identifiers: Punctuated::<Ident, Token![,]>::parse_terminated(input)?
.into_iter()
.collect(),
})
}
}
38 changes: 38 additions & 0 deletions macro/src/parse/pass_set.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
let prefix = input.parse()?;
<Token![,]>::parse(input)?;

Ok(Self {
prefix,
identifiers: {
let content;
bracketed!(content in input);
content.parse::<IdentifierList>()?
},
})
}
}