| 1 | // Copyright (c) 2017 Ernest Micklei |
| 2 | // |
| 3 | // MIT License |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining |
| 6 | // a copy of this software and associated documentation files (the |
| 7 | // "Software"), to deal in the Software without restriction, including |
| 8 | // without limitation the rights to use, copy, modify, merge, publish, |
| 9 | // distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | // permit persons to whom the Software is furnished to do so, subject to |
| 11 | // the following conditions: |
| 12 | // |
| 13 | // The above copyright notice and this permission notice shall be |
| 14 | // included in all copies or substantial portions of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | |
| 24 | package proto |
| 25 | |
| 26 | import ( |
| 27 | "fmt" |
| 28 | "text/scanner" |
| 29 | ) |
| 30 | |
| 31 | // Option is a protoc compiler option |
| 32 | type Option struct { |
| 33 | Position scanner.Position |
| 34 | Comment *Comment |
| 35 | Name string |
| 36 | Constant Literal |
| 37 | IsEmbedded bool |
| 38 | // AggregatedConstants is DEPRECATED. These Literals are populated into Constant.OrderedMap |
| 39 | AggregatedConstants []*NamedLiteral |
| 40 | InlineComment *Comment |
| 41 | Parent Visitee |
| 42 | } |
| 43 | |
| 44 | // parse reads an Option body |
| 45 | // ( ident | //... | "(" fullIdent ")" ) { "." ident } "=" constant ";" |
| 46 | func (o *Option) parse(p *Parser) error { |
| 47 | consumeOptionComments(o, p) |
| 48 | |
| 49 | if err := o.parseOptionName(p); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | // check for = |
| 53 | pos, tok, lit := p.next() |
| 54 | if tEQUALS != tok { |
| 55 | return p.unexpected(lit, "option value assignment =", o) |
| 56 | } |
| 57 | // parse value |
| 58 | r := p.peekNonWhitespace() |
| 59 | var err error |
| 60 | // values of an option can have illegal escape sequences |
| 61 | // for the standard Go scanner used by this package. |
| 62 | p.ignoreIllegalEscapesWhile(func() { |
| 63 | if r == '{' { |
| 64 | // aggregate |
| 65 | p.next() // consume { |
| 66 | err = o.parseAggregate(p) |
| 67 | } else { |
| 68 | // non aggregate |
| 69 | l := new(Literal) |
| 70 | l.Position = pos |
| 71 | if e := l.parse(p); e != nil { |
| 72 | err = e |
| 73 | } |
| 74 | o.Constant = *l |
| 75 | } |
| 76 | }) |
| 77 | consumeOptionComments(o, p) |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | // https://protobuf.dev/reference/protobuf/proto3-spec/#option |
| 82 | func (o *Option) parseOptionName(p *Parser) error { |
| 83 | name := "" |
| 84 | for { |
| 85 | pos, tok, lit := p.nextIdent(true) |
| 86 | switch tok { |
| 87 | case tDOT: |
| 88 | name += "." |
| 89 | case tIDENT: |
| 90 | name += lit |
| 91 | case tLEFTPAREN: |
| 92 | // check for dot |
| 93 | dot := "" // none |
| 94 | if p.peekNonWhitespace() == '.' { |
| 95 | p.next() // consume dot |
| 96 | dot = "." |
| 97 | } |
| 98 | _, tok, lit = p.nextFullIdent(true) |
| 99 | if tok != tIDENT { |
| 100 | return p.unexpected(lit, "option name", o) |
| 101 | } |
| 102 | // check for closing parenthesis |
| 103 | _, tok, _ = p.next() |
| 104 | if tok != tRIGHTPAREN { |
| 105 | return p.unexpected(lit, "option full identifier closing )", o) |
| 106 | } |
| 107 | name = fmt.Sprintf("%s(%s%s)", name, dot, lit) |
| 108 | default: |
| 109 | // put it back |
| 110 | p.nextPut(pos, tok, lit) |
| 111 | goto done |
| 112 | } |
| 113 | } |
| 114 | done: |
| 115 | o.Name = name |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | // inlineComment is part of commentInliner. |
| 120 | func (o *Option) inlineComment(c *Comment) { |
| 121 | o.InlineComment = c |
| 122 | } |
| 123 | |
| 124 | // Accept dispatches the call to the visitor. |
| 125 | func (o *Option) Accept(v Visitor) { |
| 126 | v.VisitOption(o) |
| 127 | } |
| 128 | |
| 129 | // Doc is part of Documented |
| 130 | func (o *Option) Doc() *Comment { |
| 131 | return o.Comment |
| 132 | } |
| 133 | |
| 134 | // parseAggregate reads options written using aggregate syntax. |
| 135 | // tLEFTCURLY { has been consumed |
| 136 | func (o *Option) parseAggregate(p *Parser) error { |
| 137 | constants, err := parseAggregateConstants(p, o) |
| 138 | literalMap := map[string]*Literal{} |
| 139 | for _, each := range constants { |
| 140 | literalMap[each.Name] = each.Literal |
| 141 | } |
| 142 | o.Constant = Literal{Map: literalMap, OrderedMap: constants, Position: o.Position} |
| 143 | |
| 144 | // reconstruct the old, deprecated field |
| 145 | o.AggregatedConstants = collectAggregatedConstants(literalMap) |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | func (o *Option) parent(v Visitee) { o.Parent = v } |