class

Logos::Lexer(Token, Source, Extras, Error)

Inherits Iterator < Enumerable < Reference < Object

Lexer is the main struct that allows you to read through a Source and produce tokens for enums implementing the Logos trait.

Constructors

new(source : Source, extras : Extras = Extras.new)

Create a new Lexer with the provided Extras.

Due to type inference, it might be more ergonomic to construct it by calling Token.lexer_with_extras on any Token with derived Logos.

Source

Instance methods

bump(n : Int32) : Nil

Bumps the end of currently lexed token by n bytes.

Panics if adding n to current offset would place the Lexer beyond the last byte, or in the middle of an UTF-8 code point (does not apply when lexing raw Slice(UInt8)).

Source
callback_value

Last callback return value (for tokens with associated data)

Source
callback_value=(callback_value : CallbackValueBase | Nil)

Last callback return value (for tokens with associated data)

Source
callback_value_as(type : T.class) : T(Token, Error) | Nil forall T

Get callback value as a specific type.

Source
clear_callback_value

Clear callback value

Source
clone
Source
extras

Extras associated with the Token.

Source
extras=(extras : Extras)

Extras associated with the Token.

Source
float_value

Get callback value as Float64

Source
int_value

Get callback value as Int64

Source
morph(token_type : Token2.class) : Lexer(Token2, Source, Extras, Error) forall Token2

Turn this lexer into a lexer for a new token type.

The new lexer continues to point at the same span as the current lexer, and the current token becomes the error token of the new token type.

Source
morph(token_type : Token2.class, & : Extras -> Extras2) : Lexer(Token2, Source, Extras2, Error) forall Token2, Extras2
Source
next

Get the next token from the source.

Source
payload_for(result : Result(Token, Error), expected_token : Token, type : T.class) : T(Token, Error) | Nil forall T

Return a typed callback payload when the result is the expected token variant.

Source
payload_for!(result : Result(Token, Error), expected_token : Token, type : T.class) : T(Token, Error) forall T

Same as payload_for, but raises if the token doesn't match or payload type is missing.

Source
read_bytes(bytes : Int32, offset : Int32 = 0) : Slice(UInt8) | Nil

Read bytes bytes starting at current position of the Lexer plus offset. If end of the Source has been reached, this will return nil.

Source
read_u8(offset : Int32 = 0) : UInt8 | Nil

Read a single byte at current position of the Lexer plus offset. If end of the Source has been reached, this will return nil.

Source
remainder

Get a slice of remaining source, starting at the end of current token.

Source
slice

Get a string slice of the current token.

Source
source

Source from which this Lexer is reading tokens.

Source
span

Get the range for the current token in Source.

Source
spanned

Wrap the Lexer in an Iterator that produces tuples of (Token, Span).

tokens = Token.lexer("42 3.14 -5 f").spanned.to_a
# => [{Token::Integer(42), 0..2}, {Token::Float(3.14), 3..7}, ...]
Source
string_value

Get callback value as String

Source