コード例 #1
0
        private void P_EndBlock()
        {
            if (P_TableStack.Count > 0)
            {
                P_ThrowError(1);
            }

            // Output
            if (P_ErrorCode == 0)
            {
                _output.Add(P_ValueParser_TakeValue());
            }
            else
            {
                TableValue TableResult = new TableValue();
                TableResult[1] = new StringValue("Error");
                TableResult[2] = new IntValue(P_ErrorCode);
                _output.Add(TableResult);
            }

            // Clear up broken business
            P_TableStack.Clear();
            P_ValueParser_StringMode   = false;
            P_CharEscapedInString      = false;
            P_ValueParser_ValueAtReady = null;
            P_ValueParser_ValueRaw.Clear();
            P_ValueParser_ValueType = -1;

            P_ErrorCode = 0;

            // Purge the last block from the input stream to save memory.
            _input     = _input.Substring(P_Position);
            P_Position = 0;
        }
コード例 #2
0
        public static string EncodeTable(TableValue Table)
        {
            StringBuilder r = new StringBuilder();

            // Turn everything into text.
            r.Append('[');
            List <IAbstractValue> processedKeys = new List <IAbstractValue>();

            for (int i = 1; Table.ContainsKey(i); i++)
            {
                IAbstractValue item = Table[i];
                r.Append(EncoderStream.EncodeValue(item));
                r.Append(';');
                processedKeys.Add(new IntValue(i));
                item = null;
            }
            foreach (KeyValuePair <IAbstractValue, IAbstractValue> item2 in Table.Dictionary)
            {
                if (!processedKeys.Contains(item2.Key))
                {
                    r.Append(item2.Key.EncodeIntoValue());
                    r.Append(':');
                    IAbstractValue v = item2.Value;
                    r.Append(EncoderStream.EncodeValue(v));
                    r.Append(';');
                }
            }
            r.Append(']');

            return(r.ToString());
        }
コード例 #3
0
 private void P_ValueParser_OutputValue(IAbstractValue Value)
 {
     P_ValueParser_ValueRaw.Clear();
     P_ValueParser_ValueAtReady = Value;
     P_ValueParser_ValueType    = -2;
     P_ValueParser_ValueIsReady = true;
 }
コード例 #4
0
        /// <summary>
        /// Visits the call.
        /// </summary>
        /// <param name="destination">The destination.</param>
        /// <param name="receiver">The receiver.</param>
        /// <param name="callee">The callee.</param>
        /// <param name="arguments">The arguments.</param>
        /// <param name="isVirtualCall">if set to <c>true</c> [is virtual call].</param>
        /// <param name="programContext">The program context.</param>
        /// <param name="stateBeforeInstruction">The state before instruction.</param>
        /// <param name="stateAfterInstruction">The state after instruction.</param>
        public override void VisitCall(
            Variable destination,
            Variable receiver,
            Method callee,
            ExpressionList arguments,
            bool isVirtualCall,
            Microsoft.Fugue.IProgramContext programContext,
            Microsoft.Fugue.IExecutionState stateBeforeInstruction,
            Microsoft.Fugue.IExecutionState stateAfterInstruction)
        {
            if ((callee.DeclaringType.GetRuntimeType() == typeof(X509ServiceCertificateAuthentication) ||
                 callee.DeclaringType.GetRuntimeType() == typeof(X509ClientCertificateAuthentication)) &&
                (callee.Name.Name.Equals("set_CertificateValidationMode", StringComparison.InvariantCultureIgnoreCase)))
            {
                IAbstractValue value    = stateBeforeInstruction.Lookup((Variable)arguments[0]);
                IIntValue      intValue = value.IntValue(stateBeforeInstruction);

                if (intValue != null)
                {
                    X509CertificateValidationMode mode = (X509CertificateValidationMode)intValue.Value;
                    if (mode != X509CertificateValidationMode.ChainTrust)
                    {
                        Resolution resolution = base.GetResolution(mode.ToString(),
                                                                   X509CertificateValidationMode.ChainTrust.ToString());
                        Problem problem = new Problem(resolution, programContext);
                        base.Problems.Add(problem);
                    }
                }
            }

            base.VisitCall(destination, receiver, callee, arguments, isVirtualCall, programContext, stateBeforeInstruction, stateAfterInstruction);
        }
