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