token

package
v0.0.0-...-9378d0a Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Keywords = map[string]Type{
	"nil":        NIL,
	"false":      FALSE,
	"true":       TRUE,
	"if":         IF,
	"else":       ELSE,
	"elsif":      ELSIF,
	"unless":     UNLESS,
	"while":      WHILE,
	"until":      UNTIL,
	"loop":       LOOP,
	"for":        FOR,
	"fornum":     FORNUM,
	"in":         IN,
	"halt":       HALT,
	"break":      BREAK,
	"continue":   CONTINUE,
	"return":     RETURN,
	"yield":      YIELD,
	"async":      ASYNC,
	"await":      AWAIT,
	"go":         GO,
	"def":        DEF,
	"sig":        SIG,
	"end":        END,
	"then":       THEN,
	"init":       INIT,
	"class":      CLASS,
	"struct":     STRUCT,
	"module":     MODULE,
	"mixin":      MIXIN,
	"interface":  INTERFACE,
	"include":    INCLUDE,
	"extend":     EXTEND,
	"enhance":    ENHANCE,
	"enum":       ENUM,
	"type":       TYPE,
	"typedef":    TYPEDEF,
	"var":        VAR,
	"val":        VAL,
	"const":      CONST,
	"throw":      THROW,
	"try":        TRY,
	"catch":      CATCH,
	"do":         DO,
	"ensure":     ENSURE,
	"finally":    FINALLY,
	"alias":      ALIAS,
	"as":         AS,
	"is":         IS,
	"self":       SELF,
	"super":      SUPER,
	"switch":     SWITCH,
	"case":       CASE,
	"match":      MATCH,
	"with":       WITH,
	"using":      USING,
	"breakpoint": BREAKPOINT,
	"getter":     GETTER,
	"setter":     SETTER,
	"accessor":   ACCESSOR,
	"must":       MUST,
	"singleton":  SINGLETON,
	"abstract":   ABSTRACT,
	"sealed":     SEALED,
}

Maps keywords to their Token Type.

Functions

This section is empty.

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(span *position.Span, tokenType Type) *Token

Creates a new token.

func NewWithValue

func NewWithValue(span *position.Span, 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) Color

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

Text color for go-prompt.

func (*Token) DisplayAttributes

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

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

func (*Token) FirstByteIndex

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

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

func (*Token) InspectValue

func (t *Token) InspectValue() string

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

func (*Token) LastByteIndex

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

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

func (*Token) SetSpan

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

func (*Token) Span

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

func (*Token) String

func (t *Token) String() string

Implements the fmt.Stringer interface.

func (*Token) StringValue

func (t *Token) StringValue() string

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

type Type

type Type uint8

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
	NEWLINE                       // Newline `\n`, `\r\n`
	SEMICOLON                     // SEMICOLON `;`
	LPAREN                        // Left parenthesis `(`
	RPAREN                        // Right parenthesis `)`
	LBRACE                        // Left brace `{`
	RBRACE                        // Right brace `}`
	LBRACKET                      // Left bracket `[`
	QUESTION_LBRACKET             // Safe access `?[`
	RBRACKET                      // Right bracket `]`
	COMMA                         // Comma `,`
	DOT                           // Dot `.`
	QUESTION_DOT                  // Safe method call operator `?.`
	COLON                         // Colon `:`
	QUESTION                      // Question mark `?`

	// Operators start here
	LABEL_OP_BEG

	THIN_ARROW   // Thin arrow `->` (function 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

	SCOPE_RES_OP        // Scope resolution operator `::`
	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 `!==`

	// 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 `^`
	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 `:>>`
	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
	PUBLIC_IDENTIFIER    // Identifier
	PRIVATE_IDENTIFIER   // Identifier with a initial underscore
	PUBLIC_CONSTANT      // Constant (identifier with an initial capital letter)
	PRIVATE_CONSTANT     // Constant with an initial underscore
	LABEL_IDENTIFIER_END // Identifiers end here

	INSTANCE_VARIABLE  // Instance variable token eg. `@foo`
	SPECIAL_IDENTIFIER // Special identifier 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

	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`
	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`
	HALT              // Keyword `halt`
	BREAK             // Keyword `break`
	CONTINUE          // Keyword `continue`
	RETURN            // Keyword `return`
	YIELD             // Keyword `yield`
	ASYNC             // Keyword `async`
	AWAIT             // Keyword `await`
	GO                // Keyword `go`
	DEF               // Keyword `def`
	SIG               // Keyword `sig`
	END               // Keyword `end`
	THEN              // Keyword `then`
	INIT              // Keyword `init`
	CLASS             // Keyword `class`
	STRUCT            // Keyword `struct`
	MODULE            // Keyword `module`
	MIXIN             // Keyword `mixin`
	INTERFACE         // Keyword `interface`
	INCLUDE           // Keyword `include`
	EXTEND            // Keyword `extend`
	ENHANCE           // Keyword `enhance`
	ENUM              // Keyword `enum`
	TYPE              // Keyword `type`
	TYPEDEF           // Keyword `typedef`
	VAR               // Keyword `var`
	VAL               // Keyword `val`
	CONST             // Keyword `const`
	THROW             // Keyword `throw`
	TRY               // Keyword `try`
	CATCH             // Keyword `catch`
	DO                // Keyword `do`
	ENSURE            // Keyword `ensure`
	FINALLY           // Keyword `finally`
	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`
	ACCESSOR          // Keyword `accessor`
	MUST              // Keyword `must`
	SINGLETON         // Keyword `singleton`
	ABSTRACT          // Keyword `abstract`
	SEALED            // Keyword `sealed`
	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) 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) String

func (t Type) String() string

Name of the token.

Jump to

Keyboard shortcuts

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