From 246b68b0c277d627f4c210ebc10859c2d3503055 Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Sun, 6 Aug 2017 20:43:52 +0200 Subject: [PATCH] validation --- char.go | 1 + choice.go | 10 ++++++++++ parse.go | 1 + sequence.go | 14 ++++++++++++-- syntax.go | 4 ++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/char.go b/char.go index 2f2728a..ff2c184 100644 --- a/char.go +++ b/char.go @@ -27,6 +27,7 @@ func (p *charParser) nodeName() string { return p.name } func (p *charParser) nodeID() int { return p.id } func (p *charParser) setID(id int) { p.id = id } func (p *charParser) commitType() CommitType { return Alias } +func (p *charParser) validate(*registry, *idSet) error { return nil } func (p *charParser) normalize(*registry, *idSet) error { return nil } func (p *charParser) init(r *registry) error { return nil } diff --git a/choice.go b/choice.go index 57a41f1..ed1e98a 100644 --- a/choice.go +++ b/choice.go @@ -38,6 +38,16 @@ func (d *choiceDefinition) nodeID() int { return d.id } func (d *choiceDefinition) setID(id int) { d.id = id } func (d *choiceDefinition) commitType() CommitType { return d.commit } +func (d *choiceDefinition) validate(r *registry, path *idSet) error { + for i := range d.elements { + if _, ok := r.definition(d.elements[i]); !ok { + return parserNotFound(d.elements[i]) + } + } + + return nil +} + func (d *choiceDefinition) normalize(r *registry, path *idSet) error { if path.has(d.id) { return nil diff --git a/parse.go b/parse.go index cec0809..b2f9509 100644 --- a/parse.go +++ b/parse.go @@ -7,6 +7,7 @@ type definition interface { nodeID() int commitType() CommitType setID(int) + validate(*registry, *idSet) error normalize(*registry, *idSet) error init(*registry) error setIncludedBy(*registry, int, *idSet) error diff --git a/sequence.go b/sequence.go index d133ca0..b8953a1 100644 --- a/sequence.go +++ b/sequence.go @@ -44,6 +44,16 @@ func (d *sequenceDefinition) nodeID() int { return d.id } func (d *sequenceDefinition) setID(id int) { d.id = id } func (d *sequenceDefinition) commitType() CommitType { return d.commit } +func (d *sequenceDefinition) validate(r *registry, path *idSet) error { + for i := range d.items { + if _, ok := r.definition(d.items[i].Name); !ok { + return parserNotFound(d.items[i].Name) + } + } + + return nil +} + func (d *sequenceDefinition) normalize(r *registry, path *idSet) error { if path.has(d.id) { return nil @@ -51,12 +61,12 @@ func (d *sequenceDefinition) normalize(r *registry, path *idSet) error { path.set(d.id) for i := range d.items { - element, ok := r.definition(d.items[i].Name) + item, ok := r.definition(d.items[i].Name) if !ok { return parserNotFound(d.items[i].Name) } - element.normalize(r, path) + item.normalize(r, path) } return nil diff --git a/syntax.go b/syntax.go index d81deac..a9637e6 100644 --- a/syntax.go +++ b/syntax.go @@ -144,6 +144,10 @@ func (s *Syntax) Init() error { return ErrRootAlias } + if err := s.root.validate(s.registry, &idSet{}); err != nil { + return err + } + if err := s.root.normalize(s.registry, &idSet{}); err != nil { return err }