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