OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import '../boolean_selector.dart'; |
| 6 import 'ast.dart'; |
| 7 import 'evaluator.dart'; |
| 8 import 'intersection_selector.dart'; |
| 9 import 'parser.dart'; |
| 10 import 'union_selector.dart'; |
| 11 import 'validator.dart'; |
| 12 |
| 13 /// The concrete implementation of a [BooleanSelector] parsed from a string. |
| 14 /// |
| 15 /// This is separate from [BooleanSelector] so that [intersect] and [union] can |
| 16 /// check to see whether they're passed a [BooleanSelectorImpl] or a different |
| 17 /// class that implements [BooleanSelector]. |
| 18 class BooleanSelectorImpl implements BooleanSelector { |
| 19 /// The parsed AST. |
| 20 final Node _selector; |
| 21 |
| 22 /// Parses [selector]. |
| 23 /// |
| 24 /// This will throw a [SourceSpanFormatException] if the selector is |
| 25 /// malformed or if it uses an undefined variable. |
| 26 BooleanSelectorImpl.parse(String selector) |
| 27 : _selector = new Parser(selector).parse(); |
| 28 |
| 29 BooleanSelectorImpl._(this._selector); |
| 30 |
| 31 Iterable<String> get variables => _selector.variables; |
| 32 |
| 33 bool evaluate(semantics) => _selector.accept(new Evaluator(semantics)); |
| 34 |
| 35 BooleanSelector intersection(BooleanSelector other) { |
| 36 if (other == BooleanSelector.all) return this; |
| 37 if (other == BooleanSelector.none) return other; |
| 38 return other is BooleanSelectorImpl |
| 39 ? new BooleanSelectorImpl._(new AndNode(_selector, other._selector)) |
| 40 : new IntersectionSelector(this, other); |
| 41 } |
| 42 |
| 43 BooleanSelector union(BooleanSelector other) { |
| 44 if (other == BooleanSelector.all) return other; |
| 45 if (other == BooleanSelector.none) return this; |
| 46 return other is BooleanSelectorImpl |
| 47 ? new BooleanSelectorImpl._(new OrNode(_selector, other._selector)) |
| 48 : new UnionSelector(this, other); |
| 49 } |
| 50 |
| 51 void validate(bool isDefined(String variable)) { |
| 52 _selector.accept(new Validator(isDefined)); |
| 53 } |
| 54 |
| 55 String toString() => _selector.toString(); |
| 56 } |
OLD | NEW |