Beispiel #1
0
 public void EmitAnchor(bool reverse, int offset, LinkRef tail)
 {
     this.BeginLink(tail);
     this.Emit(OpCode.Anchor, PatternCompiler.MakeFlags(false, false, reverse, false));
     this.EmitLink(tail);
     this.Emit((ushort)offset);
 }
Beispiel #2
0
        public void EmitSet(char lo, BitArray set, bool negate, bool ignore, bool reverse)
        {
            this.Emit(OpCode.Set, PatternCompiler.MakeFlags(negate, ignore, reverse, false));
            this.Emit((ushort)lo);
            int num = set.Length + 15 >> 4;

            this.Emit((ushort)num);
            int num2 = 0;

            while (num-- != 0)
            {
                ushort num3 = 0;
                for (int i = 0; i < 16; i++)
                {
                    if (num2 >= set.Length)
                    {
                        break;
                    }
                    if (set[num2++])
                    {
                        num3 |= (ushort)(1 << i);
                    }
                }
                this.Emit(num3);
            }
        }
Beispiel #3
0
 public void EmitFastRepeat(int min, int max, bool lazy, LinkRef tail)
 {
     this.BeginLink(tail);
     this.Emit(OpCode.FastRepeat, PatternCompiler.MakeFlags(false, false, false, lazy));
     this.EmitLink(tail);
     this.EmitCount(min);
     this.EmitCount(max);
 }
Beispiel #4
0
 public void EmitCharacter(char c, bool negate, bool ignore, bool reverse)
 {
     this.Emit(OpCode.Character, PatternCompiler.MakeFlags(negate, ignore, reverse, false));
     if (ignore)
     {
         c = char.ToLower(c);
     }
     this.Emit((ushort)c);
 }
Beispiel #5
0
        public void EmitString(string str, bool ignore, bool reverse)
        {
            this.Emit(OpCode.String, PatternCompiler.MakeFlags(false, ignore, reverse, false));
            int length = str.Length;

            this.Emit((ushort)length);
            if (ignore)
            {
                str = str.ToLower();
            }
            for (int i = 0; i < length; i++)
            {
                this.Emit((ushort)str[i]);
            }
        }
Beispiel #6
0
        private static IMachineFactory CreateMachineFactory(string pattern, RegexOptions options)
        {
            Parser            psr = new Parser();
            RegularExpression re  = psr.ParseRegularExpression(pattern, options);

            ICompiler cmp;

            //if ((options & RegexOptions.Compiled) != 0)
            //	//throw new Exception ("Not implemented.");
            //	cmp = new CILCompiler ();
            //else
            cmp = new PatternCompiler();

            re.Compile(cmp, (options & RegexOptions.RightToLeft) != 0);

            IMachineFactory machineFactory = cmp.GetMachineFactory();

            machineFactory.Mapping      = psr.GetMapping();
            machineFactory.NamesMapping = GetGroupNamesArray(machineFactory.GroupCount, machineFactory.Mapping);

            return(machineFactory);
        }
Beispiel #7
0
        public Regex(string pattern, RegexOptions options)
        {
            this.pattern  = pattern;
            this.roptions = options;

            this.machineFactory = cache.Lookup(pattern, options);

            if (this.machineFactory == null)
            {
                // parse and install group mapping

                Parser            psr = new Parser();
                RegularExpression re  = psr.ParseRegularExpression(pattern, options);
                this.group_count = re.GroupCount;
                this.mapping     = psr.GetMapping();

                // compile

                ICompiler cmp;
                //if ((options & RegexOptions.Compiled) != 0)
                //	//throw new Exception ("Not implemented.");
                //	cmp = new CILCompiler ();
                //else
                cmp = new PatternCompiler();

                re.Compile(cmp, RightToLeft);

                // install machine factory and add to pattern cache

                this.machineFactory         = cmp.GetMachineFactory();
                this.machineFactory.Mapping = mapping;
                cache.Add(pattern, options, this.machineFactory);
            }
            else
            {
                this.group_count = this.machineFactory.GroupCount;
                this.mapping     = this.machineFactory.Mapping;
            }
        }
