async_codegen/java/
syntax.rs

1/*
2 * Copyright © 2025 Anand Beh
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use super::*;
18use crate::common::{SeparatedSeqAccept, Str};
19use crate::cstyle_common::{
20    ModsAccept, TypeArgAccept, delegate_tuple_w, delegate_tuple_ws, delegate_tuple_ww,
21};
22use crate::{Output, Writable, WritableSeq};
23
24impl<O, Type, Expr> Writable<O> for DeclareVarStmt<Type, Expr>
25where
26    O: Output,
27    Type: Writable<O>,
28    Expr: Writable<O>,
29{
30    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
31        Stmt(DeclareParam(&self.0, &self.1)).write_to(output).await
32    }
33}
34
35impl<O, Assert> Writable<O> for AssertStmt<Assert>
36where
37    O: Output,
38    Assert: Writable<O>,
39{
40    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
41        o.write("assert ").await?;
42        self.0.write_to(o).await?;
43        o.write(";\n").await
44    }
45}
46
47delegate_tuple_w!(Stmt);
48
49delegate_tuple_ww!(AssignStmt);
50
51delegate_tuple_w!(ReturnStmt);
52
53delegate_tuple_ww!(AssignExpr);
54
55delegate_tuple_ww!(IfBlock);
56
57delegate_tuple_ww!(Else);
58
59delegate_tuple_w!(Block);
60
61delegate_tuple_ww!(MemberAccess);
62
63impl<O, Mods, Name, Body> Writable<O> for ClassDef<Mods, Name, Body>
64where
65    O: Output,
66    Mods: WritableSeq<O>,
67    Name: Writable<O>,
68    Body: Writable<O>,
69{
70    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
71        self.mods.for_each(&mut ModsAccept(o)).await?;
72        o.write("class ").await?;
73        self.name.write_to(o).await?;
74        o.write(" {\n").await?;
75        self.body.write_to(o).await?;
76        o.write("}\n").await
77    }
78}
79
80impl<O, Mods, Name, Body> Writable<O> for InterfaceDef<Mods, Name, Body>
81where
82    O: Output,
83    Mods: WritableSeq<O>,
84    Name: Writable<O>,
85    Body: Writable<O>,
86{
87    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
88        self.mods.for_each(&mut ModsAccept(o)).await?;
89        o.write("interface ").await?;
90        self.name.write_to(o).await?;
91        o.write(" {\n").await?;
92        self.body.write_to(o).await?;
93        o.write("}\n").await
94    }
95}
96
97impl<O, Expr, Super> Writable<O> for Extends<Expr, Super>
98where
99    O: Output,
100    Expr: Writable<O>,
101    Super: Writable<O>,
102{
103    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
104        self.0.write_to(o).await?;
105        o.write(" extends ").await?;
106        self.1.write_to(o).await
107    }
108}
109
110impl<O, Expr, Ifaces> Writable<O> for Implements<Expr, Ifaces>
111where
112    O: Output,
113    Expr: Writable<O>,
114    Ifaces: WritableSeq<O>,
115{
116    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
117        self.0.write_to(o).await?;
118        self.1
119            .for_each(&mut SeparatedSeqAccept::new(
120                o,
121                Str(" implements "),
122                Str(", "),
123            ))
124            .await
125    }
126}
127
128impl<O> Writable<O> for Modifier
129where
130    O: Output,
131{
132    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
133        output
134            .write(match self {
135                Self::Abstract => "abstract",
136                Self::Default => "default",
137                Self::Final => "final",
138                Self::Native => "native",
139                Self::Private => "private",
140                Self::Protected => "protected",
141                Self::Public => "public",
142                Self::Static => "static",
143                Self::StrictFp => "strictfp",
144                Self::Synchronized => "synchronized",
145                Self::Transient => "transient",
146                Self::Volatile => "volatile",
147            })
148            .await
149    }
150}
151
152impl<O, Name, Args> Writable<O> for FunctionCall<Name, Args>
153where
154    O: Output,
155    Name: Writable<O>,
156    Args: WritableSeq<O>,
157{
158    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
159        self.name.write_to(output).await?;
160        output.write("(").await?;
161        self.args
162            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
163            .await?;
164        output.write(")").await
165    }
166}
167
168impl<O, Name, Args> Writable<O> for ConstructorCall<Name, Args>
169where
170    O: Output,
171    Name: Writable<O>,
172    Args: WritableSeq<O>,
173{
174    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
175        output.write("new ").await?;
176        FunctionCall {
177            name: &self.name,
178            args: &self.args,
179        }
180        .write_to(output)
181        .await
182    }
183}
184
185impl<O, Type, Name> Writable<O> for DeclareParam<Type, Name>
186where
187    O: Output,
188    Type: Writable<O>,
189    Name: Writable<O>,
190{
191    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
192        self.0.write_to(output).await?;
193        output.write(" ").await?;
194        self.1.write_to(output).await
195    }
196}
197
198delegate_tuple_ws!(Parameterized);
199
200impl<O, Name> Writable<O> for WithInference<Name>
201where
202    O: Output,
203    Name: Writable<O>,
204{
205    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
206        self.0.write_to(output).await?;
207        output.write("<>").await
208    }
209}
210
211impl<O, Component> Writable<O> for Array<Component> where O: Output, Component: Writable<O> {
212    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
213        self.0.write_to(output).await?;
214        output.write("[]").await
215    }
216}
217
218impl<O, VarName, Iter, Body> Writable<O> for EnhancedFor<VarName, Iter, Body>
219where
220    O: Output,
221    VarName: Writable<O>,
222    Iter: Writable<O>,
223    Body: Writable<O>,
224{
225    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
226        output.write("for (").await?;
227        self.var_name.write_to(output).await?;
228        output.write(" : ").await?;
229        self.iter.write_to(output).await?;
230        output.write(") {\n").await?;
231        self.body.write_to(output).await?;
232        output.write("}\n").await
233    }
234}
235
236impl<O, Init, Predicate, Update, Body> Writable<O> for TraditionalFor<Init, Predicate, Update, Body>
237where
238    O: Output,
239    Init: Writable<O>,
240    Predicate: Writable<O>,
241    Update: Writable<O>,
242    Body: Writable<O>,
243{
244    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
245        output.write("for (").await?;
246        self.init.write_to(output).await?;
247        output.write("; ").await?;
248        self.predicate.write_to(output).await?;
249        output.write("; ").await?;
250        self.update.write_to(output).await?;
251        output.write(") {\n").await?;
252        self.body.write_to(output).await?;
253        output.write("}\n").await
254    }
255}
256
257impl<O, Mods, TypeArgs, Name, Args, Throws, Body> Writable<O>
258    for ConstructorDef<Mods, TypeArgs, Name, Args, Throws, Body>
259where
260    O: Output,
261    Mods: WritableSeq<O>,
262    TypeArgs: WritableSeq<O>,
263    Name: Writable<O>,
264    Args: WritableSeq<O>,
265    Throws: WritableSeq<O>,
266    Body: Writable<O>,
267{
268    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
269        self.mods.for_each(&mut ModsAccept(output)).await?;
270        TypeArgAccept::new(output)
271            .write_in_diamond_with_space_after(&self.type_args)
272            .await?;
273        FunctionCall {
274            name: &self.name,
275            args: &self.args,
276        }
277        .write_to(output)
278        .await?;
279        self.throws.for_each(&mut throws_decl(output)).await?;
280        output.write(" {\n").await?;
281        self.body.write_to(output).await?;
282        output.write("}\n\n").await
283    }
284}
285
286impl<O, Mods, TypeArgs, Return, Name, Args, Throws, Body> Writable<O>
287    for FunctionDef<Mods, TypeArgs, Return, Name, Args, Throws, Body>
288where
289    O: Output,
290    Mods: WritableSeq<O>,
291    TypeArgs: WritableSeq<O>,
292    Return: Writable<O>,
293    Name: Writable<O>,
294    Args: WritableSeq<O>,
295    Throws: WritableSeq<O>,
296    Body: Writable<O>,
297{
298    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
299        self.mods.for_each(&mut ModsAccept(output)).await?;
300        TypeArgAccept::new(output)
301            .write_in_diamond_with_space_after(&self.type_args)
302            .await?;
303        self.return_type.write_to(output).await?;
304        output.write(" ").await?;
305        FunctionCall {
306            name: &self.name,
307            args: &self.args,
308        }
309        .write_to(output)
310        .await?;
311        self.throws.for_each(&mut throws_decl(output)).await?;
312        output.write(" {\n").await?;
313        self.body.write_to(output).await?;
314        output.write("}\n\n").await
315    }
316}
317
318fn throws_decl<'o, O>(
319    output: &'o mut O,
320) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
321    SeparatedSeqAccept::new(output, Str(" throws "), Str(", "))
322}