OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library immic.plugins.shared; | |
6 | |
7 import 'package:strings/strings.dart' as strings; | |
8 import 'package:path/path.dart' show basename, basenameWithoutExtension; | |
9 | |
10 import '../parser.dart'; | |
11 export '../parser.dart'; | |
12 | |
13 abstract class CodeGenerationVisitor extends Visitor { | |
14 final String path; | |
15 final StringBuffer buffer = new StringBuffer(); | |
16 HashMap<String, List<Type>> methodSignatures = {}; | |
17 | |
18 CodeGenerationVisitor(this.path); | |
19 | |
20 String get libraryFile => basenameWithoutExtension(path); | |
21 String get libraryName => 'immi.$libraryFile'; | |
22 String get baseName => camelize(libraryName); | |
23 String serviceName = 'ImmiServiceLayer'; | |
24 String serviceFile = 'immi_service'; | |
25 String serviceImplName = 'ImmiServiceImpl'; | |
26 String serviceImplFile = 'immi_service_impl'; | |
27 String serviceImplLib = 'immi_service_impl'; | |
28 String immiGenPkg = 'package:immi'; | |
29 String serviceGenPkg = 'package:service'; | |
30 | |
31 void write(String s) => buffer.write(s); | |
32 void writeln([String s = '']) => buffer.writeln(s); | |
33 void writeComma() => buffer.write(', '); | |
34 | |
35 String camelize(String name) { | |
36 return strings.camelize(strings.underscore(name)); | |
37 } | |
38 | |
39 void forEachSlot(Struct node, | |
40 void sep(), | |
41 void f(Type slotType, String slotName)) { | |
42 bool first = true; | |
43 for (StructSlot slot in node.layout.slots) { | |
44 if (!first && sep != null) sep(); | |
45 f(slot.slot.type, slot.slot.name); | |
46 first = false; | |
47 } | |
48 } | |
49 | |
50 void forEachSlotAndMethod(Struct node, | |
51 void sep(), | |
52 void f(field, String name)) { | |
53 bool first = true; | |
54 for (StructSlot slot in node.layout.slots) { | |
55 if (!first && sep != null) sep(); | |
56 f(slot.slot, slot.slot.name); | |
57 first = false; | |
58 } | |
59 for (Method method in node.methods) { | |
60 if (!first && sep != null) sep(); | |
61 f(method, method.name); | |
62 first = false; | |
63 } | |
64 } | |
65 | |
66 visit(Node node) => node.accept(this); | |
67 | |
68 visitStruct(Struct node) { | |
69 // TODO(kasper): Stop ignoring this. | |
70 } | |
71 | |
72 visitUnion(Union node) { | |
73 // TODO(kasper): Stop ignoring this. | |
74 } | |
75 | |
76 visitImport(Import node) { | |
77 } | |
78 | |
79 visitNodes(List<Node> nodes, String separatedBy(bool first)) { | |
80 bool first = true; | |
81 for (int i = 0; i < nodes.length; i++) { | |
82 write(separatedBy(first)); | |
83 if (first) first = false; | |
84 visit(nodes[i]); | |
85 } | |
86 } | |
87 | |
88 void collectMethodSignatures(Unit unit) { | |
89 for (var node in unit.structs) { | |
90 for (var method in node.methods) { | |
91 assert(method.returnType.isVoid); | |
92 String signature = | |
93 method.arguments.map((formal) => formal.type.identifier); | |
94 methodSignatures.putIfAbsent('$signature', () { | |
95 return method.arguments.map((formal) => formal.type); | |
96 }); | |
97 } | |
98 } | |
99 } | |
100 | |
101 String actionTypeSuffix(List<Type> types) { | |
102 if (types.isEmpty) return 'Void'; | |
103 return types.map((Type type) => camelize(type.identifier)).join(); | |
104 } | |
105 | |
106 Iterable mapWithIndex(List list, f(int i, element)) { | |
107 int i = 0; | |
108 return list.map((e) => f(i++, e)); | |
109 } | |
110 } | |
OLD | NEW |