Line data Source code
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 5 : BooleanSelectorImpl.parse(String selector) 27 10 : : _selector = Parser(selector).parse(); 28 : 29 0 : BooleanSelectorImpl._(this._selector); 30 : 31 0 : @override 32 0 : Iterable<String> get variables => _selector.variables; 33 : 34 5 : @override 35 : bool evaluate(bool Function(String variable) semantics) => 36 15 : _selector.accept(Evaluator(semantics)); 37 : 38 0 : @override 39 : BooleanSelector intersection(BooleanSelector other) { 40 0 : if (other == BooleanSelector.all) return this; 41 0 : if (other == BooleanSelector.none) return other; 42 0 : return other is BooleanSelectorImpl 43 0 : ? BooleanSelectorImpl._(AndNode(_selector, other._selector)) 44 0 : : IntersectionSelector(this, other); 45 : } 46 : 47 0 : @override 48 : BooleanSelector union(BooleanSelector other) { 49 0 : if (other == BooleanSelector.all) return other; 50 0 : if (other == BooleanSelector.none) return this; 51 0 : return other is BooleanSelectorImpl 52 0 : ? BooleanSelectorImpl._(OrNode(_selector, other._selector)) 53 0 : : UnionSelector(this, other); 54 : } 55 : 56 0 : @override 57 : void validate(bool Function(String variable) isDefined) { 58 0 : _selector.accept(Validator(isDefined)); 59 : } 60 : 61 5 : @override 62 10 : String toString() => _selector.toString(); 63 : 64 5 : @override 65 : bool operator ==(other) => 66 5 : other is BooleanSelectorImpl && _selector == other._selector; 67 : 68 0 : @override 69 0 : int get hashCode => _selector.hashCode; 70 : }