Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: petitparser/lib/src/core/actions.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « petitparser/lib/smalltalk.dart ('k') | petitparser/lib/src/core/characters.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 part of petitparser;
2
3 /**
4 * A parser that performs a transformation with a given function on the
5 * successful parse result of the delegate.
6 */
7 class ActionParser extends DelegateParser {
8 final Function _function;
9
10 ActionParser(parser, this._function) : super(parser);
11
12 @override
13 Result parseOn(Context context) {
14 var result = _delegate.parseOn(context);
15 if (result.isSuccess) {
16 return result.success(_function(result.value));
17 } else {
18 return result;
19 }
20 }
21
22 @override
23 Parser copy() => new ActionParser(_delegate, _function);
24
25 @override
26 bool hasEqualProperties(Parser other) {
27 return other is ActionParser
28 && super.hasEqualProperties(other)
29 && _function == other._function;
30 }
31 }
32
33 /**
34 * A parser that silently consumes input of another parser around
35 * its delegate.
36 */
37 class TrimmingParser extends DelegateParser {
38 Parser _left;
39 Parser _right;
40
41 TrimmingParser(parser, this._left, this._right) : super(parser);
42
43 @override
44 Result parseOn(Context context) {
45 var current = context;
46 do {
47 current = _left.parseOn(current);
48 } while (current.isSuccess);
49 var result = _delegate.parseOn(current);
50 if (result.isFailure) {
51 return result;
52 }
53 current = result;
54 do {
55 current = _right.parseOn(current);
56 } while (current.isSuccess);
57 return current.success(result.value);
58 }
59
60 @override
61 Parser copy() => new TrimmingParser(_delegate, _left, _right);
62
63 @override
64 List<Parser> get children => [_delegate, _left, _right];
65
66 @override
67 void replace(Parser source, Parser target) {
68 super.replace(source, target);
69 if (_left == source) {
70 _left = target;
71 }
72 if (_right == source) {
73 _right = target;
74 }
75 }
76 }
77
78 /**
79 * A parser that answers a substring or sub-list of the range its delegate
80 * parses.
81 */
82 class FlattenParser extends DelegateParser {
83 FlattenParser(parser) : super(parser);
84
85 @override
86 Result parseOn(Context context) {
87 var result = _delegate.parseOn(context);
88 if (result.isSuccess) {
89 var output = context.buffer is String
90 ? context.buffer.substring(context.position, result.position)
91 : context.buffer.sublist(context.position, result.position);
92 return result.success(output);
93 } else {
94 return result;
95 }
96 }
97
98 @override
99 Parser copy() => new FlattenParser(_delegate);
100 }
101
102 /**
103 * A parser that answers a token of the result its delegate parses.
104 */
105 class TokenParser extends DelegateParser {
106 TokenParser(parser) : super(parser);
107
108 @override
109 Result parseOn(Context context) {
110 var result = _delegate.parseOn(context);
111 if (result.isSuccess) {
112 var token = new Token(
113 result.value, context.buffer, context.position, result.position);
114 return result.success(token);
115 } else {
116 return result;
117 }
118 }
119
120 @override
121 Parser copy() => new TokenParser(_delegate);
122 }
OLDNEW
« no previous file with comments | « petitparser/lib/smalltalk.dart ('k') | petitparser/lib/src/core/characters.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698