-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathoption.go
More file actions
149 lines (137 loc) · 3.99 KB
/
option.go
File metadata and controls
149 lines (137 loc) · 3.99 KB
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
// Copyright (c) 2017 Ernest Micklei
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package proto
import (
"fmt"
"text/scanner"
)
// Option is a protoc compiler option
type Option struct {
Position scanner.Position
Comment *Comment
Name string
Constant Literal
IsEmbedded bool
// AggregatedConstants is DEPRECATED. These Literals are populated into Constant.OrderedMap
AggregatedConstants []*NamedLiteral
InlineComment *Comment
Parent Visitee
}
// parse reads an Option body
// ( ident | //... | "(" fullIdent ")" ) { "." ident } "=" constant ";"
func (o *Option) parse(p *Parser) error {
consumeOptionComments(o, p)
if err := o.parseOptionName(p); err != nil {
return err
}
// check for =
pos, tok, lit := p.next()
if tEQUALS != tok {
return p.unexpected(lit, "option value assignment =", o)
}
// parse value
r := p.peekNonWhitespace()
var err error
// values of an option can have illegal escape sequences
// for the standard Go scanner used by this package.
p.ignoreIllegalEscapesWhile(func() {
if r == '{' {
// aggregate
p.next() // consume {
err = o.parseAggregate(p)
} else {
// non aggregate
l := new(Literal)
l.Position = pos
if e := l.parse(p); e != nil {
err = e
}
o.Constant = *l
}
})
consumeOptionComments(o, p)
return err
}
// https://protobuf.dev/reference/protobuf/proto3-spec/#option
func (o *Option) parseOptionName(p *Parser) error {
name := ""
for {
pos, tok, lit := p.nextIdent(true)
switch tok {
case tDOT:
name += "."
case tIDENT:
name += lit
case tLEFTPAREN:
// check for dot
dot := "" // none
if p.peekNonWhitespace() == '.' {
p.next() // consume dot
dot = "."
}
_, tok, lit = p.nextFullIdent(true)
if tok != tIDENT {
return p.unexpected(lit, "option name", o)
}
// check for closing parenthesis
_, tok, _ = p.next()
if tok != tRIGHTPAREN {
return p.unexpected(lit, "option full identifier closing )", o)
}
name = fmt.Sprintf("%s(%s%s)", name, dot, lit)
default:
// put it back
p.nextPut(pos, tok, lit)
goto done
}
}
done:
o.Name = name
return nil
}
// inlineComment is part of commentInliner.
func (o *Option) inlineComment(c *Comment) {
o.InlineComment = c
}
// Accept dispatches the call to the visitor.
func (o *Option) Accept(v Visitor) {
v.VisitOption(o)
}
// Doc is part of Documented
func (o *Option) Doc() *Comment {
return o.Comment
}
// parseAggregate reads options written using aggregate syntax.
// tLEFTCURLY { has been consumed
func (o *Option) parseAggregate(p *Parser) error {
constants, err := parseAggregateConstants(p, o)
literalMap := map[string]*Literal{}
for _, each := range constants {
literalMap[each.Name] = each.Literal
}
o.Constant = Literal{Map: literalMap, OrderedMap: constants, Position: o.Position}
// reconstruct the old, deprecated field
o.AggregatedConstants = collectAggregatedConstants(literalMap)
return err
}
func (o *Option) parent(v Visitee) { o.Parent = v }