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

202 lines
4.8 KiB
Go
Raw Normal View History

2022-08-07 19:18:30 +00:00
package lexer
2022-08-10 19:02:08 +00:00
import "fmt"
import "git.tebibyte.media/sashakoshka/arf/file"
import "git.tebibyte.media/sashakoshka/arf/infoerr"
2022-08-07 19:18:30 +00:00
// TokenKind is an enum represzenting what role a token has.
type TokenKind int
const (
TokenKindNewline TokenKind = iota
TokenKindIndent
TokenKindSeparator
TokenKindPermission
2022-08-10 04:48:18 +00:00
TokenKindReturnDirection
2022-08-07 19:18:30 +00:00
TokenKindInt
2022-08-10 15:43:21 +00:00
TokenKindUInt
2022-08-07 19:18:30 +00:00
TokenKindFloat
TokenKindString
TokenKindRune
TokenKindName
TokenKindColon
TokenKindDot
2022-08-17 00:24:27 +00:00
TokenKindElipsis
2022-08-15 18:50:09 +00:00
TokenKindComma
2022-08-07 19:18:30 +00:00
TokenKindLBracket
TokenKindRBracket
TokenKindLBrace
TokenKindRBrace
2022-08-10 04:48:18 +00:00
TokenKindPlus
TokenKindMinus
2022-08-10 15:43:21 +00:00
TokenKindIncrement
TokenKindDecrement
2022-08-10 04:48:18 +00:00
TokenKindAsterisk
TokenKindSlash
TokenKindAt
TokenKindExclamation
TokenKindPercent
TokenKindTilde
// TODO: add equal to, less than or equal to, greater than or equal to,
// not equal to
TokenKindEqualTo
TokenKindNotEqualTo
TokenKindLessThanEqualTo
2022-08-10 04:48:18 +00:00
TokenKindLessThan
TokenKindLShift
TokenKindGreaterThan
TokenKindGreaterThanEqualTo
2022-08-10 04:48:18 +00:00
TokenKindRShift
TokenKindBinaryOr
TokenKindLogicalOr
TokenKindBinaryAnd
TokenKindLogicalAnd
2022-08-07 19:18:30 +00: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 19:18:30 +00: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 15:28:29 +00: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 15:28:29 +00:00
}
2022-08-07 19:18:30 +00:00
// Location returns the location of the token in its file.
func (token Token) Location () (location file.Location) {
return token.location
}
2022-08-10 19:02:08 +00:00
// NewError creates a new error at this token's location.
func (token Token) NewError (
message string,
kind infoerr.ErrorKind,
) (
err infoerr.Error,
) {
return infoerr.NewError(token.location, message, kind)
}
2022-08-10 19:02:08 +00:00
// Describe generates a textual description of the token to be used in debug
// logs.
func (token Token) Describe () (description string) {
2022-08-12 21:30:32 +00: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 19:02:08 +00:00
case TokenKindNewline:
2022-08-12 21:30:32 +00:00
description = "Newline"
2022-08-10 19:02:08 +00:00
case TokenKindIndent:
2022-08-12 21:30:32 +00:00
description = "Indent"
2022-08-10 19:02:08 +00:00
case TokenKindSeparator:
2022-08-12 21:30:32 +00:00
description = "Separator"
2022-08-10 19:02:08 +00:00
case TokenKindPermission:
2022-08-12 21:30:32 +00:00
description = "Permission"
2022-08-10 19:02:08 +00:00
case TokenKindReturnDirection:
2022-08-12 21:30:32 +00:00
description = "ReturnDirection"
2022-08-10 19:02:08 +00:00
case TokenKindInt:
2022-08-12 21:30:32 +00:00
description = "Int"
2022-08-10 19:02:08 +00:00
case TokenKindUInt:
2022-08-12 21:30:32 +00:00
description = "UInt"
2022-08-10 19:02:08 +00:00
case TokenKindFloat:
2022-08-12 21:30:32 +00:00
description = "Float"
2022-08-10 19:02:08 +00:00
case TokenKindString:
2022-08-12 21:30:32 +00:00
description = "String"
2022-08-10 19:02:08 +00:00
case TokenKindRune:
2022-08-12 21:30:32 +00:00
description = "Rune"
2022-08-10 19:02:08 +00:00
case TokenKindName:
2022-08-12 21:30:32 +00:00
description = "Name"
2022-08-10 19:02:08 +00:00
case TokenKindColon:
2022-08-12 21:30:32 +00:00
description = "Colon"
2022-08-10 19:02:08 +00:00
case TokenKindDot:
2022-08-12 21:30:32 +00:00
description = "Dot"
2022-08-17 00:24:27 +00:00
case TokenKindElipsis:
description = "Elipsis"
2022-08-15 18:50:09 +00:00
case TokenKindComma:
description = "Comma"
2022-08-10 19:02:08 +00:00
case TokenKindLBracket:
2022-08-12 21:30:32 +00:00
description = "LBracket"
2022-08-10 19:02:08 +00:00
case TokenKindRBracket:
2022-08-12 21:30:32 +00:00
description = "RBracket"
2022-08-10 19:02:08 +00:00
case TokenKindLBrace:
2022-08-12 21:30:32 +00:00
description = "LBrace"
2022-08-10 19:02:08 +00:00
case TokenKindRBrace:
2022-08-12 21:30:32 +00:00
description = "RBrace"
2022-08-10 19:02:08 +00:00
case TokenKindPlus:
2022-08-12 21:30:32 +00:00
description = "Plus"
2022-08-10 19:02:08 +00:00
case TokenKindMinus:
2022-08-12 21:30:32 +00:00
description = "Minus"
2022-08-10 19:02:08 +00:00
case TokenKindIncrement:
2022-08-12 21:30:32 +00:00
description = "Increment"
2022-08-10 19:02:08 +00:00
case TokenKindDecrement:
2022-08-12 21:30:32 +00:00
description = "Decrement"
2022-08-10 19:02:08 +00:00
case TokenKindAsterisk:
2022-08-12 21:30:32 +00:00
description = "Asterisk"
2022-08-10 19:02:08 +00:00
case TokenKindSlash:
2022-08-12 21:30:32 +00:00
description = "Slash"
2022-08-10 19:02:08 +00:00
case TokenKindAt:
2022-08-12 21:30:32 +00:00
description = "At"
2022-08-10 19:02:08 +00:00
case TokenKindExclamation:
2022-08-12 21:30:32 +00:00
description = "Exclamation"
2022-08-10 19:02:08 +00:00
case TokenKindPercent:
2022-08-12 21:30:32 +00:00
description = "Percent"
2022-08-10 19:02:08 +00:00
case TokenKindTilde:
2022-08-12 21:30:32 +00:00
description = "Tilde"
2022-08-10 19:02:08 +00:00
case TokenKindLessThan:
2022-08-12 21:30:32 +00:00
description = "LessThan"
2022-08-10 19:02:08 +00:00
case TokenKindLShift:
2022-08-12 21:30:32 +00:00
description = "LShift"
2022-08-10 19:02:08 +00:00
case TokenKindGreaterThan:
2022-08-12 21:30:32 +00:00
description = "GreaterThan"
2022-08-10 19:02:08 +00:00
case TokenKindRShift:
2022-08-12 21:30:32 +00:00
description = "RShift"
2022-08-10 19:02:08 +00:00
case TokenKindBinaryOr:
2022-08-12 21:30:32 +00:00
description = "BinaryOr"
2022-08-10 19:02:08 +00:00
case TokenKindLogicalOr:
2022-08-12 21:30:32 +00:00
description = "LogicalOr"
2022-08-10 19:02:08 +00:00
case TokenKindBinaryAnd:
2022-08-12 21:30:32 +00:00
description = "BinaryAnd"
2022-08-10 19:02:08 +00:00
case TokenKindLogicalAnd:
2022-08-12 21:30:32 +00:00
description = "LogicalAnd"
2022-08-10 19:02:08 +00:00
}
return
}