| OLD | NEW |
| 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.scope; | 5 library fasta.scope; |
| 6 | 6 |
| 7 import 'builder.dart' show Builder, MixedAccessor; | 7 import 'builder.dart' show Builder, MixedAccessor; |
| 8 | 8 |
| 9 import '../errors.dart' show internalError; | 9 import '../errors.dart' show internalError; |
| 10 | 10 |
| 11 class Scope { | 11 class Scope { |
| 12 /// Names declared in this scope. | 12 /// Names declared in this scope. |
| 13 final Map<String, Builder> local; | 13 final Map<String, Builder> local; |
| 14 | 14 |
| 15 /// The scope that this scope is nested within, or `null` if this is the top | 15 /// The scope that this scope is nested within, or `null` if this is the top |
| 16 /// level scope. | 16 /// level scope. |
| 17 final Scope parent; | 17 final Scope parent; |
| 18 | 18 |
| 19 /// Indicates whether an attempt to declare new names in this scope should | 19 /// Indicates whether an attempt to declare new names in this scope should |
| 20 /// succeed. | 20 /// succeed. |
| 21 final bool isModifiable; | 21 final bool isModifiable; |
| 22 | 22 |
| 23 Map<String, Builder> labels; | 23 Map<String, Builder> labels; |
| 24 | 24 |
| 25 Map<String, Builder> forwardDeclaredLabels; |
| 26 |
| 25 Scope(this.local, this.parent, {this.isModifiable: true}); | 27 Scope(this.local, this.parent, {this.isModifiable: true}); |
| 26 | 28 |
| 27 Scope createNestedScope({bool isModifiable: true}) { | 29 Scope createNestedScope({bool isModifiable: true}) { |
| 28 return new Scope(<String, Builder>{}, this, isModifiable: isModifiable); | 30 return new Scope(<String, Builder>{}, this, isModifiable: isModifiable); |
| 29 } | 31 } |
| 30 | 32 |
| 31 Builder lookup(String name, int charOffset, Uri fileUri) { | 33 Builder lookup(String name, int charOffset, Uri fileUri) { |
| 32 Builder builder = local[name]; | 34 Builder builder = local[name]; |
| 33 if (builder != null) { | 35 if (builder != null) { |
| 34 if (builder.next != null) { | 36 if (builder.next != null) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 94 |
| 93 void declareLabel(String name, Builder target) { | 95 void declareLabel(String name, Builder target) { |
| 94 if (isModifiable) { | 96 if (isModifiable) { |
| 95 labels ??= <String, Builder>{}; | 97 labels ??= <String, Builder>{}; |
| 96 labels[name] = target; | 98 labels[name] = target; |
| 97 } else { | 99 } else { |
| 98 internalError("Can't extend an unmodifiable scope."); | 100 internalError("Can't extend an unmodifiable scope."); |
| 99 } | 101 } |
| 100 } | 102 } |
| 101 | 103 |
| 104 void forwardDeclareLabel(String name, Builder target) { |
| 105 declareLabel(name, target); |
| 106 forwardDeclaredLabels ??= <String, Builder>{}; |
| 107 forwardDeclaredLabels[name] = target; |
| 108 } |
| 109 |
| 110 void claimLabel(String name) { |
| 111 forwardDeclaredLabels.remove(name); |
| 112 if (forwardDeclaredLabels.length == 0) { |
| 113 forwardDeclaredLabels = null; |
| 114 } |
| 115 } |
| 116 |
| 117 Map<String, Builder> get unclaimedForwardDeclarations { |
| 118 return forwardDeclaredLabels; |
| 119 } |
| 120 |
| 102 Builder lookupLabel(String name) { | 121 Builder lookupLabel(String name) { |
| 103 return (labels == null ? null : labels[name]) ?? parent?.lookupLabel(name); | 122 return (labels == null ? null : labels[name]) ?? parent?.lookupLabel(name); |
| 104 } | 123 } |
| 105 | 124 |
| 106 // TODO(ahe): Rename to extend or something. | 125 // TODO(ahe): Rename to extend or something. |
| 107 void operator []=(String name, Builder member) { | 126 void operator []=(String name, Builder member) { |
| 108 if (isModifiable) { | 127 if (isModifiable) { |
| 109 local[name] = member; | 128 local[name] = member; |
| 110 } else { | 129 } else { |
| 111 internalError("Can't extend an unmodifiable scope."); | 130 internalError("Can't extend an unmodifiable scope."); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 class AmbiguousBuilder extends Builder { | 168 class AmbiguousBuilder extends Builder { |
| 150 final Builder builder; | 169 final Builder builder; |
| 151 | 170 |
| 152 AmbiguousBuilder(this.builder, int charOffset, Uri fileUri) | 171 AmbiguousBuilder(this.builder, int charOffset, Uri fileUri) |
| 153 : super(null, charOffset, fileUri); | 172 : super(null, charOffset, fileUri); |
| 154 | 173 |
| 155 get target => null; | 174 get target => null; |
| 156 | 175 |
| 157 bool get hasProblem => true; | 176 bool get hasProblem => true; |
| 158 } | 177 } |
| OLD | NEW |