Example #1
0
        public void Whitespace(Runtime runtime, StateForCompiler state)
        {
            if (state.Whitespace == null)
            {
                return;
            }

            if (state.Whitespace == Katahdin.Base.Whitespace.pattern)
            {
                LoadLexer();
                Call(typeof(Lexer).GetMethod("QuickWhitespace"));
                return;
            }

            if (state.Whitespace == DefaultWhitespace.pattern)
            {
                LoadLexer();
                Call(typeof(Lexer).GetMethod("QuickDefaultWhitespace"));
                return;
            }

            Pattern oldWhitespace = state.Whitespace;

            state.Whitespace = null;

            Emit(oldWhitespace.CompileCall(runtime, state));
            Pop();

            state.Whitespace = oldWhitespace;
        }
Example #2
0
        private void Restore(StateForCompiler state, ParserBlock block,
                             BlockLocal oldLeftHandSide,
                             BlockLocal oldPrecedenceCanEqualCurrent,
                             BlockLocal oldRecursionExclude)
        {
            block.LoadState();
            block.LoadLocal(oldRecursionExclude);
            block.SetProperty(typeof(ParserState).GetProperty("RecursionExclude"));

            BlockLabel nullLabel = new BlockLabel("null");

            block.LoadLocal(oldRecursionExclude);
            block.LoadNull();
            block.BranchIfEqual(nullLabel);

            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
            block.LoadLocal(oldRecursionExclude);
            block.Call(typeof(Multiset <Pattern>).GetMethod("Add"));

            block.MarkLabel(nullLabel);

            if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
            {
                block.LoadState();
                block.LoadLocal(oldPrecedenceCanEqualCurrent);
                block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            }
            else if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
            {
                block.LoadState();
                block.LoadLocal(oldLeftHandSide);
                block.SetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
            }
        }
Example #3
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

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

            // todo enter

            block.BeginScope();

            BlockLocal start = block.SavePosition();

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

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

            block.DeclareLocal(bodyTree);
            block.Dup();
            block.StoreLocal(bodyTree);

            BlockLabel yes = new BlockLabel("yes");

            block.BranchIfNotNo(yes);

            // todo no

            block.LoadNo();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.MarkLabel(yes);

            // todo yes

            block.LoadLexer();
            block.GetProperty(typeof(Lexer).GetProperty("Text"));
            block.LoadLocal(start);
            block.LoadLexer();
            block.GetProperty(typeof(Lexer).GetProperty("Position"));
            block.LoadLocal(start);
            block.Sub();
            block.Call(typeof(string).GetMethod("Substring", new Type[] { typeof(int), typeof(int) }));
            block.New(typeof(ParseTree).GetConstructor(new Type[] { typeof(object) }));

            block.LoadLocal(bodyTree);
            block.Call(typeof(ParseTree).GetMethod("ExtendFields"));

            block.MarkLabel(returnLabel);

            block.EndScope();

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

            return(block);
        }
Example #4
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Comment("start of token --------------------");
     
     // todo enter
     
     block.BeginScope();
     
     BlockLocal start = block.SavePosition();
     
     block.Emit(body.Compile(runtime, state));
     
     BlockLocal bodyTree = new BlockLocal(typeof(ParseTree));
     block.DeclareLocal(bodyTree);
     block.Dup();
     block.StoreLocal(bodyTree);
     
     BlockLabel yes = new BlockLabel("yes");
     block.BranchIfNotNo(yes);
     
     // todo no
     
     block.LoadNo();
     
     BlockLabel returnLabel = new BlockLabel("return");
     block.Branch(returnLabel);
     
     block.MarkLabel(yes);
     
     // todo yes
     
     block.LoadLexer();
     block.GetProperty(typeof(Lexer).GetProperty("Text"));
     block.LoadLocal(start);
     block.LoadLexer();
     block.GetProperty(typeof(Lexer).GetProperty("Position"));
     block.LoadLocal(start);
     block.Sub();
     block.Call(typeof(string).GetMethod("Substring", new Type[]{typeof(int), typeof(int)}));
     block.New(typeof(ParseTree).GetConstructor(new Type[]{typeof(object)}));
     
     block.LoadLocal(bodyTree);
     block.Call(typeof(ParseTree).GetMethod("ExtendFields"));
     
     block.MarkLabel(returnLabel);
     
     block.EndScope();
     
     block.Comment("end of token --------------------");
     
     return block;
 }
Example #5
0
 public ParseTree ParseUsingCompiler(Lexer lexer, ParserState state)
 {
     StateForCompiler stateForCompiler = new StateForCompiler();
     stateForCompiler.BuildTextNodes = state.BuildTextNodes;
     stateForCompiler.RecursionBehaviour = state.RecursionBehaviour;
     stateForCompiler.LeftRecursiveAlts = state.LeftRecursiveAlts;
     stateForCompiler.Whitespace = lexer.WhitespacePattern;
     
     PatternTrampoline trampoline = GetTrampoline(state.RuntimeState.Runtime, stateForCompiler);
     return trampoline.Implementation(lexer, state);
 }
Example #6
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Load(this);
     block.LoadLexer();
     block.LoadState();
     block.Call(typeof(UserDefinedNode).GetMethod("Parse"));
     
     return block;
 }
Example #7
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Load(this);
            block.LoadLexer();
            block.LoadState();
            block.Call(typeof(UserDefinedNode).GetMethod("Parse"));

            return(block);
        }
Example #8
0
        public virtual Block Compile(Runtime runtime, StateForCompiler state)
        {
            Block block;

            if (!blockCache.TryGetValue(state, out block))
            {
                block             = CompileNewState(runtime, state);
                blockCache[state] = block;
            }

            return(block);
        }
Example #9
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 #10
0
        public ParseTree ParseUsingCompiler(Lexer lexer, ParserState state)
        {
            StateForCompiler stateForCompiler = new StateForCompiler();

            stateForCompiler.BuildTextNodes     = state.BuildTextNodes;
            stateForCompiler.RecursionBehaviour = state.RecursionBehaviour;
            stateForCompiler.LeftRecursiveAlts  = state.LeftRecursiveAlts;
            stateForCompiler.Whitespace         = lexer.WhitespacePattern;

            PatternTrampoline trampoline = GetTrampoline(state.RuntimeState.Runtime, stateForCompiler);

            return(trampoline.Implementation(lexer, state));
        }
Example #11
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 #12
0
        public PatternTrampoline GetTrampoline(Runtime runtime, StateForCompiler state)
        {
            PatternTrampoline trampoline;

            if (!trampolineMap.TryGetValue(state, out trampoline))
            {
                state = state.Copy();

                trampoline = new PatternTrampoline(this, runtime, state);
                trampolines.Add(trampoline);
                trampolineMap[state] = trampoline;
            }

            return(trampoline);
        }
