OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library unittest.backend.platform_selector; |
| 6 |
| 7 import 'package:source_span/source_span.dart'; |
| 8 |
| 9 import 'operating_system.dart'; |
| 10 import 'platform_selector/ast.dart'; |
| 11 import 'platform_selector/evaluator.dart'; |
| 12 import 'platform_selector/parser.dart'; |
| 13 import 'platform_selector/visitor.dart'; |
| 14 import 'test_platform.dart'; |
| 15 |
| 16 /// The set of all valid variable names. |
| 17 final _validVariables = |
| 18 new Set<String>.from(["posix", "dart-vm", "browser", "js", "blink"]) |
| 19 ..addAll(TestPlatform.all.map((platform) => platform.identifier)) |
| 20 ..addAll(OperatingSystem.all.map((os) => os.name)); |
| 21 |
| 22 /// An expression for selecting certain platforms, including operating systems |
| 23 /// and browsers. |
| 24 /// |
| 25 /// The syntax is mostly Dart's expression syntax restricted to boolean |
| 26 /// operations. See the README for full details. |
| 27 class PlatformSelector { |
| 28 /// The parsed AST. |
| 29 final Node _selector; |
| 30 |
| 31 /// Parses [selector]. |
| 32 /// |
| 33 /// This will throw a [SourceSpanFormatException] if the selector is |
| 34 /// malformed or if it uses an undefined variable. |
| 35 PlatformSelector.parse(String selector) |
| 36 : _selector = new Parser(selector).parse() { |
| 37 _selector.accept(const _VariableValidator()); |
| 38 } |
| 39 |
| 40 /// Returns whether the selector matches the given [platform] and [os]. |
| 41 /// |
| 42 /// [os] defaults to [OperatingSystem.none]. |
| 43 bool evaluate(TestPlatform platform, {OperatingSystem os}) => |
| 44 _selector.accept(new Evaluator(platform, os: os)); |
| 45 } |
| 46 |
| 47 /// An AST visitor that ensures that all variables are valid. |
| 48 /// |
| 49 /// This isn't done when evaluating to ensure that errors are eagerly detected, |
| 50 /// and it isn't done when parsing to avoid coupling the syntax too tightly to |
| 51 /// the semantics. |
| 52 class _VariableValidator extends RecursiveVisitor { |
| 53 const _VariableValidator(); |
| 54 |
| 55 void visitVariable(VariableNode node) { |
| 56 if (_validVariables.contains(node.name)) return; |
| 57 throw new SourceSpanFormatException("Undefined variable.", node.span); |
| 58 } |
| 59 } |
OLD | NEW |