| 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 | // Group represents a (proto2 only) group. |
| 31 | // https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#group_field |
| 32 | type Group struct { |
| 33 | Position scanner.Position |
| 34 | Comment *Comment |
| 35 | Name string |
| 36 | Optional bool |
| 37 | Repeated bool |
| 38 | Required bool |
| 39 | Sequence int |
| 40 | Elements []Visitee |
| 41 | Parent Visitee |
| 42 | } |
| 43 | |
| 44 | // Accept dispatches the call to the visitor. |
| 45 | func (g *Group) Accept(v Visitor) { |
| 46 | v.VisitGroup(g) |
| 47 | } |
| 48 | |
| 49 | // addElement is part of elementContainer |
| 50 | func (g *Group) addElement(v Visitee) { |
| 51 | v.parent(g) |
| 52 | g.Elements = append(g.Elements, v) |
| 53 | } |
| 54 | |
| 55 | // elements is part of elementContainer |
| 56 | func (g *Group) elements() []Visitee { |
| 57 | return g.Elements |
| 58 | } |
| 59 | |
| 60 | // Doc is part of Documented |
| 61 | func (g *Group) Doc() *Comment { |
| 62 | return g.Comment |
| 63 | } |
| 64 | |
| 65 | // takeLastComment is part of elementContainer |
| 66 | // removes and returns the last element of the list if it is a Comment. |
| 67 | func (g *Group) takeLastComment(expectedOnLine int) (last *Comment) { |
| 68 | last, g.Elements = takeLastCommentIfEndsOnLine(g.Elements, expectedOnLine) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | // parse expects: |
| 73 | // groupName "=" fieldNumber { messageBody } |
| 74 | func (g *Group) parse(p *Parser) error { |
| 75 | _, tok, lit := p.next() |
| 76 | if tok != tIDENT { |
| 77 | if !isKeyword(tok) { |
| 78 | return p.unexpected(lit, "group name", g) |
| 79 | } |
| 80 | } |
| 81 | g.Name = lit |
| 82 | _, tok, lit = p.next() |
| 83 | if tok != tEQUALS { |
| 84 | return p.unexpected(lit, "group =", g) |
| 85 | } |
| 86 | i, err := p.nextInteger() |
| 87 | if err != nil { |
| 88 | return p.unexpected(lit, "group sequence number", g) |
| 89 | } |
| 90 | g.Sequence = i |
| 91 | consumeCommentFor(p, g) |
| 92 | _, tok, lit = p.next() |
| 93 | if tok != tLEFTCURLY { |
| 94 | return p.unexpected(lit, "group opening {", g) |
| 95 | } |
| 96 | return parseMessageBody(p, g) |
| 97 | } |
| 98 | |
| 99 | func (g *Group) parent(v Visitee) { g.Parent = v } |