Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
see last commit msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Micklei authored and Ernest Micklei committed Feb 2, 2017
1 parent f3932ef commit 01d521f
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cmd/protofmt/unformatted.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ enum enum {
enum = 0;
}
message message {
message message = 1;
sometype message = 1;
}
8 changes: 5 additions & 3 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func (f *NormalField) columns() (cols []aligned) {
} else {
cols = append(cols, alignedSpace)
}
cols = append(cols, leftAligned(f.Name), rightAligned(f.Type), alignedEquals, rightAligned(strconv.Itoa(f.Sequence)))
cols = append(cols, rightAligned(f.Type), alignedSpace, leftAligned(f.Name), alignedEquals, rightAligned(strconv.Itoa(f.Sequence)))
if len(f.Options) > 0 {
cols = append(cols, leftAligned(" ["))
for i, each := range f.Options {
if i > 0 {
cols = append(cols, alignedComma)
}
cols = append(cols, each.keyValuePair()...)
cols = append(cols, each.keyValuePair(true)...)
}
cols = append(cols, leftAligned("]"))
}
Expand Down Expand Up @@ -73,7 +73,9 @@ done:
func parseFieldAfterType(f *Field, p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
return p.unexpected(lit, "field identifier", f)
if !isKeyword(tok) {
return p.unexpected(lit, "field identifier", f)
}
}
f.Name = lit
tok, lit = p.scanIgnoreWhitespace()
Expand Down
14 changes: 3 additions & 11 deletions formatter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func (f *Formatter) begin(stmt string) {
if f.lastStmt != stmt && len(f.lastStmt) > 0 { // not the first line
// add separator because stmt is changed, unless it nested thingy
if !strings.Contains("comment enum service", f.lastStmt) {
if !strings.Contains("comment ", f.lastStmt) {
io.WriteString(f.w, "\n")
}
}
Expand Down Expand Up @@ -70,14 +70,6 @@ func (f *Formatter) printListOfColumns(list []columnsPrintable) {
f.nl()
}

// paddedTo return the word padding with spaces to match the length.
func paddedTo(word string, length int) string {
if len(word) >= length {
return word
}
return word + strings.Repeat(" ", length-len(word))
}

func (f *Formatter) nl() *Formatter {
io.WriteString(f.w, "\n")
return f
Expand All @@ -95,21 +87,21 @@ func (f *Formatter) printAsGroups(list []Visitee) {
printable, isColumnsPrintable := each.(columnsPrintable)
if isColumnsPrintable {
if lastGroupName != groupName {
lastGroupName = groupName
// print current group
if len(group) > 0 {
f.printListOfColumns(group)
lastGroupName = groupName
// begin new group
group = []columnsPrintable{}
}
}
group = append(group, printable)
} else {
// not printable in group
lastGroupName = groupName
// print current group
if len(group) > 0 {
f.printListOfColumns(group)
lastGroupName = groupName
// begin new group
group = []columnsPrintable{}
}
Expand Down
4 changes: 3 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func (m *Message) Accept(v Visitor) {
func (m *Message) parse(p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
return p.unexpected(lit, "message identifier", m)
if !isKeyword(tok) {
return p.unexpected(lit, "message identifier", m)
}
}
m.Name = lit
tok, lit = p.scanIgnoreWhitespace()
Expand Down
4 changes: 3 additions & 1 deletion oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (o *OneOfField) Accept(v Visitor) {
func (o *OneOfField) parse(p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
return p.unexpected(lit, "oneof field identifier", o)
if !isKeyword(tok) {
return p.unexpected(lit, "oneof field identifier", o)
}
}
o.Name = lit
tok, lit = p.scanIgnoreWhitespace()
Expand Down
10 changes: 7 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ func (o *Option) columns() (cols []aligned) {
} else {
cols = append(cols, leftAligned(" ["))
}
cols = append(cols, o.keyValuePair()...)
cols = append(cols, o.keyValuePair(o.IsEmbedded)...)
if o.IsEmbedded {
cols = append(cols, leftAligned("]"))
}
return
}

// keyValuePair returns key = value or "value"
func (o *Option) keyValuePair() (cols []aligned) {
return append(cols, leftAligned(o.Name), alignedShortEquals, rightAligned(o.Constant.String()))
func (o *Option) keyValuePair(embedded bool) (cols []aligned) {
equals := alignedEquals
if embedded {
equals = alignedShortEquals
}
return append(cols, leftAligned(o.Name), equals, rightAligned(o.Constant.String()))
}

// parse reads an Option body
Expand Down
6 changes: 0 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ func (p *Parser) scanIgnoreWhitespace() (tok token, lit string) {
return
}

// scanIdent scans all whitespaces and scans the next non-whitespace identifier (not a keyword).
func (p *Parser) scanIdent() (tok token, lit string) {
p.s.skipWhitespace()
return p.s.scanIdent()
}

// unscan pushes the previously read token back onto the buffer.
func (p *Parser) unscan() { p.buf.n = 1 }

Expand Down
11 changes: 0 additions & 11 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,3 @@ func newParserOn(def string) *Parser {
p.debug = true
return p
}

func TestScanIdent(t *testing.T) {
p := NewParser(strings.NewReader(" message "))
tok, lit := p.scanIdent()
if got, want := tok, tIDENT; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := lit, "message"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
2 changes: 1 addition & 1 deletion proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (proto *Proto) parse(p *Parser) error {
case tEOF:
goto done
default:
return p.unexpected(lit, "comment|option|import|syntax|enum|service|package|message", p)
return p.unexpected(lit, ".proto element {comment|option|import|syntax|enum|service|package|message}", p)
}
}
done:
Expand Down
26 changes: 2 additions & 24 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *scanner) scan() (tok token, lit string) {
return s.scanWhitespace()
} else if isLetter(ch) {
s.unread(ch)
return s.scanKeyword()
return s.scanIdent()
}

// Otherwise read the individual character.
Expand Down Expand Up @@ -123,27 +123,6 @@ func (s *scanner) scanIdent() (tok token, lit string) {
var buf bytes.Buffer
buf.WriteRune(s.read())

// Read every subsequent ident character into the buffer.
// Non-ident characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isLetter(ch) && !isDigit(ch) && ch != '_' && ch != '.' { // underscore and dot can be part of identifier
s.unread(ch)
break
} else {
_, _ = buf.WriteRune(ch)
}
}
return tIDENT, buf.String()
}

// scanKeyword consumes the current rune and all contiguous ident runes.
func (s *scanner) scanKeyword() (tok token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())

// Read every subsequent ident character into the buffer.
// Non-ident characters and EOF will cause the loop to exit.
for {
Expand Down Expand Up @@ -195,8 +174,7 @@ func (s *scanner) scanKeyword() (tok token, lit string) {
return tOPTIONAL, buf.String()
}

// Otherwise return as a regular identifier.
return tIDENT, ident
return tIDENT, buf.String()
}

// read reads the next rune from the bufferred reader.
Expand Down
4 changes: 3 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func (s *Service) Accept(v Visitor) {
func (s *Service) parse(p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
return p.unexpected(lit, "service identifier", s)
if !isKeyword(tok) {
return p.unexpected(lit, "service identifier", s)
}
}
s.Name = lit
tok, lit = p.scanIgnoreWhitespace()
Expand Down
8 changes: 8 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
tDOT // .

// Keywords
KeywordsStart
tSYNTAX
tSERVICE
tRPC
Expand All @@ -50,6 +51,8 @@ const (

// proto2
tOPTIONAL
tGROUP
KeywordsEnd
)

const typeTokens = "double float int32 int64 uint32 uint64 sint32 sint64 fixed32 sfixed32 sfixed64 bool string bytes"
Expand All @@ -58,3 +61,8 @@ const typeTokens = "double float int32 int64 uint32 uint64 sint32 sint64 fixed32
const (
iSTREAM = "stream"
)

// isKeyword returns if tok is in the keywords range
func isKeyword(tok token) bool {
return KeywordsStart < tok && tok < KeywordsEnd
}

0 comments on commit 01d521f

Please sign in to comment.