tests: Move namespaces.vala into parser and merge its main methods
[vala-gnome.git] / valadoc / initializerbuilder.vala
blobd95b7fe9b07b523200988e9d3be0b15facf29cb6
1 /* initializerbuilder.vala
3 * Copyright (C) 2011 Florian Brosch
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Florian Brosch <flo.brosch@gmail.com>
24 using Valadoc.Content;
26 private class Valadoc.Api.InitializerBuilder : Vala.CodeVisitor {
27 private Vala.HashMap<Vala.Symbol, Symbol> symbol_map;
28 private SignatureBuilder signature;
30 private Symbol? resolve (Vala.Symbol symbol) {
31 return symbol_map.get (symbol);
34 private void write_node (Vala.Symbol vsymbol) {
35 signature.append_symbol (resolve (vsymbol));
38 private void write_type (Vala.DataType vsymbol) {
39 if (vsymbol.data_type != null) {
40 write_node (vsymbol.data_type);
41 } else {
42 signature.append_literal ("null");
45 var type_args = vsymbol.get_type_arguments ();
46 if (type_args.size > 0) {
47 signature.append ("<");
48 bool first = true;
49 foreach (Vala.DataType type_arg in type_args) {
50 if (!first) {
51 signature.append (",");
52 } else {
53 first = false;
55 if (!type_arg.value_owned) {
56 signature.append_keyword ("weak");
58 signature.append (type_arg.to_qualified_string (null));
60 signature.append (">");
63 if (vsymbol.nullable) {
64 signature.append ("?");
68 /**
69 * {@inheritDoc}
71 public override void visit_array_creation_expression (Vala.ArrayCreationExpression expr) {
72 signature.append_keyword ("new");
73 write_type (expr.element_type);
74 signature.append ("[", false);
76 bool first = true;
77 foreach (Vala.Expression size in expr.get_sizes ()) {
78 if (!first) {
79 signature.append (", ", false);
81 size.accept (this);
82 first = false;
85 signature.append ("]", false);
87 if (expr.initializer_list != null) {
88 signature.append (" ", false);
89 expr.initializer_list.accept (this);
93 public InitializerBuilder (SignatureBuilder signature, Vala.HashMap<Vala.Symbol, Symbol> symbol_map) {
94 this.symbol_map = symbol_map;
95 this.signature = signature;
98 /**
99 * {@inheritDoc}
101 public override void visit_binary_expression (Vala.BinaryExpression expr) {
102 expr.left.accept (this);
104 switch (expr.operator) {
105 case Vala.BinaryOperator.PLUS:
106 signature.append ("+ ");
107 break;
109 case Vala.BinaryOperator.MINUS:
110 signature.append ("- ");
111 break;
113 case Vala.BinaryOperator.MUL:
114 signature.append ("* ");
115 break;
117 case Vala.BinaryOperator.DIV:
118 signature.append ("/ ");
119 break;
121 case Vala.BinaryOperator.MOD:
122 signature.append ("% ");
123 break;
125 case Vala.BinaryOperator.SHIFT_LEFT:
126 signature.append ("<< ");
127 break;
129 case Vala.BinaryOperator.SHIFT_RIGHT:
130 signature.append (">> ");
131 break;
133 case Vala.BinaryOperator.LESS_THAN:
134 signature.append ("< ");
135 break;
137 case Vala.BinaryOperator.GREATER_THAN:
138 signature.append ("> ");
139 break;
141 case Vala.BinaryOperator.LESS_THAN_OR_EQUAL:
142 signature.append ("<= ");
143 break;
145 case Vala.BinaryOperator.GREATER_THAN_OR_EQUAL:
146 signature.append (">= ");
147 break;
149 case Vala.BinaryOperator.EQUALITY:
150 signature.append ("== ");
151 break;
153 case Vala.BinaryOperator.INEQUALITY:
154 signature.append ("!= ");
155 break;
157 case Vala.BinaryOperator.BITWISE_AND:
158 signature.append ("& ");
159 break;
161 case Vala.BinaryOperator.BITWISE_OR:
162 signature.append ("| ");
163 break;
165 case Vala.BinaryOperator.BITWISE_XOR:
166 signature.append ("^ ");
167 break;
169 case Vala.BinaryOperator.AND:
170 signature.append ("&& ");
171 break;
173 case Vala.BinaryOperator.OR:
174 signature.append ("|| ");
175 break;
177 case Vala.BinaryOperator.IN:
178 signature.append_keyword ("in");
179 signature.append (" ");
180 break;
182 case Vala.BinaryOperator.COALESCE:
183 signature.append ("?? ");
184 break;
186 default:
187 assert_not_reached ();
190 expr.right.accept (this);
194 * {@inheritDoc}
196 public override void visit_unary_expression (Vala.UnaryExpression expr) {
197 switch (expr.operator) {
198 case Vala.UnaryOperator.PLUS:
199 signature.append ("+");
200 break;
202 case Vala.UnaryOperator.MINUS:
203 signature.append ("-");
204 break;
206 case Vala.UnaryOperator.LOGICAL_NEGATION:
207 signature.append ("!");
208 break;
210 case Vala.UnaryOperator.BITWISE_COMPLEMENT:
211 signature.append ("~");
212 break;
214 case Vala.UnaryOperator.INCREMENT:
215 signature.append ("++");
216 break;
218 case Vala.UnaryOperator.DECREMENT:
219 signature.append ("--");
220 break;
222 case Vala.UnaryOperator.REF:
223 signature.append_keyword ("ref");
224 break;
226 case Vala.UnaryOperator.OUT:
227 signature.append_keyword ("out");
228 break;
230 default:
231 assert_not_reached ();
233 expr.inner.accept (this);
237 * {@inheritDoc}
239 public override void visit_assignment (Vala.Assignment a) {
240 a.left.accept (this);
242 switch (a.operator) {
243 case Vala.AssignmentOperator.SIMPLE:
244 signature.append ("=");
245 break;
247 case Vala.AssignmentOperator.BITWISE_OR:
248 signature.append ("|");
249 break;
251 case Vala.AssignmentOperator.BITWISE_AND:
252 signature.append ("&");
253 break;
255 case Vala.AssignmentOperator.BITWISE_XOR:
256 signature.append ("^");
257 break;
259 case Vala.AssignmentOperator.ADD:
260 signature.append ("+");
261 break;
263 case Vala.AssignmentOperator.SUB:
264 signature.append ("-");
265 break;
267 case Vala.AssignmentOperator.MUL:
268 signature.append ("*");
269 break;
271 case Vala.AssignmentOperator.DIV:
272 signature.append ("/");
273 break;
275 case Vala.AssignmentOperator.PERCENT:
276 signature.append ("%");
277 break;
279 case Vala.AssignmentOperator.SHIFT_LEFT:
280 signature.append ("<<");
281 break;
283 case Vala.AssignmentOperator.SHIFT_RIGHT:
284 signature.append (">>");
285 break;
287 default:
288 assert_not_reached ();
291 a.right.accept (this);
295 * {@inheritDoc}
297 public override void visit_cast_expression (Vala.CastExpression expr) {
298 if (expr.is_non_null_cast) {
299 signature.append ("(!)");
300 expr.inner.accept (this);
301 return;
304 if (!expr.is_silent_cast) {
305 signature.append ("(", false);
306 write_type (expr.type_reference);
307 signature.append (")", false);
310 expr.inner.accept (this);
312 if (expr.is_silent_cast) {
313 signature.append_keyword ("as");
314 write_type (expr.type_reference);
319 * {@inheritDoc}
321 public override void visit_initializer_list (Vala.InitializerList list) {
322 signature.append ("{", false);
324 bool first = true;
325 foreach (Vala.Expression initializer in list.get_initializers ()) {
326 if (!first) {
327 signature.append (", ", false);
329 first = false;
330 initializer.accept (this);
333 signature.append ("}", false);
337 * {@inheritDoc}
339 public override void visit_member_access (Vala.MemberAccess expr) {
340 if (expr.symbol_reference != null) {
341 expr.symbol_reference.accept (this);
342 } else {
343 signature.append (expr.member_name);
348 * {@inheritDoc}
350 public override void visit_element_access (Vala.ElementAccess expr) {
351 expr.container.accept (this);
352 signature.append ("[", false);
354 bool first = true;
355 foreach (Vala.Expression index in expr.get_indices ()) {
356 if (!first) {
357 signature.append (", ", false);
359 first = false;
361 index.accept (this);
364 signature.append ("]", false);
368 * {@inheritDoc}
370 public override void visit_pointer_indirection (Vala.PointerIndirection expr) {
371 signature.append ("*", false);
372 expr.inner.accept (this);
376 * {@inheritDoc}
378 public override void visit_addressof_expression (Vala.AddressofExpression expr) {
379 signature.append ("&", false);
380 expr.inner.accept (this);
384 * {@inheritDoc}
386 public override void visit_reference_transfer_expression (Vala.ReferenceTransferExpression expr) {
387 signature.append ("(", false).append_keyword ("owned", false).append (")", false);
388 expr.inner.accept (this);
392 * {@inheritDoc}
394 public override void visit_type_check (Vala.TypeCheck expr) {
395 expr.expression.accept (this);
396 signature.append_keyword ("is");
397 write_type (expr.type_reference);
401 * {@inheritDoc}
403 public override void visit_method_call (Vala.MethodCall expr) {
404 // symbol-name:
405 expr.call.symbol_reference.accept (this);
407 // parameters:
408 signature.append (" (", false);
409 bool first = true;
410 foreach (Vala.Expression literal in expr.get_argument_list ()) {
411 if (!first) {
412 signature.append (", ", false);
415 literal.accept (this);
416 first = false;
418 signature.append (")", false);
422 * {@inheritDoc}
424 public override void visit_slice_expression (Vala.SliceExpression expr) {
425 expr.container.accept (this);
426 signature.append ("[", false);
427 expr.start.accept (this);
428 signature.append (":", false);
429 expr.stop.accept (this);
430 signature.append ("]", false);
434 * {@inheritDoc}
436 public override void visit_base_access (Vala.BaseAccess expr) {
437 signature.append_keyword ("base", false);
441 * {@inheritDoc}
443 public override void visit_postfix_expression (Vala.PostfixExpression expr) {
444 expr.inner.accept (this);
445 if (expr.increment) {
446 signature.append ("++", false);
447 } else {
448 signature.append ("--", false);
453 * {@inheritDoc}
455 public override void visit_object_creation_expression (Vala.ObjectCreationExpression expr) {
456 if (!expr.struct_creation) {
457 signature.append_keyword ("new");
460 signature.append_symbol (resolve (expr.symbol_reference));
462 signature.append (" (", false);
464 //TODO: rm conditional space
465 bool first = true;
466 foreach (Vala.Expression arg in expr.get_argument_list ()) {
467 if (!first) {
468 signature.append (", ", false);
470 arg.accept (this);
471 first = false;
474 signature.append (")", false);
478 * {@inheritDoc}
480 public override void visit_sizeof_expression (Vala.SizeofExpression expr) {
481 signature.append_keyword ("sizeof", false).append (" (", false);
482 write_type (expr.type_reference);
483 signature.append (")", false);
487 * {@inheritDoc}
489 public override void visit_typeof_expression (Vala.TypeofExpression expr) {
490 signature.append_keyword ("typeof", false).append (" (", false);
491 write_type (expr.type_reference);
492 signature.append (")", false);
496 * {@inheritDoc}
498 public override void visit_lambda_expression (Vala.LambdaExpression expr) {
499 signature.append ("(", false);
501 bool first = true;
502 foreach (Vala.Parameter param in expr.get_parameters ()) {
503 if (!first) {
504 signature.append (", ", false);
506 signature.append (param.name, false);
507 first = false;
511 signature.append (") => {", false);
512 signature.append_highlighted (" [...] ", false);
513 signature.append ("}", false);
519 * {@inheritDoc}
521 public override void visit_boolean_literal (Vala.BooleanLiteral lit) {
522 signature.append_literal (lit.to_string (), false);
526 * {@inheritDoc}
528 public override void visit_character_literal (Vala.CharacterLiteral lit) {
529 signature.append_literal (lit.to_string (), false);
533 * {@inheritDoc}
535 public override void visit_integer_literal (Vala.IntegerLiteral lit) {
536 signature.append_literal (lit.to_string (), false);
540 * {@inheritDoc}
542 public override void visit_real_literal (Vala.RealLiteral lit) {
543 signature.append_literal (lit.to_string (), false);
547 * {@inheritDoc}
549 public override void visit_regex_literal (Vala.RegexLiteral lit) {
550 signature.append_literal (lit.to_string (), false);
554 * {@inheritDoc}
556 public override void visit_string_literal (Vala.StringLiteral lit) {
557 signature.append_literal (lit.to_string (), false);
561 * {@inheritDoc}
563 public override void visit_null_literal (Vala.NullLiteral lit) {
564 signature.append_literal (lit.to_string (), false);
570 * {@inheritDoc}
572 public override void visit_field (Vala.Field field) {
573 write_node (field);
577 * {@inheritDoc}
579 public override void visit_constant (Vala.Constant constant) {
580 write_node (constant);
584 * {@inheritDoc}
586 public override void visit_enum_value (Vala.EnumValue ev) {
587 write_node (ev);
591 * {@inheritDoc}
593 public override void visit_error_code (Vala.ErrorCode ec) {
594 write_node (ec);
598 * {@inheritDoc}
600 public override void visit_delegate (Vala.Delegate d) {
601 write_node (d);
605 * {@inheritDoc}
607 public override void visit_method (Vala.Method m) {
608 write_node (m);
612 * {@inheritDoc}
614 public override void visit_creation_method (Vala.CreationMethod m) {
615 write_node (m);
619 * {@inheritDoc}
621 public override void visit_signal (Vala.Signal sig) {
622 write_node (sig);
626 * {@inheritDoc}
628 public override void visit_class (Vala.Class c) {
629 write_node (c);
633 * {@inheritDoc}
635 public override void visit_struct (Vala.Struct s) {
636 write_node (s);
640 * {@inheritDoc}
642 public override void visit_interface (Vala.Interface i) {
643 write_node (i);
647 * {@inheritDoc}
649 public override void visit_enum (Vala.Enum en) {
650 write_node (en);
654 * {@inheritDoc}
656 public override void visit_error_domain (Vala.ErrorDomain ed) {
657 write_node (ed);
661 * {@inheritDoc}
663 public override void visit_property (Vala.Property prop) {
664 write_node (prop);