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