tokens

package
v0.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Char

type Char struct {
	Rune   rune
	Size   int // Size in bytes
	Line   int
	Column int
}

Represents a character in the source code, with its position.

func NewChar

func NewChar(line, column, size int, rune rune) Char

Creates a new Char at line and column with the given rune and size.

func (Char) At

func (p Char) At() (line, column int)

Returns the line and column of the character.

func (Char) Is

func (p Char) Is(r rune) bool

Checks if the character rune matches the given rune.

func (Char) IsOneOf

func (p Char) IsOneOf(runes ...rune) bool

Checks if the character rune is one of the given runes.

type Token

type Token struct {
	Type       TokenType
	Literal    string
	FromLine   int
	FromColumn int
	ToLine     int
	ToColumn   int
}

Represents a token in the source code.

tokens.NewToken(EOF, "").WithRangeChars(from, to)

func NewToken

func NewToken(t TokenType, l string) *Token

Creates a new token.

func (*Token) DebugString

func (t *Token) DebugString() string

Pretty string representation of the token.

func (*Token) From added in v0.2.0

func (t *Token) From() (line, column int)

Returns the initial position of the token.

func (*Token) IsLiteral

func (t *Token) IsLiteral(lit string) bool

Returns true if the token is the given literal.

func (*Token) IsOneOfLiterals

func (t *Token) IsOneOfLiterals(lits ...string) bool

Returns true if the token is one of the given literals.

func (*Token) IsOneOfTypes added in v0.2.0

func (t *Token) IsOneOfTypes(tys ...TokenType) bool

Returns true if the token is of the given type.

func (*Token) IsType

func (t *Token) IsType(ty TokenType) bool

Returns true if the token is of the given type.

func (*Token) Range added in v0.2.0

func (t *Token) Range() (fromLine, fromColumn, toLine, toColumn int)

Returns the range of the token.

func (*Token) RangeColumns added in v0.2.0

func (t *Token) RangeColumns() (from, to int)

Returns the range of the token ignoring the lines. Starting at 1.

func (*Token) RangeLines added in v0.2.0

func (t *Token) RangeLines() (from, to int)

Returns the range of the starting and final lines. Starting at 1.

func (*Token) To added in v0.2.0

func (t *Token) To() (line, column int)

Returns the final position of the token.

func (Token) WithLiteral added in v0.2.0

func (t Token) WithLiteral(lit string) *Token

Creates a new token with the given literal. You can use this method as a chain.

func (Token) WithRange added in v0.2.0

func (t Token) WithRange(fromLine, fromColumn, toLine, toColumn int) *Token

Creates a new token with the given range. You can use this method as a chain.

func (Token) WithRangeChars added in v0.2.0

func (t Token) WithRangeChars(from, to Char) *Token

Creates a new token with the given char range. You can use this method as a chain.

func (Token) WithType added in v0.2.0

func (t Token) WithType(tp TokenType) *Token

Creates a new token with the given type. You can use this method as a chain.

type TokenType

type TokenType string
const (
	UNKNOWN TokenType = "unknown"
	INVALID TokenType = "invalid"
	EOF     TokenType = "eof"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL