| 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 | "text/scanner" |
| 28 | ) |
| 29 | |
| 30 | // Extensions declare that a range of field numbers in a message are available for third-party extensions. |
| 31 | // proto2 only |
| 32 | type Extensions struct { |
| 33 | Position scanner.Position |
| 34 | Comment *Comment |
| 35 | Ranges []Range |
| 36 | InlineComment *Comment |
| 37 | Parent Visitee |
| 38 | Options []*Option |
| 39 | } |
| 40 | |
| 41 | // inlineComment is part of commentInliner. |
| 42 | func (e *Extensions) inlineComment(c *Comment) { |
| 43 | e.InlineComment = c |
| 44 | } |
| 45 | |
| 46 | // Accept dispatches the call to the visitor. |
| 47 | func (e *Extensions) Accept(v Visitor) { |
| 48 | v.VisitExtensions(e) |
| 49 | } |
| 50 | |
| 51 | // parse expects ranges |
| 52 | func (e *Extensions) parse(p *Parser) error { |
| 53 | list, err := parseRanges(p, e) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | e.Ranges = list |
| 58 | |
| 59 | // see if there are options |
| 60 | pos, tok, lit := p.next() |
| 61 | if tLEFTSQUARE != tok { |
| 62 | p.nextPut(pos, tok, lit) |
| 63 | return nil |
| 64 | } |
| 65 | // consume options (copied from normal field parsing) |
| 66 | for { |
| 67 | o := new(Option) |
| 68 | o.Position = pos |
| 69 | o.IsEmbedded = true |
| 70 | o.parent(e) |
| 71 | err := o.parse(p) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | e.Options = append(e.Options, o) |
| 76 | |
| 77 | pos, tok, lit = p.next() |
| 78 | if tRIGHTSQUARE == tok { |
| 79 | break |
| 80 | } |
| 81 | if tCOMMA != tok { |
| 82 | return p.unexpected(lit, "option ,", o) |
| 83 | } |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | // parent is part of elementContainer |
| 89 | func (e *Extensions) parent(p Visitee) { e.Parent = p } |