Example #13
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Comment("start of fail --------------------");
     
     // todo no
     
     block.LoadNo();
     
     block.Comment("end of fail --------------------");
     
     return block;
 }
Example #14
0
 public PatternTrampoline GetTrampoline(Runtime runtime, StateForCompiler state)
 {
     PatternTrampoline trampoline;
     
     if (!trampolineMap.TryGetValue(state, out trampoline))
     {
         state = state.Copy();
         
         trampoline = new PatternTrampoline(this, runtime, state);
         trampolines.Add(trampoline);
         trampolineMap[state] = trampoline;
     }
     
     return trampoline;
 }
Example #15
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

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

            // todo no

            block.LoadNo();

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

            return(block);
        }
Example #16
0
 public Block Compile(Runtime runtime, StateForCompiler state)
 {
     Block block;
     
     if (!blockCache.TryGetValue(state, out block))
     {
         block = CompileNewState(runtime, state);
         blockCache[state] = block;
         
         
         
         /*Console.WriteLine("testing " + GetType().Name);
         
         ParserBlock methodBlock = new ParserBlock();
     
         methodBlock.Emit(block);
         methodBlock.Return();
     
         DynamicMethod compiled = new DynamicMethod(
             "test",
             typeof(ParseTree), 
             new Type[]{typeof(Lexer), typeof(ParserState)},
             runtime.CompilerModule.ModuleBuilder);
 
         methodBlock.Build(new DynamicMethodProxy(compiled));
         
         try
         {
             compiled.Invoke(null, new object[]{null, null});
         }
         catch (InvalidProgramException exception)
         {
             throw exception;
         }
         catch
         {
         }
         
         Console.WriteLine("done testing " + GetType().Name);*/
         
         
         
         
         
     }
     
     return block;
 }
Example #17
0
        public PatternTrampoline(Pattern pattern, Runtime runtime,
                                 StateForCompiler state)
        {
            this.pattern = pattern;
            this.runtime = runtime;
            this.state   = state;

            callBlock = new ParserBlock();
            callBlock.Comment("start of call to " + pattern.Type.Name + " -------------");
            callBlock.Load(this);
            callBlock.GetProperty(typeof(PatternTrampoline).GetProperty("Implementation"));
            callBlock.LoadLexer();
            callBlock.LoadState();
            callBlock.Call(typeof(ParseDelegate).GetMethod("Invoke"));
            callBlock.Comment("end of call to " + pattern.Type.Name + " -------------");
        }
Example #18
0
 public PatternTrampoline(Pattern pattern, Runtime runtime,
     StateForCompiler state)
 {
     this.pattern = pattern;
     this.runtime = runtime;
     this.state = state;
     
     callBlock = new ParserBlock();
     callBlock.Comment("start of call to " + pattern.Type.Name + " -------------");
     callBlock.Load(this);
     callBlock.GetProperty(typeof(PatternTrampoline).GetProperty("Implementation"));
     callBlock.LoadLexer();
     callBlock.LoadState();
     callBlock.Call(typeof(ParseDelegate).GetMethod("Invoke"));
     callBlock.Comment("end of call to " + pattern.Type.Name + " -------------");
 }
Example #19
0
        public ParseDelegate CompileImplementation(Runtime runtime,
                                                   StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

            block.Emit(CompileNew(runtime, state));
            block.Return();

            DynamicMethod implementation = new DynamicMethod(
                "parse" + type.Name,
                typeof(ParseTree),
                new Type[] { typeof(Lexer), typeof(ParserState) },
                runtime.CompilerModule.ModuleBuilder);

            block.Build(new DynamicMethodProxy(implementation));

            return((ParseDelegate)implementation.CreateDelegate(typeof(ParseDelegate)));
        }
Example #20
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

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

            BlockLabel returnLabel = new BlockLabel("returnLabel");

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

            block.Dup();
            block.BranchIfNo(returnLabel);

            block.BeginScope();

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

            block.DeclareLocal(bodyTree);
            block.StoreLocal(bodyTree);

            block.NewParseTree();

            block.LoadLocal(bodyTree);
            block.Call(typeof(ParseTree).GetMethod("ExtendFields"));

            block.Dup();
            block.GetProperty(typeof(ParseTree).GetProperty("Fields"));

            block.Load(label);

            block.LoadLocal(bodyTree);
            block.GetProperty(typeof(ParseTree).GetProperty("Value"));

            block.SetProperty(typeof(Dictionary <string, object>).GetProperty("Item"));

            block.EndScope();

            block.MarkLabel(returnLabel);

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

            return(block);
        }
Example #21
0
        public Block Compile(Runtime runtime, StateForCompiler state)
        {
            Block block;

            if (!blockCache.TryGetValue(state, out block))
            {
                block             = CompileNewState(runtime, state);
                blockCache[state] = block;



                /*Console.WriteLine("testing " + GetType().Name);
                 *
                 * ParserBlock methodBlock = new ParserBlock();
                 *
                 * methodBlock.Emit(block);
                 * methodBlock.Return();
                 *
                 * DynamicMethod compiled = new DynamicMethod(
                 *  "test",
                 *  typeof(ParseTree),
                 *  new Type[]{typeof(Lexer), typeof(ParserState)},
                 *  runtime.CompilerModule.ModuleBuilder);
                 *
                 * methodBlock.Build(new DynamicMethodProxy(compiled));
                 *
                 * try
                 * {
                 *  compiled.Invoke(null, new object[]{null, null});
                 * }
                 * catch (InvalidProgramException exception)
                 * {
                 *  throw exception;
                 * }
                 * catch
                 * {
                 * }
                 *
                 * Console.WriteLine("done testing " + GetType().Name);*/
            }

            return(block);
        }
Example #22
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Comment("start of label --------------------");
     
     BlockLabel returnLabel = new BlockLabel("returnLabel");
     
     block.Emit(body.Compile(runtime, state));
     
     block.Dup();
     block.BranchIfNo(returnLabel);
     
     block.BeginScope();
     
     BlockLocal bodyTree = new BlockLocal(typeof(ParseTree));
     block.DeclareLocal(bodyTree);
     block.StoreLocal(bodyTree);
     
     block.NewParseTree();
     
     block.LoadLocal(bodyTree);
     block.Call(typeof(ParseTree).GetMethod("ExtendFields"));
     
     block.Dup();
     block.GetProperty(typeof(ParseTree).GetProperty("Fields"));
     
     block.Load(label);
     
     block.LoadLocal(bodyTree);
     block.GetProperty(typeof(ParseTree).GetProperty("Value"));
     
     block.SetProperty(typeof(Dictionary<string, object>).GetProperty("Item"));
     
     block.EndScope();
     
     block.MarkLabel(returnLabel);
     
     block.Comment("end of label --------------------");
     
     return block;
 }
Example #23
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

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

            BlockLabel returnLabel = new BlockLabel("return");

            block.BeginScope();

            BlockLocal start = block.SavePosition();

            // todo enter

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

            BlockLabel yes = new BlockLabel("yes");

            block.BranchIfNotNo(yes);

            // todo no

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

            block.MarkLabel(yes);

            // todo yes

            block.RestorePosition(start);
            block.LoadYes();

            block.MarkLabel(returnLabel);

            block.EndScope();

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

            return(block);
        }
Example #24
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Comment("start of not --------------------");
     
     BlockLabel returnLabel = new BlockLabel("return");
     
     block.BeginScope();
     
     BlockLocal start = block.SavePosition();
     
     // todo enter
     
     block.Emit(body.Compile(runtime, state));
     
     BlockLabel yes = new BlockLabel("yes");
     block.BranchIfNotNo(yes);
     
     // todo yes
     
     block.LoadYes();
     block.Branch(returnLabel);
     
     block.MarkLabel(yes);
     
     // todo no
     
     block.RestorePosition(start);
     block.LoadNo();
     
     block.MarkLabel(returnLabel);
     
     block.EndScope();
     
     block.Comment("end of not --------------------");
     
     return block;
 }
Example #25
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 #26
0
     public ParseDelegate CompileImplementation(Runtime runtime,
         StateForCompiler state)
     {
         ParserBlock block = new ParserBlock();
         
         block.Emit(CompileNew(runtime, state));
         block.Return();
     
         DynamicMethod implementation = new DynamicMethod(
             "parse" + type.Name,
             typeof(ParseTree), 
             new Type[]{typeof(Lexer), typeof(ParserState)},
             runtime.CompilerModule.ModuleBuilder);
 
         block.Build(new DynamicMethodProxy(implementation));
         
         return (ParseDelegate) implementation.CreateDelegate(typeof(ParseDelegate));
     }
Example #27
0
 public Block CompileCall(Runtime runtime, StateForCompiler state)
 {
     return GetTrampoline(runtime, state).CallBlock;
 }
Example #28
0
 public abstract Block CompileNew(Runtime runtime, StateForCompiler state);
Example #29
0
 public override Block CompileNewState(Runtime runtime, StateForCompiler state)
 {
     return(implementation.Compile(runtime, state));
 }
Example #30
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 #31
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

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

            block.Enter(this, TextEscape.Quote(text));

            block.BeginScope();

            block.Whitespace(runtime, state);
            BlockLocal start = block.SavePosition();

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

            block.DeclareLocal(n);

            BlockLabel reitterate = new BlockLabel("reitterate");

            block.MarkLabel(reitterate);

            block.Comment("itteration");

            BlockLocal character = new BlockLocal(typeof(char));

            block.DeclareLocal(character);

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

            block.Dup();
            block.StoreLocal(character);

            block.Load(text);
            block.LoadLocal(n);
            block.GetProperty(typeof(string).GetProperty("Chars"));

            BlockLabel matched = new BlockLabel("matched");

            block.BranchIfEqual(matched);

            block.Comment("handle the failure");

            // todo error string

            // todo specifics
            block.No(this, start);

            block.LoadNo();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.MarkLabel(matched);

            block.Comment("increment and check the loop variable");

            block.LoadLocal(n);
            block.Increment();
            block.Dup();

            block.StoreLocal(n);

            block.Load(text.Length);
            block.BranchIfLess(reitterate);

            block.Comment("skip over the text");

            block.LoadLexer();
            block.Load(text.Length);
            block.Call(typeof(Lexer).GetMethod("Skip"));

            // todo specifics
            block.Yes(this, start);

            block.Comment("create the parse tree");

            if (state.BuildTextNodes)
            {
                block.Load(text);
                block.New(typeof(ParseTree).GetConstructor(
                              new Type[] { typeof(object) }));
            }
            else
            {
                block.LoadYes();
            }

            block.EndScope();

            block.MarkLabel(returnLabel);

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

            return(block);
        }
