token

package
v0.78.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

There are 2 types of Token, constant ones (with no "value") and the ones with attached value that is variable (e.g. IDENT, INT, FLOAT, STRING, *COMMENT). We might have used the upcoming unique https://tip.golang.org/doc/go1.23#new-unique-package but we want this to run on 1.22 and earlier and rolled our own, not multi threaded.

Index

Constants

This section is empty.

Variables

View Source
var (
	EOLT   = &Token{tokenType: EOL}
	EOFT   = &Token{tokenType: EOF}
	TRUET  *Token
	FALSET *Token
)

Functions

func Init added in v0.25.0

func Init()

func ResetInterning added in v0.25.0

func ResetInterning()

Types

type GrolInfo added in v0.35.0

type GrolInfo struct {
	// Keywords is a map of all known keywords.
	Keywords sets.Set[string]
	Builtins sets.Set[string]
	Tokens   sets.Set[string]
}

Info enables introspection of known keywords and tokens and operators.

func Info added in v0.35.0

func Info() GrolInfo

type Token

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

func ByType added in v0.25.0

func ByType(t Type) *Token

ByType is the cheapest lookup for all the tokens whose type only have one possible instance/value (ie all the tokens except for the first 4 value tokens). TODO: codegen all the token constants to avoid needing this function. (even though that's better than string comparisons).

func ConstantTokenChar added in v0.25.0

func ConstantTokenChar(literal byte) *Token

func ConstantTokenChar2 added in v0.27.0

func ConstantTokenChar2(c1, c2 byte) *Token

func Intern added in v0.25.0

func Intern(t Type, literal string) *Token

func InternToken added in v0.25.0

func InternToken(t *Token) *Token

Lookup a unique pointer to a token of same values, if it exists, otherwise store the passed in one for future lookups.

func LookupIdent

func LookupIdent(ident string) *Token

func (*Token) DebugString added in v0.25.0

func (t *Token) DebugString() string

func (*Token) Literal

func (t *Token) Literal() string

func (*Token) Type

func (t *Token) Type() Type

type Type

type Type uint8
const (
	ILLEGAL Type = iota
	EOL

	// Identifiers + literals. with attached value.
	IDENT  // add, foobar, x, y, ...
	INT    // 1343456
	FLOAT  // .5, 3.14159,...
	STRING // "foo bar" or `foo bar`
	LINECOMMENT
	BLOCKCOMMENT
	REGISTER // not used for parsing, only to tag object.Register as ast node of unique type.

	// Single character operators.
	ASSIGN
	PLUS
	MINUS
	BANG
	ASTERISK
	SLASH
	PERCENT
	LT
	GT
	BITAND
	BITOR
	BITXOR
	BITNOT

	// Delimiters.
	COMMA
	SEMICOLON

	LPAREN
	RPAREN
	LBRACE
	RBRACE
	LBRACKET
	RBRACKET
	COLON
	DOT

	// LT, GT or equal variants.
	LTEQ
	GTEQ
	EQ
	NOTEQ
	INCR
	DECR
	DOTDOT
	OR
	AND
	LEFTSHIFT
	RIGHTSHIFT
	LAMBDA // => lambda short cut: `a,b => a+b` alias for `func(a,b) {a+b}`
	DEFINE // := (force create new variable instead of possible ref to upper scope)

	// Keywords.
	FUNC
	TRUE
	FALSE
	IF
	ELSE
	RETURN
	FOR
	BREAK
	CONTINUE
	// Macro magic.
	MACRO
	QUOTE
	UNQUOTE
	// Built-in functions.
	LEN
	FIRST
	REST
	PRINT
	PRINTLN
	LOG
	ERROR
	CATCH

	EOF
)

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