| 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 class CGBlock { | 5 class CGBlock { |
| 6 int _blockType; // Code type of this block | 6 int _blockType; // Code type of this block |
| 7 int _indent; // Number of spaces to prefix for each statement | 7 int _indent; // Number of spaces to prefix for each statement |
| 8 bool _inEach; // This block or any currently active blocks is a | 8 bool _inEach; // This block or any currently active blocks is a |
| 9 // #each. If so then any element marked with a | 9 // #each. If so then any element marked with a |
| 10 // var attribute is repeated therefore the var | 10 // var attribute is repeated therefore the var |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 void pop() { | 51 void pop() { |
| 52 _stmts.removeLast(); | 52 _stmts.removeLast(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void add(String value) { | 55 void add(String value) { |
| 56 if (_stmts.last() != null) { | 56 if (_stmts.last() != null) { |
| 57 _stmts.last().add(value); | 57 _stmts.last().add(value); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool get anyStatements() => _stmts.length > 0; |
| 62 |
| 61 CGStatement get last() => _stmts.last(); | 63 CGStatement get last() => _stmts.last(); |
| 62 | 64 |
| 63 /** | 65 /** |
| 64 * Returns mixed list of elements marked with the var attribute. If the | 66 * Returns mixed list of elements marked with the var attribute. If the |
| 65 * element is inside of a #each the name exposed is: | 67 * element is inside of a #each the name exposed is: |
| 66 * | 68 * |
| 67 * List varName; | 69 * List varName; |
| 68 * | 70 * |
| 69 * otherwise it's: | 71 * otherwise it's: |
| 70 * | 72 * |
| (...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 if (block.isEach) { | 565 if (block.isEach) { |
| 564 result = result || true; | 566 result = result || true; |
| 565 } | 567 } |
| 566 } | 568 } |
| 567 | 569 |
| 568 return result; | 570 return result; |
| 569 } | 571 } |
| 570 | 572 |
| 571 bool pushBlock([int indent = 4, int blockType = CGBlock.CONSTRUCTOR, | 573 bool pushBlock([int indent = 4, int blockType = CGBlock.CONSTRUCTOR, |
| 572 String itemName = null]) { | 574 String itemName = null]) { |
| 573 closeStatement(); | |
| 574 if (itemName != null && matchBlocksLocalName(itemName)) { | 575 if (itemName != null && matchBlocksLocalName(itemName)) { |
| 575 world.error("Active block already exist with local name: ${itemName}."); | 576 world.error("Active block already exist with local name: ${itemName}."); |
| 576 return false; | 577 return false; |
| 577 } else if (itemName == null && this.isNestedBlock()) { | 578 } else if (itemName == null && this.isNestedBlock()) { |
| 578 world.error(''' | 579 world.error(''' |
| 579 Nested #each or #with must have a localName; | 580 Nested #each or #with must have a localName; |
| 580 \n #each list [localName]\n #with object [localName]'''); | 581 \n #each list [localName]\n #with object [localName]'''); |
| 581 return false; | 582 return false; |
| 582 } | 583 } |
| 583 _cgBlocks.add( | 584 _cgBlocks.add( |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 835 | 836 |
| 836 popBlock(); | 837 popBlock(); |
| 837 | 838 |
| 838 funcBuff.add(" }\n"); | 839 funcBuff.add(" }\n"); |
| 839 funcBuff.add(" }\n"); | 840 funcBuff.add(" }\n"); |
| 840 | 841 |
| 841 eachs[eachIndex] = funcBuff.toString(); | 842 eachs[eachIndex] = funcBuff.toString(); |
| 842 | 843 |
| 843 // If nested each then we want to pass the parent otherwise we'll use the | 844 // If nested each then we want to pass the parent otherwise we'll use the |
| 844 // varName. | 845 // varName. |
| 845 var varName = nestedImmediateEach ? "parent" : lastBlock.last.variableName; | 846 var varName = nestedImmediateEach ? "parent" : lastBlockVarName; |
| 846 | 847 |
| 847 pushExactStatement(elem, parentVarOrIdx); | 848 pushExactStatement(elem, parentVarOrIdx); |
| 848 | 849 |
| 849 // Setup call to each func as "each_n(xxxxx, " the parent param is filled | 850 // Setup call to each func as "each_n(xxxxx, " the parent param is filled |
| 850 // in later when we known the parent variable. | 851 // in later when we known the parent variable. |
| 851 String eachParam = | 852 String eachParam = |
| 852 (itemName == null) ? eachIterNameToItem(iterName) : iterName; | 853 (itemName == null) ? eachIterNameToItem(iterName) : iterName; |
| 853 add("${funcName}(${eachParam}, ${varName})"); | 854 add("${funcName}(${eachParam}, ${varName})"); |
| 854 } | 855 } |
| 855 | 856 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 884 addScope(4, funcBuff, itemName); | 885 addScope(4, funcBuff, itemName); |
| 885 funcBuff.add(codeBody); | 886 funcBuff.add(codeBody); |
| 886 removeScope(4, funcBuff, itemName); | 887 removeScope(4, funcBuff, itemName); |
| 887 | 888 |
| 888 popBlock(); | 889 popBlock(); |
| 889 | 890 |
| 890 funcBuff.add(" }\n"); | 891 funcBuff.add(" }\n"); |
| 891 | 892 |
| 892 withs[withIndex] = funcBuff.toString(); | 893 withs[withIndex] = funcBuff.toString(); |
| 893 | 894 |
| 894 var varName = lastBlock.last.variableName; | 895 // Compute parent node variable before pushing with statement. |
| 896 String parentVarName = lastBlockVarName; |
| 895 | 897 |
| 896 pushExactStatement(elem, parentVarIndex); | 898 pushExactStatement(elem, parentVarIndex); |
| 897 | 899 |
| 898 // Setup call to each func as "each_n(xxxxx, " the parent param is filled | 900 // Setup call to each func as "each_n(xxxxx, " the parent param is filled |
| 899 // in later when we known the parent variable. | 901 // in later when we known the parent variable. |
| 900 add("${funcName}(${withName}, ${varName})"); | 902 add("${funcName}(${withName}, ${parentVarName})"); |
| 903 } |
| 904 |
| 905 String get lastBlockVarName() { |
| 906 var varName; |
| 907 if (lastBlock != null && lastBlock.anyStatements) { |
| 908 varName = lastBlock.last.variableName; |
| 909 } else { |
| 910 varName = "_fragment"; |
| 911 } |
| 912 |
| 913 return varName; |
| 901 } | 914 } |
| 902 | 915 |
| 903 String injectParamName(String name) { | 916 String injectParamName(String name) { |
| 904 // Local name _item is reserved. | 917 // Local name _item is reserved. |
| 905 if (name != null && name == "_item") { | 918 if (name != null && name == "_item") { |
| 906 return null; // Local name is not valid. | 919 return null; // Local name is not valid. |
| 907 } | 920 } |
| 908 | 921 |
| 909 return (name == null) ? "_item" : name; | 922 return (name == null) ? "_item" : name; |
| 910 } | 923 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 937 for (String name in names) { | 950 for (String name in names) { |
| 938 buff.add(" var ${name} = _scopes[\"${name}\"];\n"); | 951 buff.add(" var ${name} = _scopes[\"${name}\"];\n"); |
| 939 } | 952 } |
| 940 buff.add("\n"); | 953 buff.add("\n"); |
| 941 } | 954 } |
| 942 | 955 |
| 943 return buff.toString(); | 956 return buff.toString(); |
| 944 } | 957 } |
| 945 | 958 |
| 946 } | 959 } |
| OLD | NEW |