| OLD | NEW |
| (Empty) |
| 1 part of petitparser; | |
| 2 | |
| 3 /** | |
| 4 * An immutable parse context. | |
| 5 */ | |
| 6 class Context { | |
| 7 const Context(this.buffer, this.position); | |
| 8 | |
| 9 /** | |
| 10 * The buffer we are working on. | |
| 11 */ | |
| 12 final buffer; | |
| 13 | |
| 14 /** | |
| 15 * The current position in the buffer. | |
| 16 */ | |
| 17 final int position; | |
| 18 | |
| 19 /** | |
| 20 * Returns a result indicating a parse success. | |
| 21 */ | |
| 22 Result success(result, [int position]) { | |
| 23 return new Success(buffer, position == null ? this.position : position, resu
lt); | |
| 24 } | |
| 25 | |
| 26 /** | |
| 27 * Returns a result indicating a parse failure. | |
| 28 */ | |
| 29 Result failure(String message, [int position]) { | |
| 30 return new Failure(buffer, position == null ? this.position : position, mess
age); | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * Returns a human readable string of the current context. | |
| 35 */ | |
| 36 String toString() => 'Context[${toPositionString()}]'; | |
| 37 | |
| 38 /** | |
| 39 * Returns the line:column if the input is a string, otherwise the position. | |
| 40 */ | |
| 41 String toPositionString() => Token.positionString(buffer, position); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * An immutable parse result. | |
| 46 */ | |
| 47 abstract class Result extends Context { | |
| 48 const Result(buffer, position) : super(buffer, position); | |
| 49 | |
| 50 /** | |
| 51 * Returns [true] if this result indicates a parse success. | |
| 52 */ | |
| 53 bool get isSuccess => false; | |
| 54 | |
| 55 /** | |
| 56 * Returns [true] if this result indicates a parse failure. | |
| 57 */ | |
| 58 bool get isFailure => false; | |
| 59 | |
| 60 /** | |
| 61 * Returns the parse result of the current context. | |
| 62 */ | |
| 63 get value; | |
| 64 | |
| 65 /** | |
| 66 * Returns the parse message of the current context. | |
| 67 */ | |
| 68 String get message; | |
| 69 } | |
| 70 | |
| 71 /** | |
| 72 * An immutable parse result in case of a successful parse. | |
| 73 */ | |
| 74 class Success extends Result { | |
| 75 const Success(buffer, position, this.value) : super(buffer, position); | |
| 76 | |
| 77 @override | |
| 78 bool get isSuccess => true; | |
| 79 | |
| 80 @override | |
| 81 final value; | |
| 82 | |
| 83 @override | |
| 84 String get message => null; | |
| 85 | |
| 86 @override | |
| 87 String toString() => 'Success[${toPositionString()}]: $value'; | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * An immutable parse result in case of a failed parse. | |
| 92 */ | |
| 93 class Failure extends Result { | |
| 94 const Failure(buffer, position, this.message) : super(buffer, position); | |
| 95 | |
| 96 @override | |
| 97 bool get isFailure => true; | |
| 98 | |
| 99 @override | |
| 100 get value => throw new ParserError(this); | |
| 101 | |
| 102 @override | |
| 103 final String message; | |
| 104 | |
| 105 @override | |
| 106 String toString() => 'Failure[${toPositionString()}]: $message'; | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * An exception raised in case of a parse error. | |
| 111 */ | |
| 112 class ParserError extends Error { | |
| 113 final Failure failure; | |
| 114 | |
| 115 ParserError(this.failure); | |
| 116 | |
| 117 @override | |
| 118 String toString() => '${failure.message} at ${failure.toPositionString()}'; | |
| 119 } | |
| OLD | NEW |