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

171 lines
3.9 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 "github.com/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
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 == testToken
}
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
// Describe generates a textual description of the token to be used in debug
// logs.
func (token Token) Describe () (description string) {
switch token.kind {
case TokenKindNewline:
description += "Newline"
case TokenKindIndent:
description += "Indent"
case TokenKindSeparator:
description += "Separator"
case TokenKindPermission:
description += "Permission"
case TokenKindReturnDirection:
description += "ReturnDirection"
case TokenKindInt:
description += "Int"
case TokenKindUInt:
description += "UInt"
case TokenKindFloat:
description += "Float"
case TokenKindString:
description += "String"
case TokenKindRune:
description += "Rune"
case TokenKindName:
description += "Name"
case TokenKindColon:
description += "Colon"
case TokenKindDot:
description += "Dot"
case TokenKindLBracket:
description += "LBracket"
case TokenKindRBracket:
description += "RBracket"
case TokenKindLBrace:
description += "LBrace"
case TokenKindRBrace:
description += "RBrace"
case TokenKindPlus:
description += "Plus"
case TokenKindMinus:
description += "Minus"
case TokenKindIncrement:
description += "Increment"
case TokenKindDecrement:
description += "Decrement"
case TokenKindAsterisk:
description += "Asterisk"
case TokenKindSlash:
description += "Slash"
case TokenKindAt:
description += "At"
case TokenKindExclamation:
description += "Exclamation"
case TokenKindPercent:
description += "Percent"
case TokenKindTilde:
description += "Tilde"
case TokenKindLessThan:
description += "LessThan"
case TokenKindLShift:
description += "LShift"
case TokenKindGreaterThan:
description += "GreaterThan"
case TokenKindRShift:
description += "RShift"
case TokenKindBinaryOr:
description += "BinaryOr"
case TokenKindLogicalOr:
description += "LogicalOr"
case TokenKindBinaryAnd:
description += "BinaryAnd"
case TokenKindLogicalAnd:
description += "LogicalAnd"
}
if token.value != nil {
description += fmt.Sprint(": ", token.value)
}
return
}