Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Side by Side Diff: pkg/front_end/lib/src/fasta/analyzer/ast_builder.dart

Issue 2709843004: Support for modifiers. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/test/generated/parser_fasta_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 library fasta.analyzer.ast_builder; 5 library fasta.analyzer.ast_builder;
6 6
7 import 'package:front_end/src/fasta/scanner/token.dart' 7 import 'package:front_end/src/fasta/scanner/token.dart'
8 show BeginGroupToken, Token; 8 show BeginGroupToken, Token;
9 9
10 import 'package:analyzer/analyzer.dart'; 10 import 'package:analyzer/analyzer.dart';
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 push(ast.throwExpression(toAnalyzerToken(throwToken), pop())); 502 push(ast.throwExpression(toAnalyzerToken(throwToken), pop()));
503 } 503 }
504 504
505 void endFormalParameter(Token thisKeyword) { 505 void endFormalParameter(Token thisKeyword) {
506 debugEvent("FormalParameter"); 506 debugEvent("FormalParameter");
507 if (thisKeyword != null) { 507 if (thisKeyword != null) {
508 internalError("'this' can't be used here."); 508 internalError("'this' can't be used here.");
509 } 509 }
510 SimpleIdentifier name = pop(); 510 SimpleIdentifier name = pop();
511 TypeName type = pop(); 511 TypeName type = pop();
512 pop(); // Modifiers. 512 Token keyword = _popOptionalSingleModifier();
513 pop(); // Metadata. 513 pop(); // Metadata.
514 SimpleFormalParameter node = ast.simpleFormalParameter( 514 SimpleFormalParameter node = ast.simpleFormalParameter(
515 null, null, toAnalyzerToken(thisKeyword), type, name); 515 null, null, toAnalyzerToken(keyword), type, name);
516 scope[name.name] = name.staticElement = new AnalyzerParameterElement(node); 516 scope[name.name] = name.staticElement = new AnalyzerParameterElement(node);
517 push(node); 517 push(node);
518 } 518 }
519 519
520 void endFormalParameters(int count, Token beginToken, Token endToken) { 520 void endFormalParameters(int count, Token beginToken, Token endToken) {
521 debugEvent("FormalParameters"); 521 debugEvent("FormalParameters");
522 List<FormalParameter> parameters = popList(count) ?? <FormalParameter>[]; 522 List<FormalParameter> parameters = popList(count) ?? <FormalParameter>[];
523 push(ast.formalParameterList(toAnalyzerToken(beginToken), parameters, null, 523 push(ast.formalParameterList(toAnalyzerToken(beginToken), parameters, null,
524 null, toAnalyzerToken(endToken))); 524 null, toAnalyzerToken(endToken)));
525 } 525 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 push(ast.prefixExpression(toAnalyzerToken(token), pop())); 603 push(ast.prefixExpression(toAnalyzerToken(token), pop()));
604 } 604 }
605 605
606 void handleUnaryPostfixAssignmentExpression(Token token) { 606 void handleUnaryPostfixAssignmentExpression(Token token) {
607 debugEvent("UnaryPostfixAssignmentExpression"); 607 debugEvent("UnaryPostfixAssignmentExpression");
608 push(ast.postfixExpression(pop(), toAnalyzerToken(token))); 608 push(ast.postfixExpression(pop(), toAnalyzerToken(token)));
609 } 609 }
610 610
611 void handleModifier(Token token) { 611 void handleModifier(Token token) {
612 debugEvent("Modifier"); 612 debugEvent("Modifier");
613 // TODO(ahe): Don't ignore modifiers. 613 push(token);
614 } 614 }
615 615
616 void handleModifiers(int count) { 616 void handleModifiers(int count) {
617 debugEvent("Modifiers"); 617 debugEvent("Modifiers");
618 // TODO(ahe): Don't ignore modifiers. 618 push(popList(count) ?? const <Token>[]);
619 push(NullValue.Modifiers);
620 } 619 }
621 620
622 FunctionBody _endFunctionBody() { 621 FunctionBody _endFunctionBody() {
623 AstNode body = pop(); 622 AstNode body = pop();
624 // TODO(paulberry): asyncMarker should have a type that allows constructing 623 // TODO(paulberry): asyncMarker should have a type that allows constructing
625 // the necessary analyzer AST data structures. 624 // the necessary analyzer AST data structures.
626 AsyncMarker asyncMarker = pop(); 625 AsyncMarker asyncMarker = pop();
627 assert(asyncMarker == AsyncMarker.Sync); 626 assert(asyncMarker == AsyncMarker.Sync);
628 analyzer.Token asyncKeyword = null; 627 analyzer.Token asyncKeyword = null;
629 analyzer.Token star = null; 628 analyzer.Token star = null;
(...skipping 12 matching lines...) Expand all
642 void endTopLevelMethod(Token beginToken, Token getOrSet, Token endToken) { 641 void endTopLevelMethod(Token beginToken, Token getOrSet, Token endToken) {
643 // TODO(paulberry): set up scopes properly to resolve parameters and type 642 // TODO(paulberry): set up scopes properly to resolve parameters and type
644 // variables. 643 // variables.
645 debugEvent("TopLevelMethod"); 644 debugEvent("TopLevelMethod");
646 FunctionBody body = _endFunctionBody(); 645 FunctionBody body = _endFunctionBody();
647 FormalParameterList parameters = pop(); 646 FormalParameterList parameters = pop();
648 TypeParameterList typeParameters = pop(); 647 TypeParameterList typeParameters = pop();
649 SimpleIdentifier name = pop(); 648 SimpleIdentifier name = pop();
650 analyzer.Token propertyKeyword = toAnalyzerToken(getOrSet); 649 analyzer.Token propertyKeyword = toAnalyzerToken(getOrSet);
651 TypeAnnotation returnType = pop(); 650 TypeAnnotation returnType = pop();
652 // TODO(paulberry): handle modifiers. 651 Token externalKeyword = _popOptionalSingleModifier();
653 var modifiers = pop();
654 assert(modifiers == null);
655 analyzer.Token externalKeyword = null;
656 List<Annotation> metadata = pop(); 652 List<Annotation> metadata = pop();
657 // TODO(paulberry): capture doc comments. See dartbug.com/28851. 653 // TODO(paulberry): capture doc comments. See dartbug.com/28851.
658 Comment comment = null; 654 Comment comment = null;
659 push(ast.functionDeclaration( 655 push(ast.functionDeclaration(
660 comment, 656 comment,
661 metadata, 657 metadata,
662 externalKeyword, 658 toAnalyzerToken(externalKeyword),
663 returnType, 659 returnType,
664 propertyKeyword, 660 propertyKeyword,
665 name, 661 name,
666 ast.functionExpression(typeParameters, parameters, body))); 662 ast.functionExpression(typeParameters, parameters, body)));
667 } 663 }
668 664
669 @override 665 @override
670 void endTopLevelDeclaration(Token token) { 666 void endTopLevelDeclaration(Token token) {
671 debugEvent("TopLevelDeclaration"); 667 debugEvent("TopLevelDeclaration");
672 } 668 }
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 TypeParameterList typeParameters = pop(); 829 TypeParameterList typeParameters = pop();
834 SimpleIdentifier name = pop(); 830 SimpleIdentifier name = pop();
835 Token classKeyword; 831 Token classKeyword;
836 // TODO(paulberry,ahe): This is a hack. The parser should give us the class 832 // TODO(paulberry,ahe): This is a hack. The parser should give us the class
837 // keyword. 833 // keyword.
838 if (optional('abstract', beginToken)) { 834 if (optional('abstract', beginToken)) {
839 classKeyword = beginToken.next; 835 classKeyword = beginToken.next;
840 } else { 836 } else {
841 classKeyword = beginToken; 837 classKeyword = beginToken;
842 } 838 }
843 var modifiers = pop(); 839 Token abstractKeyword = _popOptionalSingleModifier();
844 assert(modifiers == null); // TODO(paulberry)
845 analyzer.Token abstractKeyword;
846 List<Annotation> metadata = pop(); 840 List<Annotation> metadata = pop();
847 // TODO(paulberry): capture doc comments. See dartbug.com/28851. 841 // TODO(paulberry): capture doc comments. See dartbug.com/28851.
848 Comment comment = null; 842 Comment comment = null;
849 push(ast.classDeclaration( 843 push(ast.classDeclaration(
850 comment, 844 comment,
851 metadata, 845 metadata,
852 abstractKeyword, 846 toAnalyzerToken(abstractKeyword),
853 toAnalyzerToken(classKeyword), 847 toAnalyzerToken(classKeyword),
854 name, 848 name,
855 typeParameters, 849 typeParameters,
856 extendsClause, 850 extendsClause,
857 withClause, 851 withClause,
858 implementsClause, 852 implementsClause,
859 toAnalyzerToken(body.beginToken), 853 toAnalyzerToken(body.beginToken),
860 body.members, 854 body.members,
861 toAnalyzerToken(body.endToken))); 855 toAnalyzerToken(body.endToken)));
862 } 856 }
863 857
864 @override 858 @override
865 void endMixinApplication() { 859 void endMixinApplication() {
866 debugEvent("MixinApplication"); 860 debugEvent("MixinApplication");
867 List<TypeName> mixinTypes = pop(); 861 List<TypeName> mixinTypes = pop();
868 // TODO(paulberry,ahe): the parser doesn't give us enough information to 862 // TODO(paulberry,ahe): the parser doesn't give us enough information to
869 // locate the "with" keyword. 863 // locate the "with" keyword.
870 Token withKeyword; 864 Token withKeyword;
871 TypeName supertype = pop(); 865 TypeName supertype = pop();
872 push(new _MixinApplication(supertype, withKeyword, mixinTypes)); 866 push(new _MixinApplication(supertype, withKeyword, mixinTypes));
873 } 867 }
874 868
875 @override 869 @override
876 void endNamedMixinApplication( 870 void endNamedMixinApplication(Token beginToken, Token equalsToken,
877 Token beginToken, Token equalsToken, Token implementsKeyword, Token endTok en) { 871 Token implementsKeyword, Token endToken) {
878 debugEvent("NamedMixinApplication"); 872 debugEvent("NamedMixinApplication");
879 ImplementsClause implementsClause; 873 ImplementsClause implementsClause;
880 if (implementsKeyword != null) { 874 if (implementsKeyword != null) {
881 List<TypeName> interfaces = pop(); 875 List<TypeName> interfaces = pop();
882 implementsClause = 876 implementsClause =
883 ast.implementsClause(toAnalyzerToken(implementsKeyword), interfaces); 877 ast.implementsClause(toAnalyzerToken(implementsKeyword), interfaces);
884 } 878 }
885 _MixinApplication mixinApplication = pop(); 879 _MixinApplication mixinApplication = pop();
886 var superclass = mixinApplication.supertype; 880 var superclass = mixinApplication.supertype;
887 var withClause = ast.withClause( 881 var withClause = ast.withClause(
888 toAnalyzerToken(mixinApplication.withKeyword), 882 toAnalyzerToken(mixinApplication.withKeyword),
889 mixinApplication.mixinTypes); 883 mixinApplication.mixinTypes);
890 analyzer.Token equals = toAnalyzerToken(equalsToken); 884 analyzer.Token equals = toAnalyzerToken(equalsToken);
891 TypeParameterList typeParameters = pop(); 885 TypeParameterList typeParameters = pop();
892 SimpleIdentifier name = pop(); 886 SimpleIdentifier name = pop();
893 Token classKeyword; 887 Token classKeyword;
894 // TODO(paulberry,ahe): This is a hack. The parser should give us the class 888 // TODO(paulberry,ahe): This is a hack. The parser should give us the class
895 // keyword. 889 // keyword.
896 if (identical(beginToken.value, 'abstract')) { 890 if (identical(beginToken.value, 'abstract')) {
897 classKeyword = beginToken.next; 891 classKeyword = beginToken.next;
898 } else { 892 } else {
899 classKeyword = beginToken; 893 classKeyword = beginToken;
900 } 894 }
901 var modifiers = pop(); 895 Token abstractKeyword = _popOptionalSingleModifier();
902 assert(modifiers == null); // TODO(paulberry)
903 analyzer.Token abstractKeyword;
904 List<Annotation> metadata = pop(); 896 List<Annotation> metadata = pop();
905 // TODO(paulberry): capture doc comments. See dartbug.com/28851. 897 // TODO(paulberry): capture doc comments. See dartbug.com/28851.
906 Comment comment = null; 898 Comment comment = null;
907 push(ast.classTypeAlias( 899 push(ast.classTypeAlias(
908 comment, 900 comment,
909 metadata, 901 metadata,
910 toAnalyzerToken(classKeyword), 902 toAnalyzerToken(classKeyword),
911 name, 903 name,
912 typeParameters, 904 typeParameters,
913 equals, 905 equals,
914 abstractKeyword, 906 toAnalyzerToken(abstractKeyword),
915 superclass, 907 superclass,
916 withClause, 908 withClause,
917 implementsClause, 909 implementsClause,
918 toAnalyzerToken(endToken))); 910 toAnalyzerToken(endToken)));
919 } 911 }
920 912
921 @override 913 @override
922 void beginLibraryName(Token token) { 914 void beginLibraryName(Token token) {
923 accumulateIdentifierComponents = true; 915 accumulateIdentifierComponents = true;
924 isFirstIdentifier = true; 916 isFirstIdentifier = true;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1008 Expression initializer = pop(); 1000 Expression initializer = pop();
1009 SimpleIdentifier name = pop(); 1001 SimpleIdentifier name = pop();
1010 push(ast.variableDeclaration( 1002 push(ast.variableDeclaration(
1011 name, toAnalyzerToken(assignment), initializer)); 1003 name, toAnalyzerToken(assignment), initializer));
1012 } 1004 }
1013 1005
1014 void endTopLevelFields(int count, Token beginToken, Token endToken) { 1006 void endTopLevelFields(int count, Token beginToken, Token endToken) {
1015 debugEvent("TopLevelFields"); 1007 debugEvent("TopLevelFields");
1016 List<VariableDeclaration> variables = popList(count); 1008 List<VariableDeclaration> variables = popList(count);
1017 TypeAnnotation type = pop(); 1009 TypeAnnotation type = pop();
1018 var keyword; 1010 Token keyword = _popOptionalSingleModifier();
1019 if (optional('var', beginToken) ||
1020 optional('const', beginToken) ||
1021 optional('final', beginToken)) {
1022 keyword = beginToken;
1023 }
1024 var variableList = ast.variableDeclarationList( 1011 var variableList = ast.variableDeclarationList(
1025 null, null, toAnalyzerToken(keyword), type, variables); 1012 null, null, toAnalyzerToken(keyword), type, variables);
1026 var modifiers = pop();
1027 assert(modifiers == null); // TODO(paulberry)
1028 List<Annotation> metadata = pop(); 1013 List<Annotation> metadata = pop();
1029 // TODO(paulberry): capture doc comments. See dartbug.com/28851. 1014 // TODO(paulberry): capture doc comments. See dartbug.com/28851.
1030 Comment comment = null; 1015 Comment comment = null;
1031 push(ast.topLevelVariableDeclaration( 1016 push(ast.topLevelVariableDeclaration(
1032 comment, metadata, variableList, toAnalyzerToken(endToken))); 1017 comment, metadata, variableList, toAnalyzerToken(endToken)));
1033 } 1018 }
1034 1019
1035 @override 1020 @override
1036 void endTypeVariable(Token token, Token extendsOrSuper) { 1021 void endTypeVariable(Token token, Token extendsOrSuper) {
1037 // TODO(paulberry): set up scopes properly to resolve parameters and type 1022 // TODO(paulberry): set up scopes properly to resolve parameters and type
(...skipping 23 matching lines...) Expand all
1061 debugEvent("Method"); 1046 debugEvent("Method");
1062 FunctionBody body = _endFunctionBody(); 1047 FunctionBody body = _endFunctionBody();
1063 ConstructorName redirectedConstructor = null; // TODO(paulberry) 1048 ConstructorName redirectedConstructor = null; // TODO(paulberry)
1064 List<ConstructorInitializer> initializers = null; // TODO(paulberry) 1049 List<ConstructorInitializer> initializers = null; // TODO(paulberry)
1065 Token separator = null; // TODO(paulberry) 1050 Token separator = null; // TODO(paulberry)
1066 FormalParameterList parameters = pop(); 1051 FormalParameterList parameters = pop();
1067 TypeParameterList typeParameters = pop(); 1052 TypeParameterList typeParameters = pop();
1068 var name = pop(); 1053 var name = pop();
1069 analyzer.Token propertyKeyword = toAnalyzerToken(getOrSet); 1054 analyzer.Token propertyKeyword = toAnalyzerToken(getOrSet);
1070 TypeAnnotation returnType = pop(); 1055 TypeAnnotation returnType = pop();
1071 // TODO(paulberry): handle modifiers. 1056
1072 var modifiers = pop(); 1057 Token externalKeyword = null;
1073 assert(modifiers == null); 1058 Token constKeyword = null;
1074 Token externalKeyword = null; // TODO(paulberry) 1059 Token factoryKeyword = null;
1075 Token constKeyword = null; // TODO(paulberry) 1060 List<Token> modifiers = pop();
1076 Token factoryKeyword = null; // TODO(paulberry) 1061 for (Token modifier in modifiers) {
1062 String value = modifier.stringValue;
1063 if (identical('external', value)) {
1064 // TODO(scheglov): Check the order and uniqueness.
1065 externalKeyword = modifier;
1066 } else if (identical('const', value)) {
1067 // TODO(scheglov): Check the order and uniqueness.
1068 constKeyword = modifier;
1069 } else if (identical('factory', value)) {
1070 // TODO(scheglov): Check the order and uniqueness.
1071 factoryKeyword = modifier;
1072 } else {
1073 // TODO(scheglov): Report error.
Paul Berry 2017/02/21 23:09:11 The TODO is good, but for safety, let's also use "
scheglov 2017/02/22 03:08:23 Done.
ahe 2017/02/22 09:08:17 Alternatively, try this: printUnexpected(uri, mod
scheglov 2017/02/22 16:32:46 There are 331 usage of internalError() and just 2
ahe 2017/02/27 08:39:26 We talked about the differences between internalEr
1074 }
1075 }
1076
1077 List<Annotation> metadata = pop(); 1077 List<Annotation> metadata = pop();
1078 // TODO(paulberry): capture doc comments. See dartbug.com/28851. 1078 // TODO(paulberry): capture doc comments. See dartbug.com/28851.
1079 Comment comment = null; 1079 Comment comment = null;
1080 SimpleIdentifier returnType2; 1080 SimpleIdentifier returnType2;
1081 Token period; 1081 Token period;
1082 SimpleIdentifier name2; 1082 SimpleIdentifier name2;
1083 if (name is SimpleIdentifier) { 1083 if (name is SimpleIdentifier) {
1084 SimpleIdentifier returnType2 = name; 1084 SimpleIdentifier returnType2 = name;
1085 } 1085 }
1086 push(ast.constructorDeclaration( 1086 push(ast.constructorDeclaration(
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 push(ast.functionTypeAlias( 1127 push(ast.functionTypeAlias(
1128 comment, 1128 comment,
1129 metadata, 1129 metadata,
1130 toAnalyzerToken(typedefKeyword), 1130 toAnalyzerToken(typedefKeyword),
1131 returnType, 1131 returnType,
1132 name, 1132 name,
1133 typeParameters, 1133 typeParameters,
1134 parameters, 1134 parameters,
1135 toAnalyzerToken(endToken))); 1135 toAnalyzerToken(endToken)));
1136 } 1136 }
1137
1138 /**
1139 * Pop the modifiers list, if the list is empty return `null`, if the list
1140 * has one item return it; otherwise return `null`.
1141 */
1142 Token _popOptionalSingleModifier() {
1143 List<Token> modifiers = pop();
1144 if (modifiers.length == 0) {
1145 return null;
1146 } else if (modifiers.length == 1) {
1147 // TODO(scheglov): Verify that the modifier is valid.
1148 return modifiers[0];
1149 } else {
1150 // TODO(scheglov): Report error.
Paul Berry 2017/02/21 23:09:11 Ditto.
scheglov 2017/02/22 03:08:24 Done.
1151 return null;
1152 }
1153 }
1137 } 1154 }
1138 1155
1139 /// Data structure placed on the stack to represent a class body. 1156 /// Data structure placed on the stack to represent a class body.
1140 /// 1157 ///
1141 /// This is needed because analyzer has no separate AST representation of a 1158 /// This is needed because analyzer has no separate AST representation of a
1142 /// class body; it simply stores all of the relevant data in the 1159 /// class body; it simply stores all of the relevant data in the
1143 /// [ClassDeclaration] object. 1160 /// [ClassDeclaration] object.
1144 class _ClassBody { 1161 class _ClassBody {
1145 final Token beginToken; 1162 final Token beginToken;
1146 1163
(...skipping 12 matching lines...) Expand all
1159 /// [ClassDeclaration] or [ClassTypeAlias] object. 1176 /// [ClassDeclaration] or [ClassTypeAlias] object.
1160 class _MixinApplication { 1177 class _MixinApplication {
1161 final TypeName supertype; 1178 final TypeName supertype;
1162 1179
1163 final Token withKeyword; 1180 final Token withKeyword;
1164 1181
1165 final List<TypeName> mixinTypes; 1182 final List<TypeName> mixinTypes;
1166 1183
1167 _MixinApplication(this.supertype, this.withKeyword, this.mixinTypes); 1184 _MixinApplication(this.supertype, this.withKeyword, this.mixinTypes);
1168 } 1185 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/generated/parser_fasta_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698