2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro;
5
6
import 'package:_fe_analyzer_shared/src/messages/codes.dart' ;
6
7
import 'package:_fe_analyzer_shared/src/parser/parser.dart' ;
8
+ import 'package:_fe_analyzer_shared/src/parser/quote.dart' ;
7
9
import 'package:_fe_analyzer_shared/src/scanner/error_token.dart' ;
8
10
import 'package:_fe_analyzer_shared/src/scanner/token.dart' ;
9
11
@@ -15,7 +17,6 @@ import '../../builder/prefix_builder.dart';
15
17
import '../../scope.dart' ;
16
18
import '../../source/diet_parser.dart' ;
17
19
import '../../source/source_library_builder.dart' ;
18
-
19
20
import 'macro.dart' ;
20
21
21
22
List <MacroApplication >? prebuildAnnotations (
@@ -78,7 +79,35 @@ class _NoArgumentsNode implements _Node {
78
79
}
79
80
80
81
class _ArgumentsNode implements _Node {
81
- _ArgumentsNode ();
82
+ final List <Object ?> positionalArguments;
83
+ final Map <String , Object ?> namedArguments;
84
+
85
+ _ArgumentsNode (this .positionalArguments, this .namedArguments);
86
+ }
87
+
88
+ class _PrimitiveValueNode implements _Node {
89
+ final Object ? value;
90
+
91
+ _PrimitiveValueNode (this .value);
92
+ }
93
+
94
+ class _TokenNode implements _Node {
95
+ final Token token;
96
+
97
+ _TokenNode (this .token);
98
+ }
99
+
100
+ class _NamedArgumentIdentifierNode implements _Node {
101
+ final String name;
102
+
103
+ _NamedArgumentIdentifierNode (this .name);
104
+ }
105
+
106
+ class _NamedArgumentNode implements _Node {
107
+ final String name;
108
+ final Object ? value;
109
+
110
+ _NamedArgumentNode (this .name, this .value);
82
111
}
83
112
84
113
class _MacroListener implements Listener {
@@ -154,8 +183,11 @@ class _MacroListener implements Listener {
154
183
if (macroClass != null &&
155
184
constructorName != null &&
156
185
argumentsNode is _ArgumentsNode ) {
157
- push (new _MacroApplicationNode (
158
- new MacroApplication (macroClass, constructorName)));
186
+ push (new _MacroApplicationNode (new MacroApplication (
187
+ macroClass,
188
+ constructorName,
189
+ new macro .Arguments (argumentsNode.positionalArguments,
190
+ argumentsNode.namedArguments))));
159
191
return ;
160
192
}
161
193
}
@@ -211,6 +243,9 @@ class _MacroListener implements Listener {
211
243
pushUnsupported ();
212
244
}
213
245
break ;
246
+ case IdentifierContext .namedArgumentReference:
247
+ push (new _NamedArgumentIdentifierNode (token.lexeme));
248
+ break ;
214
249
default :
215
250
pushUnsupported ();
216
251
break ;
@@ -224,11 +259,27 @@ class _MacroListener implements Listener {
224
259
225
260
@override
226
261
void endArguments (int count, Token beginToken, Token endToken) {
227
- if (count == 0 ) {
228
- push (new _ArgumentsNode ());
229
- } else {
230
- // TODO(johnniwinther): Handle arguments.
262
+ if (unrecognized) {
263
+ pushUnsupported ();
264
+ return ;
265
+ }
266
+ List <Object ?> positionalArguments = [];
267
+ Map <String , Object ?> namedArguments = {};
268
+ for (int i = 0 ; i < count; i++ ) {
269
+ _Node node = pop ();
270
+ if (node is _PrimitiveValueNode ) {
271
+ positionalArguments.add (node.value);
272
+ } else if (node is _NamedArgumentNode &&
273
+ ! namedArguments.containsKey (node.name)) {
274
+ namedArguments[node.name] = node.value;
275
+ } else {
276
+ _unsupported ();
277
+ }
278
+ }
279
+ if (unrecognized) {
231
280
pushUnsupported ();
281
+ } else {
282
+ push (new _ArgumentsNode (positionalArguments, namedArguments));
232
283
}
233
284
}
234
285
@@ -248,6 +299,99 @@ class _MacroListener implements Listener {
248
299
// context.
249
300
}
250
301
302
+ @override
303
+ void handleNamedArgument (Token colon) {
304
+ if (unrecognized) {
305
+ pushUnsupported ();
306
+ } else {
307
+ _Node value = pop ();
308
+ _Node name = pop ();
309
+ if (name is _NamedArgumentIdentifierNode &&
310
+ value is _PrimitiveValueNode ) {
311
+ push (new _NamedArgumentNode (name.name, value.value));
312
+ } else {
313
+ pushUnsupported ();
314
+ }
315
+ }
316
+ }
317
+
318
+ @override
319
+ void handleLiteralNull (Token token) {
320
+ push (new _PrimitiveValueNode (null ));
321
+ }
322
+
323
+ @override
324
+ void handleLiteralBool (Token token) {
325
+ push (new _PrimitiveValueNode (token.lexeme == 'true' ));
326
+ }
327
+
328
+ @override
329
+ void handleLiteralDouble (Token token) {
330
+ push (new _PrimitiveValueNode (double .parse (token.lexeme)));
331
+ }
332
+
333
+ @override
334
+ void handleLiteralInt (Token token) {
335
+ push (new _PrimitiveValueNode (int .parse (token.lexeme)));
336
+ }
337
+
338
+ @override
339
+ void beginLiteralString (Token token) {
340
+ push (new _TokenNode (token));
341
+ }
342
+
343
+ @override
344
+ void endLiteralString (int interpolationCount, Token endToken) {
345
+ if (unrecognized) {
346
+ pushUnsupported ();
347
+ return ;
348
+ }
349
+ if (interpolationCount == 0 ) {
350
+ _Node node = pop ();
351
+ if (node is _TokenNode ) {
352
+ String text = unescapeString (node.token.lexeme, node.token, this );
353
+ if (unrecognized) {
354
+ pushUnsupported ();
355
+ } else {
356
+ push (new _PrimitiveValueNode (text));
357
+ }
358
+ } else {
359
+ pushUnsupported ();
360
+ }
361
+ } else {
362
+ // TODO(johnniwinther): Should we support this?
363
+ pushUnsupported ();
364
+ }
365
+ }
366
+
367
+ @override
368
+ void handleStringPart (Token token) {
369
+ // TODO(johnniwinther): Should we support this?
370
+ _unhandled ();
371
+ }
372
+
373
+ @override
374
+ void handleStringJuxtaposition (Token startToken, int literalCount) {
375
+ if (unrecognized) {
376
+ pushUnsupported ();
377
+ } else {
378
+ List <String > values = [];
379
+ for (int i = 0 ; i < literalCount; i++ ) {
380
+ _Node node = pop ();
381
+ if (node is _PrimitiveValueNode && node.value is String ) {
382
+ values.add (node.value as String );
383
+ } else {
384
+ _unsupported ();
385
+ }
386
+ }
387
+ if (unrecognized) {
388
+ pushUnsupported ();
389
+ } else {
390
+ push (new _PrimitiveValueNode (values.reversed.join ()));
391
+ }
392
+ }
393
+ }
394
+
251
395
//////////////////////////////////////////////////////////////////////////////
252
396
// Stub implementation
253
397
//////////////////////////////////////////////////////////////////////////////
@@ -552,11 +696,6 @@ class _MacroListener implements Listener {
552
696
_unexpected ();
553
697
}
554
698
555
- @override
556
- void beginLiteralString (Token token) {
557
- _unhandled ();
558
- }
559
-
560
699
@override
561
700
void beginLiteralSymbol (Token token) {
562
701
_unhandled ();
@@ -1125,11 +1264,6 @@ class _MacroListener implements Listener {
1125
1264
_unexpected ();
1126
1265
}
1127
1266
1128
- @override
1129
- void endLiteralString (int interpolationCount, Token endToken) {
1130
- _unhandled ();
1131
- }
1132
-
1133
1267
@override
1134
1268
void endLiteralSymbol (Token hashToken, int identifierCount) {
1135
1269
_unhandled ();
@@ -1636,21 +1770,6 @@ class _MacroListener implements Listener {
1636
1770
_unsupported ();
1637
1771
}
1638
1772
1639
- @override
1640
- void handleLiteralBool (Token token) {
1641
- _unhandled ();
1642
- }
1643
-
1644
- @override
1645
- void handleLiteralDouble (Token token) {
1646
- _unhandled ();
1647
- }
1648
-
1649
- @override
1650
- void handleLiteralInt (Token token) {
1651
- _unhandled ();
1652
- }
1653
-
1654
1773
@override
1655
1774
void handleLiteralList (
1656
1775
int count, Token leftBracket, Token ? constKeyword, Token rightBracket) {
@@ -1662,11 +1781,6 @@ class _MacroListener implements Listener {
1662
1781
_unhandled ();
1663
1782
}
1664
1783
1665
- @override
1666
- void handleLiteralNull (Token token) {
1667
- _unhandled ();
1668
- }
1669
-
1670
1784
@override
1671
1785
void handleLiteralSetOrMap (int count, Token leftBrace, Token ? constKeyword,
1672
1786
Token rightBrace, bool hasSetEntry) {
@@ -1683,11 +1797,6 @@ class _MacroListener implements Listener {
1683
1797
_unexpected ();
1684
1798
}
1685
1799
1686
- @override
1687
- void handleNamedArgument (Token colon) {
1688
- _unhandled ();
1689
- }
1690
-
1691
1800
@override
1692
1801
void handleNamedMixinApplicationWithClause (Token withKeyword) {
1693
1802
_unexpected ();
@@ -1839,16 +1948,6 @@ class _MacroListener implements Listener {
1839
1948
_unsupported ();
1840
1949
}
1841
1950
1842
- @override
1843
- void handleStringJuxtaposition (Token startToken, int literalCount) {
1844
- _unhandled ();
1845
- }
1846
-
1847
- @override
1848
- void handleStringPart (Token token) {
1849
- _unhandled ();
1850
- }
1851
-
1852
1951
@override
1853
1952
void handleSuperExpression (Token token, IdentifierContext context) {
1854
1953
_unsupported ();
0 commit comments