Skip to content

Commit

Permalink
all: remove manual work via //go:generate stringer
Browse files Browse the repository at this point in the history
Using go run, meaning that we track the x/tools version via go.mod.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I4e93f045bb76d3ce00ea004bca2d68d4ee0bf4b2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1186238
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Mar 28, 2024
1 parent 2137bea commit 64c9b62
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 187 deletions.
139 changes: 30 additions & 109 deletions cue/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,41 @@
// programming language and basic operations on tokens (printing, predicates).
package token // import "cuelang.org/go/cue/token"

import "strconv"

// Token is the set of lexical tokens of the CUE configuration language.
type Token int

//go:generate go run golang.org/x/tools/cmd/stringer -type=Token -linecomment

// The list of tokens.
const (
// Special tokens
ILLEGAL Token = iota
EOF
COMMENT
ATTRIBUTE // @foo(bar,baz=4)
// e.g. @foo(bar,baz=4)
ATTRIBUTE

literalBeg
// Identifiers and basic type literals
// (these tokens stand for classes of literals)
IDENT // main, _tmp
INT // 12_345Mi, 0700, 0xdeadbeef, 1.2M
FLOAT // 123.45,
// DURATION // 3m4s TODO
STRING // "abc"
INTERPOLATION // a part of a template string, e.g. `"age: \(`
BOTTOM // _|_
literalBeg
// e.g. main, _tmp
IDENT
// e.g. 12_345Mi, 0700, 0xdeadbeef, 1.2M
INT
// e.g. 123.45
FLOAT
// e.g. 3m4s; TODO
// DURATION
// e.g. "abc"
STRING
// a part of a template string, e.g. `"age: \(`
INTERPOLATION
BOTTOM // _|_

literalEnd

operatorBeg
// Operators and delimiters
operatorBeg
ADD // +
SUB // -
MUL // *
Expand Down Expand Up @@ -92,106 +99,20 @@ const (

keywordBeg

IF
FOR
IN
LET
FUNC // experimental
IF // if
FOR // for
IN // in
LET // let
// experimental
FUNC // func

TRUE
FALSE
NULL
TRUE // true
FALSE // false
NULL // null

keywordEnd
)

var tokens = [...]string{
ILLEGAL: "ILLEGAL",

EOF: "EOF",
COMMENT: "COMMENT",

IDENT: "IDENT",
INT: "INT",
FLOAT: "FLOAT",
STRING: "STRING",
INTERPOLATION: "INTERPOLATION",
ATTRIBUTE: "ATTRIBUTE",

ADD: "+",
SUB: "-",
MUL: "*",
POW: "^",
QUO: "/",

IQUO: "quo",
IREM: "rem",
IDIV: "div",
IMOD: "mod",

AND: "&",
OR: "|",

LAND: "&&",
LOR: "||",

BIND: "=",
EQL: "==",
LSS: "<",
GTR: ">",
NOT: "!",
ARROW: "<-",

NEQ: "!=",
LEQ: "<=",
GEQ: ">=",

MAT: "=~",
NMAT: "!~",

LPAREN: "(",
LBRACK: "[",
LBRACE: "{",
COMMA: ",",
PERIOD: ".",
ELLIPSIS: "...",

RPAREN: ")",
RBRACK: "]",
RBRACE: "}",
SEMICOLON: ";",
COLON: ":",
OPTION: "?",

BOTTOM: "_|_",

FALSE: "false",
TRUE: "true",
NULL: "null",

FOR: "for",
IF: "if",
IN: "in",
LET: "let",
FUNC: "func",
}

// String returns the string corresponding to the token tok.
// For operators, delimiters, and keywords the string is the actual
// token character sequence (e.g., for the token ADD, the string is
// "+"). For all other tokens the string corresponds to the token
// constant name (e.g. for the token IDENT, the string is "IDENT").
func (tok Token) String() string {
s := ""
if 0 <= tok && tok < Token(len(tokens)) {
s = tokens[tok]
}
if s == "" {
s = "token(" + strconv.Itoa(int(tok)) + ")"
}
return s
}

// A set of constants for precedence-based expression parsing.
// Non-operators have lowest precedence, followed by operators
// starting with precedence 1 up to unary operators. The highest
Expand Down Expand Up @@ -236,8 +157,8 @@ var keywords map[string]Token

func init() {
keywords = make(map[string]Token)
for i := keywordBeg + 1; i < keywordEnd; i++ {
keywords[tokens[i]] = i
for tok := keywordBeg + 1; tok < keywordEnd; tok++ {
keywords[tok.String()] = tok
}
}

Expand Down
82 changes: 82 additions & 0 deletions cue/token/token_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 33 additions & 66 deletions internal/core/adt/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,46 @@ import "cuelang.org/go/cue/token"
// use to evaluate a value.
type Op int

func (o Op) String() string {
return opToString[o]
}
//go:generate go run golang.org/x/tools/cmd/stringer -type=Op -linecomment

// Values of Op.
const (
NoOp Op = iota

AndOp
OrOp

SelectorOp
IndexOp
SliceOp
CallOp

BoolAndOp
BoolOrOp

EqualOp
NotOp
NotEqualOp
LessThanOp
LessEqualOp
GreaterThanOp
GreaterEqualOp

MatchOp
NotMatchOp

AddOp
SubtractOp
MultiplyOp
FloatQuotientOp
IntQuotientOp
IntRemainderOp
IntDivideOp
IntModuloOp

InterpolationOp
AndOp // &
OrOp // |

SelectorOp // .
IndexOp // []
SliceOp // [:]
CallOp // ()

BoolAndOp // &&
BoolOrOp // ||

EqualOp // ==
NotOp // !
NotEqualOp // !=
LessThanOp // <
LessEqualOp // <=
GreaterThanOp // >
GreaterEqualOp // >=

MatchOp // =~
NotMatchOp // !~

AddOp // +
SubtractOp // -
MultiplyOp // *
FloatQuotientOp // /
IntQuotientOp // quo
IntRemainderOp // rem
IntDivideOp // div
IntModuloOp // mod

InterpolationOp // \()
)

var opToString = map[Op]string{
AndOp: "&",
OrOp: "|",
BoolAndOp: "&&",
BoolOrOp: "||",
EqualOp: "==",
NotOp: "!",
NotEqualOp: "!=",
LessThanOp: "<",
LessEqualOp: "<=",
GreaterThanOp: ">",
GreaterEqualOp: ">=",
MatchOp: "=~",
NotMatchOp: "!~",
AddOp: "+",
SubtractOp: "-",
MultiplyOp: "*",
FloatQuotientOp: "/",
IntQuotientOp: "quo",
IntRemainderOp: "rem",
IntDivideOp: "div",
IntModuloOp: "mod",

SelectorOp: ".",
IndexOp: "[]",
SliceOp: "[:]",
CallOp: "()",

InterpolationOp: `\()`,
}

// OpFromToken converts a token.Token to an Op.
func OpFromToken(t token.Token) Op {
return tokenMap[t]
Expand Down
Loading

0 comments on commit 64c9b62

Please sign in to comment.