parsers library
Properties
final Parser eof #
final Parser eof = new Parser((s, pos) =>
pos.offset >= s.length
? _success(null, s, pos)
: _failure(s, pos, _singleExpectation("eof", pos)))
Functions
Parser<String> noneOf(String chars) #
Parser<String> noneOf(String chars) =>
pred((c) => !chars.contains(c)).expecting("none of '$chars'");
Parser<String> oneOf(String chars) #
Parser<String> oneOf(String chars) =>
pred((c) => chars.contains(c)).expecting("one of '$chars'");
Parser choice(List<Parser> ps) #
Parser choice(List<Parser> ps) {
// Imperative version for efficiency
return new Parser((s, pos) {
var exps = _emptyExpectation(pos);
for (final p in ps) {
final res = p._run(s, pos);
exps = exps.best(res.expectations);
if (res.isSuccess) {
return res.copy(expectations: exps);
} else if (res.isCommitted) {
return res;
}
}
return _failure(s, pos, exps);
});
}
Parser string(String str) #
Parser string(String str) {
// Primitive version for efficiency
return new Parser((s, pos) {
final int offset = pos.offset;
final int max = offset + str.length;
int newline = pos.line;
int newchar = pos.character;
// This replicates Position#addChar for efficiency purposes.
void update(c) {
final isNewLine = c == '\n';
newline = newline + (isNewLine ? 1 : 0);
newchar = isNewLine ? 1 : newchar + 1;
}
bool match = s.length >= max;
for (int i = 0; i < str.length && match; i++) {
final c = s[offset + i];
match = match && c == str[i];
update(c);
}
if (match) {
return _success(str, s, new Position(max, newline, newchar));
} else {
return _failure(s, pos, _singleExpectation("'$str'", pos));
}
});
}