github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/cmd/compile/internal/syntax/tokens.go (about) 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 import "fmt" 8 9 type token uint 10 11 const ( 12 _ token = iota 13 _EOF 14 15 // names and literals 16 _Name 17 _Literal 18 19 // operators and operations 20 _Operator // excluding '*' (_Star) 21 _AssignOp 22 _IncOp 23 _Assign 24 _Define 25 _Larrow 26 _Rarrow 27 _Star 28 29 // delimitors 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 44 _Case 45 _Chan 46 _Const 47 _Continue 48 _Default 49 _Defer 50 _Else 51 _Fallthrough 52 _For 53 _Func 54 _Go 55 _Goto 56 _If 57 _Import 58 _Interface 59 _Map 60 _Package 61 _Range 62 _Return 63 _Select 64 _Struct 65 _Switch 66 _Type 67 _Var 68 69 tokenCount 70 ) 71 72 const ( 73 // for AliasDecl 74 Const = _Const 75 Type = _Type 76 Var = _Var 77 Func = _Func 78 79 // for BranchStmt 80 Break = _Break 81 Continue = _Continue 82 Fallthrough = _Fallthrough 83 Goto = _Goto 84 85 // for CallStmt 86 Go = _Go 87 Defer = _Defer 88 ) 89 90 var tokstrings = [...]string{ 91 // source control 92 _EOF: "EOF", 93 94 // names and literals 95 _Name: "name", 96 _Literal: "literal", 97 98 // operators and operations 99 _Operator: "op", 100 _AssignOp: "op=", 101 _IncOp: "opop", 102 _Assign: "=", 103 _Define: ":=", 104 _Larrow: "<-", 105 _Rarrow: "=>", 106 _Star: "*", 107 108 // delimitors 109 _Lparen: "(", 110 _Lbrack: "[", 111 _Lbrace: "{", 112 _Rparen: ")", 113 _Rbrack: "]", 114 _Rbrace: "}", 115 _Comma: ",", 116 _Semi: ";", 117 _Colon: ":", 118 _Dot: ".", 119 _DotDotDot: "...", 120 121 // keywords 122 _Break: "break", 123 _Case: "case", 124 _Chan: "chan", 125 _Const: "const", 126 _Continue: "continue", 127 _Default: "default", 128 _Defer: "defer", 129 _Else: "else", 130 _Fallthrough: "fallthrough", 131 _For: "for", 132 _Func: "func", 133 _Go: "go", 134 _Goto: "goto", 135 _If: "if", 136 _Import: "import", 137 _Interface: "interface", 138 _Map: "map", 139 _Package: "package", 140 _Range: "range", 141 _Return: "return", 142 _Select: "select", 143 _Struct: "struct", 144 _Switch: "switch", 145 _Type: "type", 146 _Var: "var", 147 } 148 149 func (tok token) String() string { 150 var s string 151 if 0 <= tok && int(tok) < len(tokstrings) { 152 s = tokstrings[tok] 153 } 154 if s == "" { 155 s = fmt.Sprintf("<tok-%d>", tok) 156 } 157 return s 158 } 159 160 // Make sure we have at most 64 tokens so we can use them in a set. 161 const _ uint64 = 1 << (tokenCount - 1) 162 163 // contains reports whether tok is in tokset. 164 func contains(tokset uint64, tok token) bool { 165 return tokset&(1<<tok) != 0 166 } 167 168 type LitKind uint 169 170 const ( 171 IntLit LitKind = iota 172 FloatLit 173 ImagLit 174 RuneLit 175 StringLit 176 ) 177 178 type Operator uint 179 180 const ( 181 _ Operator = iota 182 Def // := 183 Not // ! 184 Recv // <- 185 186 // precOrOr 187 OrOr // || 188 189 // precAndAnd 190 AndAnd // && 191 192 // precCmp 193 Eql // == 194 Neq // != 195 Lss // < 196 Leq // <= 197 Gtr // > 198 Geq // >= 199 200 // precAdd 201 Add // + 202 Sub // - 203 Or // | 204 Xor // ^ 205 206 // precMul 207 Mul // * 208 Div // / 209 Rem // % 210 And // & 211 AndNot // &^ 212 Shl // << 213 Shr // >> 214 ) 215 216 var opstrings = [...]string{ 217 // prec == 0 218 Def: ":", // : in := 219 Not: "!", 220 Recv: "<-", 221 222 // precOrOr 223 OrOr: "||", 224 225 // precAndAnd 226 AndAnd: "&&", 227 228 // precCmp 229 Eql: "==", 230 Neq: "!=", 231 Lss: "<", 232 Leq: "<=", 233 Gtr: ">", 234 Geq: ">=", 235 236 // precAdd 237 Add: "+", 238 Sub: "-", 239 Or: "|", 240 Xor: "^", 241 242 // precMul 243 Mul: "*", 244 Div: "/", 245 Rem: "%", 246 And: "&", 247 AndNot: "&^", 248 Shl: "<<", 249 Shr: ">>", 250 } 251 252 func (op Operator) String() string { 253 var s string 254 if 0 <= op && int(op) < len(opstrings) { 255 s = opstrings[op] 256 } 257 if s == "" { 258 s = fmt.Sprintf("<op-%d>", op) 259 } 260 return s 261 } 262 263 // Operator precedences 264 const ( 265 _ = iota 266 precOrOr 267 precAndAnd 268 precCmp 269 precAdd 270 precMul 271 )