| OLD | NEW |
| (Empty) |
| 1 part of petitparser; | |
| 2 | |
| 3 /** | |
| 4 * Returns a parser that consumes nothing and succeeds. | |
| 5 * | |
| 6 * For example, `char('a').or(epsilon())` is equivalent to | |
| 7 * `char('a').optional()`. | |
| 8 */ | |
| 9 Parser epsilon([result]) => new EpsilonParser(result); | |
| 10 | |
| 11 /** | |
| 12 * A parser that consumes nothing and succeeds. | |
| 13 */ | |
| 14 class EpsilonParser extends Parser { | |
| 15 final _result; | |
| 16 | |
| 17 EpsilonParser(this._result); | |
| 18 | |
| 19 @override | |
| 20 Result parseOn(Context context) => context.success(_result); | |
| 21 | |
| 22 @override | |
| 23 Parser copy() => new EpsilonParser(_result); | |
| 24 | |
| 25 @override | |
| 26 bool hasEqualProperties(Parser other) { | |
| 27 return other is EpsilonParser | |
| 28 && super.hasEqualProperties(other) | |
| 29 && _result == other._result; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * Returns a parser that consumes nothing and fails. | |
| 35 * | |
| 36 * For example, `failure()` always fails, no matter what input it is given. | |
| 37 */ | |
| 38 Parser failure([String message = 'unable to parse']) { | |
| 39 return new FailureParser(message); | |
| 40 } | |
| 41 | |
| 42 /** | |
| 43 * A parser that consumes nothing and fails. | |
| 44 */ | |
| 45 class FailureParser extends Parser { | |
| 46 final String _message; | |
| 47 | |
| 48 FailureParser(this._message); | |
| 49 | |
| 50 @override | |
| 51 Result parseOn(Context context) => context.failure(_message); | |
| 52 | |
| 53 @override | |
| 54 String toString() => '${super.toString()}[$_message]'; | |
| 55 | |
| 56 @override | |
| 57 Parser copy() => new FailureParser(_message); | |
| 58 | |
| 59 @override | |
| 60 bool hasEqualProperties(Parser other) { | |
| 61 return other is FailureParser | |
| 62 && super.hasEqualProperties(other) | |
| 63 && _message == other._message; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * Returns a parser that is not defined, but that can be set at a later | |
| 69 * point in time. | |
| 70 * | |
| 71 * For example, the following code sets up a parser that points to itself | |
| 72 * and that accepts a sequence of a's ended with the letter b. | |
| 73 * | |
| 74 * var p = undefined(); | |
| 75 * p.set(char('a').seq(p).or(char('b'))); | |
| 76 */ | |
| 77 SettableParser undefined([String message = 'undefined parser']) { | |
| 78 return failure(message).settable(); | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * A parser that is not defined, but that can be set at a later | |
| 83 * point in time. | |
| 84 */ | |
| 85 class SettableParser extends DelegateParser { | |
| 86 SettableParser(parser) : super(parser); | |
| 87 | |
| 88 /** | |
| 89 * Sets the receiver to delegate to [parser]. | |
| 90 */ | |
| 91 void set(Parser parser) => replace(children[0], parser); | |
| 92 | |
| 93 @override | |
| 94 Parser copy() => new SettableParser(_delegate); | |
| 95 } | |
| OLD | NEW |