cuelang.org/go@v0.10.1/cue/token/token.go (about)

     1  // Copyright 2018 The CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package token defines constants representing the lexical tokens of the Go
    16  // programming language and basic operations on tokens (printing, predicates).
    17  package token
    18  
    19  // Token is the set of lexical tokens of the CUE configuration language.
    20  type Token int
    21  
    22  //go:generate go run golang.org/x/tools/cmd/stringer -type=Token -linecomment
    23  
    24  // The list of tokens.
    25  const (
    26  	// Special tokens
    27  	ILLEGAL Token = iota
    28  	EOF
    29  	COMMENT
    30  	// e.g. @foo(bar,baz=4)
    31  	ATTRIBUTE
    32  
    33  	// Identifiers and basic type literals
    34  	// (these tokens stand for classes of literals)
    35  	literalBeg
    36  	// e.g. main, _tmp
    37  	IDENT
    38  	// e.g. 12_345Mi, 0700, 0xdeadbeef, 1.2M
    39  	INT
    40  	// e.g. 123.45
    41  	FLOAT
    42  	// e.g. 3m4s; TODO
    43  	// DURATION
    44  	// e.g. "abc"
    45  	STRING
    46  	// a part of a template string, e.g. `"age: \(`
    47  	INTERPOLATION
    48  	BOTTOM // _|_
    49  
    50  	literalEnd
    51  
    52  	// Operators and delimiters
    53  	operatorBeg
    54  	ADD // +
    55  	SUB // -
    56  	MUL // *
    57  	POW // ^
    58  	QUO // /
    59  
    60  	IQUO // quo
    61  	IREM // rem
    62  	IDIV // div
    63  	IMOD // mod
    64  
    65  	AND // &
    66  	OR  // |
    67  
    68  	LAND // &&
    69  	LOR  // ||
    70  
    71  	BIND  // =
    72  	EQL   // ==
    73  	LSS   // <
    74  	GTR   // >
    75  	NOT   // !
    76  	ARROW // <-
    77  
    78  	NEQ // !=
    79  	LEQ // <=
    80  	GEQ // >=
    81  
    82  	MAT  // =~
    83  	NMAT // !~
    84  
    85  	LPAREN   // (
    86  	LBRACK   // [
    87  	LBRACE   // {
    88  	COMMA    // ,
    89  	PERIOD   // .
    90  	ELLIPSIS // ...
    91  
    92  	RPAREN    // )
    93  	RBRACK    // ]
    94  	RBRACE    // }
    95  	SEMICOLON // ;
    96  	COLON     // :
    97  	OPTION    // ?
    98  	operatorEnd
    99  
   100  	keywordBeg
   101  
   102  	IF  // if
   103  	FOR // for
   104  	IN  // in
   105  	LET // let
   106  	// experimental
   107  	FUNC // func
   108  
   109  	TRUE  // true
   110  	FALSE // false
   111  	NULL  // null
   112  
   113  	keywordEnd
   114  )
   115  
   116  // A set of constants for precedence-based expression parsing.
   117  // Non-operators have lowest precedence, followed by operators
   118  // starting with precedence 1 up to unary operators. The highest
   119  // precedence serves as "catch-all" precedence for selector,
   120  // indexing, and other operator and delimiter tokens.
   121  const (
   122  	LowestPrec  = lowestPrec
   123  	UnaryPrec   = unaryPrec
   124  	HighestPrec = highestPrec
   125  )
   126  
   127  const (
   128  	lowestPrec  = 0 // non-operators
   129  	unaryPrec   = 8
   130  	highestPrec = 9
   131  )
   132  
   133  // Precedence returns the operator precedence of the binary
   134  // operator op. If op is not a binary operator, the result
   135  // is LowestPrecedence.
   136  func (tok Token) Precedence() int {
   137  	switch tok {
   138  	case OR:
   139  		return 1
   140  	case AND:
   141  		return 2
   142  	case LOR:
   143  		return 3
   144  	case LAND:
   145  		return 4
   146  	case EQL, NEQ, LSS, LEQ, GTR, GEQ, MAT, NMAT:
   147  		return 5
   148  	case ADD, SUB:
   149  		return 6
   150  	case MUL, QUO, IDIV, IMOD, IQUO, IREM:
   151  		return 7
   152  	}
   153  	return lowestPrec
   154  }
   155  
   156  var keywords map[string]Token
   157  
   158  func init() {
   159  	keywords = make(map[string]Token)
   160  	for tok := keywordBeg + 1; tok < keywordEnd; tok++ {
   161  		keywords[tok.String()] = tok
   162  	}
   163  }
   164  
   165  // Lookup maps an identifier to its keyword token or IDENT (if not a keyword).
   166  func Lookup(ident string) Token {
   167  	if tok, isKeyword := keywords[ident]; isKeyword {
   168  		return tok
   169  	}
   170  	return IDENT
   171  }
   172  
   173  // Predicates
   174  
   175  // IsLiteral returns true for tokens corresponding to identifiers
   176  // and basic type literals; it returns false otherwise.
   177  func (tok Token) IsLiteral() bool { return literalBeg < tok && tok < literalEnd }
   178  
   179  // IsOperator returns true for tokens corresponding to operators and
   180  // delimiters; it returns false otherwise.
   181  func (tok Token) IsOperator() bool { return operatorBeg < tok && tok < operatorEnd }
   182  
   183  // IsKeyword returns true for tokens corresponding to keywords;
   184  // it returns false otherwise.
   185  func (tok Token) IsKeyword() bool { return keywordBeg < tok && tok < keywordEnd }