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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enum, import, option. broke some tests
Change-Id: Ifaf1e6157255a4f358c6c767a4000e8ddedec5c8
- Loading branch information
Showing
15 changed files
with
291 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package proto3parser | ||
|
||
import "fmt" | ||
|
||
type Enum struct { | ||
Line int | ||
Name string | ||
Options []*Option | ||
EnumFields []*EnumField | ||
} | ||
|
||
type EnumField struct { | ||
Name string | ||
Constant string | ||
ValueOption *Option | ||
} | ||
|
||
func (f *EnumField) parse(p *Parser) error { | ||
tok, lit := p.scanIgnoreWhitespace() | ||
if tok != IDENT { | ||
return fmt.Errorf("found %q, expected identifier", lit) | ||
} | ||
tok, lit = p.scanIgnoreWhitespace() | ||
if tok != EQUALS { | ||
return fmt.Errorf("found %q, expected =", lit) | ||
} | ||
ns := p.s.scanIntegerString() | ||
if len(ns) != 0 { | ||
f.Constant = ns | ||
} else { | ||
tok, lit = p.scanIgnoreWhitespace() | ||
if tok != IDENT { | ||
return fmt.Errorf("found %q, expected string", lit) | ||
} | ||
} | ||
tok, lit = p.scanIgnoreWhitespace() | ||
if tok == LEFTSQUARE { | ||
o := new(Option) | ||
err := o.parse(p) | ||
if err != nil { | ||
return err | ||
} | ||
f.ValueOption = o | ||
tok, lit = p.scanIgnoreWhitespace() | ||
if tok != RIGHTSQUARE { | ||
return fmt.Errorf("found %q, expected ]", lit) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *Enum) parse(p *Parser) error { | ||
tok, lit := p.scanIgnoreWhitespace() | ||
if tok != IDENT { | ||
return fmt.Errorf("found %q, expected identifier", lit) | ||
} | ||
e.Name = lit | ||
tok, lit = p.scanIgnoreWhitespace() | ||
if tok != LEFTCURLY { | ||
return fmt.Errorf("found %q, expected {", lit) | ||
} | ||
for { | ||
tok, lit = p.scanIgnoreWhitespace() | ||
switch tok { | ||
case RIGHTCURLY: | ||
goto done | ||
case SEMICOLON: | ||
case OPTION: | ||
v := new(Option) | ||
err := v.parse(p) | ||
if err != nil { | ||
return err | ||
} | ||
e.Options = append(e.Options, v) | ||
default: | ||
p.unscan() | ||
f := new(EnumField) | ||
err := f.parse(p) | ||
if err != nil { | ||
return err | ||
} | ||
e.EnumFields = append(e.EnumFields, f) | ||
} | ||
} | ||
done: | ||
if tok != RIGHTCURLY { | ||
return fmt.Errorf("found %q, expected }", lit) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package proto3parser | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestEnum(t *testing.T) { | ||
proto := `enum EnumAllowingAlias {}` | ||
p := NewParser(strings.NewReader(proto)) | ||
_, tok := p.scanIgnoreWhitespace() // consume first token | ||
if got, want := tok, "enum"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
e := new(Enum) | ||
err := e.parse(p) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, want := e.Name, "EnumAllowingAlias"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
} | ||
|
||
func TestEnumWithBody(t *testing.T) { | ||
proto := ` | ||
enum EnumAllowingAlias { | ||
option allow_alias = true; | ||
UNKNOWN = 0; | ||
STARTED = 1; | ||
RUNNING = 2 [(custom_option) = "hello world"]; | ||
}` | ||
p := NewParser(strings.NewReader(proto)) | ||
pr, err := p.Parse() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, want := len(pr.Enums), 1; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package proto3parser | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Import struct { | ||
Line int | ||
Filename string | ||
Kind string // weak, public | ||
} | ||
|
||
func (i *Import) parse(p *Parser) error { | ||
tok, lit := p.scanIgnoreWhitespace() | ||
if tok != IDENT || !strings.Contains("weak public", lit) { | ||
return fmt.Errorf("found %q, expected kind (weak|public)", lit) | ||
} | ||
i.Line = p.s.line | ||
i.Kind = lit | ||
name := p.s.scanUntil('\n') | ||
if len(name) == 0 { | ||
return fmt.Errorf("unexpected end of quoted string") | ||
} | ||
i.Filename = name | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package proto3parser | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParseImport(t *testing.T) { | ||
proto := `import public "other.proto";` | ||
p := NewParser(strings.NewReader(proto)) | ||
p.scanIgnoreWhitespace() // consume first token | ||
i := new(Import) | ||
err := i.parse(p) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, want := i.Filename, "other.proto"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
if got, want := i.Kind, "public"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
) | ||
|
||
type Message struct { | ||
Line int | ||
Name string | ||
Fields []*Field | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package proto3parser | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestOption(t *testing.T) { | ||
proto := `option java_package = "com.example.foo";` | ||
p := NewParser(strings.NewReader(proto)) | ||
p.scanIgnoreWhitespace() // consume first token | ||
o := new(Option) | ||
err := o.parse(p) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, want := o.Name, "java_package"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
if got, want := o.String, "com.example.foo"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
} | ||
|
||
func TestOptionFull(t *testing.T) { | ||
proto := `option (full.java_package) = "com.example.foo";` | ||
p := NewParser(strings.NewReader(proto)) | ||
p.scanIgnoreWhitespace() // consume first token | ||
o := new(Option) | ||
err := o.parse(p) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, want := o.Name, "full.java_package"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
if got, want := o.String, "com.example.foo"; got != want { | ||
t.Errorf("got [%v] want [%v]", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.