Example #32
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 #33
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 #34
0
        public override Block CompileNew(Runtime runtime, StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);
            
            block.Comment("start of concrete pattern -------------");
            
            BlockLabel returnLabel = new BlockLabel("return");
            
            block.BeginScope();
            
            block.Enter(this, Type.Name);
            
            BlockLocal start = block.SavePosition();
            
            BlockLabel notExcluded = new BlockLabel("notExcluded");
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
            block.Load(this);
            block.Call(typeof(Multiset<Pattern>).GetMethod("Contains"));
            block.BranchIfFalse(notExcluded);
            
            block.No(this, "Excluded");
            
            block.LoadNo();
            block.Branch(returnLabel);
            
            block.MarkLabel(notExcluded);
            
            BlockLocal key = new BlockLocal(typeof(ParserMemoryKey));
            block.DeclareLocal(key);
            block.Load(this);
            block.LoadLocal(start);
            block.New(typeof(ParserMemoryKey).GetConstructor(new Type[]{typeof(ConcretePattern), typeof(int)}));
            block.StoreLocal(key);
            
            BlockLocal oldSkipMemoryForLeftRecursion = new BlockLocal(typeof(bool));
            block.DeclareLocal(oldSkipMemoryForLeftRecursion);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("SkipMemoryForLeftRecursion"));
            block.Dup();
            block.StoreLocal(oldSkipMemoryForLeftRecursion);
            
            BlockLabel checkMemory = new BlockLabel("checkMemory");
            block.BranchIfFalse(checkMemory);
            
            block.LoadState();
            block.Load(false);
            block.SetProperty(typeof(ParserState).GetProperty("SkipMemoryForLeftRecursion"));
            
            BlockLabel setRecursion = new BlockLabel("setRecursion");
            block.Branch(setRecursion);
            
            block.MarkLabel(checkMemory);
            
            block.BeginScope();
            
            BlockLocal entry = new BlockLocal(typeof(ParserMemoryEntry));
            block.DeclareLocal(entry);
            
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("Memory"));
            block.LoadLocal(key);
            block.LoadLocalAddress(entry);
            block.Call(typeof(Dictionary<ParserMemoryKey, ParserMemoryEntry>).GetMethod("TryGetValue"));
            block.BranchIfFalse(setRecursion);
            
            // todo link next
            
            BlockLabel memoryYes = new BlockLabel("memoryYes");
            block.LoadLocal(entry);
            block.GetProperty(typeof(ParserMemoryEntry).GetProperty("Tree"));
            
            BlockLabel returnTree = new BlockLabel("returnTree");
            block.BranchIfNotNo(memoryYes);
            
            block.No(this, start, "from memory");
            
            block.Branch(returnTree);
            
            block.MarkLabel(memoryYes);
            
            block.Yes(this, start, "from memory");
            
            block.LoadLexer();
            block.LoadLocal(entry);
            block.GetProperty(typeof(ParserMemoryEntry).GetProperty("End"));
            block.SetProperty(typeof(Lexer).GetProperty("Position"));
            
            block.MarkLabel(returnTree);
            
            block.LoadLocal(entry);
            block.GetProperty(typeof(ParserMemoryEntry).GetProperty("Tree"));
            
            block.Branch(returnLabel);
            
            block.MarkLabel(setRecursion);
            
            BlockLocal oldRecursionExclude = new BlockLocal(typeof(ConcretePattern));
            block.DeclareLocal(oldRecursionExclude);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
            block.StoreLocal(oldRecursionExclude);
            
            if (RecursionBehaviour != RecursionBehaviour.Recursive)
            {
                BlockLabel recursionExcludeSaveNull = new BlockLabel("recursionExcludeSaveNull");
                block.LoadLocal(oldRecursionExclude);
                block.BranchIfNull(recursionExcludeSaveNull);
                
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                block.LoadLocal(oldRecursionExclude);
                block.Call(typeof(Multiset<Pattern>).GetMethod("Remove", new Type[]{typeof(Pattern)}));
                
                block.MarkLabel(recursionExcludeSaveNull);
                
                block.LoadState();
                block.Load(this);
                block.SetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
                
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                block.Load(this);
                block.Call(typeof(Multiset<Pattern>).GetMethod("Add", new Type[]{typeof(Pattern)}));
            }
            
            BlockLocal oldRecursionBehaviour = new BlockLocal(typeof(RecursionBehaviour));
            block.DeclareLocal(oldRecursionBehaviour);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("RecursionBehaviour"));
            block.StoreLocal(oldRecursionBehaviour);
            
            block.LoadState();
            block.Load(Convert.ToInt32(RecursionBehaviour));
            block.SetProperty(typeof(ParserState).GetProperty("RecursionBehaviour"));
            
            RecursionBehaviour oldRecursionBehaviourState = state.RecursionBehaviour;
            state.RecursionBehaviour = RecursionBehaviour;
            
            BlockLocal oldCurrentPrecedence = new BlockLocal(typeof(Precedence));
            block.DeclareLocal(oldCurrentPrecedence);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
            block.StoreLocal(oldCurrentPrecedence);
            
            BlockLabel doesntOverwrite = new BlockLabel("doesntOverwrite");
            block.Load(Precedence);
            block.LoadLocal(oldCurrentPrecedence);
            block.Call(typeof(Precedence).GetMethod("Overwrites"));
            block.BranchIfFalse(doesntOverwrite);

            block.LoadState();
            block.Load(Precedence);
            block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
            
            block.MarkLabel(doesntOverwrite);
            
            BlockLocal oldPrecedenceCanEqualCurrent = new BlockLocal(typeof(bool));
            block.DeclareLocal(oldPrecedenceCanEqualCurrent);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            block.StoreLocal(oldPrecedenceCanEqualCurrent);
            
            block.LoadState();
            block.Load(RecursionBehaviour == RecursionBehaviour.Recursive);
            block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            
            BlockLocal tree = new BlockLocal(typeof(ParseTree));
            block.DeclareLocal(tree);
            block.Emit(ParseGraph.Compile(runtime, state));
            block.StoreLocal(tree);
            
            block.LoadState();
            block.LoadLocal(oldCurrentPrecedence);
            block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
            
            block.LoadState();
            block.LoadLocal(oldPrecedenceCanEqualCurrent);
            block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            
            block.LoadState();
            block.LoadLocal(oldRecursionBehaviour);
            block.SetProperty(typeof(ParserState).GetProperty("RecursionBehaviour"));
            
            state.RecursionBehaviour = oldRecursionBehaviourState;
            
            block.LoadState();
            block.LoadLocal(oldRecursionExclude);
            block.SetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
            
            block.LoadState();
            block.LoadLocal(oldSkipMemoryForLeftRecursion);
            block.SetProperty(typeof(ParserState).GetProperty("SkipMemoryForLeftRecursion"));
            
            if (RecursionBehaviour != RecursionBehaviour.Recursive)
            {
                BlockLabel recursionExcludeRestoreNull = new BlockLabel("recursionExcludeRestoreNull");
                block.LoadLocal(oldRecursionExclude);
                block.BranchIfNull(recursionExcludeRestoreNull);
                
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                block.LoadLocal(oldRecursionExclude);
                block.Call(typeof(Multiset<Pattern>).GetMethod("Add", new Type[]{typeof(Pattern)}));
                
                block.MarkLabel(recursionExcludeRestoreNull);
                
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                block.Load(this);
                block.Call(typeof(Multiset<Pattern>).GetMethod("Remove", new Type[]{typeof(Pattern)}));
            }
            
            BlockLabel no = new BlockLabel("yes1");
            block.LoadLocal(tree);
            block.BranchIfNo(no);
            
            block.Whitespace(runtime, state);
            
            CompileInstantiate(block, start, tree);
            
            block.MarkLabel(no);
            
            BlockLocal nextTag = new BlockLocal(typeof(object));
            block.DeclareLocal(nextTag);
            block.New(typeof(object));
            block.StoreLocal(nextTag);
            
            // todo tag next
            
            BlockLabel yes = new BlockLabel("yes");
            block.LoadLocal(tree);
            block.BranchIfNotNo(yes);
            
            block.No(this, start, "a");
            
            BlockLabel finish = new BlockLabel("finish");
            block.Branch(finish);
            
            block.MarkLabel(yes);
            
            block.Yes(this, start);
            
            if (RecursionBehaviour == RecursionBehaviour.LeftRecursive)
                CompileLeftRecurse(runtime, state, block, start, tree);
            
            block.MarkLabel(finish);
            
            if (ShouldRemember)
            {
                // TODO - can remove some other code if not remembering
                
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Memory"));
                block.LoadLocal(key);
            
                block.LoadLocal(tree);
                block.LoadLexer();
                block.GetProperty(typeof(Lexer).GetProperty("Position"));
                block.LoadLocal(nextTag);
                block.New(typeof(ParserMemoryEntry).GetConstructor(new Type[]{typeof(ParseTree), typeof(int), typeof(object)}));
            
                block.SetProperty(typeof(Dictionary<ParserMemoryKey, ParserMemoryEntry>).GetProperty("Item"));
            }
            
            block.LoadLocal(tree);
             
            block.EndScope();
            
            block.MarkLabel(returnLabel);
            
            block.Comment("end of concrete pattern -------------");
            
            return block;
        }
