token

package
v0.0.0-...-eeb96fe Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package token declares the type representing a lexical token of Lox code.

Index

Constants

View Source
const (
	// PlaceholderIdent is the special identifier which can be used as a placeholder in declarations and assignments.
	PlaceholderIdent = "_"
	// CurrentInstanceIdent is the identifier used to refer the current instance of the class in a method.
	CurrentInstanceIdent = thisIdent
	// ConstructorIdent is the identifier used for the constructor method for classes.
	ConstructorIdent = "init"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File is a simple representation of a file.

func NewFile

func NewFile(name string, contents []byte) *File

NewFile returns a new File with the given contents.

func (*File) Line

func (f *File) Line(n int) []byte

Line returns the nth line of the file.

func (*File) Name

func (f *File) Name() string

Name returns the name of the file.

type Position

type Position struct {
	File   *File
	Line   int // 1-based line number
	Column int // 0-based byte offset from the start of the line
}

Position is a position in a file.

func (Position) Compare

func (p Position) Compare(other Position) int

Compare returns

-1 if p's file comes before other's or p and other are in the same file and p comes before other,
 0 if p and other are the same position in the same file,
+1 if p's file comes after other's or p and other are in the same file and p comes after other.

func (Position) Format

func (p Position) Format(f fmt.State, verb rune)

Format implements fmt.Formatter. All verbs have the default behaviour, except for 'm' (message) which formats the position for use in an error message.

func (Position) String

func (p Position) String() string

type Range

type Range interface {
	Start() Position // Start returns the position of the first character of the range.
	End() Position   // End returns the position of the character immediately after the range.
}

Range describes a range of characters in the source code.

type Token

type Token struct {
	StartPos Position // Position of the first character of the token
	EndPos   Position // Position of the character immediately after the token
	Type     Type
	Lexeme   string
}

Token is a lexical token of Lox code.

func (Token) End

func (t Token) End() Position

End returns the position of the character immediately after the token.

func (Token) IsZero

func (t Token) IsZero() bool

IsZero reports whether t is the zero value.

func (Token) Start

func (t Token) Start() Position

Start returns the position of the first character of the token.

func (Token) String

func (t Token) String() string

type Type

type Type int

Type is the type of a lexical token of Lox code.

const (
	Illegal Type = iota
	EOF

	Print
	Var
	True
	False
	Nil
	If
	Else
	And
	Or
	While
	For
	Break
	Continue
	Fun
	Return
	Class
	This
	Super
	Static
	Get
	Set

	// Literals
	Ident
	String
	Number
	Comment

	// Symbols
	Semicolon
	Comma
	Dot
	Equal
	Plus
	Minus
	Asterisk
	Slash
	Percent
	Less
	LessEqual
	Greater
	GreaterEqual
	EqualEqual
	BangEqual
	Bang
	Question
	Colon
	LeftParen
	RightParen
	LeftBrace
	RightBrace
)

The list of all token types.

func IdentType

func IdentType(ident string) Type

IdentType returns the type of the keyword with the given identifier, or Ident if the identifier is not a keyword.

func (Type) Format

func (t Type) Format(f fmt.State, verb rune)

Format implements fmt.Formatter. All verbs have the default behaviour, except for 'm' (message) which formats the type for use in an error message.

func (Type) String

func (i Type) String() string

Jump to

Keyboard shortcuts

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