diff --git a/parser/grammar.peg b/parser/grammar.peg index 53a9947..1bed281 100644 --- a/parser/grammar.peg +++ b/parser/grammar.peg @@ -177,15 +177,31 @@ StructLike ← name:Identifier __ '{' __ fields:FieldList '}' EOS { 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)? ListSeparator? { +FieldID ← id:IntConstant _ ':' { + return int(id.(int64)), nil +} + +Field ← id:FieldID? _ req:FieldReq? _ typ:FieldType _ name:Identifier __ def:('=' _ ConstValue)? ListSeparator? { + parsedID := -1 + if id != nil { + parsedID = id.(int) + } + f := &Field{ - ID : int(id.(int64)), + ID : parsedID, Name : string(name.(Identifier)), Type : typ.(*Type), }