| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library("status_expression"); | 5 #library("status_expression"); |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Parse and evaluate expressions in a .status file for Dart and V8. | 8 * Parse and evaluate expressions in a .status file for Dart and V8. |
| 9 * There are set expressions and Boolean expressions in a .status file. | 9 * There are set expressions and Boolean expressions in a .status file. |
| 10 * The grammar is: | 10 * The grammar is: |
| 11 * BooleanExpression := $variableName == value | $variableName | | 11 * BooleanExpression := $variableName == value | $variableName | |
| 12 * (BooleanExpression) | | 12 * (BooleanExpression) | |
| 13 * BooleanExpression && BooleanExpression | | 13 * BooleanExpression && BooleanExpression | |
| 14 * BooleanExpression || BooleanExpression | 14 * BooleanExpression || BooleanExpression |
| 15 * | 15 * |
| 16 * SetExpression := value | (SetExpression) | | 16 * SetExpression := value | (SetExpression) | |
| 17 * SetExpression || SetExpression | | 17 * SetExpression || SetExpression | |
| 18 * SetExpression if BooleanExpression | | 18 * SetExpression if BooleanExpression | |
| 19 * SetExpression , SetExpression | 19 * SetExpression , SetExpression |
| 20 * | 20 * |
| 21 * Productions are listed in order of precedence, and the || and , operators | 21 * Productions are listed in order of precedence, and the || and , operators |
| 22 * both evaluate to set union, but with different precedence. | 22 * both evaluate to set union, but with different precedence. |
| 23 * | 23 * |
| 24 * Values and variableNames are non-empty strings of word characters, matching | 24 * Values and variableNames are non-empty strings of word characters, matching |
| 25 * the RegExp \w+. | 25 * the RegExp \w+. |
| 26 * | 26 * |
| 27 * Expressions evaluate as expected, with values of variables found in | 27 * Expressions evaluate as expected, with values of variables found in |
| 28 * an environment passed to the evaluator. The SetExpression "value" | 28 * an environment passed to the evaluator. The SetExpression "value" |
| 29 * evaluates to a singleton set containing that value. "A if B" evaluates | 29 * evaluates to a singleton set containing that value. "A if B" evaluates |
| 30 * to A if B is true, and to the empty set if B is false. | 30 * to A if B is true, and to the empty set if B is false. |
| 31 */ | 31 */ |
| 32 | 32 |
| 33 | 33 |
| 34 class Token | 34 class Token { |
| 35 { | |
| 36 static const String LEFT_PAREN = "("; | 35 static const String LEFT_PAREN = "("; |
| 37 static const String RIGHT_PAREN = ")"; | 36 static const String RIGHT_PAREN = ")"; |
| 38 static const String DOLLAR_SYMBOL = r"$"; | 37 static const String DOLLAR_SYMBOL = r"$"; |
| 39 static const String UNION = ","; | 38 static const String UNION = ","; |
| 40 static const String EQUALS = "=="; | 39 static const String EQUALS = "=="; |
| 41 static const String AND = "&&"; | 40 static const String AND = "&&"; |
| 42 static const String OR = "||"; | 41 static const String OR = "||"; |
| 43 } | 42 } |
| 44 | 43 |
| 45 | 44 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 58 List<String> tokenize() { | 57 List<String> tokenize() { |
| 59 if (!testRegexp.hasMatch(expression)) { | 58 if (!testRegexp.hasMatch(expression)) { |
| 60 throw new ExpectException("Syntax error in '$expression'"); | 59 throw new ExpectException("Syntax error in '$expression'"); |
| 61 } | 60 } |
| 62 for (Match match in regexp.allMatches(expression)) tokens.add(match[0]); | 61 for (Match match in regexp.allMatches(expression)) tokens.add(match[0]); |
| 63 return tokens; | 62 return tokens; |
| 64 } | 63 } |
| 65 } | 64 } |
| 66 | 65 |
| 67 | 66 |
| 68 interface BooleanExpression { | 67 abstract class BooleanExpression { |
| 69 bool evaluate(Map<String, String> environment); | 68 bool evaluate(Map<String, String> environment); |
| 70 } | 69 } |
| 71 | 70 |
| 72 | 71 |
| 73 interface SetExpression { | 72 abstract class SetExpression { |
| 74 Set<String> evaluate(Map<String, String> environment); | 73 Set<String> evaluate(Map<String, String> environment); |
| 75 } | 74 } |
| 76 | 75 |
| 77 | 76 |
| 78 class Comparison implements BooleanExpression { | 77 class Comparison implements BooleanExpression { |
| 79 TermVariable left; | 78 TermVariable left; |
| 80 TermConstant right; | 79 TermConstant right; |
| 81 | 80 |
| 82 Comparison(this.left, this.right); | 81 Comparison(this.left, this.right); |
| 83 | 82 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 96 } | 95 } |
| 97 | 96 |
| 98 | 97 |
| 99 class TermConstant { | 98 class TermConstant { |
| 100 String value; | 99 String value; |
| 101 | 100 |
| 102 TermConstant(String this.value); | 101 TermConstant(String this.value); |
| 103 | 102 |
| 104 String termValue(environment) => value; | 103 String termValue(environment) => value; |
| 105 } | 104 } |
| 106 | 105 |
| 107 | 106 |
| 108 class BooleanVariable implements BooleanExpression { | 107 class BooleanVariable implements BooleanExpression { |
| 109 TermVariable variable; | 108 TermVariable variable; |
| 110 | 109 |
| 111 BooleanVariable(this.variable); | 110 BooleanVariable(this.variable); |
| 112 | 111 |
| 113 bool evaluate(environment) => variable.termValue(environment) == 'true'; | 112 bool evaluate(environment) => variable.termValue(environment) == 'true'; |
| 114 String toString() => "(bool \$${variable.name})"; | 113 String toString() => "(bool \$${variable.name})"; |
| 115 } | 114 } |
| 116 | 115 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 "Expected identifier in expression, got ${scanner.current}"); | 288 "Expected identifier in expression, got ${scanner.current}"); |
| 290 TermConstant right = new TermConstant(scanner.current); | 289 TermConstant right = new TermConstant(scanner.current); |
| 291 scanner.advance(); | 290 scanner.advance(); |
| 292 return new Comparison(left, right); | 291 return new Comparison(left, right); |
| 293 } else { | 292 } else { |
| 294 return new BooleanVariable(left); | 293 return new BooleanVariable(left); |
| 295 } | 294 } |
| 296 } | 295 } |
| 297 } | 296 } |
| 298 | 297 |
| OLD | NEW |