github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/compiler/token/token.go (about)

     1  // Original: src/go/token/token.go
     2  //
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Portions Copyright 2016 Hiroshi Ioka. All rights reserved.
     5  //
     6  // Redistribution and use in source and binary forms, with or without
     7  // modification, are permitted provided that the following conditions are
     8  // met:
     9  //
    10  //    * Redistributions of source code must retain the above copyright
    11  // notice, this list of conditions and the following disclaimer.
    12  //    * Redistributions in binary form must reproduce the above
    13  // copyright notice, this list of conditions and the following disclaimer
    14  // in the documentation and/or other materials provided with the
    15  // distribution.
    16  //    * Neither the name of Google Inc. nor the names of its
    17  // contributors may be used to endorse or promote products derived from
    18  // this software without specific prior written permission.
    19  //
    20  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    24  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    25  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    26  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    27  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    28  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    29  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    30  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31  
    32  package token
    33  
    34  import (
    35  	"github.com/hirochachacha/plua/position"
    36  )
    37  
    38  type Token struct {
    39  	Type
    40  	Pos position.Position
    41  	Lit string
    42  }
    43  
    44  func Lookup(name string) Type {
    45  	if typ, ok := keywords[name]; ok {
    46  		return typ
    47  	}
    48  	return NAME
    49  }
    50  
    51  // A set of constants for precedence-based expression parsing.
    52  // Non-operators have lowest precedence, followed by operators
    53  // starting with precedence 1 up to unary operators. The highest
    54  // precedence serves as "catch-all" precedence for selector,
    55  // indexing, and other operator and delimiter tokens.
    56  //
    57  const (
    58  	LowestPrec  = 0 // non-operators
    59  	UnaryPrec   = 12
    60  	HighestPrec = 14
    61  )
    62  
    63  // Precedence returns the operator precedence of the binary
    64  // operator op. If op is not a binary operator, the result
    65  // is LowestPrecedence.
    66  //
    67  func (op Type) Precedence() (left int, right int) {
    68  	switch op {
    69  	case OR:
    70  		return 1, 1
    71  	case AND:
    72  		return 2, 2
    73  	case EQ, NE, LT, LE, GT, GE:
    74  		return 3, 3
    75  	case BOR:
    76  		return 4, 4
    77  	case BXOR:
    78  		return 5, 5
    79  	case BAND:
    80  		return 6, 6
    81  	case SHL, SHR:
    82  		return 7, 7
    83  	case CONCAT: // right associative
    84  		return 9, 8
    85  	case ADD, SUB:
    86  		return 10, 10
    87  	case MUL, DIV, IDIV, MOD:
    88  		return 11, 11
    89  	case POW: // right associative
    90  		return 14, 13
    91  	}
    92  	return LowestPrec, LowestPrec
    93  }
    94  
    95  func (typ Type) IsLiteral() bool {
    96  	return literal_beg < typ && typ < literal_end
    97  }
    98  
    99  func (typ Type) IsOperator() bool {
   100  	return operator_beg < typ && typ < operator_end
   101  }
   102  
   103  func (typ Type) IsKeyword() bool {
   104  	return keyword_beg < typ && typ < keyword_end
   105  }