Beispiel #1
0
 public void Reset()
 {
     CurrentIndex = -1;
     CurrentKey   = String.Empty;
     CurrentValue = PipeValue.Undefined;
     enumerator   = null;
 }
Beispiel #2
0
 public static PipeValue[] CopyAsArray(this PipeValue arr)
 {
     if (arr.IsArray)
     {
         return(((IEnumerable)arr.Value).OfType <object>().Select(v => new PipeValue(v)).ToArray());
     }
     return(new[] { arr });
 }
Beispiel #3
0
 public PipeValue[] TakeArguments(int count)
 {
     PipeValue[] arr = new PipeValue[count];
     for (int i = 0; i < count; i++)
     {
         arr[i] = TakeArgument();
     }
     return(arr);
 }
Beispiel #4
0
 public void Add(PipeValue value, string key)
 {
     if (isArray)
     {
         array.Add(value.Value);
     }
     else
     {
         array.Add(new KeyValuePair <string, object>(key, value.Value));
     }
 }
Beispiel #5
0
        public static PipeValue Map(this PipeValue value, PipeLambda map)
        {
            CommonHelper.ConfirmNotNull(map, "map");
            PipeValuePropertyEnumerator enumerator = value.GetEnumerator();
            PipeValueObjectBuilder      collection = new PipeValueObjectBuilder(value.IsArray);

            while (enumerator.MoveNext())
            {
                collection.Add(map.Invoke(enumerator), enumerator.CurrentKey);
            }
            return(collection);
        }
Beispiel #6
0
 protected override PipeValue InvokeInternal(PipeValue obj, PipeValue index)
 {
     try {
         Hashtable ht = new Hashtable {
             { index.ToString(), obj }
         };
         IEnumerator enumerator = new PipeValue(ht).GetEnumerator();
         enumerator.MoveNext();
         pipe.context.PushObjectStack(enumerator);
         return(pipe.Evaluate());
     } finally {
         pipe.context.PopObjectStack();
     }
 }
Beispiel #7
0
        public static PipeValue First(this PipeValue value, PipeLambda fn, bool returnBoolean = false, bool negate = false)
        {
            CommonHelper.ConfirmNotNull(fn, "fn");
            PipeValuePropertyEnumerator enumerator = value.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (negate ^ (bool)fn.Invoke(enumerator))
                {
                    return(returnBoolean ? true : enumerator.CurrentValue);
                }
            }
            return(returnBoolean ? false : PipeValue.Undefined);
        }
Beispiel #8
0
 public bool MoveNext()
 {
     if (enumerator == null)
     {
         enumerator = GetEnumerator();
     }
     if (enumerator.MoveNext())
     {
         CurrentIndex++;
         CurrentKey   = (string)enumerator.Current;
         CurrentValue = GetValue(enumerator);
         return(true);
     }
     return(false);
 }
Beispiel #9
0
        public static PipeValue Where(this PipeValue value, PipeLambda filter)
        {
            CommonHelper.ConfirmNotNull(filter, "filter");
            PipeValuePropertyEnumerator enumerator = value.GetEnumerator();
            PipeValueObjectBuilder      collection = new PipeValueObjectBuilder(value.IsArray);

            while (enumerator.MoveNext())
            {
                if ((bool)filter.Invoke(enumerator))
                {
                    collection.Add(enumerator.CurrentValue, enumerator.CurrentKey);
                }
            }
            return(collection);
        }
Beispiel #10
0
        internal PipeValue Evaluate()
        {
            bool             isValid;
            PipeValue        input       = ObjectPath.Empty.Evaluate(context);
            PipeValue        value       = (pipe.Count > start ? pipe[start].ObjectPath : ObjectPath.Empty).Evaluate(context, true, out isValid);
            List <PipeValue> returnArray = new List <PipeValue>();

            i = start + (isValid ? 1 : 0);
            while (i <= end)
            {
                int    startpos = i;
                string name     = pipe[i++].TextValue;
                if (name == "|")
                {
                    returnArray.Add(value);
                    value = HasArgument(true) ? TakeArgument() : input;
                }
                else if (name == "&&" || name == "||")
                {
                    if ((bool)value ^ (name == "&&"))
                    {
                        break;
                    }
                    PipeLambda fn = TakeFunction();
                    if (fn != null)
                    {
                        value = fn.Invoke(input);
                    }
                    else if (HasArgument(true))
                    {
                        value = TakeArgument();
                    }
                    else
                    {
                        value = input;
                    }
                }
                else
                {
                    this.Value = value;
                    try {
                        PipeFunction fn;
                        try {
                            fn = context.ResolveFunction(name);
                        } catch (InvalidPipeFunctionException) {
                            if (startpos == start)
                            {
                                value = PipeValue.Undefined;
                                continue;
                            }
                            throw;
                        }
                        value = fn.Invoke(this);
                    } catch (Exception ex) {
                        if (ex is TargetInvocationException)
                        {
                            ex = ex.InnerException;
                        }
                        PipeExecutionException wrappedException = new PipeExecutionException(ex.Message, ex, new WaterpipeException.CallSite {
                            InputString    = context.InputString,
                            ConstructStart = pipe.StartIndex,
                            ConstructEnd   = pipe.EndIndex,
                            HighlightStart = pipe[startpos].StartIndex,
                            HighlightEnd   = pipe[i - 1].EndIndex
                        });
                        context.AddException(wrappedException);
                    }
                }
            }
            if (returnArray.Count > 0)
            {
                returnArray.Add(value);
                return(new PipeValueObjectBuilder(returnArray, true));
            }
            return(value);
        }
Beispiel #11
0
        public PipeLambda TakeArgumentAsConstantFunction()
        {
            PipeValue value = TakeArgument();

            return(new PipeLambda((x, y) => value));
        }
Beispiel #12
0
 protected virtual PipeValue InvokeInternal(PipeValue obj, PipeValue index)
 {
     return(fn(obj, index));
 }
Beispiel #13
0
 public PipeValue Invoke(PipeValue obj, PipeValue index)
 {
     return(InvokeInternal(obj, index));
 }
Beispiel #14
0
 public PipeValue Invoke(PipeValue obj)
 {
     return(InvokeInternal(obj, PipeValue.Undefined));
 }