token

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Keywords = map[string]Type{}/* 103 elements not displayed */

Maps keywords to their Token Type.

Functions

func Length

func Length() int

func Types

func Types() iter.Seq2[uint16, string]

Types

type Token

type Token struct {
	Type
	Value string // Literal value of the token, will be empty for tokens with non-dynamic lexemes
	// contains filtered or unexported fields
}

Represents a single token produced by the lexer.

func New

func New(loc *position.Location, tokenType Type) *Token

Creates a new token.

func NewWithValue

func NewWithValue(loc *position.Location, tokenType Type, value string) *Token

Creates a new token with the specified value.

func (*Token) AnsiStyling

func (t *Token) AnsiStyling() []color.Attribute

Returns the ANSI font styling for the github.com/fatih/color package.

func (*Token) BackgroundColor

func (t *Token) BackgroundColor() prompt.Color

Background color for go-prompt.

func (*Token) Class

func (*Token) Class() *value.Class

func (*Token) Color

func (t *Token) Color() prompt.Color

Text color for go-prompt.

func (*Token) Copy

func (t *Token) Copy() value.Reference

func (*Token) DirectClass

func (*Token) DirectClass() *value.Class

func (*Token) DisplayAttributes

func (t *Token) DisplayAttributes() []prompt.DisplayAttribute

Display attributes for go-prompt eg. bold, italic, underline.

func (*Token) Equal

func (t *Token) Equal(other *Token) bool

func (*Token) Error

func (t *Token) Error() string

func (*Token) FetchValue

func (t *Token) FetchValue() string

When the Value field of the token is empty, the string will be fetched from a global map.

func (*Token) FirstByteIndex

func (t *Token) FirstByteIndex() pstrings.ByteNumber

Index of the first byte of the lexeme. Used by go-prompt.

func (*Token) Inspect

func (t *Token) Inspect() string

func (*Token) InspectValue

func (t *Token) InspectValue() string

Returns a shortened version of the value which resembles source code.

func (*Token) InstanceVariables

func (t *Token) InstanceVariables() *value.InstanceVariables

func (*Token) LastByteIndex

func (t *Token) LastByteIndex() pstrings.ByteNumber

Index of the last byte of the lexeme. Used by go-prompt.

func (*Token) Location

func (t *Token) Location() *position.Location

func (*Token) SetLocation

func (t *Token) SetLocation(loc *position.Location)

func (*Token) SetSpan

func (t *Token) SetSpan(span *position.Span)

func (*Token) SingletonClass

func (*Token) SingletonClass() *value.Class

func (*Token) Span

func (t *Token) Span() *position.Span

func (*Token) Splice

func (t *Token) Splice(loc *position.Location, unquote bool) *Token

func (*Token) String

func (t *Token) String() string

Implements the fmt.Stringer interface.

type Type

type Type uint16

Represents the type of token

