Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap input in Buffer #55

Closed
renggli opened this issue Dec 19, 2016 · 1 comment
Closed

Wrap input in Buffer #55

renggli opened this issue Dec 19, 2016 · 1 comment

Comments

@renggli
Copy link
Member

renggli commented Dec 19, 2016

Currently code is scattered with type-checks on the parsed input being a String or a List. Having a buffer could avoid this problem and enable further extensibility on the data being parsed.

/// An immutable parse buffer.
abstract class Buffer<E, S> {

  ///
  factory Buffer.from(Object input) {
    if (input is String) {
      return new _StringBuffer(input, input.length);
    } else if (input is List) {
      return new _ListBuffer(input, input.length);
    }
  }

  /// Return the underlying data structure.
  S data;

  /// Return the size of the buffer.
  int get size;

  /// Return the element at position [value].
  E operator [](int index);

  /// Return the sequence from [start] to [stop].
  S sequence(int start, int stop);

}

class _StringBuffer implements Buffer<int, String> {

  @override
  final String data;

  @override
  final int length;

  _StringBuffer(this.data, this.length);

  @override
  int operator [](int index) => data.codeUnitAt(index);

  @override
  String sequence(int startIndex, int stopIndex) => data.substring(startIndex, stopIndex);

}

class _ListBuffer<T> implements Buffer<T, List<T>> {

  @override
  final List<T> data;

  @override
  final int length;

  _ListBuffer(this.data, this.length);

  @override
  T operator [](int index) => data[index];

  @override
  List<T> sequence(int startIndex, int stopIndex) => data.sublist(startIndex, stopIndex);

}
@renggli
Copy link
Member Author

renggli commented Mar 8, 2019

No longer relevant, since PetitParser dropped support for non-string parsing.

@renggli renggli closed this as completed Mar 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant