1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use proc_macro::TokenStream;
use syn::{parse_macro_input, AttributeArgs};

#[cfg(feature = "global_function")]
mod global_function;

#[cfg(any(feature = "nonvisualobject", feature = "visualobject"))]
mod object;

/// 生成全局函数
///
/// # Parameters
/// - `name`: 映射的PB函数名 (默认为Rust函数名)
///
/// # Examples
/// ```no_run
/// #[global_function(name = "gf_bitor")]
/// fn bit_or(session: Session, a: pblong, b: pblong) -> pblong { a | b }
/// ```
#[cfg(feature = "global_function")]
#[proc_macro_attribute]
pub fn global_function(args: TokenStream, input: TokenStream) -> TokenStream {
    match global_function::gen(parse_macro_input!(args as AttributeArgs), input) {
        Ok(stream) => stream,
        Err(e) => e.to_compile_error().into()
    }
}

/// 生成不可视对象
///
/// # Parameters
///
/// - `name`: 映射的PB对象类名 (默认为Rust对象名)
/// - `inherit`: 继承的对象 (此对象的字段)
///
/// # Notice
///
/// 继承模式不支持覆盖(override)方法实现,并且在PB端需要将父类的方法声明在子类中重新声明
///
/// # Examples
///
/// - PowerBuilder
///
/// ```vbscript
/// /* 父类声明 */
/// forward
/// global type n_parent from nonvisualobject
/// end type
/// end forward
///
/// global type n_parent from nonvisualobject native "pbrs.dll"
/// public function string of_hello (string world)
/// end type
/// global n_parent n_parent
///
/// type variables
/// end variables
///
/// on n_parent.create
/// call super::create
/// TriggerEvent( this, "constructor" )
/// end on
///
/// on n_parent.destroy
/// TriggerEvent( this, "destructor" )
/// call super::destroy
/// end on
///
/// /* 子类声明 */
/// forward
/// global type n_child from n_parent
/// end type
/// end forward
///
/// global type n_child from n_parent native "pbrs.dll"
/// // 重新声明父类方法
/// public function string of_hello (string world)
/// // 声明子类私有的方法
/// public function string of_foo (string bar)
/// end type
/// global n_child n_child
///
/// type variables
/// end variables
///
/// on n_child.create
/// call super::create
/// TriggerEvent( this, "constructor" )
/// end on
///
/// on n_child.destroy
/// TriggerEvent( this, "destructor" )
/// call super::destroy
/// end on
/// ```
///
/// - Rust(pbni-rs)
///
/// ```no_run
/// struct RustObject {
///     session: Session,
///     ctx: ContextObject
/// }
///
/// #[nonvisualobject(name = "n_pbni")]
/// impl RustObject {
///     #[constructor]
///     fn new(session: Session, ctx: ContextObject) -> RustObject {
///         RustObject {
///             session,
///             ctx
///         }
///     }
///     #[method(name="of_Hello")]
///     fn hello(&self, world: String) -> String {
///         format!("hello {}!",world)
///     }
/// }
///
/// struct RustChildObject {
///     parent: RustObject
/// }
///
/// #[nonvisualobject(name = "n_pbni_child", inherit = "parent")]
/// impl RustChildObject {
///     #[constructor]
///     fn new(session: Session, ctx: ContextObject) -> RustChildObject {
///         RustChildObject {
///             parent : RustObject {
///                 session,
///                 ctx
///             }
///         }
///     }
///     #[method(name="of_Foo")]
///     fn foo(&self, bar: String) -> String {
///         format!("foo {}!",bar)
///     }
/// }
/// ```
#[cfg(feature = "nonvisualobject")]
#[proc_macro_attribute]
pub fn nonvisualobject(args: TokenStream, input: TokenStream) -> TokenStream {
    match object::gen_nvo(parse_macro_input!(args as AttributeArgs), input) {
        Ok(stream) => stream,
        Err(e) => e.to_compile_error().into()
    }
}

/// 生成可视对象
///
/// # Parameters
///
/// - `name`: 映射的PB对象类名 (默认为Rust对象名)
/// - `inherit`: 继承的对象 (此对象的字段)
///
/// # Notice
///
/// 继承模式不支持覆盖(override)方法实现,并且在PB端需要将父类的方法声明在子类中重新声明,参见[`nonvisualobject`]说明
///
/// [`nonvisualobject`]: macro@nonvisualobject
///
/// # Examples
///
/// ```no_run
/// struct RustObject {
///     session: Session,
///     ctx: ContextObject
/// }
///
/// #[visualobject(name = "u_canvas")]
/// impl RustObject {
///     #[constructor]
///     fn new(session: Session, ctx: ContextObject) -> RustObject {
///         RustObject {
///             session,
///             ctx
///         }
///     }
///     #[method(name="of_Hello")]
///     fn hello(&self, world: String) -> String {
///         format!("hello {}!",world)
///     }
/// }
/// ```
#[cfg(feature = "visualobject")]
#[proc_macro_attribute]
pub fn visualobject(args: TokenStream, input: TokenStream) -> TokenStream {
    match object::gen_vo(parse_macro_input!(args as AttributeArgs), input) {
        Ok(stream) => stream,
        Err(e) => e.to_compile_error().into()
    }
}

/// 标记对象的构造函数
///
/// # Examples
///
/// ```no_run
/// #[constructor]
/// fn new(session: Session, ctx: ContextObject) -> RustObject {
///     RustObject {
///         session,
///         ctx
///     }
/// }
/// ```
#[cfg(any(feature = "nonvisualobject", feature = "visualobject"))]
#[proc_macro_attribute]
pub fn constructor(_: TokenStream, input: TokenStream) -> TokenStream { input }

/// 标记对象函数
///
/// # Parameters
///
/// - `name`: 映射的PB函数类名 (默认为Rust函数名)
/// - `overload`: 重载次数 (默认0,意为没有重载)
///
/// # Examples
///
/// ```no_run
/// #[method(name="of_Hello")]
/// fn hello(&self, world: String) -> String {
///     format!("hello {}!",world)
/// }
/// ```
#[cfg(any(feature = "nonvisualobject", feature = "visualobject"))]
#[proc_macro_attribute]
pub fn method(_: TokenStream, input: TokenStream) -> TokenStream { input }

/// 标记对象事件,如果方法体没有代码,则自动生成对应的调用代码
///
/// # Parameters
///
/// - `name`: 映射的PB事件名 (默认为Rust函数名)
///
/// # Required
///
/// 自动生成事件代码,需要对象实现`context_mut`方法:
///
/// # Examples
///
/// ```no_run
/// struct RustObject {
///     session: Session,
///     ctx: ContextObject
/// }
///
/// impl RustObject {
///     fn context_mut(&mut self) -> &mut ContextObject { &mut self.ctx }
/// }
///
/// #[nonvisualobject(name = "n_pbni")]
/// impl RustObject {
///     #[constructor]
///     fn new(session: Session, ctx: ContextObject) -> RustObject {
///         RustObject {
///             session,
///             ctx
///         }
///     }
///     #[event(name="onFire")]
///     fn on_fire(&mut self) {}
/// }
/// ```
#[cfg(any(feature = "nonvisualobject", feature = "visualobject"))]
#[proc_macro_attribute]
pub fn event(args: TokenStream, input: TokenStream) -> TokenStream {
    match object::gen_event(parse_macro_input!(args as AttributeArgs), input) {
        Ok(stream) => stream,
        Err(e) => e.to_compile_error().into()
    }
}