This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
forked from emicklei/proto
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption.go
117 lines (107 loc) · 2.72 KB
/
option.go
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
package proto
import "fmt"
// Option is a protoc compiler option
type Option struct {
Name string
Constant Literal
IsEmbedded bool
Comment *Comment
}
// inlineComment is part of commentInliner.
func (o *Option) inlineComment(c *Comment) {
o.Comment = c
}
// Accept dispatches the call to the visitor.
func (o *Option) Accept(v Visitor) {
v.VisitOption(o)
}
// columns returns printable source tokens
func (o *Option) columns() (cols []aligned) {
if !o.IsEmbedded {
cols = append(cols, leftAligned("option "))
} else {
cols = append(cols, leftAligned(" ["))
}
cols = append(cols, o.keyValuePair(o.IsEmbedded)...)
if o.IsEmbedded {
cols = append(cols, leftAligned("]"))
}
if !o.IsEmbedded {
cols = append(cols, alignedSemicolon)
if o.Comment != nil {
cols = append(cols, notAligned(" //"), notAligned(o.Comment.Message))
}
}
return
}
// keyValuePair returns key = value or "value"
func (o *Option) keyValuePair(embedded bool) (cols []aligned) {
equals := alignedEquals
name := o.Name
if embedded {
return append(cols, notAligned(name), equals, rightAligned(o.Constant.String()))
}
return append(cols, rightAligned(name), equals, rightAligned(o.Constant.String()))
}
// parse reads an Option body
// ( ident | "(" fullIdent ")" ) { "." ident } "=" constant ";"
func (o *Option) parse(p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
if tLEFTPAREN == tok {
tok, lit = p.scanIgnoreWhitespace()
if tok != tIDENT {
if !isKeyword(tok) {
return p.unexpected(lit, "option full identifier", o)
}
}
tok, _ = p.scanIgnoreWhitespace()
if tok != tRIGHTPAREN {
return p.unexpected(lit, "full identifier closing )", o)
}
o.Name = fmt.Sprintf("(%s)", lit)
} else {
// non full ident
if tIDENT != tok {
if !isKeyword(tok) {
return p.unexpected(lit, "option identifier", o)
}
}
o.Name = lit
}
tok, lit = p.scanIgnoreWhitespace()
if tDOT == tok {
// extend identifier
tok, lit = p.scanIgnoreWhitespace()
if tok != tIDENT {
return p.unexpected(lit, "option postfix identifier", o)
}
o.Name = fmt.Sprintf("%s.%s", o.Name, lit)
tok, lit = p.scanIgnoreWhitespace()
}
if tEQUALS != tok {
return p.unexpected(lit, "option constant =", o)
}
l := new(Literal)
if err := l.parse(p); err != nil {
return err
}
o.Constant = *l
return nil
}
// Literal represents intLit,floatLit,strLit or boolLit
type Literal struct {
Source string
IsString bool
}
// String returns the source (if quoted then use double quote).
func (l Literal) String() string {
if l.IsString {
return "\"" + l.Source + "\""
}
return l.Source
}
// parse expects to read a literal constant after =.
func (l *Literal) parse(p *Parser) error {
l.Source, l.IsString = p.s.scanLiteral()
return nil
}