cue/ast: add NewLit helper
Change-Id: I36810984e1b6e8ad164263510ff8fc5c31bbb095
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4903
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/ast/ast.go b/cue/ast/ast.go
index d723e24..f4042b3 100644
--- a/cue/ast/ast.go
+++ b/cue/ast/ast.go
@@ -425,6 +425,13 @@
return &BasicLit{Kind: token.STRING, ValuePos: token.NoPos, Value: str}
}
+// NewLit creates a new BasicLit with from a token type and string without
+// position.
+// Useful for ASTs generated by code other than the CUE parser.
+func NewLit(tok token.Token, s string) *BasicLit {
+ return &BasicLit{Kind: tok, Value: s}
+}
+
// NewBool creates a new BasicLit with a bool value without position.
// Useful for ASTs generated by code other than the CUE parser.
func NewBool(b bool) *BasicLit {
diff --git a/cue/ast/astutil/apply.go b/cue/ast/astutil/apply.go
index 76d7907..05954d2 100644
--- a/cue/ast/astutil/apply.go
+++ b/cue/ast/astutil/apply.go
@@ -178,7 +178,7 @@
decls = a
info.current.decls = decls
}
- path := &ast.BasicLit{Kind: token.STRING, Value: quoted}
+ path := ast.NewLit(token.STRING, quoted)
spec = &ast.ImportSpec{
Name: ast.NewIdent(name),
Path: path,
diff --git a/cue/ast/astutil/apply_test.go b/cue/ast/astutil/apply_test.go
index 09bc8f4..f6e412a 100644
--- a/cue/ast/astutil/apply_test.go
+++ b/cue/ast/astutil/apply_test.go
@@ -184,7 +184,7 @@
c.Replace(ast.NewIdent("s"))
}
case token.INT:
- c.Replace(&ast.BasicLit{Kind: token.INT, Value: "4"})
+ c.Replace(ast.NewLit(token.INT, "4"))
}
}
return true
diff --git a/cue/export.go b/cue/export.go
index 4677550..cc79bd5 100644
--- a/cue/export.go
+++ b/cue/export.go
@@ -125,7 +125,7 @@
orig := p.ctx.labelStr(f)
str := strconv.Quote(orig)
if len(orig)+2 < len(str) || (strings.HasPrefix(orig, "_") && f&1 == 0) {
- return &ast.BasicLit{Value: str}
+ return ast.NewLit(token.STRING, str)
}
for i, r := range orig {
if unicode.IsLetter(r) || r == '_' || r == '$' {
@@ -134,7 +134,7 @@
if i > 0 && unicode.IsDigit(r) {
continue
}
- return &ast.BasicLit{Value: str}
+ return ast.NewLit(token.STRING, str)
}
return &ast.Ident{Name: orig}
}
diff --git a/cue/format/format_test.go b/cue/format/format_test.go
index 2935933..40c0373 100644
--- a/cue/format/format_test.go
+++ b/cue/format/format_test.go
@@ -239,18 +239,17 @@
out string
}{{
name: "old-style octal numbers",
- in: &ast.BasicLit{Kind: token.INT, Value: "0123"},
+ in: ast.NewLit(token.INT, "0123"),
out: "0o123",
}, {
name: "labels with multi-line strings",
in: &ast.Field{
- Label: &ast.BasicLit{
- Kind: token.STRING,
- Value: `"""
+ Label: ast.NewLit(token.STRING,
+ `"""
foo
bar
"""`,
- },
+ ),
Value: ast.NewIdent("goo"),
},
out: `"foo\nbar": goo`,
diff --git a/cue/value.go b/cue/value.go
index 5df4323..2a47aeb 100644
--- a/cue/value.go
+++ b/cue/value.go
@@ -368,11 +368,7 @@
}
func parseInt(k kind, s string) *numLit {
- n := &ast.BasicLit{
- Kind: token.INT,
- Value: s,
- }
- num := newInt(newExpr(n), 0)
+ num := newInt(newExpr(ast.NewLit(token.INT, s)), 0)
_, _, err := num.v.SetString(s)
if err != nil {
panic(err)
@@ -381,11 +377,7 @@
}
func parseFloat(s string) *numLit {
- n := &ast.BasicLit{
- Kind: token.FLOAT,
- Value: s,
- }
- num := newFloat(newExpr(n), 0)
+ num := newFloat(newExpr(ast.NewLit(token.FLOAT, s)), 0)
_, _, err := num.v.SetString(s)
if err != nil {
panic(err)
diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go
index 2cca457..c5c8c14 100644
--- a/encoding/protobuf/parse.go
+++ b/encoding/protobuf/parse.go
@@ -630,7 +630,7 @@
// Add enum value to map
f := &ast.Field{
Label: p.stringLit(y.Position, y.Name),
- Value: &ast.BasicLit{Value: strconv.Itoa(y.Integer)},
+ Value: ast.NewLit(token.INT, strconv.Itoa(y.Integer)),
}
valueMap.Elts = append(valueMap.Elts, f)
diff --git a/pkg/tool/os/env_test.go b/pkg/tool/os/env_test.go
index a5bfae9..40c6161 100644
--- a/pkg/tool/os/env_test.go
+++ b/pkg/tool/os/env_test.go
@@ -104,14 +104,14 @@
},
"CUEOSTESTBI": &ast.BinaryExpr{
Op: token.OR,
- X: &ast.BasicLit{Kind: token.INT, Value: "1"},
+ X: ast.NewLit(token.INT, "1"),
Y: ast.NewBool(true),
},
"CUEOSTESTNUM": &ast.BasicLit{Kind: token.INT, Value: "34K"},
"CUEOSTESTNUMD": ast.NewString("not a num"),
"CUEOSTESTMULTI": &ast.BinaryExpr{
Op: token.OR,
- X: &ast.BasicLit{Kind: token.INT, Value: "10"},
+ X: ast.NewLit(token.INT, "10"),
Y: ast.NewString("10"),
},
"CUEOSTESTNULL": nil,