Example #35
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 #36
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 #37
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 #38
0
 private void Restore(StateForCompiler state, ParserBlock block,
     BlockLocal oldLeftHandSide,
     BlockLocal oldPrecedenceCanEqualCurrent,
     BlockLocal oldRecursionExclude)
 {
     block.LoadState();
     block.LoadLocal(oldRecursionExclude);
     block.SetProperty(typeof(ParserState).GetProperty("RecursionExclude"));
 
     BlockLabel nullLabel = new BlockLabel("null");
 
     block.LoadLocal(oldRecursionExclude);
     block.LoadNull();
     block.BranchIfEqual(nullLabel);
 
     block.LoadState();
     block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
     block.LoadLocal(oldRecursionExclude);
     block.Call(typeof(Multiset<Pattern>).GetMethod("Add"));
 
     block.MarkLabel(nullLabel);
     
     if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
     {
         block.LoadState();
         block.LoadLocal(oldPrecedenceCanEqualCurrent);
         block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
     }
     else if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
     {
         block.LoadState();
         block.LoadLocal(oldLeftHandSide);
         block.SetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
     }
 }
Example #39
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

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

            if (simple)
            {
                block.Emit(pattern.CompileCall(runtime, state));
            }
            else
            {
                BlockLabel returnLabel = new BlockLabel("return");

                block.BeginScope();

                BlockLocal patternLocal = new BlockLocal(typeof(Pattern));
                block.DeclareLocal(patternLocal);
                block.Load(pattern);
                block.StoreLocal(patternLocal);

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

                BlockLabel parseNew = new BlockLabel("parseNew");
                block.BranchIfNull(parseNew);

                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
                block.GetProperty(typeof(ParseTree).GetProperty("Value"));
                block.Call(typeof(object).GetMethod("GetType"));
                BlockLocal leftType = new BlockLocal(typeof(Type));
                block.DeclareLocal(leftType);
                block.Dup();
                block.StoreLocal(leftType);

                block.LoadLocal(patternLocal);
                block.GetProperty(typeof(Pattern).GetProperty("Type"));
                BlockLocal patternType = new BlockLocal(typeof(Type));
                block.DeclareLocal(patternType);
                block.Dup();
                block.StoreLocal(patternType);

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

                block.LoadLocal(leftType);
                block.LoadLocal(patternType);
                block.Call(typeof(Type).GetMethod("IsSubclassOf"));

                block.BranchIfTrue(recurse);

                block.Branch(parseNew);

                block.MarkLabel(recurse);

                block.Single("left hand side from recursion");

                block.LoadLexer();
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("LeftHandSideEndPos"));
                block.SetProperty(typeof(Lexer).GetProperty("Position"));

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

                block.Branch(returnLabel);

                block.MarkLabel(parseNew);

                block.Emit(pattern.CompileCall(runtime, state));

                block.MarkLabel(returnLabel);

                block.EndScope();
            }

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

            return(block);
        }
Example #40
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock(runtime);
     
     block.Comment("start of pattern --------------------");
     
     if (simple)
     {
         block.Emit(pattern.CompileCall(runtime, state));
     }
     else
     {
         BlockLabel returnLabel = new BlockLabel("return");
     
         block.BeginScope();
     
         BlockLocal patternLocal = new BlockLocal(typeof(Pattern));
         block.DeclareLocal(patternLocal);
         block.Load(pattern);
         block.StoreLocal(patternLocal);
     
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
     
         BlockLabel parseNew = new BlockLabel("parseNew");
         block.BranchIfNull(parseNew);
     
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
         block.GetProperty(typeof(ParseTree).GetProperty("Value"));
         block.Call(typeof(object).GetMethod("GetType"));
         BlockLocal leftType = new BlockLocal(typeof(Type));
         block.DeclareLocal(leftType);
         block.Dup();
         block.StoreLocal(leftType);
     
         block.LoadLocal(patternLocal);
         block.GetProperty(typeof(Pattern).GetProperty("Type"));
         BlockLocal patternType = new BlockLocal(typeof(Type));
         block.DeclareLocal(patternType);
         block.Dup();
         block.StoreLocal(patternType);
     
         BlockLabel recurse = new BlockLabel("recurse");
         block.BranchIfEqual(recurse);
     
         block.LoadLocal(leftType);
         block.LoadLocal(patternType);
         block.Call(typeof(Type).GetMethod("IsSubclassOf"));
     
         block.BranchIfTrue(recurse);
     
         block.Branch(parseNew);
     
         block.MarkLabel(recurse);
     
         block.Single("left hand side from recursion");
     
         block.LoadLexer();
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("LeftHandSideEndPos"));
         block.SetProperty(typeof(Lexer).GetProperty("Position"));
         
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("LeftHandSide"));
         
         block.Branch(returnLabel);
         
         block.MarkLabel(parseNew);
         
         block.Emit(pattern.CompileCall(runtime, state));
         
         block.MarkLabel(returnLabel);
     
         block.EndScope();
     }
     
     block.Comment("end of pattern ---------------------");
     
     return block;
 }
Example #41
0
 public virtual Block Compile(Runtime runtime, StateForCompiler state)
 {
     Block block;
     
     if (!blockCache.TryGetValue(state, out block))
     {
         block = CompileNewState(runtime, state);
         blockCache[state] = block;
     }
     
     return block;
 }
Example #42
0
        public override Block CompileNew(Runtime runtime, StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

            block.Comment("start of abstract pattern -------------");

            BlockLabel returnLabel = new BlockLabel("return");

            block.Enter(this, Type.Name);

            block.BeginScope();

            BlockLocal start = block.SavePosition();

            BlockLabel notExcluded = new BlockLabel("notExcluded");

            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
            block.Load(this);
            block.Call(typeof(Multiset <Pattern>).GetMethod("Contains"));
            block.BranchIfFalse(notExcluded);

            block.No(this);

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

            block.MarkLabel(notExcluded);

            BlockLocal oldCurrentPrecedence = new BlockLocal(typeof(Precedence));

            block.DeclareLocal(oldCurrentPrecedence);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
            block.StoreLocal(oldCurrentPrecedence);

            BlockLabel doesntOverwrite = new BlockLabel("doesntOverwrite");

            block.Load(Precedence);
            block.LoadLocal(oldCurrentPrecedence);
            block.Call(typeof(Precedence).GetMethod("Overwrites"));
            block.BranchIfFalse(doesntOverwrite);

            block.LoadState();
            block.Load(Precedence);
            block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));

            block.MarkLabel(doesntOverwrite);

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

            block.LoadState();
            block.LoadLocal(oldCurrentPrecedence);
            block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));

            block.Dup();

            BlockLabel yes = new BlockLabel("yes");

            block.BranchIfNotNo(yes);

            block.No(this, start);

            block.Branch(returnLabel);

            block.MarkLabel(yes);

            block.Yes(this, start);

            block.EndScope();

            block.MarkLabel(returnLabel);

            block.Comment("end of abstract pattern -------------");

            return(block);
        }
Example #43
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 #44
0
 public Block CompileCall(Runtime runtime, StateForCompiler state)
 {
     return(GetTrampoline(runtime, state).CallBlock);
 }
