syntax.Buffer
type pub BufferA type that represents a bunch of bytes to process using a lexer.
Fields
bytes
let pub @bytes: ref ByteArrayThe bytes that are to be turned into a token stream.
offset
let pub @offset: IntThe current byte offset.
Static methods
new
Show source codeHide source code
fn pub static new(bytes: ref ByteArray) -> Buffer {
Buffer(bytes: bytes, offset: 0)
}fn pub static new(bytes: ref ByteArray) -> BufferReturns a new Buffer that wraps the given ByteArray.
Instance methods
advance_while
Show source codeHide source code
fn pub mut advance_while(condition: fn (Int) -> Bool) {
while
@offset < @bytes.size and condition.call(@bytes.get(@offset).or_panic)
{
@offset += 1
}
}fn pub mut advance_while(condition: fn (Int) -> Bool)Advances the cursor while the given closure returns true.
get
Show source codeHide source code
fn pub get -> Int {
peek(0)
}fn pub get -> IntReturns the byte at the current byte offset.
peek
Show source codeHide source code
fn pub peek(amount: Int) -> Int {
let idx = @offset + amount
if idx < @bytes.size { @bytes.get(idx).or_panic } else { EOF }
}fn pub peek(amount: Int) -> IntReturns 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 codeHide source code
fn pub size -> Int {
@bytes.size
}fn pub size -> IntReturns the total number of bytes in the buffer.
slice
Show source codeHide source code
fn pub slice(start: Int, end: Int) -> String {
@bytes.slice(start, end).to_string
}fn pub slice(start: Int, end: Int) -> StringSlices the buffer into a substring.
token
Show source codeHide source code
fn pub token(kind: TokenKind, start: Int) -> Token {
Token.new(kind, value: slice(start, @offset))
}fn pub token(kind: TokenKind, start: Int) -> TokenReturns a new token starting at the given offset that ranges until the current offset.