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