Dart DocumentationpetitparserToken

Token class

A token represents a parsed part of the input stream. The token holds the parsed input, the input buffer, and the start and stop position in the input buffer.

class Token {

 final dynamic _value;
 final dynamic _buffer;
 final int _start;
 final int _stop;

 const Token(this._value, this._buffer, this._start, this._stop);

 bool operator == (Token other) {
   return other is Token
     && _value == other._value
     && _start == other._start
     && _stop == other._stop;
 }

 int get hashCode => _value.hashCode + _start.hashCode + _stop.hashCode;

 /**
  * Returns the parsed value.
  */
 dynamic get value => _value;


 /**
  * Returns the input buffer.
  */
 dynamic get buffer => _buffer;

 /**
  * Returns the start position in the input buffer.
  */
 int get start => _start;

 /**
  * Returns the stop position in the input buffer.
  */
 int get stop => _stop;

 /**
  * Returns the length of the token.
  */
 int get length => _stop - _start;

 /**
  * Returns the line number of the token.
  */
 int get line => Token.lineAndColumnOf(_buffer, _start)[0];

 /**
  * Returns the column number of this token.
  */
 int get column => Token.lineAndColumnOf(_buffer, _start)[1];

 String toString() => 'Token[start: $start, stop: $stop, value: $value]';

 static final Parser _NEWLINE_PARSER =
     char('\n').or(char('\r').seq(char('\n').optional()));

 static Parser newlineParser() => _NEWLINE_PARSER;

 static List<int> lineAndColumnOf(String buffer, int position) {
   var line = 1, offset = 0;
   for (var token in newlineParser().token().matchesSkipping(buffer)) {
     if (position < token.stop) {
       return [line, position - offset + 1];
     }
     line++;
     offset = token.stop;
   }
   return [line, position - offset + 1];
 }

}

Static Methods

Parser newlineParser() #

static Parser newlineParser() => _NEWLINE_PARSER;

List<int> lineAndColumnOf(String buffer, int position) #

static List<int> lineAndColumnOf(String buffer, int position) {
 var line = 1, offset = 0;
 for (var token in newlineParser().token().matchesSkipping(buffer)) {
   if (position < token.stop) {
     return [line, position - offset + 1];
   }
   line++;
   offset = token.stop;
 }
 return [line, position - offset + 1];
}

Constructors

const Token(_value, _buffer, int _start, int _stop) #

const Token(this._value, this._buffer, this._start, this._stop);

Properties

final buffer #

Returns the input buffer.

dynamic get buffer => _buffer;

final int column #

Returns the column number of this token.

int get column => Token.lineAndColumnOf(_buffer, _start)[1];

final int hashCode #

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

docs inherited from Object
int get hashCode => _value.hashCode + _start.hashCode + _stop.hashCode;

final int length #

Returns the length of the token.

int get length => _stop - _start;

final int line #

Returns the line number of the token.

int get line => Token.lineAndColumnOf(_buffer, _start)[0];

final int start #

Returns the start position in the input buffer.

int get start => _start;

final int stop #

Returns the stop position in the input buffer.

int get stop => _stop;

final value #

Returns the parsed value.

dynamic get value => _value;

Operators

bool operator ==(Token other) #

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator == (Token other) {
 return other is Token
   && _value == other._value
   && _start == other._start
   && _stop == other._stop;
}

Methods

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() => 'Token[start: $start, stop: $stop, value: $value]';