token

package
v0.0.0-...-a9e085c Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: Unlicense Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LowestPrec  = 0 // non-operators
	UnaryPrec   = 6
	HighestPrec = 7
)

A set of constants for precedence-based expression parsing. Non-operators have lowest precedence, followed by operators starting with precedence 1 up to unary operators. The highest precedence serves as "catch-all" precedence for selector, indexing, and other operator and delimiter tokens.

Variables

This section is empty.

Functions

func IsIdentifier

func IsIdentifier(name string) bool

IsIdentifier reports whether name is a Go identifier, that is, a non-empty string made up of letters, digits, and underscores, where the first character is not a digit. Keywords are not identifiers.

func IsKeyword

func IsKeyword(name string) bool

IsKeyword reports whether name is a Go keyword, such as "fn" or "return".

Types

type Token

type Token int
const (
	Illegal Token = iota
	EOF
	Comment

	Identifier // main
	Integer    // 12345
	Float      // 123.45
	Imaginary  // 123.45i
	Character  // 'abc'
	String     // "abc"

	Assign // =

	Add       // +
	Substract // -
	Multiply  // *
	Quotient  // /
	Remainder // %

	AddAssign       // +=
	SubstractAssign // -=
	MultiplyAssign  // *=
	QuotientAssign  // /=
	RemainderAssign // %=

	And        // &
	Or         // |
	Xor        // ^
	ShiftLeft  // <<
	ShiftRight // >>

	AndAssign        // &=
	OrAssign         // |=
	XorAssign        // ^=
	ShiftLeftAssign  // <<=
	ShiftRightAssign // >>=

	LogicAnd  // &&
	LogicOr   // ||
	Increment // ++
	Decrement // --

	Equal   // ==
	Less    // <
	Greater // >
	Not     // !

	NotEqual     // !=
	LessEqual    // <=
	GreaterEqual // >=

	// Delimiters
	Comma     // ,
	Period    // .
	Ellipsis  // ...
	SemiColon // ;
	Colon     // :

	LeftParenthesis  // (
	RightParenthesis // )
	LeftBrace        // {
	RightBrace       // }
	LeftBracket      // [
	RightBracket     // ]

	Function // fn
	Let

	Switch
	Case
	Default
	Continue
	Fallthrough
	Break

	If
	Else

	Defer

	Goto
	Return
)

func Lookup

func Lookup(ident string) Token

Lookup maps an identifier to its keyword token or IDENT (if not a keyword).

func (Token) IsKeyword

func (tok Token) IsKeyword() bool

IsKeyword returns true for tokens corresponding to keywords; it returns false otherwise.

func (Token) IsLiteral

func (tok Token) IsLiteral() bool

IsLiteral returns true for tokens corresponding to identifiers and basic type literals; it returns false otherwise.

func (Token) IsOperator

func (tok Token) IsOperator() bool

IsOperator returns true for tokens corresponding to operators and delimiters; it returns false otherwise.

func (Token) Precedence

func (op Token) Precedence() int

Precedence returns the operator precedence of the binary operator op. If op is not a binary operator, the result is LowestPrecedence.

func (Token) String

func (tok Token) String() string

String returns the string corresponding to the token tok. For operators, delimiters, and keywords the string is the actual token character sequence (e.g., for the token Add, the string is "+"). For all other tokens the string corresponds to the token constant name (e.g. for the token Ident, the string is "IDENT").

Jump to

Keyboard shortcuts

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