Exemplo n.º 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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 3
0
        public void Import(string fileName)
        {
            if (!hasBeenSetUp)
            {
                throw new Exception();
            }

            fileName = pathResolver.Resolve(fileName);

            string absoluteFileName = Path.GetFullPath(fileName);

            if (imported.Contains(absoluteFileName))
            {
                return;
            }

            if (verbose)
            {
                Console.WriteLine("importing " + fileName);
            }

            imported.Add(absoluteFileName);

            if (Path.GetExtension(fileName).ToLower() == ".dll")
            {
                Assembly.LoadFrom(fileName);
            }
            else
            {
                pathResolver.PushDirectory(fileName);

                Lexer        lexer        = new Lexer(parseTrace, fileName);
                RuntimeState runtimeState = new RuntimeState(this, rootModule);

                currentLexer = lexer;

                Pattern oldRoot = grammar.RootPattern;

                try
                {
                    string extension = Path.GetExtension(fileName).ToLower();

                    if ((extension != "") && (extension != ".kat"))
                    {
                        IDictionary languages = (IDictionary)rootModule.GetName("languages");
                        object      callable  = languages[extension];
                        Type        rootType  = (Type)CallNode.Call(runtimeState, callable, null);

                        grammar.RootPattern = Pattern.PatternForType(rootType);
                    }

                    grammar.Parse(lexer, runtimeState);
                }
                catch (Exception exception)
                {
                    if (runtimeState.RunningSource != null)
                    {
                        Console.WriteLine(runtimeState.RunningSource);
                    }

                    throw exception;
                }
                finally
                {
                    grammar.RootPattern = oldRoot;
                }

                pathResolver.PopDirectory();
            }
        }