query.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use proc_macro::TokenStream;
  2. use quote::{format_ident, quote};
  3. use syn::{parse_macro_input, DeriveInput};
  4. use syn::parse::{Parse,ParseStream};
  5. use std::rc::Rc;
  6. /*
  7. SELECT [(Column, Column, ...)] FROM Type
  8. [WHERE (Column = NAMED_PLACEHOLDER)]
  9. */
  10. mod kw {
  11. syn::custom_keyword!(SELECT);
  12. syn::custom_keyword!(FROM);
  13. syn::custom_keyword!(WHERE);
  14. }
  15. enum Expr {
  16. Column(syn::Ident),
  17. Constant,
  18. Placeholder(&'static str),
  19. And(Rc<Expr>, Rc<Expr>),
  20. Or(Rc<Expr>, Rc<Expr>),
  21. Equal(Rc<Expr>, Rc<Expr>),
  22. }
  23. impl Parse for Expr {
  24. fn parse(input: ParseStream) -> syn::Result<Self> {
  25. // input.peek(syn::Ident)
  26. todo!()
  27. }
  28. }
  29. type ColumnList = syn::punctuated::Punctuated<syn::Ident, syn::Token![,]>;
  30. struct Select {
  31. select_token: kw::SELECT,
  32. column_list: Option<ColumnList>,
  33. from_token: kw::FROM,
  34. what: syn::TypePath,
  35. where_token: Option<kw::WHERE>,
  36. where_clause: Option<Expr>
  37. }
  38. impl Parse for Select {
  39. fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
  40. let mut where_token = None;
  41. Ok(Self {
  42. select_token: input.parse()?,
  43. column_list: {
  44. if input.peek(kw::FROM) {
  45. None
  46. }
  47. else {
  48. Some(ColumnList::parse_separated_nonempty(input)?)
  49. }
  50. },
  51. from_token: input.parse()?,
  52. what: input.parse()?,
  53. where_token: {
  54. where_token = input.parse()?;
  55. where_token
  56. },
  57. where_clause: {
  58. if where_token.is_some() {
  59. Some(input.parse()?)
  60. }
  61. else {
  62. None
  63. }
  64. }
  65. })
  66. }
  67. }
  68. pub fn do_query(tokens: TokenStream) -> TokenStream {
  69. let input = parse_macro_input!(tokens as Select);
  70. quote!{
  71. }.into()
  72. }