Example #45
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock();

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

            // todo enter

            block.BeginScope();

            block.Comment("save");

            // todo can remove this when no longer mixing jit and aot
            BlockLocal oldBuildTextNodes = null;

            BlockLocal oldWhitespace                = null;
            BlockLocal excludeLocal                 = null;
            BlockLocal oldCurrentPrecedence         = null;
            BlockLocal oldPrecedenceCanEqualCurrent = null;

            if (buildTextNodes.HasValue)
            {
                oldBuildTextNodes = new BlockLocal(typeof(bool));
                block.DeclareLocal(oldBuildTextNodes);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
                block.StoreLocal(oldBuildTextNodes);

                block.LoadState();
                block.Load(buildTextNodes.Value);
                block.SetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
            }

            if (whitespace.HasValue)
            {
                block.Whitespace(runtime, state);

                oldWhitespace = new BlockLocal(typeof(Pattern));
                block.DeclareLocal(oldWhitespace);

                block.LoadLexer();
                block.GetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
                block.StoreLocal(oldWhitespace);

                block.LoadLexer();
                block.Load(whitespace.Value);
                block.SetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
            }

            if (exclude.HasValue)
            {
                if (exclude.Value == null)
                {
                    throw new Exception();
                }

                excludeLocal = new BlockLocal(typeof(Pattern));
                block.DeclareLocal(excludeLocal);
                block.Load(exclude.Value);
                block.StoreLocal(excludeLocal);

                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
                block.LoadLocal(excludeLocal);
                block.Call(typeof(Multiset <Pattern>).GetMethod("Add"));
            }

            if (dropPrecedence.HasValue && dropPrecedence.Value)
            {
                oldCurrentPrecedence = new BlockLocal(typeof(Precedence));
                block.DeclareLocal(oldCurrentPrecedence);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
                block.StoreLocal(oldCurrentPrecedence);

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

                oldPrecedenceCanEqualCurrent = new BlockLocal(typeof(bool));
                block.DeclareLocal(oldPrecedenceCanEqualCurrent);
                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
                block.StoreLocal(oldPrecedenceCanEqualCurrent);

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

            bool    oldBuildTextNodesState = state.BuildTextNodes;
            Pattern oldWhitespaceState     = state.Whitespace;

            if (buildTextNodes.HasValue)
            {
                state.BuildTextNodes = buildTextNodes.Value;
            }

            if (whitespace.HasValue)
            {
                state.Whitespace = whitespace.Value;
            }

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

            state.BuildTextNodes = oldBuildTextNodesState;
            state.Whitespace     = oldWhitespaceState;

            block.Comment("restore");

            if (buildTextNodes.HasValue)
            {
                block.LoadState();
                block.LoadLocal(oldBuildTextNodes);
                block.SetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
            }

            if (whitespace.HasValue)
            {
                block.LoadLexer();
                block.LoadLocal(oldWhitespace);
                block.SetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
            }

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

            if (dropPrecedence.HasValue && dropPrecedence.Value)
            {
                block.LoadState();
                block.LoadLocal(oldCurrentPrecedence);
                block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));

                block.LoadState();
                block.LoadLocal(oldPrecedenceCanEqualCurrent);
                block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            }

            // todo yes or no

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

            block.EndScope();

            return(block);
        }
Example #46
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 #47
0
 public override Block CompileNew(Runtime runtime, StateForCompiler state)
 {
     ParserBlock block = new ParserBlock(runtime);
     
     block.Comment("start of abstract pattern -------------");
     
     BlockLabel returnLabel = new BlockLabel("return");
     
     block.Enter(this, Type.Name);
     
     block.BeginScope();
     
     BlockLocal start = block.SavePosition();
     
     BlockLabel notExcluded = new BlockLabel("notExcluded");
     block.LoadState();
     block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
     block.Load(this);
     block.Call(typeof(Multiset<Pattern>).GetMethod("Contains"));
     block.BranchIfFalse(notExcluded);
     
     block.No(this);
     
     block.LoadNo();
     block.Branch(returnLabel);
     
     block.MarkLabel(notExcluded);
     
     BlockLocal oldCurrentPrecedence = new BlockLocal(typeof(Precedence));
     block.DeclareLocal(oldCurrentPrecedence);
     block.LoadState();
     block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
     block.StoreLocal(oldCurrentPrecedence);
     
     BlockLabel doesntOverwrite = new BlockLabel("doesntOverwrite");
     block.Load(Precedence);
     block.LoadLocal(oldCurrentPrecedence);
     block.Call(typeof(Precedence).GetMethod("Overwrites"));
     block.BranchIfFalse(doesntOverwrite);
     
     block.LoadState();
     block.Load(Precedence);
     block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
     
     block.MarkLabel(doesntOverwrite);
     
     block.Emit(ParseGraph.Compile(runtime, state));
     
     block.LoadState();
     block.LoadLocal(oldCurrentPrecedence);
     block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
     
     block.Dup();
     
     BlockLabel yes = new BlockLabel("yes");
     block.BranchIfNotNo(yes);
     
     block.No(this, start);
     
     block.Branch(returnLabel);
     
     block.MarkLabel(yes);
     
     block.Yes(this, start);
     
     block.EndScope();
     
     block.MarkLabel(returnLabel);
     
     block.Comment("end of abstract pattern -------------");
     
     return block;
 }
Example #48
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock(runtime);
     
     block.Comment("start of text --------------------");
     
     block.Enter(this, TextEscape.Quote(text));
     
     block.BeginScope();
     
     block.Whitespace(runtime, state);
     BlockLocal start = block.SavePosition();
     
     BlockLocal n = new BlockLocal(typeof(int));
     block.DeclareLocal(n);
     
     BlockLabel reitterate = new BlockLabel("reitterate");
     block.MarkLabel(reitterate);
     
     block.Comment("itteration");
     
     BlockLocal character = new BlockLocal(typeof(char));
     block.DeclareLocal(character);
     
     block.LoadLexer();
     block.LoadLocal(n);
     block.Call(typeof(Lexer).GetMethod("Peek",
         new Type[]{typeof(int)}));
     
     block.Dup();
     block.StoreLocal(character);
     
     block.Load(text);
     block.LoadLocal(n);
     block.GetProperty(typeof(string).GetProperty("Chars"));
     
     BlockLabel matched = new BlockLabel("matched");
     block.BranchIfEqual(matched);
     
     block.Comment("handle the failure");
     
     // todo error string
     
     // todo specifics
     block.No(this, start);
     
     block.LoadNo();
     
     BlockLabel returnLabel = new BlockLabel("return");
     block.Branch(returnLabel);
     
     block.MarkLabel(matched);
     
     block.Comment("increment and check the loop variable");
     
     block.LoadLocal(n);
     block.Increment();
     block.Dup();
     
     block.StoreLocal(n);
     
     block.Load(text.Length);
     block.BranchIfLess(reitterate);
     
     block.Comment("skip over the text");
     
     block.LoadLexer();
     block.Load(text.Length);
     block.Call(typeof(Lexer).GetMethod("Skip"));
     
     // todo specifics
     block.Yes(this, start);
     
     block.Comment("create the parse tree");
     
     if (state.BuildTextNodes)
     {
         block.Load(text);
         block.New(typeof(ParseTree).GetConstructor(
             new Type[]{typeof(object)}));
     }
     else
     {
         block.LoadYes();
     }
     
     block.EndScope();
     
     block.MarkLabel(returnLabel);
     
     block.Comment("end of text --------------------");
     
     return block;
 }
