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

Commit

Permalink
reduced package visibiltiy
Browse files Browse the repository at this point in the history
Change-Id: I8137f815fef3a84f1a90af52982e42354eca1023
  • Loading branch information
emicklei committed Jan 27, 2017
1 parent fdf6013 commit 06bb4b2
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 52 deletions.
16 changes: 8 additions & 8 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ type Field struct {
Sequence int
}

// ParseField parsers one field.
func ParseField(f *Field, p *Parser) error {
// parseField parsers one field.
func parseField(f *Field, p *Parser) error {
for {
tok, lit := p.scanIgnoreWhitespace()
switch tok {
case IDENT:
// normal type?
if strings.Contains(TypeTokens, lit) {
if strings.Contains(typeTokens, lit) {
f.Type = lit
return ParseNormalField(f, p)
return parseNormalField(f, p)
}
//if tok == ONEOF {}
//if tok == ONEOFFIELD {}
case MESSAGE:
m, err := ParseMessage(p)
m, err := parseMessage(p)
if err != nil {
return err
}
f.Messages = append(f.Messages, m)
case REPEATED:
f.Repeated = true
return ParseField(f, p)
return parseField(f, p)
default:
goto done
}
Expand All @@ -44,8 +44,8 @@ done:
return nil
}

// ParseNormalField proceeds after reading the type of f.
func ParseNormalField(f *Field, p *Parser) error {
// parseNormalField proceeds after reading the type of f.
func parseNormalField(f *Field, p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
if tok != IDENT {
return fmt.Errorf("found %q, expected identifier", lit)
Expand Down
2 changes: 1 addition & 1 deletion field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestRepeatedField(t *testing.T) {
proto := `repeated string lots = 1;`
p := NewParser(strings.NewReader(proto))
f := new(Field)
err := ParseField(f, p)
err := parseField(f, p)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (m Message) String() string {
return buf.String()
}

func ParseMessage(p *Parser) (*Message, error) {
func parseMessage(p *Parser) (*Message, error) {
m := new(Message)
tok, lit := p.scanIgnoreWhitespace()
if tok != IDENT {
Expand All @@ -40,7 +40,7 @@ func ParseMessage(p *Parser) (*Message, error) {
default:
p.unscan()
f := new(Field)
err := ParseField(f, p)
err := parseField(f, p)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestMessage(t *testing.T) {
proto := `message AccountOut {}`
p := NewParser(strings.NewReader(proto))
p.scanIgnoreWhitespace() // consume first token
m, err := ParseMessage(p)
m, err := parseMessage(p)
if err != nil {
t.Fatal(err)
}
Expand All @@ -27,7 +27,7 @@ func TestMessageWithFields(t *testing.T) {
}`
p := NewParser(strings.NewReader(proto))
p.scanIgnoreWhitespace() // consume first token
m, err := ParseMessage(p)
m, err := parseMessage(p)
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import (

// Parser represents a parser.
type Parser struct {
s *Scanner
s *scanner
buf struct {
tok Token // last read token
tok token // last read token
lit string // last read literal
n int // buffer size (max=1)
}
}

// NewParser returns a new instance of Parser.
func NewParser(r io.Reader) *Parser {
return &Parser{s: NewScanner(r)}
return &Parser{s: newScanner(r)}
}

// Parse parses a proto definition.
func (p *Parser) Parse() (*Proto, error) {
proto := new(Proto)
return proto, ParseProto(proto, p)
return proto, parseProto(proto, p)
}

// scan returns the next token from the underlying scanner.
// If a token has been unscanned then read that instead.
func (p *Parser) scan() (tok Token, lit string) {
func (p *Parser) scan() (tok token, lit string) {
// If we have a token on the buffer, then return it.
if p.buf.n != 0 {
p.buf.n = 0
Expand All @@ -45,7 +45,7 @@ func (p *Parser) scan() (tok Token, lit string) {
}

// scanIgnoreWhitespace scans the next non-whitespace token.
func (p *Parser) scanIgnoreWhitespace() (tok Token, lit string) {
func (p *Parser) scanIgnoreWhitespace() (tok token, lit string) {
tok, lit = p.scan()
if tok == WS {
tok, lit = p.scan()
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestSyntax(t *testing.T) {
proto := `syntax = "proto3";`
p := NewParser(strings.NewReader(proto))
p.scanIgnoreWhitespace() // consume first token
syntax, err := ParseSyntax(p)
syntax, err := parseSyntax(p)
if err != nil {
t.Fatal(err)
}
Expand Down
16 changes: 8 additions & 8 deletions proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type Proto struct {
Comments []*Comment
}

// ParseSyntax returns the syntax value. Parser has seen "syntax".
func ParseSyntax(p *Parser) (string, error) {
// parseSyntax returns the syntax value. Parser has seen "syntax".
func parseSyntax(p *Parser) (string, error) {
if tok, lit := p.scanIgnoreWhitespace(); tok != EQUALS {
return "", fmt.Errorf("found %q, expected EQUALS", lit)
}
Expand All @@ -33,8 +33,8 @@ type Comment struct {
Message string
}

// ParseProto parsers a complete .proto definition source.
func ParseProto(proto *Proto, p *Parser) error {
// parseProto parsers a complete .proto definition source.
func parseProto(proto *Proto, p *Parser) error {
tok, lit := p.scanIgnoreWhitespace()
//log.Println(tok, lit)
switch tok {
Expand All @@ -44,25 +44,25 @@ func ParseProto(proto *Proto, p *Parser) error {
Message: lit,
})
case SYNTAX:
if syntax, err := ParseSyntax(p); err != nil {
if syntax, err := parseSyntax(p); err != nil {
return err
} else {
proto.Syntax = syntax
}
case SERVICE:
if service, err := ParseService(p); err != nil {
if service, err := parseService(p); err != nil {
return err
} else {
proto.Services = append(proto.Services, service)
}
case MESSAGE:
if msg, err := ParseMessage(p); err != nil {
if msg, err := parseMessage(p); err != nil {
return err
} else {
proto.Messages = append(proto.Messages, msg)
}
case EOF:
return nil
}
return ParseProto(proto, p)
return parseProto(proto, p)
}
24 changes: 12 additions & 12 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
"strings"
)

// Scanner represents a lexical scanner.
type Scanner struct {
// scanner represents a lexical scanner.
type scanner struct {
r *bufio.Reader
line int
}

// NewScanner returns a new instance of Scanner.
func NewScanner(r io.Reader) *Scanner {
return &Scanner{r: bufio.NewReader(r)}
// newScanner returns a new instance of Scanner.
func newScanner(r io.Reader) *scanner {
return &scanner{r: bufio.NewReader(r)}
}

// Line returns the current line being scanned
func (s *Scanner) Line() int { return s.line }
func (s *scanner) Line() int { return s.line }

// Scan returns the next token and literal value.
func (s *Scanner) Scan() (tok Token, lit string) {
func (s *scanner) Scan() (tok token, lit string) {
// Read the next rune.
ch := s.read()

Expand Down Expand Up @@ -74,7 +74,7 @@ func (s *Scanner) Scan() (tok Token, lit string) {
}

// scanWhitespace consumes the current rune and all contiguous whitespace.
func (s *Scanner) scanWhitespace() (tok Token, lit string) {
func (s *scanner) scanWhitespace() (tok token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
Expand All @@ -96,7 +96,7 @@ func (s *Scanner) scanWhitespace() (tok Token, lit string) {
}

// scanIdent consumes the current rune and all contiguous ident runes.
func (s *Scanner) scanIdent() (tok Token, lit string) {
func (s *scanner) scanIdent() (tok token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *Scanner) scanIdent() (tok Token, lit string) {

// read reads the next rune from the bufferred reader.
// Returns the rune(0) if an error occurs (or io.EOF is returned).
func (s *Scanner) read() rune {
func (s *scanner) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
Expand All @@ -154,7 +154,7 @@ func (s *Scanner) read() rune {
}

// unread places the previously read rune back on the reader.
func (s *Scanner) unread() { _ = s.r.UnreadRune() }
func (s *scanner) unread() { _ = s.r.UnreadRune() }

// isWhitespace returns true if the rune is a space, tab, or newline.
func isWhitespace(ch rune) bool { return ch == ' ' || ch == '\t' || ch == '\n' }
Expand All @@ -169,7 +169,7 @@ func isDigit(ch rune) bool { return (ch >= '0' && ch <= '9') }
var eof = rune(0)

// scanUntilLineEnd return the string up to (not including) a line end or EOF.
func (s *Scanner) scanUntilLineEnd() string {
func (s *scanner) scanUntilLineEnd() string {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
Expand Down
2 changes: 1 addition & 1 deletion scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestScanUntilLineEnd(t *testing.T) {
r := strings.NewReader(`hello
world`)
s := NewScanner(r)
s := newScanner(r)
v := s.scanUntilLineEnd()
if got, want := v, "hello"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
Expand Down
14 changes: 7 additions & 7 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type Service struct {
Name string
RPCalls []*RPCall
RPCalls []*rpcall
}

func (s Service) String() string {
Expand All @@ -20,7 +20,7 @@ func (s Service) String() string {
return buf.String()
}

func ParseService(p *Parser) (*Service, error) {
func parseService(p *Parser) (*Service, error) {
s := new(Service)
tok, lit := p.scanIgnoreWhitespace()
if tok != IDENT {
Expand All @@ -34,7 +34,7 @@ func ParseService(p *Parser) (*Service, error) {
for {
tok, lit = p.scanIgnoreWhitespace()
if tok == RPC {
if rpc, err := ParseRPC(p); err != nil {
if rpc, err := parseRPC(p); err != nil {
return nil, err
} else {
s.RPCalls = append(s.RPCalls, rpc)
Expand All @@ -51,20 +51,20 @@ func ParseService(p *Parser) (*Service, error) {
return s, nil
}

type RPCall struct {
type rpcall struct {
Method string
RequestType string
Streaming bool
ReturnsType string
}

func (r RPCall) String() string {
func (r rpcall) String() string {
return fmt.Sprintf("rpc %s (%s) returns (%s) {}", r.Method, r.RequestType, r.ReturnsType)
}

// rpc CreateAccount (CreateAccount) returns (ServiceFault) {}
func ParseRPC(p *Parser) (*RPCall, error) {
rpc := new(RPCall)
func parseRPC(p *Parser) (*rpcall, error) {
rpc := new(rpcall)
tok, lit := p.scanIgnoreWhitespace()
if tok != IDENT {
return nil, fmt.Errorf("found %q, expected method", lit)
Expand Down
8 changes: 4 additions & 4 deletions token.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package proto3parser

// Token represents a lexical token.
type Token int
// token represents a lexical token.
type token int

const (
// Special tokens
ILLEGAL Token = iota
ILLEGAL token = iota
EOF
WS

Expand Down Expand Up @@ -43,4 +43,4 @@ const (
ENUM
)

const TypeTokens = "double float int32 int64 uint32 uint64 sint32 sint64 fixed32 sfixed32 sfixed64 bool string bytes"
const typeTokens = "double float int32 int64 uint32 uint64 sint32 sint64 fixed32 sfixed32 sfixed64 bool string bytes"

0 comments on commit 06bb4b2

Please sign in to comment.