Beispiel #8
0
        private static IMachineFactory CreateMachineFactory(string pattern, RegexOptions options)
        {
            Parser            psr = new Parser();
            RegularExpression re  = psr.ParseRegularExpression(pattern, options);

#if NET_2_1
            ICompiler cmp = new PatternCompiler();
#else
            ICompiler cmp;
            if (!old_rx)
            {
                if ((options & RegexOptions.Compiled) != 0)
                {
                    cmp = new CILCompiler();
                }
                else
                {
                    cmp = new RxCompiler();
                }
            }
            else
            {
                cmp = new PatternCompiler();
            }
#endif

            re.Compile(cmp, (options & RegexOptions.RightToLeft) != 0);

            IMachineFactory machineFactory = cmp.GetMachineFactory();
            Hashtable       mapping        = new Hashtable();
            machineFactory.Gap          = psr.GetMapping(mapping);
            machineFactory.Mapping      = mapping;
            machineFactory.NamesMapping = GetGroupNamesArray(machineFactory.GroupCount, machineFactory.Mapping);

            return(machineFactory);
        }
Beispiel #9
0
        public static string DisassembleOp(ushort[] image, int pc)
        {
            OpCode  opCode;
            OpFlags opFlags;

            PatternCompiler.DecodeOp(image[pc], out opCode, out opFlags);
            string text = opCode.ToString();

            if (opFlags != OpFlags.None)
            {
                text = text + "[" + opFlags.ToString("f") + "]";
            }
            switch (opCode)
            {
            case OpCode.Position:
                text = text + " /" + (Position)image[pc + 1];
                break;

            case OpCode.String:
                text = text + " '" + Disassembler.ReadString(image, pc + 1) + "'";
                break;

            case OpCode.Reference:
            case OpCode.Open:
            case OpCode.Close:
                text = text + " " + image[pc + 1];
                break;

            case OpCode.Character:
                text = text + " '" + Disassembler.FormatChar((char)image[pc + 1]) + "'";
                break;

            case OpCode.Category:
            case OpCode.NotCategory:
                text = text + " /" + (Category)image[pc + 1];
                break;

            case OpCode.Range:
                text = text + " '" + Disassembler.FormatChar((char)image[pc + 1]) + "', ";
                text = text + " '" + Disassembler.FormatChar((char)image[pc + 2]) + "'";
                break;

            case OpCode.Set:
                text = text + " " + Disassembler.FormatSet(image, pc + 1);
                break;

            case OpCode.In:
            case OpCode.Sub:
            case OpCode.Branch:
            case OpCode.Jump:
                text = text + " :" + Disassembler.FormatAddress(pc + (int)image[pc + 1]);
                break;

            case OpCode.Balance:
            {
                string text2 = text;
                text = string.Concat(new object[]
                    {
                        text2,
                        " ",
                        image[pc + 1],
                        " ",
                        image[pc + 2]
                    });
                break;
            }

            case OpCode.IfDefined:
            case OpCode.Anchor:
                text = text + " :" + Disassembler.FormatAddress(pc + (int)image[pc + 1]);
                text = text + " " + image[pc + 2];
                break;

            case OpCode.Test:
                text = text + " :" + Disassembler.FormatAddress(pc + (int)image[pc + 1]);
                text = text + ", :" + Disassembler.FormatAddress(pc + (int)image[pc + 2]);
                break;

            case OpCode.Repeat:
            case OpCode.FastRepeat:
            {
                text = text + " :" + Disassembler.FormatAddress(pc + (int)image[pc + 1]);
                string text2 = text;
                text = string.Concat(new object[]
                    {
                        text2,
                        " (",
                        image[pc + 2],
                        ", "
                    });
                if (image[pc + 3] == 65535)
                {
                    text += "Inf";
                }
                else
                {
                    text += image[pc + 3];
                }
                text += ")";
                break;
            }

            case OpCode.Info:
            {
                text = text + " " + image[pc + 1];
                string text2 = text;
                text = string.Concat(new object[]
                    {
                        text2,
                        " (",
                        image[pc + 2],
                        ", ",
                        image[pc + 3],
                        ")"
                    });
                break;
            }
            }
            return(text);
        }
