Ejemplo n.º 1
0
        internal static bool TryParse(Node parent, Queue<Word> remainingWords, out MixinDefinition definition)
        {
            definition = null;
            if (remainingWords.Peek().Text != "@mixin")
            {
                return false;
            }

            definition = new MixinDefinition(parent);
            // remove '@mixin'
            remainingWords.Dequeue();

            var remainingNameWords = new Queue<Word>();
            while (remainingWords.Peek().Text != "{")
            {
                remainingNameWords.Enqueue(remainingWords.Dequeue());
            }

            Expression name;
            if (!Expression.TryParse(definition, remainingNameWords, out name))
            {
                throw new Exception("errp?");
            }
            definition.Name = name;

            Block block;
            if (!Block.TryParse(definition, remainingWords, out block))
            {
                throw new Exception("erh?");
            }
            definition.Value = block;
            return true;
        }
Ejemplo n.º 2
0
        private bool TryMatchNamedValues(
            MixinDefinition definition,
            out NamedValue includeName,
            out NamedValue mixinName)
        {
            includeName = Name.Children.First() as NamedValue;
            if (includeName == null)
            {
                includeName = null;
                mixinName   = null;
                return(false);
            }
            mixinName = definition.Name.Children.First() as NamedValue;
            if (mixinName == null)
            {
                includeName = null;
                mixinName   = null;
                return(false);
            }

            if (includeName.Text != mixinName.Text)
            {
                includeName = null;
                mixinName   = null;
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        private bool TryMatchMethodCalls(
            MixinDefinition definition,
            out MethodCall includeMethodCall,
            out MethodCall mixinSignature)
        {
            includeMethodCall = Name.Children.First() as MethodCall;
            if (includeMethodCall == null)
            {
                includeMethodCall = null;
                mixinSignature    = null;
                return(false);
            }
            mixinSignature = definition.Name.Children.First() as MethodCall;
            if (mixinSignature == null)
            {
                includeMethodCall = null;
                mixinSignature    = null;
                return(false);
            }

            if (includeMethodCall.Name != mixinSignature.Name)
            {
                includeMethodCall = null;
                mixinSignature    = null;
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        internal static bool TryParse(Node parent, Queue <Word> remainingWords, out Script script)
        {
            script = new Script(parent);

            do
            {
                MixinDefinition mixinDefinition;
                if (MixinDefinition.TryParse(script, remainingWords, out mixinDefinition))
                {
                    script.Children.Add(mixinDefinition);
                    continue;
                }

                VariableAssignment variableAssignment;
                if (VariableAssignment.TryParse(script, remainingWords, out variableAssignment))
                {
                    script.Children.Add(variableAssignment);
                    continue;
                }

                Selector selector;
                if (Selector.TryParse(script, remainingWords, out selector))
                {
                    script.Children.Add(selector);
                    continue;
                }
            }while (remainingWords.Count > 0);

            return(true);
        }
Ejemplo n.º 5
0
 public override Node Clone(Node newParent)
 {
     var newDefinition = new MixinDefinition(newParent);
     newDefinition.Name = (Expression)Name.Clone(newDefinition);
     newDefinition.Value = Value.Clone(newDefinition);
     return newDefinition;
 }
Ejemplo n.º 6
0
        public override Node Clone(Node newParent)
        {
            var newDefinition = new MixinDefinition(newParent);

            newDefinition.Name  = (Expression)Name.Clone(newDefinition);
            newDefinition.Value = Value.Clone(newDefinition);
            return(newDefinition);
        }
Ejemplo n.º 7
0
        private void ApplyInclude(MixinDefinition definition)
        {
            var targetBlock = Parent as Block;
            var sourceBlock = definition.Value as Block;

            foreach (var child in sourceBlock.Children)
            {
                var newChild = child.Clone(targetBlock);
                targetBlock.Children.Add(newChild);
            }
        }
Ejemplo n.º 8
0
        internal void TryInclude(MixinDefinition definition)
        {
            MethodCall includeMethodCall;
            MethodCall mixinSignature;
            if (TryMatchMethodCalls(definition, out includeMethodCall, out mixinSignature))
            {
                ApplyMethodCall(definition, includeMethodCall, mixinSignature);
                return;
            }

            NamedValue includeName;
            NamedValue mixinName;
            if (TryMatchNamedValues(definition, out includeName, out mixinName))
            {
                ApplyInclude(definition);
                return;
            }
        }
Ejemplo n.º 9
0
        internal void TryInclude(MixinDefinition definition)
        {
            MethodCall includeMethodCall;
            MethodCall mixinSignature;

            if (TryMatchMethodCalls(definition, out includeMethodCall, out mixinSignature))
            {
                ApplyMethodCall(definition, includeMethodCall, mixinSignature);
                return;
            }

            NamedValue includeName;
            NamedValue mixinName;

            if (TryMatchNamedValues(definition, out includeName, out mixinName))
            {
                ApplyInclude(definition);
                return;
            }
        }
Ejemplo n.º 10
0
        private void ApplyMethodCall(MixinDefinition definition, MethodCall includeMethodCall, MethodCall mixinSignature)
        {
            var targetBlock = Parent as Block;
            var sourceBlock = definition.Value as Block;

            foreach (var child in sourceBlock.Children)
            {
                var newChild = child.Clone(targetBlock);
                targetBlock.Children.Add(newChild);
            }

            var enumerator = new LineDanceEnumerator(
                mixinSignature.Parameters.Children,
                includeMethodCall.Parameters.Children);

            while (enumerator.MoveNext())
            {
                var currents = enumerator.Currents;

                var mixinParameterExpression = currents.First() as Expression;
                var mixinVariable            = mixinParameterExpression.Children.First() as Variable;

                var methodArgumentExpression = currents.Skip(1).First() as Expression;
                var argumentValue            = methodArgumentExpression.Children.First();

                var variableReferences = new List <Node>();
                targetBlock.Find(n => (n is Variable &&
                                       ((Variable)n).Name == mixinVariable.Name),
                                 variableReferences);

                foreach (var variableReference in variableReferences)
                {
                    var variableContainer = variableReference.Parent as Expression;
                    var variableOrdinal   = variableContainer.Children.IndexOf(variableReference);
                    var newValue          = argumentValue.Clone(variableContainer);
                    variableContainer.Children[variableOrdinal] = newValue;
                }
            }
        }
Ejemplo n.º 11
0
        internal static bool TryParse(Node parent, Queue <Word> remainingWords, out MixinDefinition definition)
        {
            definition = null;
            if (remainingWords.Peek().Text != "@mixin")
            {
                return(false);
            }

            definition = new MixinDefinition(parent);
            // remove '@mixin'
            remainingWords.Dequeue();

            var remainingNameWords = new Queue <Word>();

            while (remainingWords.Peek().Text != "{")
            {
                remainingNameWords.Enqueue(remainingWords.Dequeue());
            }

            Expression name;

            if (!Expression.TryParse(definition, remainingNameWords, out name))
            {
                throw new Exception("errp?");
            }
            definition.Name = name;

            Block block;

            if (!Block.TryParse(definition, remainingWords, out block))
            {
                throw new Exception("erh?");
            }
            definition.Value = block;
            return(true);
        }
Ejemplo n.º 12
0
 private void ApplyInclude(MixinDefinition definition)
 {
     var targetBlock = Parent as Block;
     var sourceBlock = definition.Value as Block;
     foreach (var child in sourceBlock.Children)
     {
         var newChild = child.Clone(targetBlock);
         targetBlock.Children.Add(newChild);
     }
 }
Ejemplo n.º 13
0
        private bool TryMatchNamedValues(
            MixinDefinition definition,
            out NamedValue includeName,
            out NamedValue mixinName)
        {
            includeName = Name.Children.First() as NamedValue;
            if (includeName == null)
            {
                includeName = null;
                mixinName = null;
                return false;
            }
            mixinName = definition.Name.Children.First() as NamedValue;
            if (mixinName == null)
            {
                includeName = null;
                mixinName = null;
                return false;
            }

            if (includeName.Text != mixinName.Text)
            {
                includeName = null;
                mixinName = null;
                return false;
            }

            return true;
        }
Ejemplo n.º 14
0
        private bool TryMatchMethodCalls(
            MixinDefinition definition, 
            out MethodCall includeMethodCall, 
            out MethodCall mixinSignature)
        {
            includeMethodCall = Name.Children.First() as MethodCall;
            if (includeMethodCall == null)
            {
                includeMethodCall = null;
                mixinSignature = null;
                return false;
            }
            mixinSignature = definition.Name.Children.First() as MethodCall;
            if (mixinSignature == null)
            {
                includeMethodCall = null;
                mixinSignature = null;
                return false;
            }

            if (includeMethodCall.Name != mixinSignature.Name)
            {
                includeMethodCall = null;
                mixinSignature = null;
                return false;
            }

            return true;
        }
Ejemplo n.º 15
0
        private void ApplyMethodCall(MixinDefinition definition, MethodCall includeMethodCall, MethodCall mixinSignature)
        {
            var targetBlock = Parent as Block;
            var sourceBlock = definition.Value as Block;
            foreach (var child in sourceBlock.Children)
            {
                var newChild = child.Clone(targetBlock);
                targetBlock.Children.Add(newChild);
            }

            var enumerator = new LineDanceEnumerator(
                                    mixinSignature.Parameters.Children,
                                    includeMethodCall.Parameters.Children);

            while (enumerator.MoveNext())
            {
                var currents = enumerator.Currents;

                var mixinParameterExpression = currents.First() as Expression;
                var mixinVariable = mixinParameterExpression.Children.First() as Variable;

                var methodArgumentExpression = currents.Skip(1).First() as Expression;
                var argumentValue = methodArgumentExpression.Children.First();

                var variableReferences = new List<Node>();
                targetBlock.Find(n => (n is Variable &&
                    ((Variable)n).Name == mixinVariable.Name),
                    variableReferences);

                foreach (var variableReference in variableReferences)
                {
                    var variableContainer = variableReference.Parent as Expression;
                    var variableOrdinal = variableContainer.Children.IndexOf(variableReference);
                    var newValue = argumentValue.Clone(variableContainer);
                    variableContainer.Children[variableOrdinal] = newValue;
                }
            }
        }