const (
	ZERO_VALUE       Type = iota // Zero value for Type
	ERROR                        // ERROR Token with a message
	END_OF_FILE                  // End Of File has been reached
	TEXT                         // Plain text
	NEWLINE                      // Newline `\n`, `\r\n`
	SEMICOLON                    // SEMICOLON `;`
	COMMA                        // Comma `,`
	DOT                          // Dot `.`
	QUESTION_DOT                 // Safe method call operator `?.`
	DOT_DOT                      // Cascade call operator `..`
	QUESTION_DOT_DOT             // Safe cascade call operator `?..`
	COLON                        // Colon `:`
	QUESTION                     // Question mark `?`

	// Operators start here
	LABEL_OP_BEG
	THIN_ARROW   // Thin arrow `->` (closure arrow)
	WIGGLY_ARROW // Wiggly arrow `~>` (lambda arrow)
	THICK_ARROW  // Thick arrow `=>`

	// Assignment operators start here
	LABEL_ASSIGN_OP_BEG
	EQUAL_OP                // Equal (assignment) `=`
	MINUS_EQUAL             // Minus equal `-=`
	PLUS_EQUAL              // Plus equal `+=`
	STAR_EQUAL              // Star equal `*=`
	SLASH_EQUAL             // Slash equal `/=`
	STAR_STAR_EQUAL         // Two stars equal `**=`
	TILDE_EQUAL             // Tilde equal `~=`
	AND_AND_EQUAL           // Logical and equal `&&=`
	AND_EQUAL               // Bitwise and equal `&=`
	OR_OR_EQUAL             // Logical or `||=`
	OR_EQUAL                // Bitwise or equal `|=`
	XOR_EQUAL               // Bitwise xor equal `^=`
	QUESTION_QUESTION_EQUAL // Nil coalescing equal operator `??=`
	LBITSHIFT_EQUAL         // Left bitwise shift equal `<<=`
	LTRIPLE_BITSHIFT_EQUAL  // Triple left bitwise shift equal `<<<=`
	RBITSHIFT_EQUAL         // Right bitwise shift equal `>>=`
	RTRIPLE_BITSHIFT_EQUAL  // Triple right bitwise shift equal `>>>=`
	PERCENT_EQUAL           // Percent equal `%=`
	COLON_EQUAL             // Colon equal `:=`
	LABEL_ASSIGN_OP_END     // Assignment operators end here

	SHORT_UNQUOTE_BEG      // Short unquote beginning `!{`
	LPAREN                 // Left parenthesis `(`
	RPAREN                 // Right parenthesis `)`
	LBRACE                 // Left brace `{`
	RBRACE                 // Right brace `}`
	LBRACKET               // Left bracket `[`
	QUESTION_LBRACKET      // Safe access `?[`
	RBRACKET               // Right bracket `]`
	SCOPE_RES_OP           // Scope resolution operator `::`
	COLON_COLON_LBRACKET   // Colon, colon, left bracket `::[`
	DOT_COLON              // Dot colon `.:`
	CLOSED_RANGE_OP        // Closed range operator `...`
	OPEN_RANGE_OP          // Open range operator `<.<`
	RIGHT_OPEN_RANGE_OP    // Right open range operator `..<`
	LEFT_OPEN_RANGE_OP     // Left open range operator `<..`
	PIPE_OP                // Pipe operator `|>`
	AND_AND                // Logical and `&&`
	AND_BANG               // Logical expression sequencing operator `&!` with the precedence of `&&`
	OR_OR                  // Logical or `||`
	OR_BANG                // Logical expression sequencing operator `|!` with the precedence of `||`
	NOT_EQUAL              // Not equal `!=`
	LAX_NOT_EQUAL          // Lax not equal operator `!~`
	STRICT_EQUAL           // Strict equal `===`
	STRICT_NOT_EQUAL       // Strict not equal `!==`
	QUESTION_QUESTION      // Nil coalescing operator `??`
	BANG                   // Logical not `!`
	ISA_OP                 // "is a" operator `<:`
	REVERSE_ISA_OP         // Reverse "is a" operator `:>`
	INSTANCE_OF_OP         // Instance of operator `<<:`
	REVERSE_INSTANCE_OF_OP // Reverse instance of operator `:>>`

	// Overridable operators start here
	LABEL_OVERRIDABLE_OP_BEG
	PLUS_PLUS        // Increment operator `++`
	MINUS_MINUS      // Decrement operator `--`
	PLUS_AT          // Negate `+@`
	MINUS_AT         // Negate `-@`
	MINUS            // Minus `-`
	PLUS             // Plus `+`
	STAR             // Star `*`
	SLASH            // Slash `/`
	STAR_STAR        // Two stars `**`
	LESS             // Less than `<`
	LESS_EQUAL       // Less than or equal `<=`
	GREATER          // Greater than `>`
	GREATER_EQUAL    // Greater than or equal `>=`
	SPACESHIP_OP     // Spaceship operator `<=>`
	EQUAL_EQUAL      // Equal (comparison) `==`
	LAX_EQUAL        // Lax equality operator `=~`
	TILDE            // Tilde `~`
	AND              // Bitwise and `&`
	AND_TILDE        // Bitwise and not `&~`
	OR               // Bitwise or `|`
	XOR              // Bitwise xor `^`
	LBITSHIFT        // Left bitwise shift `<<`
	LTRIPLE_BITSHIFT // Triple left bitwise shift `<<<`
	RBITSHIFT        // Right bitwise shift `>>`
	RTRIPLE_BITSHIFT // Triple right bitwise shift `>>>`
	PERCENT          // Percent `%`
	LABEL_OP_END     // Operators end here

	// Identifiers start here
	LABEL_IDENTIFIER_BEG
	DOLLAR_IDENTIFIER    // Dollar Identifier eg. `$foo`
	PUBLIC_IDENTIFIER    // Identifier eg. `foo`
	PRIVATE_IDENTIFIER   // Identifier with a initial underscore eg. `_foo`
	PUBLIC_CONSTANT      // Constant (identifier with an initial capital letter) eg. `Foo`
	PRIVATE_CONSTANT     // Constant with an initial underscore eg. `_Foo`
	LABEL_IDENTIFIER_END // Identifiers end here

	INSTANCE_VARIABLE // Instance variable token eg. `@foo`

	// Literals start here
	LABEL_LITERAL_BEG

	// Special collection literals start here
	LABEL_SPECIAL_COLLECTION_LITERAL_BEG

	WORD_ARRAY_LIST_BEG   // Word array literal beginning `\w[`
	WORD_ARRAY_LIST_END   // Word array literal end `]`
	SYMBOL_ARRAY_LIST_BEG // Symbol array literal beginning `\s[`
	SYMBOL_ARRAY_LIST_END // Symbol array literal end `]`
	HEX_ARRAY_LIST_BEG    // Hexadecimal integer array literal beginning `\x[`
	HEX_ARRAY_LIST_END    // Hexadecimal integer array literal end `]`
	BIN_ARRAY_LIST_BEG    // Binary integer array literal beginning `\b[`
	BIN_ARRAY_LIST_END    // Binary integer array literal end `]`

	WORD_HASH_SET_BEG   // Word set literal beginning `^w[`
	WORD_HASH_SET_END   // Word set literal end `]`
	SYMBOL_HASH_SET_BEG // Symbol set literal beginning `^s[`
	SYMBOL_HASH_SET_END // Symbol set literal end `]`
	HEX_HASH_SET_BEG    // Hexadecimal integer set literal beginning `^x[`
	HEX_HASH_SET_END    // Hexadecimal integer set literal end `]`
	BIN_HASH_SET_BEG    // Binary integer set literal beginning `^b[`
	BIN_HASH_SET_END    // Binary integer set literal end `]`

	WORD_ARRAY_TUPLE_BEG   // Word arrayTuple literal beginning `%w[`
	WORD_ARRAY_TUPLE_END   // Word arrayTuple literal end `]`
	SYMBOL_ARRAY_TUPLE_BEG // Symbol arrayTuple literal beginning `%s[`
	SYMBOL_ARRAY_TUPLE_END // Symbol arrayTuple literal end `]`
	HEX_ARRAY_TUPLE_BEG    // Hexadecimal integer arrayTuple literal beginning `%x[`
	HEX_ARRAY_TUPLE_END    // Hexadecimal integer arrayTuple literal end `]`
	BIN_ARRAY_TUPLE_BEG    // Binary integer arrayTuple literal beginning `%b[`
	BIN_ARRAY_TUPLE_END    // Binary integer arrayTuple literal end `]`

	LABEL_SPECIAL_COLLECTION_LITERAL_END // Special collection literals end here

	CLOSURE_TYPE_BEG     // Beginning of a closure type `%|`
	HASH_SET_LITERAL_BEG // HashHashSet literal beginning `^[`
	TUPLE_LITERAL_BEG    // ArrayTuple literal beginning `%[`
	RECORD_LITERAL_BEG   // Record literal beginning `%{`
	DOC_COMMENT          // Documentation comment `##[` ... `]##`
	RAW_STRING           // Raw String literal delimited by single quotes `'` ... `'`
	CHAR_LITERAL         // Character literal delimited by backticks eg. `f`
	RAW_CHAR_LITERAL     // Raw Character literal delimited by r` eg. r`f`
	REGEX_BEG            // Beginning delimiter of Regex literals `%/`
	REGEX_CONTENT        // Regex literal content
	REGEX_INTERP_BEG     // Beginning of regex interpolation `${`
	REGEX_INTERP_END     // End of regex interpolation `}`
	REGEX_END            // Ending delimiter of Regex literals `/`

	// Regex flags start here
	LABEL_REGEX_FLAG_BEG

	REGEX_FLAG_i // Regex flag i
	REGEX_FLAG_m // Regex flag m
	REGEX_FLAG_U // Regex flag U
	REGEX_FLAG_a // Regex flag a
	REGEX_FLAG_x // Regex flag x
	REGEX_FLAG_s // Regex flag s

	LABEL_REGEX_FLAG_END // Regex flags end here

	STRING_BEG                     // Beginning delimiter of String literals `"`
	STRING_CONTENT                 // String literal content
	STRING_INTERP_LOCAL            // A local embedded in string interpolation eg. `$foo`
	STRING_INTERP_CONSTANT         // A constant embedded in string interpolation eg. `$Foo`
	STRING_INTERP_BEG              // Beginning of string interpolation `${`
	STRING_INSPECT_INTERP_BEG      // Beginning of string inspect interpolation `#{`
	STRING_INSPECT_INTERP_CONSTANT // A constant embedded in inspect string interpolation eg. `#Foo`
	STRING_INSPECT_INTERP_LOCAL    // A local embedded in inspect string interpolation eg. `#foo`
	STRING_INTERP_END              // End of string interpolation `}`
	STRING_END                     // Ending delimiter of String literals `"`

	// Int literals start here
	LABEL_INT_LITERAL_BEG
	INT                   // Int literal eg. `23`
	UINT                  // UInt literal eg. `23u`
	INT64                 // Int64 literal eg. `23i64`
	UINT64                // UInt64 literal eg. `23u64`
	INT32                 // Int32 literal eg. `23i32`
	UINT32                // UInt32 literal eg. `23u32`
	INT16                 // Int16 literal eg. `23i16`
	UINT16                // UInt16 literal eg. `23u16`
	INT8                  // Int8 literal eg. `23i8`
	UINT8                 // UInt8 literal eg. `23u8`
	LABEL_INT_LITERAL_END // Int literals end here

	// Float literals start here
	LABEL_FLOAT_LITERAL_BEG
	FLOAT                   // Float literal eg. `2.5`
	BIG_FLOAT               // BigFloat literal eg. `2.5bf`
	FLOAT64                 // Float64 literal eg. `2.5f64`
	FLOAT32                 // Float32 literal eg. `2.5f32`
	LABEL_FLOAT_LITERAL_END // Float literals end here

	LABEL_LITERAL_END // Literals end here

	// Keywords start here
	LABEL_KEYWORD_BEG
	NIL               // Keyword `nil`
	FALSE             // Keyword `false`
	TRUE              // Keyword `true`
	IF                // Keyword `if`
	ELSE              // Keyword `else`
	ELSIF             // Keyword `elsif`
	UNLESS            // Keyword `unless`
	WHILE             // Keyword `while`
	UNTIL             // Keyword `until`
	LOOP              // Keyword `loop`
	FOR               // Keyword `for`
	FORNUM            // Keyword `fornum`
	IN                // Keyword `in`
	OF                // Keyword `of`
	BREAK             // Keyword `break`
	CONTINUE          // Keyword `continue`
	RETURN            // Keyword `return`
	YIELD             // Keyword `yield`
	ASYNC             // Keyword `async`
	AWAIT             // Keyword `await`
	AWAIT_SYNC        // Keyword `await_sync`
	GO                // Keyword `go`
	DEF               // Keyword `def`
	SIG               // Keyword `sig`
	END               // Keyword `end`
	THEN              // Keyword `then`
	INIT              // Keyword `init`
	NOINIT            // Keyword `noinit`
	CLASS             // Keyword `class`
	STRUCT            // Keyword `struct`
	MODULE            // Keyword `module`
	MIXIN             // Keyword `mixin`
	INTERFACE         // Keyword `interface`
	INCLUDE           // Keyword `include`
	IMPLEMENT         // Keyword `implement`
	EXTEND            // Keyword `extend`
	ENUM              // Keyword `enum`
	TYPE              // Keyword `type`
	TYPEDEF           // Keyword `typedef`
	TYPEOF            // Keyword `typeof`
	VAR               // Keyword `var`
	VAL               // Keyword `val`
	CONST             // Keyword `const`
	THROW             // Keyword `throw`
	TRY               // Keyword `try`
	CATCH             // Keyword `catch`
	DO                // Keyword `do`
	FINALLY           // Keyword `finally`
	DEFER             // Keyword `defer`
	ALIAS             // Keyword `alias`
	AS                // Keyword `as`
	IS                // Keyword `is`
	SELF              // Keyword `self`
	SUPER             // Keyword `super`
	SWITCH            // Keyword `switch`
	CASE              // Keyword `case`
	MATCH             // Keyword `match`
	WITH              // Keyword `with`
	USING             // Keyword `using`
	BREAKPOINT        // Keyword `breakpoint`
	GETTER            // Keyword `getter`
	SETTER            // Keyword `setter`
	ATTR              // Keyword `attr`
	MUST              // Keyword `must`
	SINGLETON         // Keyword `singleton`
	ABSTRACT          // Keyword `abstract`
	SEALED            // Keyword `sealed`
	VOID              // Keyword `void`
	NEVER             // Keyword `never`
	NOTHING           // Keyword `nothing`
	ANY               // Keyword `any`
	PRIMITIVE         // Keyword `primitive`
	PUBLIC            // Keyword `public`
	PRIVATE           // Keyword `private`
	PROTECTED         // Keyword `protected`
	NATIVE            // Keyword `native`
	DEFAULT           // Keyword `default`
	MACRO             // Keyword `macro`
	BOOL              // Keyword `bool`
	NEW               // Keyword `new`
	EXTERN            // Keyword `extern`
	IMPORT            // Keyword `import`
	EXPORT            // Keyword `export`
	WHERE             // Keyword `where`
	UNTYPED           // Keyword `untyped`
	UNCHECKED         // Keyword `unchecked`
	GOTO              // Keyword `goto`
	QUOTE             // Keyword `quote`
	QUOTE_EXPR        // Keyword `quote_expr`
	QUOTE_TYPE        // Keyword `quote_type`
	QUOTE_PATTERN     // Keyword `quote_pattern`
	UNQUOTE           // Keyword `unquote`
	UNQUOTE_EXPR      // Keyword `unquote_expr`
	UNQUOTE_TYPE      // Keyword `unquote_type`
	UNQUOTE_IDENT     // Keyword `unquote_ident`
	UNQUOTE_CONST     // Keyword `unquote_const`
	UNQUOTE_IVAR      // Keyword `unquote_ivar`
	UNQUOTE_PATTERN   // Keyword `unquote_pattern`
	UNDEFINED         // Keyword `undefined`
	FUNC              // Keyword `func`
	OVERLOAD          // Keyword `overload`
	UNSAFE            // Keyword `unsafe`
	PATTERN           // Keyword `pattern`
	LABEL_KEYWORD_END // Keywords end here
)

