token

package
v0.0.0-...-9834360 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 3 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Token

type Token struct {
	Type    Type
	Literal string
	Pos     int
}

A Token represents a known token with its literal representation

func NewToken

func NewToken(typ Type, literal string, pos int) Token

NewToken returns a new Token associated with the given Type typ, the Literal literal and the Position pos

func (Token) IsAssignOperator

func (t Token) IsAssignOperator() bool

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

func (Token) IsKeyword

func (t Token) IsKeyword() bool

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

func (Token) IsLiteral

func (t Token) IsLiteral() bool

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

func (Token) IsOperator

func (t Token) IsOperator() bool

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

type Type

type Type int

A Type represents a type of a known token

const (
	ILLEGAL Type = iota // An illegal/unknown character
	EOF                 // end of input

	IDENT
	CONST
	GLOBAL
	INT
	STRING

	ASSIGN    // =
	ADDASSIGN // +=
	SUBASSIGN // -=
	MULASSIGN // *=
	DIVASSIGN // /=
	MODASSIGN // %=

	PLUS       // +
	MINUS      // -
	BANG       // !
	ASTERISK   // *
	SLASH      // /
	MODULO     // %
	AND        // &
	LOGICALAND // &&
	PIPE       // |
	LOGICALOR  // ||

	LT        // <
	LTE       // <=
	GT        // >
	GTE       // >=
	EQ        // ==
	NOTEQ     // !=
	SPACESHIP // <=>
	LSHIFT    // <<

	HASHROCKET // =>

	NEWLINE // \n
	COMMA
	SEMICOLON
	HASH // #

	CAPTURE  // &
	DOT      // .
	COLON    // :
	LPAREN   // (
	RPAREN   // )
	LBRACE   // {
	RBRACE   // }
	LBRACKET // [
	RBRACKET // ]

	SCOPE // ::
	AT    // @

	QMARK  // ?
	SYMBEG // :

	DEF
	SELF
	END
	IF
	THEN
	ELSE
	UNLESS
	TRUE
	FALSE
	RETURN
	NIL
	MODULE
	CLASS
	DO
	YIELD
	BEGIN
	RESCUE
	WHILE
	KEYWORD__FILE__
)

Recognized token types

func LookupIdent

func LookupIdent(ident string) Type

LookupIdent returns a keyword Type if ident is a keyword. If ident starts with an upper character it returns CONST. In any other case it returns IDENT

func (Type) IsAssignOperator

func (tok Type) IsAssignOperator() bool

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

func (Type) IsKeyword

func (tok Type) IsKeyword() bool

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

func (Type) IsLiteral

func (tok Type) IsLiteral() bool

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

func (Type) IsOperator

func (tok Type) IsOperator() bool

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

func (Type) String

func (tok Type) 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