jsyntax 0.1.0

Structured Java source construction for Rust decompilers and code generators
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use super::{
    Annotation, AnnotationValue, ArrayDimension, Block, Comment, Expr, ReturnType, Type,
    TypeParameter, VariableInitializer,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Modifier {
    Public,
    Protected,
    Private,
    Abstract,
    Static,
    Final,
    Transient,
    Volatile,
    Synchronized,
    Native,
    Strictfp,
    Default,
    Sealed,
    NonSealed,
}

impl Modifier {
    pub const fn keyword(self) -> &'static str {
        match self {
            Self::Public => "public",
            Self::Protected => "protected",
            Self::Private => "private",
            Self::Abstract => "abstract",
            Self::Static => "static",
            Self::Final => "final",
            Self::Transient => "transient",
            Self::Volatile => "volatile",
            Self::Synchronized => "synchronized",
            Self::Native => "native",
            Self::Strictfp => "strictfp",
            Self::Default => "default",
            Self::Sealed => "sealed",
            Self::NonSealed => "non-sealed",
        }
    }
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct DeclarationMeta {
    pub comments: Vec<Comment>,
    pub annotations: Vec<Annotation>,
    pub modifiers: Vec<Modifier>,
}

pub trait Decorated: Sized {
    fn meta_mut(&mut self) -> &mut DeclarationMeta;
    fn with_modifier(mut self, modifier: Modifier) -> Self {
        self.meta_mut().modifiers.push(modifier);
        self
    }
    fn with_annotation(mut self, annotation: Annotation) -> Self {
        self.meta_mut().annotations.push(annotation);
        self
    }
    fn with_comment(mut self, comment: Comment) -> Self {
        self.meta_mut().comments.push(comment);
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypeDecl {
    pub meta: DeclarationMeta,
    pub name: String,
    pub type_parameters: Vec<TypeParameter>,
    pub kind: TypeDeclKind,
    pub members: Vec<Member>,
}

impl TypeDecl {
    pub fn new(name: impl Into<String>, kind: TypeDeclKind) -> Self {
        Self {
            meta: DeclarationMeta::default(),
            name: name.into(),
            type_parameters: Vec::new(),
            kind,
            members: Vec::new(),
        }
    }
    pub fn class(name: impl Into<String>) -> Self {
        Self::new(
            name,
            TypeDeclKind::Class {
                extends: None,
                implements: Vec::new(),
                permits: Vec::new(),
            },
        )
    }
    pub fn interface(name: impl Into<String>) -> Self {
        Self::new(
            name,
            TypeDeclKind::Interface {
                extends: Vec::new(),
                permits: Vec::new(),
            },
        )
    }
    pub fn record(
        name: impl Into<String>,
        components: impl IntoIterator<Item = Parameter>,
    ) -> Self {
        Self::new(
            name,
            TypeDeclKind::Record {
                components: components.into_iter().collect(),
                implements: Vec::new(),
            },
        )
    }
    pub fn enumeration(
        name: impl Into<String>,
        constants: impl IntoIterator<Item = EnumConstant>,
    ) -> Self {
        Self::new(
            name,
            TypeDeclKind::Enum {
                constants: constants.into_iter().collect(),
                implements: Vec::new(),
            },
        )
    }
    pub fn annotation(name: impl Into<String>) -> Self {
        Self::new(name, TypeDeclKind::Annotation)
    }
    pub fn with_member(mut self, member: impl Into<Member>) -> Self {
        self.members.push(member.into());
        self
    }
    pub fn with_type_parameter(mut self, parameter: TypeParameter) -> Self {
        self.type_parameters.push(parameter);
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum TypeDeclKind {
    Class {
        extends: Option<Type>,
        implements: Vec<Type>,
        permits: Vec<Type>,
    },
    Interface {
        extends: Vec<Type>,
        permits: Vec<Type>,
    },
    Enum {
        constants: Vec<EnumConstant>,
        implements: Vec<Type>,
    },
    Record {
        components: Vec<Parameter>,
        implements: Vec<Type>,
    },
    Annotation,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Member {
    Field(FieldDecl),
    Method(MethodDecl),
    Constructor(ConstructorDecl),
    Type(Box<TypeDecl>),
    Initializer { static_: bool, body: Block },
    AnnotationElement(AnnotationElement),
    Empty,
}

#[derive(Debug, Clone, PartialEq)]
pub struct FieldDecl {
    pub meta: DeclarationMeta,
    pub ty: Type,
    pub variables: Vec<Variable>,
}

impl FieldDecl {
    pub fn new(ty: Type, name: impl Into<String>) -> Self {
        Self {
            meta: DeclarationMeta::default(),
            ty,
            variables: vec![Variable::new(name)],
        }
    }
    pub fn initialized(
        ty: Type,
        name: impl Into<String>,
        initializer: impl Into<VariableInitializer>,
    ) -> Self {
        Self {
            meta: DeclarationMeta::default(),
            ty,
            variables: vec![Variable::new(name).initialized(initializer)],
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Variable {
    pub name: String,
    pub dimensions: Vec<ArrayDimension>,
    pub initializer: Option<VariableInitializer>,
}

impl Variable {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            dimensions: Vec::new(),
            initializer: None,
        }
    }
    pub fn initialized(mut self, initializer: impl Into<VariableInitializer>) -> Self {
        self.initializer = Some(initializer.into());
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Parameter {
    pub annotations: Vec<Annotation>,
    pub final_: bool,
    pub ty: Type,
    pub name: String,
    pub dimensions: Vec<ArrayDimension>,
    /// An empty annotation list still represents an ordinary varargs ellipsis.
    pub varargs: Option<Vec<Annotation>>,
}

impl Parameter {
    pub fn new(ty: Type, name: impl Into<String>) -> Self {
        Self {
            annotations: Vec::new(),
            final_: false,
            ty,
            name: name.into(),
            dimensions: Vec::new(),
            varargs: None,
        }
    }
    pub fn varargs(mut self) -> Self {
        self.varargs = Some(Vec::new());
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ReceiverParameter {
    pub annotations: Vec<Annotation>,
    pub ty: Type,
    pub qualifier: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MethodDecl {
    pub meta: DeclarationMeta,
    pub type_parameters: Vec<TypeParameter>,
    pub return_type: ReturnType,
    pub name: String,
    pub receiver: Option<ReceiverParameter>,
    pub parameters: Vec<Parameter>,
    pub return_dimensions: Vec<ArrayDimension>,
    pub throws: Vec<Type>,
    pub body: Option<Block>,
}

impl MethodDecl {
    pub fn new(name: impl Into<String>, return_type: impl Into<ReturnType>) -> Self {
        Self {
            meta: DeclarationMeta::default(),
            type_parameters: Vec::new(),
            return_type: return_type.into(),
            name: name.into(),
            receiver: None,
            parameters: Vec::new(),
            return_dimensions: Vec::new(),
            throws: Vec::new(),
            body: None,
        }
    }
    pub fn with_parameter(mut self, parameter: Parameter) -> Self {
        self.parameters.push(parameter);
        self
    }
    pub fn with_body(mut self, body: Block) -> Self {
        self.body = Some(body);
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ConstructorDecl {
    pub meta: DeclarationMeta,
    pub type_parameters: Vec<TypeParameter>,
    pub name: String,
    pub parameters: ConstructorParameters,
    pub throws: Vec<Type>,
    pub body: ConstructorBody,
}

impl ConstructorDecl {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            meta: DeclarationMeta::default(),
            type_parameters: Vec::new(),
            name: name.into(),
            parameters: ConstructorParameters::Explicit {
                receiver: None,
                parameters: Vec::new(),
            },
            throws: Vec::new(),
            body: ConstructorBody::default(),
        }
    }
    pub fn compact(name: impl Into<String>, body: Block) -> Self {
        Self {
            parameters: ConstructorParameters::Compact,
            body: ConstructorBody {
                prologue: Block::default(),
                invocation: None,
                epilogue: body,
            },
            ..Self::new(name)
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ConstructorParameters {
    Explicit {
        receiver: Option<ReceiverParameter>,
        parameters: Vec<Parameter>,
    },
    Compact,
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct ConstructorBody {
    pub prologue: Block,
    pub invocation: Option<ConstructorInvocation>,
    pub epilogue: Block,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ConstructorInvocation {
    pub target: ConstructorTarget,
    pub type_arguments: Vec<Type>,
    pub arguments: Vec<Expr>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ConstructorTarget {
    This,
    Super(Option<Box<Expr>>),
}

#[derive(Debug, Clone, PartialEq)]
pub struct EnumConstant {
    pub comments: Vec<Comment>,
    pub annotations: Vec<Annotation>,
    pub name: String,
    pub arguments: Vec<Expr>,
    pub body: Option<Vec<Member>>,
}

impl EnumConstant {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            comments: Vec::new(),
            annotations: Vec::new(),
            name: name.into(),
            arguments: Vec::new(),
            body: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct AnnotationElement {
    pub meta: DeclarationMeta,
    pub ty: Type,
    pub name: String,
    pub dimensions: Vec<ArrayDimension>,
    pub default: Option<AnnotationValue>,
}

macro_rules! decorated {
    ($($ty:ty),* $(,)?) => { $(impl Decorated for $ty { fn meta_mut(&mut self) -> &mut DeclarationMeta { &mut self.meta } })* };
}
decorated!(
    TypeDecl,
    FieldDecl,
    MethodDecl,
    ConstructorDecl,
    AnnotationElement
);

macro_rules! member_from {
    ($($ty:ty => $variant:ident),* $(,)?) => { $(impl From<$ty> for Member { fn from(value: $ty) -> Self { Self::$variant(value) } })* };
}
member_from!(FieldDecl => Field, MethodDecl => Method, ConstructorDecl => Constructor, AnnotationElement => AnnotationElement);
impl From<TypeDecl> for Member {
    fn from(value: TypeDecl) -> Self {
        Self::Type(Box::new(value))
    }
}