From 1052a242989678cf658ffd9cfe67a79a91b6ed26 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Mon, 4 Jan 2016 11:35:35 -0800 Subject: [PATCH] Check field-num uniqueness --- parser/grammar.peg | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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), }