OLD | NEW |
(Empty) | |
| 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 library fasta.modifier; |
| 6 |
| 7 import 'errors.dart' show |
| 8 internalError; |
| 9 |
| 10 enum ModifierEnum { |
| 11 Abstract, |
| 12 Const, |
| 13 External, |
| 14 Final, |
| 15 Static, |
| 16 |
| 17 // Not a real modifier. |
| 18 Var, |
| 19 } |
| 20 |
| 21 const int abstractMask = 1; |
| 22 |
| 23 const int constMask = abstractMask << 1; |
| 24 |
| 25 const int externalMask = constMask << 1; |
| 26 |
| 27 const int finalMask = externalMask << 1; |
| 28 |
| 29 const int staticMask = finalMask << 1; |
| 30 |
| 31 /// Not a real modifier, and by setting it to null, it is automatically |
| 32 /// ignored by [Modifier.validate] below. |
| 33 const int varMask = 0; |
| 34 |
| 35 const Modifier Abstract = const Modifier(ModifierEnum.Abstract, abstractMask); |
| 36 |
| 37 const Modifier Const = const Modifier(ModifierEnum.Const, constMask); |
| 38 |
| 39 const Modifier External = const Modifier(ModifierEnum.External, externalMask); |
| 40 |
| 41 const Modifier Final = const Modifier(ModifierEnum.Final, finalMask); |
| 42 |
| 43 const Modifier Static = const Modifier(ModifierEnum.Static, staticMask); |
| 44 |
| 45 /// Not a real modifier. |
| 46 const Modifier Var = const Modifier(ModifierEnum.Var, varMask); |
| 47 |
| 48 class Modifier { |
| 49 final ModifierEnum kind; |
| 50 |
| 51 final int mask; |
| 52 |
| 53 const Modifier(this.kind, this.mask); |
| 54 |
| 55 factory Modifier.fromString(String string) { |
| 56 if (identical('abstract', string)) return Abstract; |
| 57 if (identical('const', string)) return Const; |
| 58 if (identical('external', string)) return External; |
| 59 if (identical('final', string)) return Final; |
| 60 if (identical('static', string)) return Static; |
| 61 if (identical('var', string)) return Var; |
| 62 return internalError("Unhandled modifier: $string"); |
| 63 } |
| 64 |
| 65 toString() => "modifier(${'$kind'.substring('ModifierEnum.'.length)})"; |
| 66 |
| 67 static int validate(List<Modifier> modifiers, {bool isAbstract: false}) { |
| 68 // TODO(ahe): Implement modifier validation: ordering and uniqueness. |
| 69 int result = isAbstract ? abstractMask : 0; |
| 70 if (modifiers == null) return result; |
| 71 for (Modifier modifier in modifiers) { |
| 72 result |= modifier.mask; |
| 73 } |
| 74 return result; |
| 75 } |
| 76 } |
OLD | NEW |