tokens.go raw
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package syntax
6
7 type Token uint32
8
9 //go:generate stringer -type token -linecomment tokens.go
10
11 const (
12 _ Token = iota
13 EOF // EOF
14
15 // names and literals
16 NameType // name
17 Literal // literal
18
19 // operators and operations
20 // Operator is excluding '*' (Star)
21 OperatorType // op
22 AssignOp // op=
23 IncOp // opop
24 Assign // =
25 Define // :=
26 Arrow // <-
27 Star // *
28
29 // delimiters
30 Lparen // (
31 Lbrack // [
32 Lbrace // {
33 Rparen // )
34 Rbrack // ]
35 Rbrace // }
36 Comma // ,
37 Semi // ;
38 Colon // :
39 Dot // .
40 DotDotDot // ...
41
42 // keywords
43 Break // break
44 Case // case
45 Chan // chan
46 Const // const
47 Continue // continue
48 Default // default
49 Defer // defer
50 Else // else
51 Fallthrough // fallthrough
52 For // for
53 Func // func
54 Go // go
55 Goto // goto
56 If // if
57 Import // import
58 Interface // interface
59 Map // map
60 Package // package
61 Range // range
62 Return // return
63 Select // select
64 Struct // struct
65 Switch // switch
66 TypeType // type
67 Var // var
68
69 // empty line comment to exclude it from .String
70 tokenCount //
71 )
72
73 // Make sure we have at most 64 tokens so we can use them in a set.
74 const _ uint64 = 1 << (tokenCount - 1)
75
76 // contains reports whether tok is in tokset.
77 func contains(tokset uint64, tok Token) bool {
78 return tokset&(1<<tok) != 0
79 }
80
81 type LitKind uint8
82
83 // TODO(gri) With the 'i' (imaginary) suffix now permitted on integer
84 // and floating-point numbers, having a single ImagLit does
85 // not represent the literal kind well anymore. Remove it?
86 const (
87 IntLit LitKind = iota
88 FloatLit
89 ImagLit
90 RuneLit
91 StringLit
92 )
93
94 type Operator uint32
95
96 const (
97 _ Operator = iota
98
99 // Def is the : in :=
100 Def // :
101 Not // !
102 Recv // <-
103 Tilde // ~
104
105 // precOrOr
106 OrOr // ||
107
108 // precAndAnd
109 AndAnd // &&
110
111 // precCmp
112 Eql // ==
113 Neq // !=
114 Lss // <
115 Leq // <=
116 Gtr // >
117 Geq // >=
118
119 // precAdd
120 Add // +
121 Sub // -
122 Or // |
123 Xor // ^
124
125 // precMul
126 Mul // *
127 Div // /
128 Rem // %
129 And // &
130 AndNot // &^
131 Shl // <<
132 Shr // >>
133 )
134
135 // OperatorType precedences
136 const (
137 _ = iota
138 PrecOrOr
139 PrecAndAnd
140 PrecCmp
141 PrecAdd
142 PrecMul
143 )
144