OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Utilities for building JS ASTs at runtime. Contains a builder class | 5 // Utilities for building JS ASTs at runtime. Contains a builder class |
6 // and a parser that parses part of the language. | 6 // and a parser that parses part of the language. |
7 | 7 |
8 part of js_ast; | 8 part of js_ast; |
9 | 9 |
10 | 10 |
(...skipping 822 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 * ( ) | 833 * ( ) |
834 * ( ... BindingIdentifier ) | 834 * ( ... BindingIdentifier ) |
835 * ( Expression , ... BindingIdentifier ) | 835 * ( Expression , ... BindingIdentifier ) |
836 */ | 836 */ |
837 Expression parseExpressionOrArrowFunction() { | 837 Expression parseExpressionOrArrowFunction() { |
838 if (acceptCategory(RPAREN)) { | 838 if (acceptCategory(RPAREN)) { |
839 expectCategory(ARROW); | 839 expectCategory(ARROW); |
840 return parseArrowFunctionBody(<Parameter>[]); | 840 return parseArrowFunctionBody(<Parameter>[]); |
841 } | 841 } |
842 if (acceptCategory(ELLIPSIS)) { | 842 if (acceptCategory(ELLIPSIS)) { |
843 var params = <Parameter>[new RestParameter(parseParameter())]; | 843 var params = <Parameter>[parseParameter(isRest: true)]; |
844 expectCategory(RPAREN); | 844 expectCategory(RPAREN); |
845 expectCategory(ARROW); | 845 expectCategory(ARROW); |
846 return parseArrowFunctionBody(params); | 846 return parseArrowFunctionBody(params); |
847 } | 847 } |
848 Expression expression = parseAssignment(); | 848 Expression expression = parseAssignment(); |
849 while (acceptCategory(COMMA)) { | 849 while (acceptCategory(COMMA)) { |
850 if (acceptCategory(ELLIPSIS)) { | 850 if (acceptCategory(ELLIPSIS)) { |
851 var params = <Parameter>[]; | 851 var params = <Parameter>[]; |
852 _expressionToParameterList(expression, params); | 852 _expressionToParameterList(expression, params); |
853 params.add(new RestParameter(parseParameter())); | 853 params.add(parseParameter(isRest: true)); |
854 expectCategory(RPAREN); | 854 expectCategory(RPAREN); |
855 expectCategory(ARROW); | 855 expectCategory(ARROW); |
856 return parseArrowFunctionBody(params); | 856 return parseArrowFunctionBody(params); |
857 } | 857 } |
858 Expression right = parseAssignment(); | 858 Expression right = parseAssignment(); |
859 expression = new Binary(',', expression, right); | 859 expression = new Binary(',', expression, right); |
860 } | 860 } |
861 expectCategory(RPAREN); | 861 expectCategory(RPAREN); |
862 if (acceptCategory(ARROW)) { | 862 if (acceptCategory(ARROW)) { |
863 var params = <Parameter>[]; | 863 var params = <Parameter>[]; |
864 _expressionToParameterList(expression, params); | 864 _expressionToParameterList(expression, params); |
865 return parseArrowFunctionBody(params); | 865 return parseArrowFunctionBody(params); |
866 } | 866 } |
867 return expression; | 867 return expression; |
868 } | 868 } |
869 | 869 |
870 /** | 870 /** |
871 * Converts a parenthesized expression into a list of parameters, issuing an | 871 * Converts a parenthesized expression into a list of parameters, issuing an |
872 * error if the conversion fails. | 872 * error if the conversion fails. |
873 */ | 873 */ |
874 void _expressionToParameterList(Expression node, List<Parameter> params) { | 874 void _expressionToParameterList(Expression node, List<Parameter> params) { |
875 if (node is Identifier) { | 875 if (node is Identifier) { |
876 params.add(node); | 876 params.add(new Parameter(node)); |
877 } else if (node is Binary && node.op == ',') { | 877 } else if (node is Binary && node.op == ',') { |
878 // TODO(jmesserly): this will allow illegal parens, such as | 878 // TODO(jmesserly): this will allow illegal parens, such as |
879 // `((a, b), (c, d))`. Fixing it on the left side needs an explicit | 879 // `((a, b), (c, d))`. Fixing it on the left side needs an explicit |
880 // ParenthesizedExpression node, so we can distinguish | 880 // ParenthesizedExpression node, so we can distinguish |
881 // `((a, b), c)` from `(a, b, c)`. | 881 // `((a, b), c)` from `(a, b, c)`. |
882 _expressionToParameterList(node.left, params); | 882 _expressionToParameterList(node.left, params); |
883 _expressionToParameterList(node.right, params); | 883 _expressionToParameterList(node.right, params); |
884 } else if (node is InterpolatedExpression) { | 884 } else if (node is InterpolatedExpression) { |
885 params.add(new InterpolatedParameter(node.nameOrPosition)); | 885 params.add(new InterpolatedParameter(node.nameOrPosition)); |
886 } else { | 886 } else { |
(...skipping 21 matching lines...) Expand all Loading... |
908 return parseFun(); | 908 return parseFun(); |
909 } | 909 } |
910 | 910 |
911 Expression parseFun() { | 911 Expression parseFun() { |
912 List<Parameter> params = <Parameter>[]; | 912 List<Parameter> params = <Parameter>[]; |
913 | 913 |
914 expectCategory(LPAREN); | 914 expectCategory(LPAREN); |
915 if (!acceptCategory(RPAREN)) { | 915 if (!acceptCategory(RPAREN)) { |
916 for (;;) { | 916 for (;;) { |
917 if (acceptCategory(ELLIPSIS)) { | 917 if (acceptCategory(ELLIPSIS)) { |
918 params.add(new RestParameter(parseParameter())); | 918 params.add(parseParameter(isRest: true)); |
919 expectCategory(RPAREN); | 919 expectCategory(RPAREN); |
920 break; | 920 break; |
921 } | 921 } |
922 | 922 |
923 params.add(parseParameter()); | 923 params.add(parseParameter()); |
924 if (!acceptCategory(COMMA)) { | 924 if (!acceptCategory(COMMA)) { |
925 expectCategory(RPAREN); | 925 expectCategory(RPAREN); |
926 break; | 926 break; |
927 } | 927 } |
928 } | 928 } |
(...skipping 10 matching lines...) Expand all Loading... |
939 asyncModifier = const AsyncModifier.syncStar(); | 939 asyncModifier = const AsyncModifier.syncStar(); |
940 } else { | 940 } else { |
941 asyncModifier = const AsyncModifier.sync(); | 941 asyncModifier = const AsyncModifier.sync(); |
942 } | 942 } |
943 expectCategory(LBRACE); | 943 expectCategory(LBRACE); |
944 Block block = parseBlock(); | 944 Block block = parseBlock(); |
945 return new Fun(params, block, asyncModifier: asyncModifier); | 945 return new Fun(params, block, asyncModifier: asyncModifier); |
946 } | 946 } |
947 | 947 |
948 /** Parse parameter name or interpolated parameter. */ | 948 /** Parse parameter name or interpolated parameter. */ |
949 Identifier parseParameter() { | 949 Parameter parseParameter({bool isRest: false}) { |
950 if (acceptCategory(HASH)) { | 950 if (acceptCategory(HASH)) { |
951 var nameOrPosition = parseHash(); | 951 var nameOrPosition = parseHash(); |
952 var parameter = new InterpolatedParameter(nameOrPosition); | 952 var parameter = new InterpolatedParameter(nameOrPosition, isRest: isRest); |
953 interpolatedValues.add(parameter); | 953 interpolatedValues.add(parameter); |
954 return parameter; | 954 return parameter; |
955 } else { | 955 } else { |
956 // TODO(jmesserly): validate this is not a keyword | 956 // TODO(jmesserly): validate this is not a keyword |
957 String argumentName = lastToken; | 957 String argumentName = lastToken; |
958 expectCategory(ALPHA); | 958 expectCategory(ALPHA); |
959 return new Identifier(argumentName); | 959 return new Parameter(new Identifier(argumentName), isRest: isRest); |
960 } | 960 } |
961 } | 961 } |
962 | 962 |
963 Expression parseObjectInitializer() { | 963 Expression parseObjectInitializer() { |
964 List<Property> properties = <Property>[]; | 964 List<Property> properties = <Property>[]; |
965 for (;;) { | 965 for (;;) { |
966 if (acceptCategory(RBRACE)) break; | 966 if (acceptCategory(RBRACE)) break; |
967 // Limited subset of ES6 object initializers. | 967 // Limited subset of ES6 object initializers. |
968 // | 968 // |
969 // PropertyDefinition : | 969 // PropertyDefinition : |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1642 expectCategory(RSQUARE); | 1642 expectCategory(RSQUARE); |
1643 return expr; | 1643 return expr; |
1644 } else if (acceptCategory(HASH)) { | 1644 } else if (acceptCategory(HASH)) { |
1645 return parseInterpolatedExpression(); | 1645 return parseInterpolatedExpression(); |
1646 } else { | 1646 } else { |
1647 error('Expected property name'); | 1647 error('Expected property name'); |
1648 return null; | 1648 return null; |
1649 } | 1649 } |
1650 } | 1650 } |
1651 } | 1651 } |
OLD | NEW |