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