Example #49
0
 public void Whitespace(Runtime runtime, StateForCompiler state)
 {
     if (state.Whitespace == null)
         return;
     
     if (state.Whitespace == Katahdin.Base.Whitespace.pattern)
     {
         LoadLexer();
         Call(typeof(Lexer).GetMethod("QuickWhitespace"));
         return;
     }
     
     if (state.Whitespace == DefaultWhitespace.pattern)
     {
         LoadLexer();
         Call(typeof(Lexer).GetMethod("QuickDefaultWhitespace"));
         return;
     }
     
     Pattern oldWhitespace = state.Whitespace;
     state.Whitespace = null;
     
     Emit(oldWhitespace.CompileCall(runtime, state));
     Pop();
     
     state.Whitespace = oldWhitespace;
 }
Example #50
0
 public override Block CompileNewState(Runtime runtime,
     StateForCompiler state)
 {
     ParserBlock block = new ParserBlock();
     
     block.Comment("start of options --------------------");
     
     // todo enter
     
     block.BeginScope();
     
     block.Comment("save");
     
     // todo can remove this when no longer mixing jit and aot
     BlockLocal oldBuildTextNodes = null;
     
     BlockLocal oldWhitespace = null;
     BlockLocal excludeLocal = null;
     BlockLocal oldCurrentPrecedence = null;
     BlockLocal oldPrecedenceCanEqualCurrent = null;
     
     if (buildTextNodes.HasValue)
     {
         oldBuildTextNodes = new BlockLocal(typeof(bool));
         block.DeclareLocal(oldBuildTextNodes);
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
         block.StoreLocal(oldBuildTextNodes);
         
         block.LoadState();
         block.Load(buildTextNodes.Value);
         block.SetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
     }
     
     if (whitespace.HasValue)
     {
         block.Whitespace(runtime, state);
         
         oldWhitespace = new BlockLocal(typeof(Pattern));
         block.DeclareLocal(oldWhitespace);
         
         block.LoadLexer();
         block.GetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
         block.StoreLocal(oldWhitespace);
         
         block.LoadLexer();
         block.Load(whitespace.Value);
         block.SetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
     }
     
     if (exclude.HasValue)
     {
         if (exclude.Value == null)
             throw new Exception();
         
         excludeLocal = new BlockLocal(typeof(Pattern));
         block.DeclareLocal(excludeLocal);
         block.Load(exclude.Value);
         block.StoreLocal(excludeLocal);
     
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
         block.LoadLocal(excludeLocal);
         block.Call(typeof(Multiset<Pattern>).GetMethod("Add"));
     }
     
     if (dropPrecedence.HasValue && dropPrecedence.Value)
     {
         oldCurrentPrecedence = new BlockLocal(typeof(Precedence));
         block.DeclareLocal(oldCurrentPrecedence);
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
         block.StoreLocal(oldCurrentPrecedence);
         
         block.LoadState();
         block.LoadNull();
         block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
         
         oldPrecedenceCanEqualCurrent = new BlockLocal(typeof(bool));
         block.DeclareLocal(oldPrecedenceCanEqualCurrent);
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
         block.StoreLocal(oldPrecedenceCanEqualCurrent);
         
         block.LoadState();
         block.Load(false);
         block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
     }
     
     bool oldBuildTextNodesState = state.BuildTextNodes;
     Pattern oldWhitespaceState = state.Whitespace;
     
     if (buildTextNodes.HasValue)
         state.BuildTextNodes = buildTextNodes.Value;
     
     if (whitespace.HasValue)
         state.Whitespace = whitespace.Value;
     
     block.Emit(body.Compile(runtime, state)); 
     
     state.BuildTextNodes = oldBuildTextNodesState;
     state.Whitespace = oldWhitespaceState;
     
     block.Comment("restore");   
 
     if (buildTextNodes.HasValue)
     {
         block.LoadState();
         block.LoadLocal(oldBuildTextNodes);
         block.SetProperty(typeof(ParserState).GetProperty("BuildTextNodes"));
     }     
 
     if (whitespace.HasValue)
     {
         block.LoadLexer();
         block.LoadLocal(oldWhitespace);
         block.SetProperty(typeof(Lexer).GetProperty("WhitespacePattern"));
     }
 
     if (exclude.HasValue)
     {
         block.LoadState();
         block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
         block.LoadLocal(excludeLocal);
         block.Call(typeof(Multiset<Pattern>).GetMethod("Remove", new Type[]{typeof(Pattern)}));
     }
 
     if (dropPrecedence.HasValue && dropPrecedence.Value)
     {
         block.LoadState();
         block.LoadLocal(oldCurrentPrecedence);
         block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
         
         block.LoadState();
         block.LoadLocal(oldPrecedenceCanEqualCurrent);
         block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
     }
     
     // todo yes or no
     
     block.Comment("end of options --------------------");
     
     block.EndScope();
     
     return block;
 }
Example #51
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 #52
0
 public abstract Block CompileNewState(Runtime runtime, StateForCompiler state);
