Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with an internal fork #96

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 236 additions & 42 deletions generator/go.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func main() {
generator := &GoGenerator{
ThriftFiles: parsedThrift,
Format: true,
Pointers: *flagGoPointers,
SignedBytes: *flagGoSignedBytes,
}
err = generator.Generate(outpath)
Expand Down
36 changes: 28 additions & 8 deletions parser/grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Include ← "include" _ file:Literal EOS {

Statement ← Include / Namespace / Const / Enum / TypeDef / Struct / Exception / Union / Service

Namespace ← "namespace" _ scope:[*a-z.-]+ _ ns:Identifier EOS {
Namespace ← "namespace" _ scope:[*a-z0-9.-]+ _ ns:Identifier EOS {
return &namespace{
scope: ifaceSliceToString(scope),
namespace: string(ns.(Identifier)),
Expand Down Expand Up @@ -135,7 +135,7 @@ Enum ← "enum" _ name:Identifier __ '{' __ values:(EnumValue __)* '}' _ annotat
next := 0
for _, v := range vs {
ev := v.([]interface{})[0].(*EnumValue)
if ev.Value < 0 {
if !ev.Set {
ev.Value = next
}
if ev.Value >= next {
Expand All @@ -149,11 +149,11 @@ Enum ← "enum" _ name:Identifier __ '{' __ values:(EnumValue __)* '}' _ annotat
EnumValue ← name:Identifier _ value:('=' _ IntConstant)? _ annotations:TypeAnnotations? ListSeparator? {
ev := &EnumValue{
Name: string(name.(Identifier)),
Value: -1,
Annotations: toAnnotations(annotations),
}
if value != nil {
ev.Value = int(value.([]interface{})[2].(int64))
ev.Set = true
}
return ev, nil
}
Expand Down Expand Up @@ -183,15 +183,31 @@ StructLike ← name:Identifier __ '{' __ fields:FieldList '}' _ annotations:Type
FieldList ← fields:(Field __)* {
fs := fields.([]interface{})
flds := make([]*Field, len(fs))
ids := make(map[int]bool, len(fs))
for i, f := range fs {
flds[i] = f.([]interface{})[0].(*Field)

if ids[flds[i].ID] {
return nil, fmt.Errorf("multiple fields with the same identifier: %d", flds[i].ID)
}

ids[flds[i].ID] = true
}
return flds, nil
}

Field ← id:IntConstant _ ':' _ req:FieldReq? _ typ:FieldType _ name:Identifier __ def:('=' _ ConstValue)? _ annotations:TypeAnnotations? ListSeparator? {
FieldID ← id:IntConstant _ ':' {
return int(id.(int64)), nil
}

Field ← id:FieldID? _ req:FieldReq? _ typ:FieldType __ name:Identifier __ def:('=' _ ConstValue)? _ annotations:TypeAnnotations? ListSeparator? {
parsedID := -1
if id != nil {
parsedID = id.(int)
}

f := &Field{
ID : int(id.(int64)),
ID : parsedID,
Name : string(name.(Identifier)),
Type : typ.(*Type),
Annotations: toAnnotations(annotations),
Expand Down Expand Up @@ -290,7 +306,7 @@ ContainerType ← typ:(MapType / SetType / ListType) {
return typ, nil
}

MapType ← CppType? "map" WS "<" WS key:FieldType WS "," WS value:FieldType WS ">" _ annotations:TypeAnnotations? {
MapType ← CppType? "map" WS "<" WS key:FieldType WS "," __ value:FieldType WS ">" _ annotations:TypeAnnotations? {
return &Type{
Name: "map",
KeyType: key.(*Type),
Expand Down Expand Up @@ -319,7 +335,7 @@ CppType ← "cpp_type" cppType:Literal {
return cppType, nil
}

ConstValue ← Literal / DoubleConstant / IntConstant / ConstMap / ConstList / Identifier
ConstValue ← Literal / DoubleConstant / IntConstant / BoolConstant / ConstMap / ConstList / Identifier

TypeAnnotations ← '(' __ annotations:TypeAnnotation* ')' {
var anns []*Annotation
Expand All @@ -340,6 +356,10 @@ TypeAnnotation ← name:Identifier _ value:('=' __ value:Literal { return value,
}, nil
}

BoolConstant ← ("true" / "false") {
return string(c.text) == "true", nil
}

IntConstant ← [-+]? Digit+ {
return strconv.ParseInt(string(c.text), 10, 64)
}
Expand All @@ -357,7 +377,7 @@ ConstList ← '[' __ values:(ConstValue __ ListSeparator? __)* __ ']' {
return vs, nil
}

ConstMap ← '{' __ values:(ConstValue __ ':' __ ConstValue __ (',' / &'}') __)* '}' {
ConstMap ← '{' __ values:(ConstValue __ ':' __ ConstValue __ (','? / &'}') __)* '}' {
if values == nil {
return nil, nil
}
Expand Down
Loading