示例#1
0
        public override ParseTree Parse(Lexer lexer, ParserState state)
        {
            int start = lexer.Position;

            if (type == null)
            {
                throw new Exception();
            }

            if (state.RuntimeState.Runtime.TraceParser)
            {
                state.RuntimeState.Runtime.ParseTrace.Enter(this,
                                                            lexer.CurrentSource(),
                                                            "User defined node " + TypeNames.GetName(type));
            }

            object instance    = NewNode.New(state.RuntimeState, type, null);
            object parseMethod = MemberNode.GetMember(instance, "Parse", true);
            object result      = CallNode.Call(state.RuntimeState, parseMethod,
                                               new object[] { lexer });

            if (result == null)
            {
                result = ParseTree.Yes;
            }
            else if (result is bool)
            {
                if ((bool)result)
                {
                    result = ParseTree.Yes;
                }
                else
                {
                    result = ParseTree.No;
                }
            }

            if (state.RuntimeState.Runtime.TraceParser)
            {
                if (result == ParseTree.No)
                {
                    state.RuntimeState.Runtime.ParseTrace.No(this,
                                                             lexer.SourceFrom(start));
                }
                else
                {
                    state.RuntimeState.Runtime.ParseTrace.Yes(this,
                                                              lexer.SourceFrom(start));
                }
            }

            return((ParseTree)result);
        }
示例#2
0
        public ParseTree Instantiate(Lexer lexer, ParserState state,
                                     int start, ParseTree tree)
        {
            Type[] parameterTypes = new Type[1 + Fields.Count];

            parameterTypes[0] = typeof(Source);

            for (int n = 1; n < parameterTypes.Length; n++)
            {
                parameterTypes[n] = typeof(object);
            }

            ConstructorInfo constructor = Type.GetConstructor(parameterTypes);

            if (constructor == null)
            {
                throw new Exception("Couldn't find constructor for "
                                    + Type.FullName);
            }

            object[] parameterValues = new object[parameterTypes.Length];

            parameterValues[0] = lexer.SourceFrom(start);

            for (int n = 0; n < Fields.Count; n++)
            {
                object fieldValue = null;
                tree.Fields.TryGetValue(Fields[n], out fieldValue);

                parameterValues[n + 1] = fieldValue;
            }

            RuntimeObject instance =
                (RuntimeObject)constructor.Invoke(parameterValues);

            object[] parsedParameters = {};

            object parsedMethod = MemberNode.GetMember(instance, "Parsed",
                                                       parsedParameters, false);

            if (parsedMethod != null)
            {
                CallNode.Call(state.RuntimeState, parsedMethod,
                              parsedParameters);
            }

            return(new ParseTree(instance));
        }