summaryrefslogtreecommitdiff
path: root/lang/token.go
blob: 7ad7bf15d6ff4d03bca55fce242d9fd3b7d15b9d (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package lang

//go:generate stringer -type=Token

// Token represents a lexical token.
type Token int

// All known tokens for the set of supported languages.
const (
	Illegal Token = iota
	Comment
	Ident

	// Literal values.
	Char
	Float
	Imag
	Int
	String

	// Binary operators (except indicated).
	// Arithmetic and bitwise binary operators.
	Add    // +
	Sub    // -
	Mul    // *
	Quo    // /
	Rem    // %
	And    // &
	Or     // |
	Xor    // ^
	Shl    // <<
	Shr    // >>
	AndNot // &^
	Period // .

	// Binary operators returning a boolean.
	Equal        // ==
	Greater      // >
	GreaterEqual // >=
	Land         // &&
	Less         // <
	LessEqual    // <=
	Lor          // ||
	NotEqual     // !=

	// Assigment operators (arithmetic and bitwise).
	Define       // :=
	Assign       // =
	AddAssign    // +=
	SubAssign    // -=
	MulAssign    // *=
	QuoAssign    // /=
	RemAssign    // %=
	AndAssign    // &=
	OrAssign     // |=
	XorAssign    // ^=
	ShlAssign    // <<=
	ShrAssign    // >>=
	AndNotAssign // &^=
	Inc          // ++
	Dec          // --

	// Unary operations.
	Plus     // unary +
	Minus    // unary -
	Addr     // unary &
	Deref    // unary *
	BitComp  // unary ^
	Arrow    // unary ->
	Ellipsis // unary ...
	Not      // unary !
	Tilde    // unary ~ (underlying type)

	// Separators (punctuation).
	Comma     // ,
	Semicolon // ;
	Colon     // :

	// Block tokens.
	ParenBlock   // (..)
	BracketBlock // [..]
	BraceBlock   // {..}

	// Reserved keywords.
	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

	// Internal virtual machine tokens (no corresponding keyword).
	Call
	CallX
	EqualSet
	Grow
	Index
	JumpFalse
	JumpSetFalse
	JumpSetTrue
	Label
	New
)

// UnaryOp contains the set of unary operators.
// TODO: define UnaryOp per language.
var UnaryOp = map[Token]Token{
	Add:   Plus,    // +
	And:   Addr,    // &
	Not:   Not,     // !
	Mul:   Deref,   // *
	Sub:   Minus,   // -
	Tilde: Tilde,   // ~
	Xor:   BitComp, // ^
}

// IsKeyword returns true if t is a keyword.
func (t Token) IsKeyword() bool { return t >= Break && t <= Var }

// IsLiteral returns true if t is a literal value.
func (t Token) IsLiteral() bool { return t >= Char && t <= String }

// IsOperator returns true if t is an operator.
func (t Token) IsOperator() bool { return t >= Add && t <= Tilde }

// IsBlock returns true if t is a block kind of token.
func (t Token) IsBlock() bool { return t >= ParenBlock && t <= BraceBlock }

// IsBoolOp returns true if t is boolean operator.
func (t Token) IsBoolOp() bool { return t >= Equal && t <= NotEqual || t == Not }

// IsBinaryOp returns true if t is a binary operator (takes 2 operands).
func (t Token) IsBinaryOp() bool { return t >= Add && t <= NotEqual }

// IsUnaryOp returns true if t is an unary operator (takes 1 operand).
func (t Token) IsUnaryOp() bool { return t >= Plus && t <= Tilde }

// IsLogicalOp returns true if t is a logical operator.
func (t Token) IsLogicalOp() bool { return t == Land || t == Lor }