Dart Documentationparsers

parsers library

Properties

final Parser<String> alphanum #

final Parser<String> alphanum = oneOf(_alphanum)

final Parser<String> anyChar #

final Parser<String> anyChar = pred((c) => true) % 'any character'

final Parser<String> digit #

final Parser<String> digit = oneOf(_digit) % 'digit'

final Parser eof #

final Parser eof = new Parser((s, pos) =>
   pos.offset >= s.length
       ? _success(null, s, pos)
       : _failure(s, pos, _singleExpectation("eof", pos)))

final Parser fail #

final Parser fail = new Parser((s, pos) => _failure(s, pos))

final Parser<String> letter #

final Parser<String> letter = oneOf(_alpha) % 'letter'

final Parser<String> lower #

final Parser<String> lower = oneOf(_lower) % 'lowercase letter'

final Parser<String> newline #

final Parser<String> newline = char('\n') % 'newline'

final Parser<Position> position #

final Parser<Position> position =
   new Parser((s, pos) => _success(pos, s, pos))

final Parser<String> space #

final Parser<String> space = oneOf(_spaces) % 'space'

final Parser spaces #

final Parser spaces = (space.many > success(null)) % 'spaces'

final Parser<String> tab #

final Parser<String> tab = char('\t') % 'tab'

final Parser<String> upper #

final Parser<String> upper = oneOf(_upper) % 'uppercase letter'

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 rec(Parser f()) #

Parser rec(Parser f()) => new Parser((s, pos) => f()._run(s, pos));

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));
   }
 });
}

Parser char(String chr) #

Parser char(String chr) => pred((c) => c == chr) % "'$chr'";

Parser pred(bool p(String char)) #

Parser pred(bool p(String char)) {
 return new Parser((s, pos) {
   if (pos.offset >= s.length) return _failure(s, pos);
   else {
     String c = s[pos.offset];
     return p(c) ? _success(c, s, pos.addChar(c))
                 : _failure(s, pos);
   }
 });
}

Parser success(value) #

Parser success(value) => new Parser((s, pos) => _success(value, s, pos));

Classes