コード例 #5
0
        /// <summary>
        /// Called internally to bring a background element into the current state
        /// </summary>
        /// <param name="obj">The element in import</param>
        public static void FinalizeImport(IComparable obj)
        {
            IAbstractValue val = obj as IAbstractValue;

            if (null != val)
            {
                val.FinalizeImport();
            }
        }
コード例 #6
0
 public IAbstractValue this[IAbstractValue key]
 {
     get
     {
         IAbstractValue r = null;
         Dictionary.TryGetValue(key, out r);
         return(r);
     }
     set { Dictionary[key] = value; }
 }
コード例 #7
0
 public static string EncodeValue(IAbstractValue Value)
 {
     if (Value != null)
     {
         return(Value.EncodeIntoValue());
     }
     else
     {
         return(EncodeNull());
     }
 }
コード例 #8
0
 public bool Compare(IAbstractValue OtherValue)
 {
     if (OtherValue != null && OtherValue.ValueType == this.ValueType)
     {
         return(Value == ((IntValue)OtherValue).Value);
     }
     else
     {
         return(false);
     }
 }
コード例 #9
0
 private IAbstractValue P_ValueParser_TakeValue()
 {
     if (P_ValueParser_ValueIsReady)
     {
         IAbstractValue r = P_ValueParser_ValueAtReady;
         P_ValueParser_ValueAtReady = null;
         return(r);
     }
     else
     {
         throw new Exception("Can't take a finished value if it's not there. Use P_ValueParser_ValueIsReady to check first.");
     }
 }
コード例 #10
0
        public static Term GetTerm(IComparable /*?*/ value)
        {
            // Case 1: null value
            if (value == null)
            {
                return(nullSymbol);
            }

            // Case 2: Literal value (int, string, bool, etc.)
            Type t = value.GetType();

            if (GetLiteralTypes().ContainsKey(t))
            {
                return(new Literal(value));
            }

            // Case 3: enum value-- encode as sort applied to string arg
            else if (value is System.Enum)
            {
                Symbol sort = TypeSort(value.GetType());

                // to do: also handle Flags attribute for enums by encoding value as a set of strings
                string label = value.ToString();
                return(new CompoundTerm(sort, new Literal(label)));
            }

            // Case 4: Term value-- return quoted term
            else if (value is Term)
            {
                return(new CompoundTerm(quoteSymbol, (Term)value));
            }

            // Case 5: value is of type IAbstractValue-- invoke property to get term
            else
            {
                IAbstractValue av = value as IAbstractValue;
                if (av != null)
                {
                    return(av.AsTerm);
                }
                else
                {
                    // Case 6: fail
                    throw new ArgumentException("AbstractValue.GetTerm(): No term can be produced for value " + value.ToString() + ". Type must implement IAbstractValue interface.");
                }
            }
        }
