OLD | NEW |
| (Empty) |
1 // Copyright (c) 2017, 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 /// Implements support for two variants of the native syntax extension. | |
6 /// | |
7 /// * The Dart VM variant, where native method bodies have this form: | |
8 /// | |
9 /// methodDeclaration() native STRING; | |
10 /// | |
11 /// * The Dart2js and DDC variant, where native method bodies have this form: | |
12 /// | |
13 /// methodDeclaration() native; | |
14 /// | |
15 /// This support is kept separate from parser.dart as this isn't specified in | |
16 /// the Dart Language Specification, also we hope to remove this syntax long | |
17 /// term and replace it with annotations and external declarations. | |
18 library fasta.parser.dart_vm_native; | |
19 | |
20 import '../../scanner/token.dart' show Token; | |
21 | |
22 import '../scanner/token_constants.dart' show STRING_TOKEN; | |
23 | |
24 import '../util/link.dart' show Link; | |
25 | |
26 import 'parser.dart' show optional; | |
27 | |
28 import '../quote.dart' show unescapeString; | |
29 | |
30 /// When parsing a library file, we may encounter a native clause | |
31 /// instead of a function body. This method skips such a clause. The | |
32 /// [expectString] argument is used to choose which variant of the native clause | |
33 /// we expect to parse. | |
34 /// | |
35 /// This method is designed to be called when encountering | |
36 /// [ErrorKind.ExpectedBlockToSkip] in [Listener.handleUnrecoverableError]. | |
37 Token skipNativeClause(Token token, bool expectString) { | |
38 if (!optional("native", token)) return null; | |
39 if (expectString) { | |
40 token = token.next; | |
41 if (token.kind != STRING_TOKEN) return null; | |
42 } | |
43 if (!optional(";", token.next)) return null; | |
44 return token; | |
45 } | |
46 | |
47 /// When parsing a library file, we may encounter native getters like | |
48 /// | |
49 /// int get length native "List_getLength"; | |
50 /// | |
51 /// This will result in [identifiers] being | |
52 /// | |
53 /// [";", '"List_getLength"', "native", "length", "get"] | |
54 /// | |
55 /// Similarly if [expectString] is false, we expect a getter like: | |
56 /// | |
57 /// int get length native; | |
58 /// | |
59 /// And [identifiers] being | |
60 /// | |
61 /// [";", "native", "length", "get"] | |
62 /// | |
63 /// This method returns a new list where '"List_getLength"' and "native" are | |
64 /// removed. | |
65 /// | |
66 /// This method is designed to be called from [Listener.handleMemberName]. | |
67 Link<Token> removeNativeClause(Link<Token> identifiers, bool expectString) { | |
68 Link<Token> result = identifiers.tail; | |
69 if (result.isEmpty) return identifiers; | |
70 if (expectString) { | |
71 if (result.head.kind != STRING_TOKEN) return identifiers; | |
72 result = result.tail; | |
73 } | |
74 if (result.isEmpty) return identifiers; | |
75 if (optional('native', result.head)) { | |
76 return result.tail.prepend(identifiers.head); | |
77 } | |
78 return identifiers; | |
79 } | |
80 | |
81 /// When the parser encounters a native clause and expects a string (like in VM | |
82 /// and flutter patch files), this method extracts the native name in that | |
83 /// string. | |
84 String extractNativeMethodName(Token token) { | |
85 return unescapeString(token.next.lexeme); | |
86 } | |
OLD | NEW |