Beispiel #10
0
        public static void DisassembleBlock(ushort[] image, int pc, int depth)
        {
            while (pc < image.Length)
            {
                OpCode  opCode;
                OpFlags opFlags;
                PatternCompiler.DecodeOp(image[pc], out opCode, out opFlags);
                Console.Write(Disassembler.FormatAddress(pc) + ": ");
                Console.Write(new string(' ', depth * 2));
                Console.Write(Disassembler.DisassembleOp(image, pc));
                Console.WriteLine();
                int num;
                switch (opCode)
                {
                case OpCode.False:
                case OpCode.True:
                case OpCode.Until:
                    num = 1;
                    break;

                case OpCode.Position:
                case OpCode.Reference:
                case OpCode.Character:
                case OpCode.Category:
                case OpCode.NotCategory:
                case OpCode.In:
                case OpCode.Open:
                case OpCode.Close:
                case OpCode.Sub:
                case OpCode.Branch:
                case OpCode.Jump:
                    num = 2;
                    break;

                case OpCode.String:
                    num = (int)(image[pc + 1] + 2);
                    break;

                case OpCode.Range:
                case OpCode.Balance:
                case OpCode.IfDefined:
                case OpCode.Test:
                case OpCode.Anchor:
                    num = 3;
                    break;

                case OpCode.Set:
                    num = (int)(image[pc + 2] + 3);
                    break;

                case OpCode.BalanceStart:
                    goto IL_F7;

                case OpCode.Repeat:
                case OpCode.FastRepeat:
                case OpCode.Info:
                    num = 4;
                    break;

                default:
                    goto IL_F7;
                }
IL_FE:
                pc += num;
                continue;
IL_F7:
                num = 1;
                goto IL_FE;
            }
        }
Beispiel #11
0
        public static string DisassembleOp(ushort[] image, int pc)
        {
            OpCode  op;
            OpFlags flags;

            PatternCompiler.DecodeOp(image[pc], out op, out flags);
            string str = op.ToString();

            if (flags != 0)
            {
                str += "[" + flags.ToString("f") + "]";
            }

            switch (op)
            {
            case OpCode.False:
            case OpCode.True:
            case OpCode.Until:
            default:
                break;

            case OpCode.Info:
                str += " " + image[pc + 1];
                str += " (" + image[pc + 2] + ", " + image[pc + 3] + ")";
                break;

            case OpCode.Character:
                str += " '" + FormatChar((char)image[pc + 1]) + "'";
                break;

            case OpCode.Category:
                str += " /" + (Category)image[pc + 1];
                break;

            case OpCode.Range:
                str += " '" + FormatChar((char)image[pc + 1]) + "', ";
                str += " '" + FormatChar((char)image[pc + 2]) + "'";
                break;

            case OpCode.Set:
                str += " " + FormatSet(image, pc + 1);
                break;

            case OpCode.String:
                str += " '" + ReadString(image, pc + 1) + "'";
                break;

            case OpCode.Position:
                str += " /" + (Position)image[pc + 1];
                break;

            case OpCode.Open:
            case OpCode.Close:
            case OpCode.Reference:
                str += " " + image[pc + 1];
                break;

            case OpCode.Balance:
                str += " " + image[pc + 1] + " " + image[pc + 2];
                break;

            case OpCode.IfDefined:
            case OpCode.Anchor:
                str += " :" + FormatAddress(pc + image[pc + 1]);
                str += " " + image[pc + 2];
                break;

            case OpCode.Sub:
            case OpCode.Branch:
            case OpCode.Jump:
            case OpCode.In:
                str += " :" + FormatAddress(pc + image[pc + 1]);
                break;

            case OpCode.Test:
                str += " :" + FormatAddress(pc + image[pc + 1]);
                str += ", :" + FormatAddress(pc + image[pc + 2]);
                break;

            case OpCode.Repeat:
            case OpCode.FastRepeat:
                str += " :" + FormatAddress(pc + image[pc + 1]);
                str += " (" + image[pc + 2] + ", ";
                if (image[pc + 3] == 0xffff)
                {
                    str += "Inf";
                }
                else
                {
                    str += image[pc + 3];
                }
                str += ")";
                break;
            }

            return(str);
        }