Example #53
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 #54
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 #55
0
        public override Block CompileNew(Runtime runtime, StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

            block.Comment("start of concrete pattern -------------");

            BlockLabel returnLabel = new BlockLabel("return");

            block.BeginScope();

            block.Enter(this, Type.Name);

            BlockLocal start = block.SavePosition();

            BlockLabel notExcluded = new BlockLabel("notExcluded");

            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("Excluded"));
            block.Load(this);
            block.Call(typeof(Multiset <Pattern>).GetMethod("Contains"));
            block.BranchIfFalse(notExcluded);

            block.No(this, "Excluded");

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

            block.MarkLabel(notExcluded);

            BlockLocal key = new BlockLocal(typeof(ParserMemoryKey));

            block.DeclareLocal(key);
            block.Load(this);
            block.LoadLocal(start);
            block.New(typeof(ParserMemoryKey).GetConstructor(new Type[] { typeof(ConcretePattern), typeof(int) }));
            block.StoreLocal(key);

            BlockLocal oldSkipMemoryForLeftRecursion = new BlockLocal(typeof(bool));

            block.DeclareLocal(oldSkipMemoryForLeftRecursion);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("SkipMemoryForLeftRecursion"));
            block.Dup();
            block.StoreLocal(oldSkipMemoryForLeftRecursion);

            BlockLabel checkMemory = new BlockLabel("checkMemory");

            block.BranchIfFalse(checkMemory);

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

            BlockLabel setRecursion = new BlockLabel("setRecursion");

            block.Branch(setRecursion);

            block.MarkLabel(checkMemory);

            block.BeginScope();

            BlockLocal entry = new BlockLocal(typeof(ParserMemoryEntry));

            block.DeclareLocal(entry);

            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("Memory"));
            block.LoadLocal(key);
            block.LoadLocalAddress(entry);
            block.Call(typeof(Dictionary <ParserMemoryKey, ParserMemoryEntry>).GetMethod("TryGetValue"));
            block.BranchIfFalse(setRecursion);

            // todo link next

            BlockLabel memoryYes = new BlockLabel("memoryYes");

            block.LoadLocal(entry);
            block.GetProperty(typeof(ParserMemoryEntry).GetProperty("Tree"));

            BlockLabel returnTree = new BlockLabel("returnTree");

            block.BranchIfNotNo(memoryYes);

            block.No(this, start, "from memory");

            block.Branch(returnTree);

            block.MarkLabel(memoryYes);

            block.Yes(this, start, "from memory");

            block.LoadLexer();
            block.LoadLocal(entry);
            block.GetProperty(typeof(ParserMemoryEntry).GetProperty("End"));
            block.SetProperty(typeof(Lexer).GetProperty("Position"));

            block.MarkLabel(returnTree);

            block.LoadLocal(entry);
            block.GetProperty(typeof(ParserMemoryEntry).GetProperty("Tree"));

            block.Branch(returnLabel);

            block.MarkLabel(setRecursion);

            BlockLocal oldRecursionExclude = new BlockLocal(typeof(ConcretePattern));

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

            if (RecursionBehaviour != RecursionBehaviour.Recursive)
            {
                BlockLabel recursionExcludeSaveNull = new BlockLabel("recursionExcludeSaveNull");
                block.LoadLocal(oldRecursionExclude);
                block.BranchIfNull(recursionExcludeSaveNull);

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

                block.MarkLabel(recursionExcludeSaveNull);

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

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

            BlockLocal oldRecursionBehaviour = new BlockLocal(typeof(RecursionBehaviour));

            block.DeclareLocal(oldRecursionBehaviour);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("RecursionBehaviour"));
            block.StoreLocal(oldRecursionBehaviour);

            block.LoadState();
            block.Load(Convert.ToInt32(RecursionBehaviour));
            block.SetProperty(typeof(ParserState).GetProperty("RecursionBehaviour"));

            RecursionBehaviour oldRecursionBehaviourState = state.RecursionBehaviour;

            state.RecursionBehaviour = RecursionBehaviour;

            BlockLocal oldCurrentPrecedence = new BlockLocal(typeof(Precedence));

            block.DeclareLocal(oldCurrentPrecedence);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));
            block.StoreLocal(oldCurrentPrecedence);

            BlockLabel doesntOverwrite = new BlockLabel("doesntOverwrite");

            block.Load(Precedence);
            block.LoadLocal(oldCurrentPrecedence);
            block.Call(typeof(Precedence).GetMethod("Overwrites"));
            block.BranchIfFalse(doesntOverwrite);

            block.LoadState();
            block.Load(Precedence);
            block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));

            block.MarkLabel(doesntOverwrite);

            BlockLocal oldPrecedenceCanEqualCurrent = new BlockLocal(typeof(bool));

            block.DeclareLocal(oldPrecedenceCanEqualCurrent);
            block.LoadState();
            block.GetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));
            block.StoreLocal(oldPrecedenceCanEqualCurrent);

            block.LoadState();
            block.Load(RecursionBehaviour == RecursionBehaviour.Recursive);
            block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));

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

            block.DeclareLocal(tree);
            block.Emit(ParseGraph.Compile(runtime, state));
            block.StoreLocal(tree);

            block.LoadState();
            block.LoadLocal(oldCurrentPrecedence);
            block.SetProperty(typeof(ParserState).GetProperty("CurrentPrecedence"));

            block.LoadState();
            block.LoadLocal(oldPrecedenceCanEqualCurrent);
            block.SetProperty(typeof(ParserState).GetProperty("PrecedenceCanEqualCurrent"));

            block.LoadState();
            block.LoadLocal(oldRecursionBehaviour);
            block.SetProperty(typeof(ParserState).GetProperty("RecursionBehaviour"));

            state.RecursionBehaviour = oldRecursionBehaviourState;

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

            block.LoadState();
            block.LoadLocal(oldSkipMemoryForLeftRecursion);
            block.SetProperty(typeof(ParserState).GetProperty("SkipMemoryForLeftRecursion"));

            if (RecursionBehaviour != RecursionBehaviour.Recursive)
            {
                BlockLabel recursionExcludeRestoreNull = new BlockLabel("recursionExcludeRestoreNull");
                block.LoadLocal(oldRecursionExclude);
                block.BranchIfNull(recursionExcludeRestoreNull);

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

                block.MarkLabel(recursionExcludeRestoreNull);

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

            BlockLabel no = new BlockLabel("yes1");

            block.LoadLocal(tree);
            block.BranchIfNo(no);

            block.Whitespace(runtime, state);

            CompileInstantiate(block, start, tree);

            block.MarkLabel(no);

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

            block.DeclareLocal(nextTag);
            block.New(typeof(object));
            block.StoreLocal(nextTag);

            // todo tag next

            BlockLabel yes = new BlockLabel("yes");

            block.LoadLocal(tree);
            block.BranchIfNotNo(yes);

            block.No(this, start, "a");

            BlockLabel finish = new BlockLabel("finish");

            block.Branch(finish);

            block.MarkLabel(yes);

            block.Yes(this, start);

            if (RecursionBehaviour == RecursionBehaviour.LeftRecursive)
            {
                CompileLeftRecurse(runtime, state, block, start, tree);
            }

            block.MarkLabel(finish);

            if (ShouldRemember)
            {
                // TODO - can remove some other code if not remembering

                block.LoadState();
                block.GetProperty(typeof(ParserState).GetProperty("Memory"));
                block.LoadLocal(key);

                block.LoadLocal(tree);
                block.LoadLexer();
                block.GetProperty(typeof(Lexer).GetProperty("Position"));
                block.LoadLocal(nextTag);
                block.New(typeof(ParserMemoryEntry).GetConstructor(new Type[] { typeof(ParseTree), typeof(int), typeof(object) }));

                block.SetProperty(typeof(Dictionary <ParserMemoryKey, ParserMemoryEntry>).GetProperty("Item"));
            }

            block.LoadLocal(tree);

            block.EndScope();

            block.MarkLabel(returnLabel);

            block.Comment("end of concrete pattern -------------");

            return(block);
        }