github.com/rajeev159/opa@v0.45.0/ast/internal/tokens/tokens.go (about)

     1  // Copyright 2020 The OPA Authors.  All rights reserved.
     2  // Use of this source code is governed by an Apache2
     3  // license that can be found in the LICENSE file.
     4  
     5  package tokens
     6  
     7  // Token represents a single Rego source code token
     8  // for use by the Parser.
     9  type Token int
    10  
    11  func (t Token) String() string {
    12  	if t < 0 || int(t) >= len(strings) {
    13  		return "unknown"
    14  	}
    15  	return strings[t]
    16  }
    17  
    18  // All tokens must be defined here
    19  const (
    20  	Illegal Token = iota
    21  	EOF
    22  	Whitespace
    23  	Ident
    24  	Comment
    25  
    26  	Package
    27  	Import
    28  	As
    29  	Default
    30  	Else
    31  	Not
    32  	Some
    33  	With
    34  	Null
    35  	True
    36  	False
    37  
    38  	Number
    39  	String
    40  
    41  	LBrack
    42  	RBrack
    43  	LBrace
    44  	RBrace
    45  	LParen
    46  	RParen
    47  	Comma
    48  	Colon
    49  
    50  	Add
    51  	Sub
    52  	Mul
    53  	Quo
    54  	Rem
    55  	And
    56  	Or
    57  	Unify
    58  	Equal
    59  	Assign
    60  	In
    61  	Neq
    62  	Gt
    63  	Lt
    64  	Gte
    65  	Lte
    66  	Dot
    67  	Semicolon
    68  
    69  	Every
    70  	Contains
    71  	If
    72  )
    73  
    74  var strings = [...]string{
    75  	Illegal:    "illegal",
    76  	EOF:        "eof",
    77  	Whitespace: "whitespace",
    78  	Comment:    "comment",
    79  	Ident:      "ident",
    80  	Package:    "package",
    81  	Import:     "import",
    82  	As:         "as",
    83  	Default:    "default",
    84  	Else:       "else",
    85  	Not:        "not",
    86  	Some:       "some",
    87  	With:       "with",
    88  	Null:       "null",
    89  	True:       "true",
    90  	False:      "false",
    91  	Number:     "number",
    92  	String:     "string",
    93  	LBrack:     "[",
    94  	RBrack:     "]",
    95  	LBrace:     "{",
    96  	RBrace:     "}",
    97  	LParen:     "(",
    98  	RParen:     ")",
    99  	Comma:      ",",
   100  	Colon:      ":",
   101  	Add:        "plus",
   102  	Sub:        "minus",
   103  	Mul:        "mul",
   104  	Quo:        "div",
   105  	Rem:        "rem",
   106  	And:        "and",
   107  	Or:         "or",
   108  	Unify:      "eq",
   109  	Equal:      "equal",
   110  	Assign:     "assign",
   111  	In:         "in",
   112  	Neq:        "neq",
   113  	Gt:         "gt",
   114  	Lt:         "lt",
   115  	Gte:        "gte",
   116  	Lte:        "lte",
   117  	Dot:        ".",
   118  	Semicolon:  ";",
   119  	Every:      "every",
   120  	Contains:   "contains",
   121  	If:         "if",
   122  }
   123  
   124  var keywords = map[string]Token{
   125  	"package": Package,
   126  	"import":  Import,
   127  	"as":      As,
   128  	"default": Default,
   129  	"else":    Else,
   130  	"not":     Not,
   131  	"some":    Some,
   132  	"with":    With,
   133  	"null":    Null,
   134  	"true":    True,
   135  	"false":   False,
   136  }
   137  
   138  // Keywords returns a copy of the default string -> Token keyword map.
   139  func Keywords() map[string]Token {
   140  	cpy := make(map[string]Token, len(keywords))
   141  	for k, v := range keywords {
   142  		cpy[k] = v
   143  	}
   144  	return cpy
   145  }
   146  
   147  // IsKeyword returns if a token is a keyword
   148  func IsKeyword(tok Token) bool {
   149  	_, ok := keywords[strings[tok]]
   150  	return ok
   151  }