Exemplo n.º 1
0
        protected ParserField CreateField(string aRegEx, string aFieldName, TParserValueType aValueType, int aCapturePos, int aCaptureLength, bool aRequiresNumberedCaptureGroup)
        {
            string      fieldName = CompressName(aFieldName);
            ParserField ret       = new ParserField(fieldName);

            // Update format specifier
            ParserFieldFormatSpecifier formatSpecifier = ret.FormatSpecifier;

            formatSpecifier.OriginalLocation = aCapturePos;
            formatSpecifier.OriginalLength   = aCaptureLength;

            // Surround in numbered group if needed...
            StringBuilder finalRegEx = new StringBuilder(aRegEx);

            if (aRequiresNumberedCaptureGroup)
            {
                finalRegEx.Insert(0, "(");
                finalRegEx.Append(")");
            }

            formatSpecifier.RegularExpressionString = finalRegEx.ToString();
            formatSpecifier.ExpectedType            = aValueType;

            return(ret);
        }
        internal virtual void SetValue(ParserFieldFormatSpecifier aFormat, ParserFieldFormatValue aValue)
        {
            ValueStore vs = GetValueStore(this);

            if (vs == null)
            {
                // Make a new "store internally" value store
                iValueStore = new ValueStore();
                vs          = iValueStore;
            }
            //
            vs.SetValue(aFormat, aValue);
        }
 internal ParserField(ParserField aField)
     : this(aField.Name)
 {
     iFormatSpecifier = new ParserFieldFormatSpecifier(this, aField.FormatSpecifier);
     iFormatValue     = new ParserFieldFormatValue(this, aField.FormatValue);
 }
 public ParserField(string aName)
     : base(aName)
 {
     iFormatSpecifier = new ParserFieldFormatSpecifier(this);
     iFormatValue     = new ParserFieldFormatValue(this);
 }
Exemplo n.º 5
0
        internal void SetValue(ParserFieldFormatSpecifier aFieldFormatSpecifier, ParserFieldFormatValue aFieldFormatValue)
        {
            CheckForValidValueStore();
            //
            if (iValueStoreType == TValueStoreType.EValueStoreTypeProperty)
            {
                // Store value to user-supplied property within object
                Binder       binder   = null;
                Type         typeInfo = iValueStore.GetType();
                PropertyInfo propInfo = typeInfo.GetProperty(iValueStoreMemberName, PropertyBindingFlags);
                if (propInfo == null)
                {
                    throw new MissingMemberException("A property called: \"" + iValueStoreMemberName + "\" was not found within object: " + iValueStore.ToString());
                }
                //
                Type     propType = propInfo.PropertyType;
                object[] args     = PrepareMethodArgument(aFieldFormatValue.Value, propType);
                typeInfo.InvokeMember(iValueStoreMemberName, PropertyBindingFlags, binder, iValueStore, args);
            }
            else if (iValueStoreType == TValueStoreType.EValueStoreTypeMethod)
            {
                object[] args = null;
                //
                try
                {
                    // Store value to user-supplied method
                    Binder binder = null;

                    // Build arguments
                    Type valueTypeInfo = aFieldFormatValue.Value.GetType();
                    TValueStoreMethodArguments[] argumentSpecification = BuildMethodArgumentSpecification(valueTypeInfo, iValueStore, iValueStoreMemberName);
                    args = BuildCustomFunctionArguments(argumentSpecification, aFieldFormatSpecifier, aFieldFormatValue);

                    // Sanity check number of arguments for method implementation actually agrees with our run-time generated
                    // object array of parameters.
                    Type            valueStoreTypeInfo = iValueStore.GetType();
                    MethodInfo      methodInfo         = valueStoreTypeInfo.GetMethod(iValueStoreMemberName, MethodBindingFlags);
                    ParameterInfo[] methodParams       = methodInfo.GetParameters();
                    if (args.Length != methodParams.Length)
                    {
                        throw new MissingMethodException("Argument specification doesn't align with method implementation");
                    }
                    else
                    {
                        valueStoreTypeInfo.InvokeMember(iValueStoreMemberName, MethodBindingFlags, binder, iValueStore, args);
                    }
                }
                catch (Exception exception)
                {
                    if (exception is TargetInvocationException ||
                        exception is MissingMethodException ||
                        exception is MissingMemberException ||
                        exception is AmbiguousMatchException)
                    {
                        StringBuilder funcDetails = new StringBuilder();
                        funcDetails.Append(iValueStoreMemberName + "( ");
                        //
                        int count = (args != null) ? args.Length : 0;
                        for (int i = 0; i < count; i++)
                        {
                            object arg         = args[i];
                            string argTypeName = (arg != null) ? arg.GetType().ToString() : "null";
                            funcDetails.Append(argTypeName);
                            //
                            if (i < count - 1)
                            {
                                funcDetails.Append(", ");
                            }
                        }
                        //
                        funcDetails.Append(" )");
                        System.Diagnostics.Debug.WriteLine("Failed to invoke method: " + funcDetails.ToString());
                    }
                    else
                    {
                        throw exception;
                    }
                }
            }
            else if (iValueStoreType == TValueStoreType.EValueStoreTypeStoreInternally)
            {
                // Store it in the value store and the client can extract it via the public properties...
                iValueStore = aFieldFormatValue.Value;
            }
        }
Exemplo n.º 6
0
        private object[] BuildCustomFunctionArguments(TValueStoreMethodArguments[] aArgumentSpecification, ParserFieldFormatSpecifier aFieldFormatSpecifier, ParserFieldFormatValue aFieldFormatValue)
        {
            List <object> args = new List <object>();

            //
            foreach (TValueStoreMethodArguments argType in aArgumentSpecification)
            {
                if (argType == TValueStoreMethodArguments.EValueStoreMethodArgumentNameAsString)
                {
                    args.Add(aFieldFormatSpecifier.Name.ToString());
                }
                else if (argType == TValueStoreMethodArguments.EValueStoreMethodArgumentNameAsObject)
                {
                    args.Add(aFieldFormatSpecifier.Name);
                }
                else if (argType == TValueStoreMethodArguments.EValueStoreMethodArgumentValue)
                {
                    args.Add(aFieldFormatValue.Value);
                }
                else if (argType == TValueStoreMethodArguments.EValueStoreMethodArgumentParagraph)
                {
                    ParserParagraph para = null;
                    //
                    if (aFieldFormatSpecifier.Field.Parent != null && aFieldFormatSpecifier.Field.Parent is ParserLine)
                    {
                        ParserLine line = (ParserLine)aFieldFormatSpecifier.Field.Parent;
                        if (line.Parent != null && line.Parent is ParserParagraph)
                        {
                            para = (ParserParagraph)line.Parent;
                        }
                    }
                    //
                    args.Add(para);
                }
                else if (argType == TValueStoreMethodArguments.EValueStoreMethodArgumentLine)
                {
                    ParserLine line = null;
                    //
                    if (aFieldFormatSpecifier.Field.Parent != null && aFieldFormatSpecifier.Field.Parent is ParserLine)
                    {
                        line = (ParserLine)aFieldFormatSpecifier.Field.Parent;
                    }
                    //
                    args.Add(line);
                }
                else if (argType == TValueStoreMethodArguments.EValueStoreMethodArgumentField)
                {
                    args.Add(aFieldFormatSpecifier.Field);
                }
                else
                {
                    System.Diagnostics.Debug.Assert(false, "Argument specification contains unresolved runtime reference");
                }
            }
            //
            return(args.ToArray());
        }