2022-08-07 13:18:30 -06:00
|
|
|
package lexer
|
|
|
|
|
2022-08-10 13:02:08 -06:00
|
|
|
import "fmt"
|
2022-08-29 23:11:10 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/file"
|
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
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
|
|
|
|
2022-09-13 15:04:43 -06:00
|
|
|
TokenKindLParen
|
|
|
|
TokenKindRParen
|
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
|
2022-08-25 21:17:42 -06:00
|
|
|
TokenKindPercentAssignment
|
2022-08-09 22:48:18 -06:00
|
|
|
TokenKindTilde
|
2022-08-25 21:17:42 -06:00
|
|
|
TokenKindTildeAssignment
|
2022-08-09 22:48:18 -06:00
|
|
|
|
2022-09-03 18:23:27 -06:00
|
|
|
TokenKindAssignment
|
2022-08-24 21:58:21 -06:00
|
|
|
TokenKindEqualTo
|
|
|
|
TokenKindNotEqualTo
|
|
|
|
TokenKindLessThanEqualTo
|
2022-08-09 22:48:18 -06:00
|
|
|
TokenKindLessThan
|
|
|
|
TokenKindLShift
|
2022-08-25 21:17:42 -06:00
|
|
|
TokenKindLShiftAssignment
|
2022-08-09 22:48:18 -06:00
|
|
|
TokenKindGreaterThan
|
2022-08-24 21:58:21 -06:00
|
|
|
TokenKindGreaterThanEqualTo
|
2022-08-09 22:48:18 -06:00
|
|
|
TokenKindRShift
|
2022-08-25 21:17:42 -06:00
|
|
|
TokenKindRShiftAssignment
|
2022-08-09 22:48:18 -06:00
|
|
|
TokenKindBinaryOr
|
2022-08-25 21:17:42 -06:00
|
|
|
TokenKindBinaryOrAssignment
|
2022-08-09 22:48:18 -06:00
|
|
|
TokenKindLogicalOr
|
|
|
|
TokenKindBinaryAnd
|
2022-08-25 21:17:42 -06:00
|
|
|
TokenKindBinaryAndAssignment
|
2022-08-09 22:48:18 -06:00
|
|
|
TokenKindLogicalAnd
|
2022-08-25 21:17:42 -06:00
|
|
|
TokenKindBinaryXor
|
|
|
|
TokenKindBinaryXorAssignment
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:38:57 -06:00
|
|
|
// NewToken provides a way for a new token to be created by other modules for
|
|
|
|
// comparison purposes.
|
|
|
|
func NewToken (kind TokenKind, value any) (token Token) {
|
|
|
|
token.kind = kind
|
|
|
|
token.value = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-07 13:18:30 -06:00
|
|
|
// Kind returns the semantic role of the token.
|
|
|
|
func (token Token) Kind () (kind TokenKind) {
|
|
|
|
return token.kind
|
|
|
|
}
|
|
|
|
|
2022-08-09 23:22:53 -06:00
|
|
|
// 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) {
|
2022-08-12 13:34:07 -06:00
|
|
|
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
|
|
|
|
2022-08-12 12:51:38 -06:00
|
|
|
// NewError creates a new error at this token's location.
|
2022-08-17 22:58:40 -06:00
|
|
|
func (token Token) NewError (
|
|
|
|
message string,
|
|
|
|
kind infoerr.ErrorKind,
|
|
|
|
) (
|
|
|
|
err infoerr.Error,
|
|
|
|
) {
|
|
|
|
return infoerr.NewError(token.location, message, kind)
|
2022-08-12 12:51:38 -06:00
|
|
|
}
|
|
|
|
|
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-09-13 15:04:43 -06:00
|
|
|
case TokenKindLParen:
|
|
|
|
description = "LParen"
|
|
|
|
case TokenKindRParen:
|
|
|
|
description = "RParen"
|
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-25 21:21:00 -06:00
|
|
|
case TokenKindPercentAssignment:
|
|
|
|
description = "PercentAssignment"
|
2022-08-10 13:02:08 -06:00
|
|
|
case TokenKindTilde:
|
2022-08-12 15:30:32 -06:00
|
|
|
description = "Tilde"
|
2022-08-25 21:21:00 -06:00
|
|
|
case TokenKindTildeAssignment:
|
|
|
|
description = "TildeAssignment"
|
2022-09-03 18:23:27 -06:00
|
|
|
case TokenKindAssignment:
|
|
|
|
description = "Assignment"
|
2022-08-24 22:01:28 -06:00
|
|
|
case TokenKindEqualTo:
|
|
|
|
description = "EqualTo"
|
|
|
|
case TokenKindNotEqualTo:
|
|
|
|
description = "NotEqualTo"
|
2022-08-10 13:02:08 -06:00
|
|
|
case TokenKindLessThan:
|
2022-08-12 15:30:32 -06:00
|
|
|
description = "LessThan"
|
2022-08-24 22:01:28 -06:00
|
|
|
case TokenKindLessThanEqualTo:
|
|
|
|
description = "LessThanEqualTo"
|
2022-08-10 13:02:08 -06:00
|
|
|
case TokenKindLShift:
|
2022-08-12 15:30:32 -06:00
|
|
|
description = "LShift"
|
2022-08-25 21:21:00 -06:00
|
|
|
case TokenKindLShiftAssignment:
|
|
|
|
description = "LShiftAssignment"
|
2022-08-10 13:02:08 -06:00
|
|
|
case TokenKindGreaterThan:
|
2022-08-12 15:30:32 -06:00
|
|
|
description = "GreaterThan"
|
2022-08-24 22:01:28 -06:00
|
|
|
case TokenKindGreaterThanEqualTo:
|
|
|
|
description = "GreaterThanEqualTo"
|
2022-08-10 13:02:08 -06:00
|
|
|
case TokenKindRShift:
|
2022-08-12 15:30:32 -06:00
|
|
|
description = "RShift"
|
2022-08-25 21:21:00 -06:00
|
|
|
case TokenKindRShiftAssignment:
|
|
|
|
description = "RShiftAssignment"
|
2022-08-10 13:02:08 -06:00
|
|
|
case TokenKindBinaryOr:
|
2022-08-12 15:30:32 -06:00
|
|
|
description = "BinaryOr"
|
2022-08-25 21:21:00 -06:00
|
|
|
case TokenKindBinaryOrAssignment:
|
|
|
|
description = "BinaryOrAssignment"
|
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-25 21:21:00 -06:00
|
|
|
case TokenKindBinaryAndAssignment:
|
|
|
|
description = "BinaryAndAssignment"
|
2022-08-10 13:02:08 -06:00
|
|
|
case TokenKindLogicalAnd:
|
2022-08-12 15:30:32 -06:00
|
|
|
description = "LogicalAnd"
|
2022-08-25 21:21:00 -06:00
|
|
|
case TokenKindBinaryXor:
|
|
|
|
description = "BinaryXor"
|
|
|
|
case TokenKindBinaryXorAssignment:
|
|
|
|
description = "BinaryXorAssignment"
|
2022-08-10 13:02:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|