| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart 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 List sorted(Iterable input, [compare, key]) { | |
| 6 comparator(compare, key) { | |
| 7 if (compare == null && key == null) | |
| 8 return (a, b) => a.compareTo(b); | |
| 9 if (compare == null) | |
| 10 return (a, b) => key(a).compareTo(key(b)); | |
| 11 if (key == null) | |
| 12 return compare; | |
| 13 return (a, b) => compare(key(a), key(b)); | |
| 14 } | |
| 15 List copy = new List.from(input); | |
| 16 copy.sort(comparator(compare, key)); | |
| 17 return copy; | |
| 18 } | |
| 19 | |
| 20 render(idl_node, [indent_str=' ']) { | |
| 21 var output = ['']; | |
| 22 var indent_stack = []; | |
| 23 | |
| 24 //indented(action()) { | |
| 25 indented(action) { | |
| 26 indent_stack.add(indent_str); | |
| 27 action(); | |
| 28 indent_stack.removeLast(); | |
| 29 } | |
| 30 | |
| 31 sort(nodes) => sorted(nodes, key: (a) => a.id); | |
| 32 | |
| 33 var w; // For some reason mutually recursive local functions don't work. | |
| 34 | |
| 35 wln([node=null]) { | |
| 36 w(node); | |
| 37 output.add('\n'); | |
| 38 } | |
| 39 | |
| 40 w = (node, [list_separator]) { | |
| 41 /* | |
| 42 Writes the given node. | |
| 43 | |
| 44 Args: | |
| 45 node -- a string, IDLNode instance or a list of such. | |
| 46 list_separator -- if provided, and node is a list, | |
| 47 list_separator will be written between the list items. | |
| 48 */ | |
| 49 if (node == null) { | |
| 50 return; | |
| 51 } else if (node is String) { | |
| 52 if (output.last().endsWith('\n')) | |
| 53 output.addAll(indent_stack); | |
| 54 output.add(node); | |
| 55 } else if (node is List) { | |
| 56 var separator = null; | |
| 57 for (var element in node) { | |
| 58 w(separator); | |
| 59 separator = list_separator; | |
| 60 w(element); | |
| 61 } | |
| 62 } else if (node is IDLFile) { | |
| 63 w(node.modules); | |
| 64 w(node.interfaces); | |
| 65 } else if (node is IDLModule) { | |
| 66 w(node.annotations); | |
| 67 w(node.extAttrs); | |
| 68 wln('module ${node.id} {'); | |
| 69 indented(() { | |
| 70 w(node.interfaces); | |
| 71 w(node.typedefs); | |
| 72 }); | |
| 73 wln('};'); | |
| 74 } else if (node is IDLInterface) { | |
| 75 w(node.annotations); | |
| 76 w(node.extAttrs); | |
| 77 w('interface ${node.id}'); | |
| 78 indented(() { | |
| 79 if (!node.parents.isEmpty()) { | |
| 80 wln(' :'); | |
| 81 w(node.parents, ',\n'); | |
| 82 } | |
| 83 wln(' {'); | |
| 84 section(list, comment) { | |
| 85 if (list != null && !list.isEmpty()) { | |
| 86 wln(); | |
| 87 wln(comment); | |
| 88 w(sort(list)); | |
| 89 } | |
| 90 } | |
| 91 section(node.constants, '/* Constants */'); | |
| 92 section(node.attributes, '/* Attributes */'); | |
| 93 section(node.operations, '/* Operations */'); | |
| 94 section(node.snippets, '/* Snippets */'); | |
| 95 }); | |
| 96 wln('};'); | |
| 97 } else if (node is IDLParentInterface) { | |
| 98 w(node.annotations); | |
| 99 w(node.type.id); | |
| 100 } else if (node is IDLAnnotations) { | |
| 101 for (var name in sorted(node.map.getKeys())) { | |
| 102 IDLAnnotation annotation = node.map[name]; | |
| 103 var args = annotation.map; | |
| 104 if (args.isEmpty()) { | |
| 105 w('@$name'); | |
| 106 } else { | |
| 107 var formattedArgs = []; | |
| 108 for (var argName in sorted(args.getKeys())) { | |
| 109 var argValue = args[argName]; | |
| 110 if (argValue == null) | |
| 111 formattedArgs.add(argName); | |
| 112 else | |
| 113 formattedArgs.add('$argName=$argValue'); | |
| 114 } | |
| 115 w('@$name(${Strings.join(formattedArgs,',')})'); | |
| 116 } | |
| 117 w(' '); | |
| 118 } | |
| 119 } else if (node is IDLExtAttrs) { | |
| 120 if(!node.map.isEmpty()) { | |
| 121 w('['); | |
| 122 var sep = null; | |
| 123 for (var name in sorted(node.map.getKeys())) { | |
| 124 w(sep); | |
| 125 sep = ', '; | |
| 126 w(name); | |
| 127 var value = node.map[name]; | |
| 128 if (value != null) { | |
| 129 w('='); | |
| 130 w(value); | |
| 131 } | |
| 132 } | |
| 133 w('] '); | |
| 134 } | |
| 135 } else if (node is IDLAttribute) { | |
| 136 w(node.annotations); | |
| 137 w(node.extAttrs); | |
| 138 if (node.isFcGetter) | |
| 139 w('getter '); | |
| 140 if (node.isFcSetter) | |
| 141 w('setter '); | |
| 142 wln('attribute ${node.type.id} ${node.id};'); | |
| 143 } else if (node is IDLConstant) { | |
| 144 w(node.annotations); | |
| 145 w(node.extAttrs); | |
| 146 wln('const ${node.type.id} ${node.id} = ${node.value};'); | |
| 147 } else if (node is IDLSnippet) { | |
| 148 w(node.annotations); | |
| 149 wln('snippet {${node.text}};'); | |
| 150 } else if (node is IDLOperation) { | |
| 151 w(node.annotations); | |
| 152 w(node.extAttrs); | |
| 153 if (node.specials != null && !node.specials.isEmpty()) { | |
| 154 w(node.specials, ' '); | |
| 155 w(' '); | |
| 156 } | |
| 157 w('${node.type.id} ${node.id}'); | |
| 158 w('('); | |
| 159 w(node.arguments, ', '); | |
| 160 wln(');'); | |
| 161 } else if (node is IDLArgument) { | |
| 162 w(node.extAttrs); | |
| 163 w('in '); | |
| 164 if (node.isOptional) | |
| 165 w('optional '); | |
| 166 w('${node.type.id} ${node.id}'); | |
| 167 } else if (node is IDLExtAttrFunctionValue) { | |
| 168 w(node.name); | |
| 169 w('('); | |
| 170 w(node.arguments, ', '); | |
| 171 w(')'); | |
| 172 } else if (node is IDLTypeDef) { | |
| 173 wln('typedef ${node.type.id} ${node.id};'); | |
| 174 } else { | |
| 175 w('// $node\n'); | |
| 176 } | |
| 177 }; | |
| 178 | |
| 179 w(idl_node); | |
| 180 return Strings.concatAll(output); | |
| 181 } | |
| OLD | NEW |