Search results

There are no results.

syntax.Buffer

class pub Buffer

A type that represents a bunch of bytes to process using a lexer.

Fields

bytes

let pub @bytes: ref ByteArray

The bytes that are to be turned into a token stream.

offset

let pub @offset: Int

The current byte offset.

Static methods

new

Show source code
Hide source code
fn pub static new(bytes: ref ByteArray) -> Buffer {
  Buffer(bytes: bytes, offset: 0)
}
fn pub static new(bytes: ref ByteArray) -> Buffer

Returns a new Buffer that wraps the given ByteArray.

Instance methods

advance_while

Show source code
Hide source code
fn pub mut advance_while(condition: fn (Int) -> Bool) {
  while @offset < @bytes.size and condition.call(@bytes.byte(@offset)) {
    @offset += 1
  }
}
fn pub mut advance_while(condition: fn (Int) -> Bool)

Advances the cursor while the given closure returns true.

get

Show source code
Hide source code
fn pub get -> Int {
  peek(0)
}
fn pub get -> Int

Returns the byte at the current byte offset.

peek

Show source code
Hide source code
fn pub peek(amount: Int) -> Int {
  let idx = @offset + amount

  if idx < @bytes.size { @bytes.byte(idx) } else { EOF }
}
fn pub peek(amount: Int) -> Int

Returns the byte that is at amount bytes relative to the current offset.

If the offset is out of range, the EOF byte is returned.

size

Show source code
Hide source code
fn pub size -> Int {
  @bytes.size
}
fn pub size -> Int

Returns the total number of bytes in the buffer.

slice

Show source code
Hide source code
fn pub slice(start: Int, size: Int) -> String {
  @bytes.slice(start, size).into_string
}
fn pub slice(start: Int, size: Int) -> String

Slices the buffer into a substring.

token

Show source code
Hide source code
fn pub token(kind: TokenKind, start: Int) -> Token {
  Token.new(kind, value: slice(start, @offset - start))
}
fn pub token(kind: TokenKind, start: Int) -> Token

Returns a new token starting at the given offset that ranges until the current offset.