Beispiel #12
0
        public static void DisassembleBlock(ushort[] image, int pc, int depth)
        {
            OpCode  op;
            OpFlags flags;

            for (;;)
            {
                if (pc >= image.Length)
                {
                    return;
                }

                PatternCompiler.DecodeOp(image[pc], out op, out flags);
                Console.Write(FormatAddress(pc) + ": ");                                // address
                Console.Write(new string (' ', depth * 2));                             // indent
                Console.Write(DisassembleOp(image, pc));                                // instruction
                Console.WriteLine();

                int skip;
                switch (op)
                {
                case OpCode.False:
                case OpCode.True:
                case OpCode.Until:
                    skip = 1;
                    break;

                case OpCode.Character:
                case OpCode.Category:
                case OpCode.Position:
                case OpCode.Open:
                case OpCode.Close:
                case OpCode.Reference:
                case OpCode.Sub:
                case OpCode.Branch:
                case OpCode.Jump:
                case OpCode.In:
                    skip = 2;
                    break;

                case OpCode.Balance:
                case OpCode.IfDefined:
                case OpCode.Range:
                case OpCode.Test:
                case OpCode.Anchor:
                    skip = 3;
                    break;

                case OpCode.Repeat:
                case OpCode.FastRepeat:
                case OpCode.Info:
                    skip = 4;
                    break;

                case OpCode.String: skip = image[pc + 1] + 2; break;

                case OpCode.Set: skip = image[pc + 2] + 3; break;

                default:
                    skip = 1;
                    break;
                }

                pc += skip;
            }
        }
Beispiel #13
0
		public Regex (string pattern, RegexOptions options) {
			this.pattern = pattern;
			this.roptions = options;
		
			this.machineFactory = cache.Lookup (pattern, options);

			if (this.machineFactory == null) {
				// parse and install group mapping

				Parser psr = new Parser ();
				RegularExpression re = psr.ParseRegularExpression (pattern, options);
				this.group_count = re.GroupCount;
				this.mapping = psr.GetMapping ();

				// compile
				
				ICompiler cmp;
				//if ((options & RegexOptions.Compiled) != 0)
				//	//throw new Exception ("Not implemented.");
				//	cmp = new CILCompiler ();
				//else
					cmp = new PatternCompiler ();

				re.Compile (cmp, RightToLeft);

				// install machine factory and add to pattern cache

				this.machineFactory = cmp.GetMachineFactory ();
				this.machineFactory.Mapping = mapping;
				cache.Add (pattern, options, this.machineFactory);
			} else {
				this.group_count = this.machineFactory.GroupCount;
				this.mapping = this.machineFactory.Mapping;
			}
		}
Beispiel #14
0
 public void EmitReference(int gid, bool ignore, bool reverse)
 {
     this.Emit(OpCode.Reference, PatternCompiler.MakeFlags(false, ignore, reverse, false));
     this.Emit((ushort)gid);
 }
Beispiel #15
0
 public void EmitRange(char lo, char hi, bool negate, bool ignore, bool reverse)
 {
     this.Emit(OpCode.Range, PatternCompiler.MakeFlags(negate, ignore, reverse, false));
     this.Emit((ushort)lo);
     this.Emit((ushort)hi);
 }
Beispiel #16
0
 public void EmitNotCategory(Category cat, bool negate, bool reverse)
 {
     this.Emit(OpCode.NotCategory, PatternCompiler.MakeFlags(negate, false, reverse, false));
     this.Emit((ushort)cat);
 }
Beispiel #17
0
 private void Emit(OpCode op, OpFlags flags)
 {
     this.Emit(PatternCompiler.EncodeOp(op, flags));
 }