Skip to content

Commit 09df37d

Browse files
Robemicklei
Rob
authored andcommitted
Add support for reserved in enums (#88)
1 parent c59086c commit 09df37d

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

enum.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func (e *Enum) parse(p *Parser) error {
9696
goto done
9797
case tSEMICOLON:
9898
maybeScanInlineComment(p, e)
99+
case tRESERVED:
100+
r := new(Reserved)
101+
r.Position = pos
102+
r.Comment = e.takeLastComment(pos.Line - 1)
103+
if err := r.parse(p); err != nil {
104+
return err
105+
}
106+
e.addElement(r)
99107
default:
100108
p.nextPut(pos, tok, lit)
101109
f := new(EnumField)

enum_test.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func TestEnum(t *testing.T) {
2929
proto := `
3030
// enum
3131
enum EnumAllowingAlias {
32+
reserved 998, 1000 to 2000;
33+
reserved "HELLO", "WORLD";
3234
option allow_alias = true;
3335
UNKNOWN = 0;
3436
STARTED = 1;
@@ -48,7 +50,7 @@ enum EnumAllowingAlias {
4850
if got, want := len(enums), 1; got != want {
4951
t.Errorf("got [%v] want [%v]", got, want)
5052
}
51-
if got, want := len(enums[0].Elements), 6; got != want {
53+
if got, want := len(enums[0].Elements), 8; got != want {
5254
t.Errorf("got [%v] want [%v]", got, want)
5355
}
5456
if got, want := enums[0].Comment != nil, true; got != want {
@@ -60,14 +62,46 @@ enum EnumAllowingAlias {
6062
if got, want := enums[0].Position.Line, 3; got != want {
6163
t.Errorf("got [%d] want [%d]", got, want)
6264
}
63-
ef1 := enums[0].Elements[1].(*EnumField)
65+
// enum reserved ids
66+
e1 := enums[0].Elements[0].(*Reserved)
67+
if got, want := len(e1.Ranges), 2; got != want {
68+
t.Errorf("got [%d] want [%d]", got, want)
69+
}
70+
e1rg0 := e1.Ranges[0]
71+
if got, want := e1rg0.From, 998; got != want {
72+
t.Errorf("got [%d] want [%d]", got, want)
73+
}
74+
if got, want := e1rg0.From, e1rg0.To; got != want {
75+
t.Errorf("got [%d] want [%d]", got, want)
76+
}
77+
e1rg1 := e1.Ranges[1]
78+
if got, want := e1rg1.From, 1000; got != want {
79+
t.Errorf("got [%d] want [%d]", got, want)
80+
}
81+
if got, want := e1rg1.To, 2000; got != want {
82+
t.Errorf("got [%d] want [%d]", got, want)
83+
}
84+
// enum reserved field names
85+
e2 := enums[0].Elements[1].(*Reserved)
86+
if got, want := len(e2.FieldNames), 2; got != want {
87+
t.Errorf("got [%d] want [%d]", got, want)
88+
}
89+
e2fn0 := e2.FieldNames[0]
90+
if got, want := e2fn0, "HELLO"; got != want {
91+
t.Errorf("got [%s] want [%s]", got, want)
92+
}
93+
e2fn1 := e2.FieldNames[1]
94+
if got, want := e2fn1, "WORLD"; got != want {
95+
t.Errorf("got [%s] want [%s]", got, want)
96+
}
97+
ef1 := enums[0].Elements[3].(*EnumField)
6498
if got, want := ef1.Integer, 0; got != want {
6599
t.Errorf("got [%v] want [%v]", got, want)
66100
}
67-
if got, want := ef1.Position.Line, 5; got != want {
101+
if got, want := ef1.Position.Line, 7; got != want {
68102
t.Errorf("got [%d] want [%d]", got, want)
69103
}
70-
ef3 := enums[0].Elements[3].(*EnumField)
104+
ef3 := enums[0].Elements[5].(*EnumField)
71105
if got, want := ef3.Integer, 2; got != want {
72106
t.Errorf("got [%v] want [%v]", got, want)
73107
}
@@ -82,10 +116,10 @@ enum EnumAllowingAlias {
82116
if got, want := ef3opt.Constant.Source, "hello world"; got != want {
83117
t.Errorf("got [%v] want [%v]", got, want)
84118
}
85-
if got, want := ef3.Position.Line, 7; got != want {
119+
if got, want := ef3.Position.Line, 9; got != want {
86120
t.Errorf("got [%d] want [%d]", got, want)
87121
}
88-
ef4 := enums[0].Elements[4].(*EnumField)
122+
ef4 := enums[0].Elements[6].(*EnumField)
89123
if got, want := ef4.Integer, -42; got != want {
90124
t.Errorf("got [%v] want [%v]", got, want)
91125
}

0 commit comments

Comments
 (0)