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 class ClassElementParser extends PartialParser { | |
6 ClassElementParser(Listener listener) : super(listener); | |
7 | |
8 Token parseClassBody(Token token) => fullParseClassBody(token); | |
9 } | |
10 | |
11 class PartialClassElement extends ClassElement { | |
12 final Token beginToken; | |
13 final Token endToken; | |
14 Node cachedNode; | |
15 | |
16 PartialClassElement(SourceString name, | |
17 Token this.beginToken, | |
18 Token this.endToken, | |
19 CompilationUnitElement enclosing) | |
20 : super(name, enclosing); | |
21 | |
22 ClassNode parseNode(DiagnosticListener diagnosticListener) { | |
23 if (cachedNode != null) return cachedNode; | |
24 MemberListener listener = new MemberListener(diagnosticListener, this); | |
25 Parser parser = new ClassElementParser(listener); | |
26 Token token = parser.parseTopLevelDeclaration(beginToken); | |
27 assert(token === endToken.next); | |
28 cachedNode = listener.popNode(); | |
29 assert(listener.nodes.isEmpty()); | |
30 return cachedNode; | |
31 } | |
32 | |
33 Token position() => beginToken; | |
34 | |
35 bool isInterface() => beginToken.stringValue === "interface"; | |
36 } | |
37 | |
38 class MemberListener extends NodeListener { | |
39 final ClassElement enclosingElement; | |
40 | |
41 MemberListener(DiagnosticListener listener, | |
42 Element enclosingElement) | |
43 : this.enclosingElement = enclosingElement, | |
44 super(listener, enclosingElement.getCompilationUnit()); | |
45 | |
46 bool isConstructorName(Node nameNode) { | |
47 if (enclosingElement === null || | |
48 enclosingElement.kind != ElementKind.CLASS) { | |
49 return false; | |
50 } | |
51 SourceString name; | |
52 if (nameNode.asIdentifier() !== null) { | |
53 name = nameNode.asIdentifier().source; | |
54 } else { | |
55 Send send = nameNode.asSend(); | |
56 name = send.receiver.asIdentifier().source; | |
57 } | |
58 return enclosingElement.name == name; | |
59 } | |
60 | |
61 SourceString getMethodNameHack(Node methodName) { | |
62 Send send = methodName.asSend(); | |
63 if (send === null) return methodName.asIdentifier().source; | |
64 Identifier receiver = send.receiver.asIdentifier(); | |
65 Identifier selector = send.selector.asIdentifier(); | |
66 if (selector.asOperator() !== null) { | |
67 return Elements.constructOperatorName(receiver.source, selector.source); | |
68 } else { | |
69 return Elements.constructConstructorName(receiver.source, | |
70 selector.source); | |
71 } | |
72 } | |
73 | |
74 void endMethod(Token getOrSet, Token beginToken, Token endToken) { | |
75 super.endMethod(getOrSet, beginToken, endToken); | |
76 FunctionExpression method = popNode(); | |
77 pushNode(null); | |
78 bool isConstructor = isConstructorName(method.name); | |
79 SourceString name = getMethodNameHack(method.name); | |
80 ElementKind kind = ElementKind.FUNCTION; | |
81 if (isConstructor) { | |
82 if (getOrSet !== null) { | |
83 recoverableError('illegal modifier', token: getOrSet); | |
84 } | |
85 kind = ElementKind.GENERATIVE_CONSTRUCTOR; | |
86 } else if (getOrSet !== null) { | |
87 kind = (getOrSet.stringValue === 'get') | |
88 ? ElementKind.GETTER : ElementKind.SETTER; | |
89 } | |
90 Element memberElement = | |
91 new PartialFunctionElement(name, beginToken, getOrSet, endToken, | |
92 kind, method.modifiers, enclosingElement); | |
93 enclosingElement.addMember(memberElement, listener); | |
94 } | |
95 | |
96 void endFactoryMethod(Token factoryKeyword, Token periodBeforeName, | |
97 Token endToken) { | |
98 super.endFactoryMethod(factoryKeyword, periodBeforeName, endToken); | |
99 FunctionExpression method = popNode(); | |
100 pushNode(null); | |
101 SourceString name = getMethodNameHack(method.name); | |
102 ElementKind kind = ElementKind.FUNCTION; | |
103 Element memberElement = | |
104 new PartialFunctionElement(name, factoryKeyword, null, endToken, | |
105 kind, method.modifiers, enclosingElement); | |
106 enclosingElement.addMember(memberElement, listener); | |
107 } | |
108 | |
109 void endFields(int count, Token beginToken, Token endToken) { | |
110 super.endFields(count, beginToken, endToken); | |
111 VariableDefinitions variableDefinitions = popNode(); | |
112 Modifiers modifiers = variableDefinitions.modifiers; | |
113 pushNode(null); | |
114 void buildFieldElement(SourceString name, Element fields) { | |
115 Element element = new VariableElement( | |
116 name, fields, ElementKind.FIELD, enclosingElement); | |
117 enclosingElement.addMember(element, listener); | |
118 } | |
119 buildFieldElements(modifiers, variableDefinitions.definitions, | |
120 enclosingElement, | |
121 buildFieldElement, beginToken, endToken); | |
122 } | |
123 | |
124 void endInitializer(Token assignmentOperator) { | |
125 pushNode(null); // Super expects an expression, but | |
126 // ClassElementParser just skips expressions. | |
127 super.endInitializer(assignmentOperator); | |
128 } | |
129 | |
130 void endInitializers(int count, Token beginToken, Token endToken) { | |
131 pushNode(null); | |
132 } | |
133 } | |
OLD | NEW |