xgo/token/token.go

226 lines
4.1 KiB
Go
Raw Normal View History

2019-01-09 10:17:42 +03:00
package token
import "strconv"
2019-12-20 22:40:38 +03:00
var keywords map[string]Token
// Token represents a token.
2019-01-09 10:17:42 +03:00
type Token int
// List of tokens
2019-01-09 10:17:42 +03:00
const (
Illegal Token = iota
EOF
Comment
_literalBeg
Ident
Int
Float
Char
String
2019-01-09 10:17:42 +03:00
_literalEnd
_operatorBeg
Add // +
Sub // -
Mul // *
Quo // /
Rem // %
And // &
Or // |
Xor // ^
Shl // <<
Shr // >>
AndNot // &^
AddAssign // +=
SubAssign // -=
MulAssign // *=
QuoAssign // /=
RemAssign // %=
AndAssign // &=
OrAssign // |=
XorAssign // ^=
ShlAssign // <<=
ShrAssign // >>=
AndNotAssign // &^=
LAnd // &&
LOr // ||
Inc // ++
Dec // --
Equal // ==
Less // <
Greater // >
Assign // =
Not // !
NotEqual // !=
LessEq // <=
GreaterEq // >=
Define // :=
Ellipsis // ...
LParen // (
LBrack // [
LBrace // {
Comma // ,
Period // .
RParen // )
RBrack // ]
RBrace // }
Semicolon // ;
Colon // :
Question // ?
2019-01-09 10:17:42 +03:00
_operatorEnd
_keywordBeg
Break
Continue
Else
For
Func
Error
2019-01-25 06:31:17 +03:00
Immutable
2019-01-09 10:17:42 +03:00
If
Return
2019-02-02 10:27:29 +03:00
Export
2019-01-09 10:17:42 +03:00
True
False
In
Undefined
2019-01-17 12:56:05 +03:00
Import
2019-01-09 10:17:42 +03:00
_keywordEnd
)
var tokens = [...]string{
Illegal: "ILLEGAL",
EOF: "EOF",
Comment: "COMMENT",
Ident: "IDENT",
Int: "INT",
Float: "FLOAT",
Char: "CHAR",
String: "STRING",
2019-01-09 10:17:42 +03:00
Add: "+",
Sub: "-",
Mul: "*",
Quo: "/",
Rem: "%",
And: "&",
Or: "|",
Xor: "^",
Shl: "<<",
Shr: ">>",
AndNot: "&^",
AddAssign: "+=",
SubAssign: "-=",
MulAssign: "*=",
QuoAssign: "/=",
RemAssign: "%=",
AndAssign: "&=",
OrAssign: "|=",
XorAssign: "^=",
ShlAssign: "<<=",
ShrAssign: ">>=",
AndNotAssign: "&^=",
LAnd: "&&",
LOr: "||",
Inc: "++",
Dec: "--",
Equal: "==",
Less: "<",
Greater: ">",
Assign: "=",
Not: "!",
NotEqual: "!=",
LessEq: "<=",
GreaterEq: ">=",
Define: ":=",
Ellipsis: "...",
LParen: "(",
LBrack: "[",
LBrace: "{",
Comma: ",",
Period: ".",
RParen: ")",
RBrack: "]",
RBrace: "}",
Semicolon: ";",
Colon: ":",
Question: "?",
2019-01-09 10:17:42 +03:00
Break: "break",
Continue: "continue",
Else: "else",
For: "for",
Func: "func",
Error: "error",
2019-01-25 06:31:17 +03:00
Immutable: "immutable",
2019-01-09 10:17:42 +03:00
If: "if",
Return: "return",
2019-02-02 10:27:29 +03:00
Export: "export",
2019-01-09 10:17:42 +03:00
True: "true",
False: "false",
In: "in",
Undefined: "undefined",
2019-01-17 12:56:05 +03:00
Import: "import",
2019-01-09 10:17:42 +03:00
}
func (tok Token) String() string {
s := ""
if 0 <= tok && tok < Token(len(tokens)) {
s = tokens[tok]
}
if s == "" {
s = "token(" + strconv.Itoa(int(tok)) + ")"
}
return s
}
// LowestPrec represents lowest operator precedence.
const LowestPrec = 0
2019-01-09 10:17:42 +03:00
// Precedence returns the precedence for the operator token.
2019-01-09 10:17:42 +03:00
func (tok Token) Precedence() int {
switch tok {
case LOr:
return 1
case LAnd:
return 2
case Equal, NotEqual, Less, LessEq, Greater, GreaterEq:
return 3
case Add, Sub, Or, Xor:
return 4
case Mul, Quo, Rem, Shl, Shr, And, AndNot:
return 5
}
return LowestPrec
}
// IsLiteral returns true if the token is a literal.
2019-01-09 10:17:42 +03:00
func (tok Token) IsLiteral() bool {
return _literalBeg < tok && tok < _literalEnd
}
// IsOperator returns true if the token is an operator.
2019-01-09 10:17:42 +03:00
func (tok Token) IsOperator() bool {
return _operatorBeg < tok && tok < _operatorEnd
}
// IsKeyword returns true if the token is a keyword.
2019-01-09 10:17:42 +03:00
func (tok Token) IsKeyword() bool {
return _keywordBeg < tok && tok < _keywordEnd
}
2019-12-20 22:40:38 +03:00
// Lookup returns corresponding keyword if ident is a keyword.
func Lookup(ident string) Token {
if tok, isKeyword := keywords[ident]; isKeyword {
return tok
}
return Ident
}
func init() {
keywords = make(map[string]Token)
for i := _keywordBeg + 1; i < _keywordEnd; i++ {
keywords[tokens[i]] = i
}
}