-
Notifications
You must be signed in to change notification settings - Fork 1
/
token_type.go
125 lines (101 loc) · 1.69 KB
/
token_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cask
import (
"bytes"
"unicode"
)
// TokenType represents a known token type.
type TokenType int
// Different token types that can be recognized.
const (
EOF TokenType = iota // end of input
ILLEGAL // an illegal/unknown character
// Identifier + literals
CONST
GLOBAL
IDENT
INT
STRING
SYMBOL // :symbol
// The % Notation
// %r{}
PNREGEXP // %r
PNSTART // left delimiter ('{' or other)
PNEND // right delimiter ('}' or other)
// Heredoc
HEREDOC
HEREDOCSTART
HEREDOCEND
// Operators
ASSIGN // =
ASTERISK // *
BANG // !
MINUS // -
PLUS // +
SLASH // /
MODULUS // %
EQ // ==
GT // >
LT // <
NOTEQ // !=
// Delimiters
COMMA // ,
NEWLINE // \n
SEMICOLON // ;
COLON // :
DOT // .
LBRACE // {
LBRACKET // [
LPAREN // (
PIPE // |
RBRACE // }
RBRACKET // ]
RPAREN // )
SCOPE // ::
// Other
REGEXP
// Keywords
CLASS
DEF
DO
ELSE
ELSEIF
END
FALSE
IF
MODULE
NIL
RETURN
SELF
THEN
TRUE
YIELD
)
var keywords = map[string]TokenType{
"class": CLASS,
"def": DEF,
"do": DO,
"else": ELSE,
"end": END,
"false": FALSE,
"if": IF,
"elsif": ELSEIF,
"module": MODULE,
"nil": NIL,
"return": RETURN,
"self": SELF,
"then": THEN,
"true": TRUE,
"yield": YIELD,
}
// LookupIdent returns a TokenType keyword if ident is in the keywords map. If
// specified ident starts with an upper character it will return a CONST
// TokenType. Otherwise, it returns IDENT.
func LookupIdent(ident string) TokenType {
if tok, ok := keywords[ident]; ok {
return tok
}
if unicode.IsUpper(bytes.Runes([]byte(ident))[0]) {
return CONST
}
return IDENT
}