Fix calling macros with zero arguments

This commit is contained in:
Rob Watson 2020-11-13 10:19:53 +01:00
parent d4d441d4e3
commit 95d1266752
2 changed files with 116 additions and 70 deletions

View File

@ -1,34 +1,46 @@
//! weblog-proc-macro contains procedural macro definitions for the
//! [`weblog`](https://crates.io/crates/weblog) crate. See its documentation for more information.
extern crate proc_macro2; extern crate proc_macro2;
mod weblog_impl; mod weblog_impl;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use std::iter; use std::iter;
use weblog_impl::{quote_console_func, ArgMode, ConsoleFunc}; use syn::parse_macro_input;
use weblog_impl::{quote_console_func, ArgMode, ConsoleFunc, InputTokens};
/// Call the browser's `console.assert()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/assert)
#[proc_macro] #[proc_macro]
pub fn console_assert(input: TokenStream) -> TokenStream { pub fn console_assert(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("assert_with_condition_and_data", 1), ConsoleFunc::variadic("assert_with_condition_and_data", 1),
iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::IntoJsValue)), iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::IntoJsValue)),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.clear()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/clear)
#[proc_macro] #[proc_macro]
pub fn console_clear(input: TokenStream) -> TokenStream { pub fn console_clear(input: TokenStream) -> TokenStream {
quote_console_func(ConsoleFunc::fixed("clear", 0), iter::empty(), input) let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::fixed("clear", 0),
iter::empty(),
crate_name,
args,
)
} }
/// Call the browser's `console.count()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/count)
#[proc_macro] #[proc_macro]
pub fn console_count(input: TokenStream) -> TokenStream { pub fn console_count(input: TokenStream) -> TokenStream {
let name = if input.is_empty() { let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
"count" "count"
} else { } else {
"count_with_label" "count_with_label"
@ -37,15 +49,17 @@ pub fn console_count(input: TokenStream) -> TokenStream {
quote_console_func( quote_console_func(
ConsoleFunc::fixed(name, 1), ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough), iter::once(ArgMode::PassThrough),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.countReset()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/countReset)
#[proc_macro] #[proc_macro]
pub fn console_count_reset(input: TokenStream) -> TokenStream { pub fn console_count_reset(input: TokenStream) -> TokenStream {
let name = if input.is_empty() { let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
"count_reset" "count_reset"
} else { } else {
"count_reset_with_label" "count_reset_with_label"
@ -54,103 +68,121 @@ pub fn console_count_reset(input: TokenStream) -> TokenStream {
quote_console_func( quote_console_func(
ConsoleFunc::fixed(name, 1), ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough), iter::once(ArgMode::PassThrough),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.debug()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/debug)
#[proc_macro] #[proc_macro]
pub fn console_debug(input: TokenStream) -> TokenStream { pub fn console_debug(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("debug", 0), ConsoleFunc::variadic("debug", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.dir()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dir)
#[proc_macro] #[proc_macro]
pub fn console_dir(input: TokenStream) -> TokenStream { pub fn console_dir(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("dir", 0), ConsoleFunc::variadic("dir", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.dirxml()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dirxml)
#[proc_macro] #[proc_macro]
pub fn console_dirxml(input: TokenStream) -> TokenStream { pub fn console_dirxml(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("dirxml", 0), ConsoleFunc::variadic("dirxml", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.error()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/error)
#[proc_macro] #[proc_macro]
pub fn console_error(input: TokenStream) -> TokenStream { pub fn console_error(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("error", 0), ConsoleFunc::variadic("error", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.exception()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/exception)
#[proc_macro] #[proc_macro]
pub fn console_exception(input: TokenStream) -> TokenStream { pub fn console_exception(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("exception", 0), ConsoleFunc::variadic("exception", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.info()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/info)
#[proc_macro] #[proc_macro]
pub fn console_info(input: TokenStream) -> TokenStream { pub fn console_info(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("info", 0), ConsoleFunc::variadic("info", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.log()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/log)
#[proc_macro] #[proc_macro]
pub fn console_log(input: TokenStream) -> TokenStream { pub fn console_log(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("log", 0), ConsoleFunc::variadic("log", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.table()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/table)
#[proc_macro] #[proc_macro]
pub fn console_table(input: TokenStream) -> TokenStream { pub fn console_table(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("table", 0), ConsoleFunc::variadic("table", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.time()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time)
#[proc_macro] #[proc_macro]
pub fn console_time(input: TokenStream) -> TokenStream { pub fn console_time(input: TokenStream) -> TokenStream {
let name = if input.is_empty() { let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
"time" "time"
} else { } else {
"time_with_label" "time_with_label"
@ -159,15 +191,17 @@ pub fn console_time(input: TokenStream) -> TokenStream {
quote_console_func( quote_console_func(
ConsoleFunc::fixed(name, 1), ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough), iter::once(ArgMode::PassThrough),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.timeEnd()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/timeEnd)
#[proc_macro] #[proc_macro]
pub fn console_time_end(input: TokenStream) -> TokenStream { pub fn console_time_end(input: TokenStream) -> TokenStream {
let name = if input.is_empty() { let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
"time_end" "time_end"
} else { } else {
"time_end_with_label" "time_end_with_label"
@ -176,26 +210,30 @@ pub fn console_time_end(input: TokenStream) -> TokenStream {
quote_console_func( quote_console_func(
ConsoleFunc::fixed(name, 1), ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough), iter::once(ArgMode::PassThrough),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.timeLog()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/timeLog)
#[proc_macro] #[proc_macro]
pub fn console_time_log(input: TokenStream) -> TokenStream { pub fn console_time_log(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("time_log_with_label_and_data", 1), ConsoleFunc::variadic("time_log_with_label_and_data", 1),
iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::IntoJsValue)), iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::IntoJsValue)),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.timeStamp()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/timeStamp)
#[proc_macro] #[proc_macro]
pub fn console_time_stamp(input: TokenStream) -> TokenStream { pub fn console_time_stamp(input: TokenStream) -> TokenStream {
let name = if input.is_empty() { let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
"time_stamp" "time_stamp"
} else { } else {
"time_stamp_with_data" "time_stamp_with_data"
@ -204,28 +242,33 @@ pub fn console_time_stamp(input: TokenStream) -> TokenStream {
quote_console_func( quote_console_func(
ConsoleFunc::fixed(name, 1), ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::IntoJsValue), iter::once(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.trace()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/trace)
#[proc_macro] #[proc_macro]
pub fn console_trace(input: TokenStream) -> TokenStream { pub fn console_trace(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("trace", 0), ConsoleFunc::variadic("trace", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }
/// Call the browser's `console.warn()` function. #[doc(hidden)]
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/warn)
#[proc_macro] #[proc_macro]
pub fn console_warn(input: TokenStream) -> TokenStream { pub fn console_warn(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func( quote_console_func(
ConsoleFunc::variadic("warn", 0), ConsoleFunc::variadic("warn", 0),
iter::repeat(ArgMode::IntoJsValue), iter::repeat(ArgMode::IntoJsValue),
input, crate_name,
args,
) )
} }

View File

@ -4,7 +4,7 @@ use quote::quote;
use std::cmp; use std::cmp;
use syn::parse::{Parse, ParseStream}; use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated; use syn::punctuated::Punctuated;
use syn::{bracketed, parse_macro_input, Expr, Result, Token}; use syn::{bracketed, Expr, Result, Token};
// The maximum number of variadic arguments accepted by web-sys function // The maximum number of variadic arguments accepted by web-sys function
// groups such as `console::log_n`: // groups such as `console::log_n`:
@ -18,7 +18,8 @@ const MAX_VARIADIC_ARGS: usize = 7;
// //
// * `func`: an object containing the target function name and parameter configuration. // * `func`: an object containing the target function name and parameter configuration.
// * `arg_modes`: an Iterator that will be called once per argument parsed from the input tokens. // * `arg_modes`: an Iterator that will be called once per argument parsed from the input tokens.
// * `input`: the raw input tokens, as provided by the macro caller. // * `crate_name`: an identifier that resolves to the name of the calling crate.
// * `in_args`: zero or more punctuated expressions passed as arguments by the caller.
pub fn quote_console_func( pub fn quote_console_func(
ConsoleFunc { ConsoleFunc {
name, name,
@ -26,9 +27,9 @@ pub fn quote_console_func(
is_variadic, is_variadic,
}: ConsoleFunc, }: ConsoleFunc,
mut arg_modes: impl Iterator<Item = ArgMode>, mut arg_modes: impl Iterator<Item = ArgMode>,
input: TokenStream, crate_name: Ident,
in_args: Punctuated<Expr, Token![,]>,
) -> TokenStream { ) -> TokenStream {
let InArgs(crate_name, in_args) = parse_macro_input!(input as InArgs);
let mut out_args = in_args let mut out_args = in_args
.iter() .iter()
.map(|arg| quote_arg(&crate_name, arg, arg_modes.next().unwrap_or_default())); .map(|arg| quote_arg(&crate_name, arg, arg_modes.next().unwrap_or_default()));
@ -67,7 +68,7 @@ pub fn quote_console_func(
(quote! { #crate_name::web_sys::console::#ident(#args) }).into() (quote! { #crate_name::web_sys::console::#ident(#args) }).into()
} }
fn quote_arg(crate_name: &TokenStream2, arg: &Expr, arg_mode: ArgMode) -> TokenStream2 { fn quote_arg(crate_name: &Ident, arg: &Expr, arg_mode: ArgMode) -> TokenStream2 {
match arg_mode { match arg_mode {
ArgMode::PassThrough => { ArgMode::PassThrough => {
quote! { #arg } quote! { #arg }
@ -119,10 +120,12 @@ impl Default for ArgMode {
} }
} }
// InArgs is a struct required during parsing of input arguments. pub struct InputTokens {
struct InArgs(TokenStream2, Punctuated<Expr, Token![,]>); pub crate_name: Ident,
pub args: Punctuated<Expr, Token![,]>,
}
impl Parse for InArgs { impl Parse for InputTokens {
fn parse(input: ParseStream) -> Result<Self> { fn parse(input: ParseStream) -> Result<Self> {
input.parse::<Token![#]>()?; input.parse::<Token![#]>()?;
input.parse::<Token![!]>()?; input.parse::<Token![!]>()?;
@ -132,9 +135,9 @@ impl Parse for InArgs {
bracketed.parse::<Token![crate]>()?; bracketed.parse::<Token![crate]>()?;
bracketed.parse::<Token![ = ]>()?; bracketed.parse::<Token![ = ]>()?;
let crate_name = bracketed.parse()?; let crate_name = bracketed.parse::<Ident>()?;
let args: Punctuated<Expr, Token![,]> = Punctuated::parse_terminated(&input)?; let args: Punctuated<Expr, Token![,]> = Punctuated::parse_terminated(&input)?;
Ok(Self(crate_name, args)) Ok(Self { crate_name, args })
} }
} }