123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- package token
- import "strconv"
- var keywords map[string]Token
- // Token represents a token.
- type Token int
- // List of tokens
- const (
- Illegal Token = iota
- EOF
- Comment
- _literalBeg
- Ident
- Int
- Float
- Char
- String
- _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 // ?
- _operatorEnd
- _keywordBeg
- Break
- Continue
- Else
- For
- Func
- Error
- Immutable
- If
- Return
- Export
- True
- False
- In
- Undefined
- Import
- _keywordEnd
- )
- var tokens = [...]string{
- Illegal: "ILLEGAL",
- EOF: "EOF",
- Comment: "COMMENT",
- Ident: "IDENT",
- Int: "INT",
- Float: "FLOAT",
- Char: "CHAR",
- String: "STRING",
- 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: "?",
- Break: "break",
- Continue: "continue",
- Else: "else",
- For: "for",
- Func: "func",
- Error: "error",
- Immutable: "immutable",
- If: "if",
- Return: "return",
- Export: "export",
- True: "true",
- False: "false",
- In: "in",
- Undefined: "undefined",
- Import: "import",
- }
- 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
- // Precedence returns the precedence for the operator token.
- 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.
- func (tok Token) IsLiteral() bool {
- return _literalBeg < tok && tok < _literalEnd
- }
- // IsOperator returns true if the token is an operator.
- func (tok Token) IsOperator() bool {
- return _operatorBeg < tok && tok < _operatorEnd
- }
- // IsKeyword returns true if the token is a keyword.
- func (tok Token) IsKeyword() bool {
- return _keywordBeg < tok && tok < _keywordEnd
- }
- // 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
- }
- }
|