func (Type) IsAssignmentOperator

func (t Type) IsAssignmentOperator() bool

Check whether the token is an assignment operator.

func (Type) IsCollectionLiteralBeg

func (t Type) IsCollectionLiteralBeg() bool

func (Type) IsComparisonOperator

func (t Type) IsComparisonOperator() bool

Check whether the token is a comparison operator.

func (Type) IsEndOfFile

func (t Type) IsEndOfFile() bool

Check whether the token marks the end of the file.

func (Type) IsEqualityOperator

func (t Type) IsEqualityOperator() bool

Check whether the token is an equality operator.

func (Type) IsFloatLiteral

func (t Type) IsFloatLiteral() bool

Check whether the token is a float literal.

func (Type) IsIdentifier

func (t Type) IsIdentifier() bool

Check whether the token is an identifier.

func (Type) IsIntLiteral

func (t Type) IsIntLiteral() bool

Check whether the token is an int literal.

func (Type) IsKeyword

func (t Type) IsKeyword() bool

Check whether the token is a keyword.

func (Type) IsLiteral

func (t Type) IsLiteral() bool

Check whether the token is a literal.

func (Type) IsNonOverridableOperator

func (t Type) IsNonOverridableOperator() bool

Check whether the token is a non overridable operator.

func (Type) IsOperator

func (t Type) IsOperator() bool

Check whether the token is a an operator.

func (Type) IsOverridableOperator

func (t Type) IsOverridableOperator() bool

Check whether the token is an overridable operator. Overridable operators can be used as method names.

func (Type) IsRegexFlag

func (t Type) IsRegexFlag() bool

Check whether the token represents a regex flag

func (Type) IsSpecialCollectionLiteral

func (t Type) IsSpecialCollectionLiteral() bool

Check whether the token represents a special collection literal like `%w[]`

func (Type) IsSpecialCollectionLiteralBeg

func (t Type) IsSpecialCollectionLiteralBeg() bool

return `true` if the token is the beginning of a special collection literal

func (Type) IsStatementSeparator

func (t Type) IsStatementSeparator() bool

Check whether the token can separate statements.

func (Type) IsValidAsArgumentToNoParenFunctionCall

func (t Type) IsValidAsArgumentToNoParenFunctionCall() bool

Returns `true` if the token can be a beginning of an argument to a function call without parentheses eg. `foo 2`

func (Type) IsValidAsEndInRangeLiteral

func (t Type) IsValidAsEndInRangeLiteral() bool

Returns `true` if the token can be an end of a range value eg. `...2`

func (Type) IsValidAsEndInRangePattern

func (t Type) IsValidAsEndInRangePattern() bool

Returns `true` if the token can be an end of a range pattern eg. `...2`

func (Type) IsValidFunctionName

func (t Type) IsValidFunctionName() bool

Check whether the token is a valid function name.

func (Type) IsValidMacroName

func (t Type) IsValidMacroName() bool

Check whether the token is a valid macro name.

func (Type) IsValidMethodName

func (t Type) IsValidMethodName() bool

Check whether the token is a valid method name (including operators).

func (Type) IsValidPublicMethodName

func (t Type) IsValidPublicMethodName() bool

Check whether the token is a valid method name in method call expressions.

func (Type) IsValidRegularMethodName

func (t Type) IsValidRegularMethodName() bool

Check whether the token is a valid method name (without operators).

func (Type) IsValidSimpleSymbolContent

func (t Type) IsValidSimpleSymbolContent() bool

Check whether the token is a valid simple symbol content.

func (Type) Name

func (t Type) Name() string

Name of the token.

func (Type) String

func (i Type) String() string

func (Type) TypeName

func (t Type) TypeName() string

Jump to

Keyboard shortcuts

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