コード例 #11
0
        public bool Compare(TableValue OtherTable)
        {
            TableValue that = OtherTable; // It's weird to use this as a parameter name.

            // Figure out whether they match in size. If it's not, immediate false.
            if (this.Dictionary.Count != that.Dictionary.Count)
            {
                return(false);
            }

            // Compare the Tables!
            foreach (KeyValuePair <IAbstractValue, IAbstractValue> item in this.Dictionary)
            {
                IAbstractValue v = null;
                if (!(that.Dictionary.TryGetValue(item.Key, out v) && v == item.Value)) // Out v?
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #12
0
        /// <summary>
        /// Determines if this value has an object id (that is, is of type <see cref="LabeledInstance"/>),
        /// or has a subvalue that has an object id (for example, a set of instances).
        /// </summary>
        /// <returns>True if this value has an object id or contains a value with an object id.</returns>
        /// <remarks> This method is invoked by the modeling library when determining whether two states are isomorphic.
        /// </remarks>
        public static bool ContainsObjectIds(IComparable obj)
        {
            if (null == obj)
            {
                return(false);
            }
            Type t = obj.GetType();

            if (GetLiteralTypes().ContainsKey(t) || t.IsEnum)
            {
                return(false);
            }
            IAbstractValue av = obj as IAbstractValue;

            if (null == av)
            {
                return(false);             // maybe throw exception?
            }
            else
            {
                return(av.ContainsObjectIds());
            }
        }
コード例 #13
0
 public bool ContainsKey(IAbstractValue key)
 {
     return(Dictionary.ContainsKey(key));
 }
コード例 #14
0
        private bool P_Step()
        {
            bool r = P_Position < _input.Length;

            if (r)
            {
                char         c = _input[P_Position];
                int          TableStackUBound = P_TableStack.Count - 1;
                P_TableLevel CurrentTable     = null;
                bool         EndingBlock      = false;

                if (P_TableStack.Count > 0)
                {
                    CurrentTable = P_TableStack[TableStackUBound];
                }

                // To know when to end a stream block
                #region BlockEnder
                if (c == '|')
                {
                    P_FailSafeEndCharCount++;
                    if (!P_CharEscapedInString)
                    {
                        P_EndCharCount++;
                    }
                    if (P_EndCharCount == 2 | P_FailSafeEndCharCount == 2)
                    {
                        EndingBlock = true;
                    }
                }
                else
                {
                    if (P_EndCharCount == 1)
                    {
                        P_ThrowError(1);
                    }
                    P_FailSafeEndCharCount = 0;
                    P_EndCharCount         = 0;
                }
                #endregion

                if (P_ErrorCode == 0) // If no error then...
                {
                    bool CharIsHandled         = false;
                    bool String_IsFirstBracket = false;

                    #region Value Type Determiner
                    if (P_ValueParser_ValueType == -1)
                    {
                        switch (c)
                        {
                        case 'T':
                        case 'F':
                            P_ValueParser_ValueType = 1;     // Boolean
                            CharIsHandled           = true;
                            break;

                        case 'N':
                            P_ValueParser_ValueType = 0;     // null
                            CharIsHandled           = true;
                            break;

                        case '\"':
                            P_ValueParser_ValueType  = 4;    // String
                            String_IsFirstBracket    = true;
                            P_ValueParser_StringMode = true;
                            CharIsHandled            = true;
                            break;

                        case '[':
                            P_TableLevel NewTableLevel = new P_TableLevel();
                            NewTableLevel.Table = new TableValue();
                            P_TableStack.Add(NewTableLevel);
                            CharIsHandled = true;
                            break;

                        default:
                            if (P_IsCharNumber(c))
                            {
                                P_ValueParser_ValueType = 2;     // Int
                                CharIsHandled           = true;
                            }

                            break;
                        }
                    }
                    #endregion

                    #region Value Extractor
                    // Parse the value.
                    if (P_ValueParser_ValueType >= 0)
                    {
                        switch (P_ValueParser_ValueType)
                        {
                        case 0:     // null
                        case 1:     // Boolean
                            if (P_ValueParser_ValueRaw.Length == 0)
                            {
                                P_ValueParser_ValueRaw.Append(c);
                            }
                            else
                            {
                                IAbstractValue o = null;
                                switch (P_ValueParser_ValueRaw[0])
                                {
                                case 'N':
                                    o = null;
                                    break;

                                default:
                                    o = new BooleanValue(P_ValueParser_ValueRaw[0] == 'T');
                                    break;
                                }
                                P_ValueParser_OutputValue(o);
                            }
                            CharIsHandled = true;
                            break;

                        case 2:     // Int
                        case 3:     // Float
                            if (P_IsCharNumber(c))
                            {
                                P_ValueParser_ValueRaw.Append(c);
                            }
                            else if (c == '.')
                            {
                                if (P_ValueParser_ValueType == 2)
                                {
                                    P_ValueParser_ValueType = 3;
                                    P_ValueParser_ValueRaw.Append(c);
                                }
                                else
                                {
                                    P_ThrowError(1);     // There should not be two decimals in the same number.
                                }
                            }
                            else if (P_ValueParser_ValueType == 2)
                            {
                                P_ValueParser_OutputValue(new IntValue(int.Parse(P_ValueParser_ValueRaw.ToString())));
                            }
                            else if (P_ValueParser_ValueType == 3)
                            {
                                P_ValueParser_OutputValue(new FloatValue(JsEncoder_Type_Float.Parse(P_ValueParser_ValueRaw.ToString())));
                            }
                            CharIsHandled = true;
                            break;

                        case 4:     // String
                            if (P_ValueParser_StringMode)
                            {
                                bool String_RecordChar = true;     // False to avoid recording current character into a string.

                                if (!P_CharEscapedInString)
                                {
                                    if (c == '\\')
                                    {
                                        String_RecordChar     = false;
                                        P_CharEscapedInString = true;
                                    }
                                    else if (c == '\"')
                                    {
                                        String_RecordChar = false;
                                        if (!String_IsFirstBracket)
                                        {
                                            P_ValueParser_StringMode = false;
                                            P_ValueParser_OutputValue(new StringValue(P_ValueParser_ValueRaw.ToString()));
                                        }
                                    }
                                }
                                else
                                {
                                    P_CharEscapedInString = false;
                                }

                                if (String_RecordChar)
                                {
                                    P_ValueParser_ValueRaw.Append(c);
                                }
                            }
                            CharIsHandled = true;
                            break;
                        }
                    }
                    #endregion

                    #region Table Parser
                    if (!P_ValueParser_StringMode && CurrentTable != null) // Only process these when not in a string.
                    {
                        int EndType = 0;
                        switch (c)
                        {
                        case ':':
                            if (!CurrentTable.PairHasCustomKey)
                            {
                                CurrentTable.PairHasCustomKey = true;
                                CurrentTable.V1         = P_ValueParser_TakeValue();
                                P_ValueParser_ValueType = -1;
                            }
                            else
                            {
                                P_ThrowError(1);
                            }
                            CharIsHandled = true;
                            break;

                        case ']':
                            EndType       = 2;
                            CharIsHandled = true;
                            break;

                        case ';':
                            EndType       = 1;
                            CharIsHandled = true;
                            break;
                        }
                        if (EndType > 0)
                        {
                            if (P_ValueParser_ValueType != -1)
                            {
                                CurrentTable.V2 = P_ValueParser_TakeValue();

                                if (CurrentTable.V2 != null)
                                {
                                    if (CurrentTable.V1 != null)
                                    {
                                        CurrentTable.Table[CurrentTable.V1] = CurrentTable.V2;
                                    }
                                    else
                                    {
                                        CurrentTable.IndexingUBound++;
                                        CurrentTable.Table[CurrentTable.IndexingUBound] = CurrentTable.V2;
                                    }
                                }
                            }

                            // Clear these so we can use them later.
                            CurrentTable.V1 = null;
                            CurrentTable.V2 = null;
                            CurrentTable.PairHasCustomKey = false;

                            if (EndType == 2)
                            {
                                // Close table and go back up.
                                P_TableStack.RemoveAt(TableStackUBound);
                                P_ValueParser_OutputValue(CurrentTable.Table);
                                P_ValueParser_ValueType = -2;
                            }
                            else //if (EndType == 1)
                            {
                                P_ValueParser_ValueType = -1;
                            }
                        }
                    }
                    #endregion

                    #region Character Ignore Handler
                    if (!CharIsHandled && (c == ' ' | c == '|' | c == '\n'))
                    {
                        CharIsHandled = true;
                    }
                    #endregion

                    // If nothing handled this character, it must be an error.
                    if (!CharIsHandled)
                    {
                        P_ThrowError(1);
                    }
                }

                P_Position++;

                if (EndingBlock)
                {
                    P_EndBlock();
                }
            }
            return(r);
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: JustinLaw64/Js-Chatterbox
        static void Main(string[] args)
        {
            #region Test1

            IAbstractValue[] Test1_InitialDataArray = new IAbstractValue[] { new IntValue(685000), new StringValue("Hello!") };
            TableValue       Test1_InitialData      = TableValue.ArrayToTable(Test1_InitialDataArray);

            Console.WriteLine("Begin Test 1");
            Console.WriteLine();

            EncoderStream Test1_EnStream = new EncoderStream();
            string        Test1_EncodeResult;

            Console.Write("Encoding...");
            Test1_EnStream.InputValue(Test1_InitialData);
            Test1_EncodeResult = Test1_EnStream.PopOutput();
            Console.WriteLine(" Done!");
            Console.WriteLine(string.Concat("Encode Result: ", Test1_EncodeResult));

            Console.WriteLine();
            Console.WriteLine("End Test 1");

            #endregion

            Console.WriteLine();

            #region Test2

            // The string that was in the Specification.odt document.
            string Test2_InitialString = "[\"My name is PC!\";800;]||[\"Data1\":0.56;\"Data2\":\"true \\| false\";]||[[\"My home.\";T;1000;F;];]||";

            Console.WriteLine("Begin Test 2");
            Console.WriteLine();
            Console.WriteLine(string.Concat("Initial String: ", Test2_InitialString));
            Console.WriteLine();

            DecoderStream    Test2_DeStream = new DecoderStream();
            EncoderStream    Test2_EnStream = new EncoderStream();
            IAbstractValue[] Test2_DecodeResult;
            string           Test2_EncodeResult;

            Console.Write("Decoding...");
            Test2_DeStream.InputValue(Test2_InitialString);
            Test2_DeStream.RunParser();
            Test2_DecodeResult = Test2_DeStream.PopOutput(); // ((TableValue)Test2_DeStream.PopOutput()[0]).Value;
            Console.WriteLine(" Done!");
            Console.Write("Encoding...");
            foreach (var item in Test2_DecodeResult)
            {
                Test2_EnStream.InputValue(item);
            }
            Test2_EncodeResult = Test2_EnStream.PopOutput();
            Console.WriteLine(" Done!");

            Console.WriteLine();
            Console.WriteLine(string.Concat("Encode Result: ", Test2_EncodeResult));
            Console.WriteLine(string.Concat("Strings Match: ", (Test2_InitialString == Test2_EncodeResult).ToString()));
            Console.WriteLine();
            Console.WriteLine("End Test 2");

            #endregion

            Console.WriteLine();
            Console.WriteLine("Exiting Program...");

            return;
        }
 public IAbstractValue GetContents(IAbstractValue value)
 {
     throw new Exception("The method or operation is not implemented.");
 }
 public IAbstractValue GetField(IAbstractValue value, Field field)
 {
     throw new Exception("The method or operation is not implemented.");
 }
 public IAbstractValue GetContents(IAbstractValue value)
 {
     throw new Exception("The method or operation is not implemented.");
 }
 public IAbstractValue GetField(IAbstractValue value, Field field)
 {
     throw new Exception("The method or operation is not implemented.");
 }
コード例 #20
0
 public void InputValue(IAbstractValue Value)
 {
     _output.Append(EncodeValue(Value));
     _output.Append("||");
     _HasOutput = true;
 }