package token
import (
"strconv"
"unicode"
"unicode/utf8"
)
type Token int
const (
ILLEGAL Token = iota
EOF
COMMENT
literal_beg
IDENT
INT
FLOAT
IMAG
CHAR
STRING
literal_end
operator_beg
ADD
SUB
MUL
QUO
REM
AND
OR
XOR
SHL
SHR
AND_NOT
ADD_ASSIGN
SUB_ASSIGN
MUL_ASSIGN
QUO_ASSIGN
REM_ASSIGN
AND_ASSIGN
OR_ASSIGN
XOR_ASSIGN
SHL_ASSIGN
SHR_ASSIGN
AND_NOT_ASSIGN
LAND
LOR
ARROW
INC
DEC
EQL
LSS
GTR
ASSIGN
NOT
NEQ
LEQ
GEQ
DEFINE
ELLIPSIS
LPAREN
LBRACK
LBRACE
COMMA
PERIOD
RPAREN
RBRACK
RBRACE
SEMICOLON
COLON
operator_end
keyword_beg
BREAK
CASE
CHAN
CONST
CONTINUE
DEFAULT
DEFER
ELSE
FALLTHROUGH
FOR
FUNC
GO
GOTO
IF
IMPORT
INTERFACE
MAP
PACKAGE
RANGE
RETURN
SELECT
STRUCT
SWITCH
TYPE
VAR
keyword_end
additional_beg
TILDE
additional_end
)
var tokens = [...]string {
ILLEGAL : "ILLEGAL" ,
EOF : "EOF" ,
COMMENT : "COMMENT" ,
IDENT : "IDENT" ,
INT : "INT" ,
FLOAT : "FLOAT" ,
IMAG : "IMAG" ,
CHAR : "CHAR" ,
STRING : "STRING" ,
ADD : "+" ,
SUB : "-" ,
MUL : "*" ,
QUO : "/" ,
REM : "%" ,
AND : "&" ,
OR : "|" ,
XOR : "^" ,
SHL : "<<" ,
SHR : ">>" ,
AND_NOT : "&^" ,
ADD_ASSIGN : "+=" ,
SUB_ASSIGN : "-=" ,
MUL_ASSIGN : "*=" ,
QUO_ASSIGN : "/=" ,
REM_ASSIGN : "%=" ,
AND_ASSIGN : "&=" ,
OR_ASSIGN : "|=" ,
XOR_ASSIGN : "^=" ,
SHL_ASSIGN : "<<=" ,
SHR_ASSIGN : ">>=" ,
AND_NOT_ASSIGN : "&^=" ,
LAND : "&&" ,
LOR : "||" ,
ARROW : "<-" ,
INC : "++" ,
DEC : "--" ,
EQL : "==" ,
LSS : "<" ,
GTR : ">" ,
ASSIGN : "=" ,
NOT : "!" ,
NEQ : "!=" ,
LEQ : "<=" ,
GEQ : ">=" ,
DEFINE : ":=" ,
ELLIPSIS : "..." ,
LPAREN : "(" ,
LBRACK : "[" ,
LBRACE : "{" ,
COMMA : "," ,
PERIOD : "." ,
RPAREN : ")" ,
RBRACK : "]" ,
RBRACE : "}" ,
SEMICOLON : ";" ,
COLON : ":" ,
BREAK : "break" ,
CASE : "case" ,
CHAN : "chan" ,
CONST : "const" ,
CONTINUE : "continue" ,
DEFAULT : "default" ,
DEFER : "defer" ,
ELSE : "else" ,
FALLTHROUGH : "fallthrough" ,
FOR : "for" ,
FUNC : "func" ,
GO : "go" ,
GOTO : "goto" ,
IF : "if" ,
IMPORT : "import" ,
INTERFACE : "interface" ,
MAP : "map" ,
PACKAGE : "package" ,
RANGE : "range" ,
RETURN : "return" ,
SELECT : "select" ,
STRUCT : "struct" ,
SWITCH : "switch" ,
TYPE : "type" ,
VAR : "var" ,
TILDE : "~" ,
}
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
}
const (
LowestPrec = 0
UnaryPrec = 6
HighestPrec = 7
)
func (op Token ) Precedence () int {
switch op {
case LOR :
return 1
case LAND :
return 2
case EQL , NEQ , LSS , LEQ , GTR , GEQ :
return 3
case ADD , SUB , OR , XOR :
return 4
case MUL , QUO , REM , SHL , SHR , AND , AND_NOT :
return 5
}
return LowestPrec
}
var keywords map [string ]Token
func init() {
keywords = make (map [string ]Token , keyword_end -(keyword_beg +1 ))
for i := keyword_beg + 1 ; i < keyword_end ; i ++ {
keywords [tokens [i ]] = i
}
}
func Lookup (ident string ) Token {
if tok , is_keyword := keywords [ident ]; is_keyword {
return tok
}
return IDENT
}
func (tok Token ) IsLiteral () bool { return literal_beg < tok && tok < literal_end }
func (tok Token ) IsOperator () bool {
return (operator_beg < tok && tok < operator_end ) || tok == TILDE
}
func (tok Token ) IsKeyword () bool { return keyword_beg < tok && tok < keyword_end }
func IsExported (name string ) bool {
ch , _ := utf8 .DecodeRuneInString (name )
return unicode .IsUpper (ch )
}
func IsKeyword (name string ) bool {
_ , ok := keywords [name ]
return ok
}
func IsIdentifier (name string ) bool {
if name == "" || IsKeyword (name ) {
return false
}
for i , c := range name {
if !unicode .IsLetter (c ) && c != '_' && (i == 0 || !unicode .IsDigit (c )) {
return false
}
}
return true
}
The pages are generated with Golds v0.7.0-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .