Example #1
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("start of any --------------------");

            BlockLabel returnLabel = new BlockLabel("return");

            // todo enter

            block.BeginScope();

            block.LoadLexer();
            block.Call(typeof(Lexer).GetMethod("Peek", new Type[] {}));

            BlockLabel failed = new BlockLabel("failed");

            block.BranchIfFalse(failed);

            block.LoadLexer();
            block.Call(typeof(Lexer).GetMethod("Read"));

            // todo yes

            if (state.BuildTextNodes)
            {
                block.Call(typeof(Convert).GetMethod("ToString", new Type[] { typeof(char) }));
                block.New(typeof(ParseTree).GetConstructor(new Type[] { typeof(object) }));
            }
            else
            {
                block.Pop();
                block.LoadYes();
            }

            block.Branch(returnLabel);

            block.MarkLabel(failed);

            // todo no

            block.LoadNo();

            block.MarkLabel(returnLabel);

            block.EndScope();

            block.Comment("end of any --------------------");

            return(block);
        }
Example #2
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("start of rep " + reps.Name + " --------------------");

            // todo enter

            block.BeginScope();

            BlockLabel returnLabel = new BlockLabel("return");

            BlockLocal start = block.SavePosition();

            block.NewParseTree();

            BlockLabel reiterate = null;
            BlockLocal matched   = null;

            if (!reps.MaximumOfOne)
            {
                reiterate = new BlockLabel("reiterate");
                block.MarkLabel(reiterate);

                matched = new BlockLocal(typeof(bool));
                block.DeclareLocal(matched);
            }

            BlockLabel breakLabel = new BlockLabel("break");

            block.Emit(body.Compile(runtime, state));

            BlockLabel yes = new BlockLabel("yes");

            block.Dup();
            block.BranchIfNotNo(yes);

            block.Pop();

            block.Branch(breakLabel);

            block.MarkLabel(yes);

            block.Call(typeof(ParseTree).GetMethod("Extend"));

            if (!reps.MaximumOfOne)
            {
                block.Load(true);
                block.StoreLocal(matched);

                block.Branch(reiterate);
            }

            block.MarkLabel(breakLabel);

            if (reps.Minimum > 0)
            {
                BlockLabel enough = new BlockLabel("enough");
                block.LoadLocal(matched);
                block.BranchIfTrue(enough);

                // todo no

                block.RestorePosition(start);

                block.Pop();
                block.LoadNo();

                block.Branch(returnLabel);

                block.MarkLabel(enough);
            }

            // todo yes

            block.MarkLabel(returnLabel);

            block.EndScope();

            block.Comment("end of rep --------------------");

            return(block);
        }
Example #3
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Comment("start of seq --------------------");

            block.BeginScope();

            // Save

            BlockLocal start = block.SavePosition();

            BlockLocal oldLeftHandSide = new BlockLocal(typeof(RuntimeObject));
            BlockLocal oldPrecedenceCanEqualCurrent = new BlockLocal(typeof(bool));
            BlockLocal oldRecursionExclude          = new BlockLocal(typeof(Pattern));

            block.DeclareLocal(oldRecursionExclude);

            if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
            {
                block.DeclareLocal(oldPrecedenceCanEqualCurrent);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
                block.StoreLocal(oldPrecedenceCanEqualCurrent);
            }

            if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
            {
                block.DeclareLocal(oldLeftHandSide);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
                block.StoreLocal(oldLeftHandSide);
            }

            block.LoadYes();

            for (int n = 0; n < nodes.Count; n++)
            {
                ParseGraphNode node = nodes[n];

                // Parse the node

                block.Emit(node.Compile(runtime, state));

                // If no

                BlockLabel yes = new BlockLabel("yes");
                block.Dup();    // dup the body tree
                block.BranchIfNotNo(yes);

                block.Pop();    // pop body tree
                block.Pop();    // pop new tree

                Restore(state, block, oldLeftHandSide,
                        oldPrecedenceCanEqualCurrent, oldRecursionExclude);

                block.RestorePosition(start);

                block.LoadNo();
                block.Branch(returnLabel);

                block.MarkLabel(yes);

                // Extend

                block.Call(typeof(ParseTree).GetMethod("Extend"));

                if (n == 0)
                {
                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
                    block.LoadNull();

                    BlockLabel recursionExcludeNull = new BlockLabel("recursionExcludeNull");
                    block.BranchIfEqual(recursionExcludeNull);

                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
                    block.StoreLocal(oldRecursionExclude);

                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                    block.LoadState();
                    block.GetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
                    block.Call(typeof(Multiset <Pattern>).GetMethod("Remove", new Type[] { typeof(Pattern) }));

                    block.LoadState();
                    block.LoadNull();
                    block.SetProperty(typeof(ParserState).GetProperty("RecursionExclude"));

                    block.MarkLabel(recursionExcludeNull);

                    if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
                    {
                        block.Comment("right recursion");

                        block.LoadState();
                        block.Load(1);
                        block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
                    }
                    else if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
                    {
                        block.Comment("left recursion");

                        block.LoadState();
                        block.LoadNull();
                        block.SetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
                    }
                }
            }

            Restore(state, block, oldLeftHandSide,
                    oldPrecedenceCanEqualCurrent, oldRecursionExclude);

            block.EndScope();

            block.MarkLabel(returnLabel);

            block.Comment("end of seq --------------------");

            return(block);
        }
Example #4
0
        public override Block CompileNewState(Runtime runtime, StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Comment("begin precedence alt -----------");

            // todo enter

            block.BeginScope();

            BlockLabel returnLabel = new BlockLabel("return");

            BlockLocal oldLeftRecursiveAlts = new BlockLocal(typeof(IParseable));

            block.DeclareLocal(oldLeftRecursiveAlts);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("LeftRecursiveAlts"));
            block.StoreLocal(oldLeftRecursiveAlts);

            ParseGraphNode oldLeftRecursiveAltsForCompiler = state.LeftRecursiveAlts;

            foreach (PrecedenceAltGroup group in groups)
            {
                block.BeginScope();

                BlockLocal groupLocal = new BlockLocal(typeof(PrecedenceAltGroup));
                block.DeclareLocal(groupLocal);
                block.Load(group);
                block.StoreLocal(groupLocal);

                BlockLabel apply         = new BlockLabel("apply");
                BlockLabel continueLabel = new BlockLabel("continue");

                BlockLocal currentPrecedence = new BlockLocal(typeof(Precedence));
                block.DeclareLocal(currentPrecedence);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
                block.Dup();
                block.StoreLocal(currentPrecedence);
                block.BranchIfNull(apply);

                BlockLocal groupPrecedence = new BlockLocal(typeof(Precedence));
                block.DeclareLocal(groupPrecedence);
                block.LoadLocal(groupLocal);
                block.GetProperty(groupLocal.Type.GetProperty("Precedence"));
                block.Dup();
                block.StoreLocal(groupPrecedence);
                block.BranchIfNull(apply);

                block.LoadLocal(currentPrecedence);
                block.GetProperty(currentPrecedence.Type.GetProperty("Group"));
                block.LoadLocal(groupPrecedence);
                block.GetProperty(groupPrecedence.Type.GetProperty("Group"));
                block.BranchIfNotEqual(apply);

                block.LoadLocal(groupPrecedence);
                block.LoadLocal(currentPrecedence);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
                block.LogicalNot();
                block.Call(groupPrecedence.Type.GetMethod("IsLowerThan"));
                block.BranchIfTrue(continueLabel);

                block.MarkLabel(apply);

                block.LoadState();
                block.LoadLocal(groupLocal);
                block.GetProperty(groupLocal.Type.GetProperty("LeftRecursiveParseGraph"));
                block.SetProperty(typeof(ParserState).GetProperty("LeftRecursiveAlts"));

                state.LeftRecursiveAlts = group.LeftRecursiveParseGraph;

                block.Emit(group.ParseGraph.Compile(runtime, state));

                block.Dup();

                BlockLabel no = new BlockLabel("no");
                block.BranchIfNo(no);

                block.LoadState();
                block.LoadLocal(oldLeftRecursiveAlts);
                block.SetProperty(typeof(ParserState).GetProperty("LeftRecursiveAlts"));

                // todo yes

                block.Branch(returnLabel);

                block.MarkLabel(no);

                block.Pop();

                block.MarkLabel(continueLabel);

                block.EndScope();
            }

            block.LoadState();
            block.LoadLocal(oldLeftRecursiveAlts);
            block.SetProperty(typeof(ParserState).GetProperty("LeftRecursiveAlts"));

            state.LeftRecursiveAlts = oldLeftRecursiveAltsForCompiler;

            block.EndScope();

            // todo no

            block.LoadNo();

            block.MarkLabel(returnLabel);

            block.Comment("end precedence alt -----------");

            return(block);
        }
Example #5
0
        public override Block CompileNewState(Runtime runtime, StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

            block.Comment("begin longest alt -----------");

            block.Enter(this, "longest alt");

            block.BeginScope();

            BlockLocal start = block.SavePosition();

            BlockLocal longestTree = new BlockLocal(typeof(ParseTree));

            block.DeclareLocal(longestTree);
            block.LoadNull();
            block.StoreLocal(longestTree);

            BlockLocal longestTreeEnd = new BlockLocal(typeof(int));

            block.DeclareLocal(longestTreeEnd);
            block.Load(-1);
            block.StoreLocal(longestTreeEnd);

            foreach (IParseable alt in alts)
            {
                block.Emit(alt.Compile(runtime, state));

                BlockLabel no = new BlockLabel("no");
                block.Dup();
                block.BranchIfNo(no);

                block.LoadLexer();
                block.GetProperty(typeof(Lexer).GetProperty("Position"));
                block.LoadLocal(longestTreeEnd);

                block.BranchIfLessOrEqual(no);

                block.StoreLocal(longestTree);

                block.LoadLexer();
                block.GetProperty(typeof(Lexer).GetProperty("Position"));
                block.StoreLocal(longestTreeEnd);

                BlockLabel continueLabel = new BlockLabel("continue");
                block.Branch(continueLabel);

                block.MarkLabel(no);
                block.Pop();

                block.MarkLabel(continueLabel);

                block.RestorePosition(start);
            }

            BlockLabel yes = new BlockLabel("null");

            block.LoadLocal(longestTree);
            block.BranchIfNotNull(yes);

            block.Comment("no");

            block.No(this, start, "No alts matched");

            block.LoadNo();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.Comment("yes");

            block.MarkLabel(yes);

            block.Yes(this, start);

            block.RestorePosition(longestTreeEnd);

            block.LoadLocal(longestTree);

            block.MarkLabel(returnLabel);

            block.Comment("end longest alt -----------");

            return(block);
        }
Example #6
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

            block.Comment("start of char --------------------");

            block.BeginScope();

            block.Whitespace(runtime, state);

            BlockLocal start = block.SavePosition();

            block.Enter(this, range.ToString());

            block.LoadLexer();
            block.Call(typeof(Lexer).GetMethod("Peek", new Type[] {}));

            BlockLabel failed = new BlockLabel("failed");

            BlockLocal character = null;

            if (range.Min == range.Max)
            {
                if (runtime.TraceParser)
                {
                    block.Dup();

                    character = new BlockLocal(typeof(char));
                    block.DeclareLocal(character);
                    block.StoreLocal(character);
                }

                block.Load(range.Min);
                block.BranchIfNotEqual(failed);
            }
            else
            {
                character = new BlockLocal(typeof(char));
                block.DeclareLocal(character);
                block.StoreLocal(character);

                block.LoadLocal(character);
                block.Load(range.Min);
                block.BranchIfLess(failed);

                block.LoadLocal(character);
                block.Load(range.Max);
                block.BranchIfGreater(failed);
            }

            block.LoadLexer();
            block.Call(typeof(Lexer).GetMethod("Read"));

            if (runtime.TraceParser)
            {
                block.LoadParseTrace();
                block.Load(this);
                block.LoadLexer();
                block.LoadLocal(start);
                block.Call(typeof(Lexer).GetMethod("SourceFrom"));
                block.LoadLocal(character);
                block.Call(typeof(TextEscape).GetMethod("Quote", new Type[] { typeof(char) }));
                block.Call(typeof(ParseTrace).GetMethod("Yes", new Type[] { typeof(object), typeof(Source), typeof(string) }));
            }

            if (state.BuildTextNodes)
            {
                if (range.Min == range.Max)
                {
                    block.Pop();
                    block.Load(Convert.ToString(range.Min));
                }
                else
                {
                    block.Call(typeof(Convert).GetMethod("ToString", new Type[] { typeof(char) }));
                }

                block.New(typeof(ParseTree).GetConstructor(new Type[] { typeof(object) }));
            }
            else
            {
                block.Pop();
                block.LoadYes();
            }

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.MarkLabel(failed);

            if (runtime.TraceParser)
            {
                block.LoadParseTrace();
                block.Load(this);
                block.LoadLexer();
                block.LoadLocal(start);
                block.Call(typeof(Lexer).GetMethod("SourceFrom"));
                block.LoadLocal(character);
                block.Call(typeof(TextEscape).GetMethod("Quote", new Type[] { typeof(char) }));
                block.Call(typeof(ParseTrace).GetMethod("No", new Type[] { typeof(object), typeof(Source), typeof(string) }));
            }

            // todo error string

            block.LoadNo();

            block.EndScope();

            block.MarkLabel(returnLabel);

            block.Comment("end of char --------------------");

            return(block);
        }
Example #7
0
        private void CompileLeftRecurse(Runtime runtime,
                                        StateForCompiler state, ParserBlock block, BlockLocal start,
                                        BlockLocal tree)
        {
            block.BeginScope();

            block.Enter(this, "left recurse");

            BlockLocal finished = block.SavePosition();

            block.LoadState();
            block.LoadLocal(tree);
            block.SetProperty(typeof(ParserState).GetProperty("LeftHandSide"));

            block.LoadState();
            block.LoadLocal(finished);
            block.SetProperty(typeof(ParserState).GetProperty("LeftHandSideEndPos"));

            block.LoadState();
            block.Load(true);
            block.SetProperty(typeof(ParserState).GetProperty("SkipMemoryForLeftRecursion"));

            block.LoadLexer();
            block.LoadLocal(start);
            block.SetProperty(typeof(Lexer).GetProperty("Position"));

            ParseGraphNode node;

            if (state.LeftRecursiveAlts != null)
            {
                node = state.LeftRecursiveAlts;
            }
            else
            {
                node = recurseNode;
            }

            block.Emit(node.Compile(runtime, state));

            block.LoadState();
            block.Load(false);
            block.SetProperty(typeof(ParserState).GetProperty("SkipMemoryForLeftRecursion"));

            block.LoadState();
            block.LoadNull();
            block.SetProperty(typeof(ParserState).GetProperty("LeftHandSide"));

            BlockLabel yes = new BlockLabel("yes");

            block.Dup();
            block.BranchIfNotNo(yes);

            block.No(this, start);

            block.Pop();
            block.RestorePosition(finished);

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.MarkLabel(yes);

            block.Yes(this, start);

            block.StoreLocal(tree);

            block.MarkLabel(returnLabel);

            block.EndScope();
        }
Example #8
0
        private void CompileInstantiate(ParserBlock block, BlockLocal start, BlockLocal tree)
        {
            block.BeginScope();

            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);
            }

            block.LoadLexer();
            block.LoadLocal(start);
            block.Call(typeof(Lexer).GetMethod("SourceFrom"));

            BlockLocal fields = new BlockLocal(typeof(Dictionary <string, object>));

            block.DeclareLocal(fields);
            block.LoadLocal(tree);
            block.GetProperty(typeof(ParseTree).GetProperty("Fields"));
            block.StoreLocal(fields);

            for (int n = 0; n < Fields.Count; n++)
            {
                block.BeginScope();

                BlockLocal fieldValue = new BlockLocal(typeof(object));
                block.DeclareLocal(fieldValue);

                block.LoadLocal(fields);
                block.Load(Fields[n]);
                block.LoadLocalAddress(fieldValue);
                block.Call(fields.Type.GetMethod("TryGetValue"));
                block.Pop();

                block.LoadLocal(fieldValue);

                block.EndScope();
            }

            block.New(constructor);

            BlockLocal instance = new BlockLocal(typeof(RuntimeObject));

            block.DeclareLocal(instance);
            block.StoreLocal(instance);

            // Call Parsed

            block.Load(0);
            block.NewArray(typeof(object));

            block.BeginScope();

            BlockLocal parsedParameters = new BlockLocal(typeof(object[]));

            block.DeclareLocal(parsedParameters);
            block.StoreLocal(parsedParameters);

            block.LoadLocal(instance);
            block.Load("Parsed");
            block.LoadLocal(parsedParameters);
            block.Load(false);

            block.Call(typeof(MemberNode).GetMethod("GetMember", new Type[] {
                typeof(object), typeof(string), typeof(object[]),
                typeof(bool)
            }));

            BlockLabel noParsedMethod = new BlockLabel("noParsedMethod");

            block.Dup();
            block.BranchIfNull(noParsedMethod);

            BlockLocal parsedMethod = new BlockLocal(typeof(object));

            block.DeclareLocal(parsedMethod);
            block.StoreLocal(parsedMethod);

            block.LoadRuntimeState();
            block.LoadLocal(parsedMethod);
            block.LoadLocal(parsedParameters);
            block.Call(typeof(CallNode).GetMethod("Call", new Type[] {
                typeof(RuntimeState), typeof(object), typeof(object[])
            }));
            block.Pop();

            BlockLabel finishedParsedCall = new BlockLabel("finishedParsedCall");

            block.Branch(finishedParsedCall);

            block.MarkLabel(noParsedMethod);

            block.Pop();

            block.MarkLabel(finishedParsedCall);

            block.EndScope();

            block.LoadLocal(instance);
            block.New(typeof(ParseTree).GetConstructor(new Type[] { typeof(object) }));

            block.StoreLocal(tree);

            block.EndScope();
        }