This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/lexer/token.go

190 lines
4.5 KiB
Go
Raw Normal View History

2022-08-07 13:18:30 -06:00
package lexer
2022-08-10 13:02:08 -06:00
import "fmt"
import "git.tebibyte.media/sashakoshka/arf/file"
2022-08-07 13:18:30 -06:00
// TokenKind is an enum represzenting what role a token has.
type TokenKind int
const (
TokenKindNewline TokenKind = iota
TokenKindIndent
TokenKindSeparator
TokenKindPermission
2022-08-09 22:48:18 -06:00
TokenKindReturnDirection
2022-08-07 13:18:30 -06:00
TokenKindInt
2022-08-10 09:43:21 -06:00
TokenKindUInt
2022-08-07 13:18:30 -06:00
TokenKindFloat
TokenKindString
TokenKindRune
TokenKindName
TokenKindColon
TokenKindDot
2022-08-16 18:24:27 -06:00
TokenKindElipsis
2022-08-15 12:50:09 -06:00
TokenKindComma
2022-08-07 13:18:30 -06:00
TokenKindLBracket
TokenKindRBracket
TokenKindLBrace
TokenKindRBrace
2022-08-09 22:48:18 -06:00
TokenKindPlus
TokenKindMinus
2022-08-10 09:43:21 -06:00
TokenKindIncrement
TokenKindDecrement
2022-08-09 22:48:18 -06:00
TokenKindAsterisk
TokenKindSlash
TokenKindAt
TokenKindExclamation
TokenKindPercent
TokenKindTilde
TokenKindLessThan
TokenKindLShift
TokenKindGreaterThan
TokenKindRShift
TokenKindBinaryOr
TokenKindLogicalOr
TokenKindBinaryAnd
TokenKindLogicalAnd
2022-08-07 13:18:30 -06:00
)
// Token represents a single token. It holds its location in the file, as well
// as a value and some semantic information defining the token's role.
type Token struct {
kind TokenKind
location file.Location
value any
}
// Kind returns the semantic role of the token.
func (token Token) Kind () (kind TokenKind) {
return token.kind
}
// Is returns whether or not the token is of kind kind.
func (token Token) Is (kind TokenKind) (match bool) {
return token.kind == kind
}
2022-08-07 13:18:30 -06:00
// Value returns the value of the token. Depending on what kind of token it is,
// this value may be nil.
func (token Token) Value () (value any) {
return token.value
}
2022-08-10 09:28:29 -06:00
// Equals returns whether this token is equal to another token
func (token Token) Equals (testToken Token) (match bool) {
return token.value == testToken.value && token.Is(testToken.kind)
2022-08-10 09:28:29 -06:00
}
2022-08-07 13:18:30 -06:00
// Location returns the location of the token in its file.
func (token Token) Location () (location file.Location) {
return token.location
}
2022-08-10 13:02:08 -06:00
// NewError creates a new error at this token's location.
func (token Token) NewError (message string, kind file.ErrorKind) (err file.Error) {
return token.location.NewError(message, kind)
}
2022-08-10 13:02:08 -06:00
// Describe generates a textual description of the token to be used in debug
// logs.
func (token Token) Describe () (description string) {
2022-08-12 15:30:32 -06:00
description = token.kind.Describe()
if token.value != nil {
description += fmt.Sprint(": ", token.value)
}
return
}
// Describe generates a textual description of the token kind to be used in
// debug logs.
func (tokenKind TokenKind) Describe () (description string) {
switch tokenKind {
2022-08-10 13:02:08 -06:00
case TokenKindNewline:
2022-08-12 15:30:32 -06:00
description = "Newline"
2022-08-10 13:02:08 -06:00
case TokenKindIndent:
2022-08-12 15:30:32 -06:00
description = "Indent"
2022-08-10 13:02:08 -06:00
case TokenKindSeparator:
2022-08-12 15:30:32 -06:00
description = "Separator"
2022-08-10 13:02:08 -06:00
case TokenKindPermission:
2022-08-12 15:30:32 -06:00
description = "Permission"
2022-08-10 13:02:08 -06:00
case TokenKindReturnDirection:
2022-08-12 15:30:32 -06:00
description = "ReturnDirection"
2022-08-10 13:02:08 -06:00
case TokenKindInt:
2022-08-12 15:30:32 -06:00
description = "Int"
2022-08-10 13:02:08 -06:00
case TokenKindUInt:
2022-08-12 15:30:32 -06:00
description = "UInt"
2022-08-10 13:02:08 -06:00
case TokenKindFloat:
2022-08-12 15:30:32 -06:00
description = "Float"
2022-08-10 13:02:08 -06:00
case TokenKindString:
2022-08-12 15:30:32 -06:00
description = "String"
2022-08-10 13:02:08 -06:00
case TokenKindRune:
2022-08-12 15:30:32 -06:00
description = "Rune"
2022-08-10 13:02:08 -06:00
case TokenKindName:
2022-08-12 15:30:32 -06:00
description = "Name"
2022-08-10 13:02:08 -06:00
case TokenKindColon:
2022-08-12 15:30:32 -06:00
description = "Colon"
2022-08-10 13:02:08 -06:00
case TokenKindDot:
2022-08-12 15:30:32 -06:00
description = "Dot"
2022-08-16 18:24:27 -06:00
case TokenKindElipsis:
description = "Elipsis"
2022-08-15 12:50:09 -06:00
case TokenKindComma:
description = "Comma"
2022-08-10 13:02:08 -06:00
case TokenKindLBracket:
2022-08-12 15:30:32 -06:00
description = "LBracket"
2022-08-10 13:02:08 -06:00
case TokenKindRBracket:
2022-08-12 15:30:32 -06:00
description = "RBracket"
2022-08-10 13:02:08 -06:00
case TokenKindLBrace:
2022-08-12 15:30:32 -06:00
description = "LBrace"
2022-08-10 13:02:08 -06:00
case TokenKindRBrace:
2022-08-12 15:30:32 -06:00
description = "RBrace"
2022-08-10 13:02:08 -06:00
case TokenKindPlus:
2022-08-12 15:30:32 -06:00
description = "Plus"
2022-08-10 13:02:08 -06:00
case TokenKindMinus:
2022-08-12 15:30:32 -06:00
description = "Minus"
2022-08-10 13:02:08 -06:00
case TokenKindIncrement:
2022-08-12 15:30:32 -06:00
description = "Increment"
2022-08-10 13:02:08 -06:00
case TokenKindDecrement:
2022-08-12 15:30:32 -06:00
description = "Decrement"
2022-08-10 13:02:08 -06:00
case TokenKindAsterisk:
2022-08-12 15:30:32 -06:00
description = "Asterisk"
2022-08-10 13:02:08 -06:00
case TokenKindSlash:
2022-08-12 15:30:32 -06:00
description = "Slash"
2022-08-10 13:02:08 -06:00
case TokenKindAt:
2022-08-12 15:30:32 -06:00
description = "At"
2022-08-10 13:02:08 -06:00
case TokenKindExclamation:
2022-08-12 15:30:32 -06:00
description = "Exclamation"
2022-08-10 13:02:08 -06:00
case TokenKindPercent:
2022-08-12 15:30:32 -06:00
description = "Percent"
2022-08-10 13:02:08 -06:00
case TokenKindTilde:
2022-08-12 15:30:32 -06:00
description = "Tilde"
2022-08-10 13:02:08 -06:00
case TokenKindLessThan:
2022-08-12 15:30:32 -06:00
description = "LessThan"
2022-08-10 13:02:08 -06:00
case TokenKindLShift:
2022-08-12 15:30:32 -06:00
description = "LShift"
2022-08-10 13:02:08 -06:00
case TokenKindGreaterThan:
2022-08-12 15:30:32 -06:00
description = "GreaterThan"
2022-08-10 13:02:08 -06:00
case TokenKindRShift:
2022-08-12 15:30:32 -06:00
description = "RShift"
2022-08-10 13:02:08 -06:00
case TokenKindBinaryOr:
2022-08-12 15:30:32 -06:00
description = "BinaryOr"
2022-08-10 13:02:08 -06:00
case TokenKindLogicalOr:
2022-08-12 15:30:32 -06:00
description = "LogicalOr"
2022-08-10 13:02:08 -06:00
case TokenKindBinaryAnd:
2022-08-12 15:30:32 -06:00
description = "BinaryAnd"
2022-08-10 13:02:08 -06:00
case TokenKindLogicalAnd:
2022-08-12 15:30:32 -06:00
description = "LogicalAnd"
2022-08-10 13:02:08 -06:00
}
return
}