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.parser; |
| 6 |
| 7 import 'package:source_span/source_span.dart'; |
| 8 |
| 9 import 'ast.dart'; |
| 10 import 'scanner.dart'; |
| 11 import 'token.dart'; |
| 12 |
| 13 /// A class for parsing a platform selector. |
| 14 /// |
| 15 /// Platform selectors use a stripped-down version of the Dart expression |
| 16 /// syntax that only contains variables, parentheses, and boolean operators. |
| 17 /// Variables may also contain dashes, contrary to Dart's syntax; this allows |
| 18 /// consistency with command-line arguments. |
| 19 class Parser { |
| 20 /// The scanner that tokenizes the selector. |
| 21 final Scanner _scanner; |
| 22 |
| 23 Parser(String selector) |
| 24 : _scanner = new Scanner(selector); |
| 25 |
| 26 /// Parses the selector. |
| 27 /// |
| 28 /// This must only be called once per parser. |
| 29 Node parse() { |
| 30 var selector = _conditional(); |
| 31 |
| 32 if (_scanner.peek().type != TokenType.endOfFile) { |
| 33 throw new SourceSpanFormatException( |
| 34 "Expected end of input.", _scanner.peek().span); |
| 35 } |
| 36 |
| 37 return selector; |
| 38 } |
| 39 |
| 40 /// Parses a conditional: |
| 41 /// |
| 42 /// conditionalExpression: |
| 43 /// logicalOrExpression ("?" conditionalExpression ":" |
| 44 /// conditionalExpression)? |
| 45 Node _conditional() { |
| 46 var condition = _or(); |
| 47 if (!_scanner.scan(TokenType.questionMark)) return condition; |
| 48 |
| 49 var whenTrue = _conditional(); |
| 50 if (!_scanner.scan(TokenType.colon)) { |
| 51 throw new SourceSpanFormatException( |
| 52 'Expected ":".', _scanner.peek().span); |
| 53 } |
| 54 |
| 55 var whenFalse = _conditional(); |
| 56 return new ConditionalNode(condition, whenTrue, whenFalse); |
| 57 } |
| 58 |
| 59 /// Parses a logical or: |
| 60 /// |
| 61 /// logicalOrExpression: |
| 62 /// logicalAndExpression ("||" logicalOrExpression)? |
| 63 Node _or() { |
| 64 var left = _and(); |
| 65 if (!_scanner.scan(TokenType.or)) return left; |
| 66 return new OrNode(left, _or()); |
| 67 } |
| 68 |
| 69 /// Parses a logical and: |
| 70 /// |
| 71 /// logicalAndExpression: |
| 72 /// simpleExpression ("&&" logicalAndExpression)? |
| 73 Node _and() { |
| 74 var left = _simpleExpression(); |
| 75 if (!_scanner.scan(TokenType.and)) return left; |
| 76 return new AndNode(left, _and()); |
| 77 } |
| 78 |
| 79 /// Parses a simple expression: |
| 80 /// |
| 81 /// simpleExpression: |
| 82 /// "!" simpleExpression | |
| 83 /// "(" conditionalExpression ")" | |
| 84 /// IDENTIFIER |
| 85 Node _simpleExpression() { |
| 86 var token = _scanner.next(); |
| 87 switch (token.type) { |
| 88 case TokenType.not: |
| 89 var child = _simpleExpression(); |
| 90 return new NotNode(child, token.span.expand(child.span)); |
| 91 |
| 92 case TokenType.leftParen: |
| 93 var child = _conditional(); |
| 94 if (!_scanner.scan(TokenType.rightParen)) { |
| 95 throw new SourceSpanFormatException( |
| 96 'Expected ")".', _scanner.peek().span); |
| 97 } |
| 98 return child; |
| 99 |
| 100 case TokenType.identifier: |
| 101 return new VariableNode(token.name, token.span); |
| 102 |
| 103 default: |
| 104 throw new SourceSpanFormatException("Expected expression.", token.span); |
| 105 } |
| 106 } |
| 107 } |
OLD | NEW |