1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"fmt"
"strings"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/scanner"
"cuelang.org/go/cue/token"
"github.com/cockroachdb/apd/v3"
)
// AttrKind indicates the location of an attribute within CUE source.
type AttrKind uint8
const (
// FieldAttr indicates an attribute is a field attribute.
// foo: bar @attr()
FieldAttr AttrKind = 1 << iota
// DeclAttr indicates an attribute was specified at a declaration position.
// foo: {
// @attr()
// }
DeclAttr
// TODO: Possible future attr kinds
// ElemAttr
// FileAttr
// ValueAttr = FieldAttr|DeclAttr|ElemAttr
)
// Attr holds positional information for a single Attr.
type Attr struct {
Name string // e.g. "json" or "protobuf"
Body string
Kind AttrKind
Fields []KeyValue
Err errors.Error
Pos token.Pos
}
// NewNonExisting creates a non-existing attribute.
func NewNonExisting(key string) Attr {
const msgNotExist = "attribute %q does not exist"
return Attr{Err: errors.Newf(token.NoPos, msgNotExist, key)}
}
type KeyValue struct {
key string
value string
text string
}
func (kv *KeyValue) Text() string {
return kv.text
}
func (kv *KeyValue) Key() string {
return kv.key
}
func (kv *KeyValue) Value() string {
return kv.value
}
func (a *Attr) hasPos(p int) error {
if a.Err != nil {
return a.Err
}
if p >= len(a.Fields) {
return fmt.Errorf("field does not exist")
}
return nil
}
// String reports the possibly empty string value at the given position or
// an error the attribute is invalid or if the position does not exist.
func (a *Attr) String(pos int) (string, error) {
if err := a.hasPos(pos); err != nil {
return "", err
}
f := a.Fields[pos]
if f.key != "" {
// When there's a key, we return the entire value.
return f.Text(), nil
}
return a.Fields[pos].Value(), nil
}
// Int reports the integer at the given position or an error if the attribute is
// invalid, the position does not exist, or the value at the given position is
// not an integer.
func (a *Attr) Int(pos int) (int64, error) {
if err := a.hasPos(pos); err != nil {
return 0, err
}
var ni literal.NumInfo
if err := literal.ParseNum(a.Fields[pos].Text(), &ni); err != nil {
return 0, err
}
var d apd.Decimal
if err := ni.Decimal(&d); err != nil {
return 0, err
}
return d.Int64()
}
// Flag reports whether an entry with the given name exists at position pos or
// onwards or an error if the attribute is invalid or if the first pos-1 entries
// are not defined.
func (a *Attr) Flag(pos int, key string) (bool, error) {
if err := a.hasPos(pos - 1); err != nil {
return false, err
}
for _, kv := range a.Fields[pos:] {
if kv.Key() == "" && kv.Value() == key {
return true, nil
}
}
return false, nil
}
// Lookup searches for an entry of the form key=value from position pos onwards
// and reports the value if found. It reports an error if the attribute is
// invalid or if the first pos-1 entries are not defined.
func (a *Attr) Lookup(pos int, key string) (val string, found bool, err error) {
if err := a.hasPos(pos - 1); err != nil {
return "", false, err
}
for _, kv := range a.Fields[pos:] {
if kv.Key() == key {
return kv.Value(), true, nil
}
}
return "", false, nil
}
func ParseAttrBody(pos token.Pos, s string) (a Attr) {
// Create temporary token.File so that scanner has something
// to work with.
// TODO it's probably possible to do this without allocations.
tmpFile := token.NewFile("", -1, len(s))
if len(s) > 0 {
tmpFile.AddLine(len(s) - 1)
}
a.Body = s
a.Pos = pos
var scan scanner.Scanner
scan.Init(tmpFile, []byte(s), nil, scanner.DontInsertCommas)
for {
start := scan.Offset()
tok, err := scanAttributeTokens(&scan, pos, 1<<token.COMMA|1<<token.BIND|1<<token.EOF)
if err != nil {
// Shouldn't happen because bracket nesting should have been checked previously by
// the regular CUE parser.
a.Err = err
return a
}
switch tok {
case token.EOF:
// Empty field.
a.appendField("", s[start:], s[start:])
return a
case token.COMMA:
val := s[start : scan.Offset()-1]
a.appendField("", val, val) // All but final comma.
continue
}
valStart := scan.Offset()
key := s[start : valStart-1] // All but =.
tok, err = scanAttributeTokens(&scan, pos, 1<<token.COMMA|1<<token.EOF)
if err != nil {
// Shouldn't happen because bracket nesting should have been checked previously by
// the regular CUE parser.
a.Err = err
return a
}
valEnd := len(s)
if tok != token.EOF {
valEnd = scan.Offset() - 1 // All but final comma
}
value := s[valStart:valEnd]
text := s[start:valEnd]
a.appendField(key, value, text)
if tok == token.EOF {
return a
}
}
}
func (a *Attr) appendField(k, v, text string) {
a.Fields = append(a.Fields, KeyValue{
key: strings.TrimSpace(k),
value: maybeUnquote(strings.TrimSpace(v)),
text: text,
})
}
func maybeUnquote(s string) string {
if !possiblyQuoted(s) {
return s
}
s1, err := literal.Unquote(s)
if err != nil {
return s
}
return s1
}
func possiblyQuoted(s string) bool {
if len(s) < 2 {
return false
}
if s[0] == '#' && s[len(s)-1] == '#' {
return true
}
if s[0] == '"' && s[len(s)-1] == '"' {
return true
}
if s[0] == '\'' && s[len(s)-1] == '\'' {
return true
}
return false
}
// scanAttributeTokens reads tokens from s until it encounters
// a close token from the given bitmask. It returns the actual close token read.
func scanAttributeTokens(s *scanner.Scanner, startPos token.Pos, close uint64) (token.Token, errors.Error) {
for {
pos, tok, _ := s.Scan()
if s.ErrorCount > 0 {
// Shouldn't happen because the text should have been scanned previously by
// the regular CUE parser.
return 0, errors.Newf(startPos.Add(pos.Offset()), "error scanning attribute text")
}
if tok < 64 && (close&(1<<tok)) != 0 {
return tok, nil
}
var err error
switch tok {
case token.EOF:
err = fmt.Errorf("attribute missing '%s'", tokenMaskStr(close))
case token.LPAREN:
_, err = scanAttributeTokens(s, startPos, 1<<token.RPAREN)
case token.LBRACE:
_, err = scanAttributeTokens(s, startPos, 1<<token.RBRACE)
case token.LBRACK:
_, err = scanAttributeTokens(s, startPos, 1<<token.RBRACK)
case token.RPAREN, token.RBRACK, token.RBRACE:
err = fmt.Errorf("unexpected '%s'", tok)
}
if err != nil {
return 0, errors.Newf(startPos.Add(pos.Offset()), "%v", err)
}
}
}
func tokenMaskStr(m uint64) string {
var buf strings.Builder
for t := token.Token(0); t < 64; t++ {
if (m & (1 << t)) != 0 {
if buf.Len() > 0 {
buf.WriteByte('|')
}
buf.WriteString(t.String())
}
}
return buf.String()
}
|