예제 #1
0
        private void CollapseAlternatives(Entity entity)
        {
            // we're only interested in Entities whose Properties belong to one
            // Alternatives consuming action

            List <ParseAction> actions =
                entity.Properties.Select(prop => prop.Source.Parent).Distinct().ToList();

            // this should be ONLY 1 and it must BE CONSUMEANY
            if (actions.Count != 1 || !(actions.First() is ConsumeAny))
            {
                return;
            }

            // all properties should be Consuming Entities
            if (entity.Properties.Where(p => p.Source is ConsumeEntity).ToList().Count() != entity.Properties.Count)
            {
                return;
            }

            this.Log("collapsing alternatives on " + entity.Name);

            ConsumeAny consume = (ConsumeAny)actions.First();

            // create a new Property to hold the outcome of the consumption
            PropertyProxy property = new PropertyProxy()
            {
                Name   = "alternative",
                Source = consume
            };

            // add the new Property as the target of the action
            consume.Property = property;

            // make all original consumers point to the new alternative property
            // this is the case for all existing properties
            foreach (Property prop in entity.Properties)
            {
                prop.Source.Property = property;
                property.Proxied.Add(prop);
                this.Log("  - " + prop.Type);
            }
            // now remove all old Properies
            entity.Clear();

            // add the new property to the Entity
            entity.Add(property);
        }
예제 #2
0
        private ParseAction ImportAlternativesExpression(Expression exp,
                                                         Entity entity,
                                                         ParseAction parent = null,
                                                         bool optional      = false)
        {
            AlternativesExpression alternative = ((AlternativesExpression)exp);

            ConsumeAny consume = new ConsumeAny()
            {
                Parent = parent
            };

            // AlternativesExpression is constructed recusively, unroll it...
            while (true)
            {
                // add first part
                consume.Actions.Add(this.ImportPropertiesAndParseActions(
                                        alternative.NonAlternativesExpression, entity, consume, optional
                                        ));
                // add remaining parts
                if (alternative.Expression is NonAlternativesExpression)
                {
                    // last part
                    consume.Actions.Add(this.ImportPropertiesAndParseActions(
                                            alternative.Expression, entity, consume, optional
                                            ));
                    break;
                }
                else
                {
                    // recurse
                    alternative =
                        (AlternativesExpression)alternative.Expression;
                }
            }

            return(consume);
        }