예제 #1
0
        public void FindAll_CmpRegex()
        {
            KeywordRule acsRule = new KeywordRule
            {
                Keywords = "abstract,as,base,bool,break,byte,case,catch,char,checked,class,const,continue,decimal,default,delegate,do,double,else,enum,event,explicit,extern,false,finally,fixed,float,for,foreach,goto,if,implicit,in,int,interface,internal,is,lock,long,namespace,new,null,object,operator,out,override,params,private,protected,public,readonly,ref,return,sbyte,sealed,short,sizeof,stackalloc,static,string,struct,switch,this,throw,true,try,typeof,uint,ulong,unchecked,unsafe,ushort,using,using,static,virtual,void,volatile,while,get,set,yield,var"
            };
            RegexRule regexRule = new RegexRule
            {
                Pattern = @"\babstract\b|\bas\b|\bbase\b|\bbool\b|\bbreak\b|\bbyte\b|\bcase\b|\bcatch\b|\bchar\b|\bchecked\b|\bclass\b|\bconst\b|\bcontinue\b|\bdecimal\b|\bdefault\b|\bdelegate\b|\bdo\b|\bdouble\b|\belse\b|\benum\b|\bevent\b|\bexplicit\b|\bextern\b|\bfalse\b|\bfinally\b|\bfixed\b|\bfloat\b|\bfor\b|\bforeach\b|\bgoto\b|\bif\b|\bimplicit\b|\bin\b|\bint\b|\binterface\b|\binternal\b|\bis\b|\block\b|\blong\b|\bnamespace\b|\bnew\b|\bnull\b|\bobject\b|\boperator\b|\bout\b|\boverride\b|\bparams\b|\bprivate\b|\bprotected\b|\bpublic\b|\breadonly\b|\bref\b|\breturn\b|\bsbyte\b|\bsealed\b|\bshort\b|\bsizeof\b|\bstackalloc\b|\bstatic\b|\bstring\b|\bstruct\b|\bswitch\b|\bthis\b|\bthrow\b|\btrue\b|\btry\b|\btypeof\b|\buint\b|\bulong\b|\bunchecked\b|\bunsafe\b|\bushort\b|\busing\b|\busing\b|\bstatic\b|\bvirtual\b|\bvoid\b|\bvolatile\b|\bwhile\b|\bget\b|\bset\b|\byield\b|\bvar\b"
            };

            string file  = Path.Combine(Environment.CurrentDirectory, @"..\..\..\UI.SyntaxBox\SyntaxRenderer.cs");
            var    input = File.ReadAllLines(file);

            var regex = input
                        .SelectMany((x) => regexRule.Match(x))
                        .OrderBy((x) => x.FromChar)
                        .ThenBy((x) => x.Length)
                        .ToList();
            var acs = input
                      .SelectMany((x) => acsRule.Match(x))
                      .OrderBy((x) => x.FromChar)
                      .ThenBy((x) => x.Length)
                      .ToList();

            Assert.AreEqual(regex.Count, acs.Count, "Not the same number of matches");
            Assert.IsTrue(Enumerable.SequenceEqual(regex, acs, new FormatInstructionCmp()), "Different matches");
        }
예제 #2
0
        public void ParseInvalidSpecificKeyword()
        {
            var t_Rule = new KeywordRule("Jmp");

            var t_Expression = t_Rule.Match(CreateState(new Token(TokenType.Keyword, "Add")));

            t_Expression
            .HasError()
            .Should()
            .BeTrue();
        }
예제 #3
0
        public void FindAll_PerformanceVsRegex()
        {
            KeywordRule acsRule = new KeywordRule
            {
                Keywords = "abstract,as,base,bool,break,byte,case,catch,char,checked,class,const,continue,decimal,default,delegate,do,double,else,enum,event,explicit,extern,false,finally,fixed,float,for,foreach,goto,if,implicit,in,int,interface,internal,is,lock,long,namespace,new,null,object,operator,out,override,params,private,protected,public,readonly,ref,return,sbyte,sealed,short,sizeof,stackalloc,static,string,struct,switch,this,throw,true,try,typeof,uint,ulong,unchecked,unsafe,ushort,using,using,static,virtual,void,volatile,while,get,set,yield,var"
            };

            acsRule.Match("empty"); // Warm it up

            RegexRule regexRule = new RegexRule
            {
                Pattern = @"\babstract\b|\bas\b|\bbase\b|\bbool\b|\bbreak\b|\bbyte\b|\bcase\b|\bcatch\b|\bchar\b|\bchecked\b|\bclass\b|\bconst\b|\bcontinue\b|\bdecimal\b|\bdefault\b|\bdelegate\b|\bdo\b|\bdouble\b|\belse\b|\benum\b|\bevent\b|\bexplicit\b|\bextern\b|\bfalse\b|\bfinally\b|\bfixed\b|\bfloat\b|\bfor\b|\bforeach\b|\bgoto\b|\bif\b|\bimplicit\b|\bin\b|\bint\b|\binterface\b|\binternal\b|\bis\b|\block\b|\blong\b|\bnamespace\b|\bnew\b|\bnull\b|\bobject\b|\boperator\b|\bout\b|\boverride\b|\bparams\b|\bprivate\b|\bprotected\b|\bpublic\b|\breadonly\b|\bref\b|\breturn\b|\bsbyte\b|\bsealed\b|\bshort\b|\bsizeof\b|\bstackalloc\b|\bstatic\b|\bstring\b|\bstruct\b|\bswitch\b|\bthis\b|\bthrow\b|\btrue\b|\btry\b|\btypeof\b|\buint\b|\bulong\b|\bunchecked\b|\bunsafe\b|\bushort\b|\busing\b|\busing\b|\bstatic\b|\bvirtual\b|\bvoid\b|\bvolatile\b|\bwhile\b|\bget\b|\bset\b|\byield\b|\bvar\b"
            };

            regexRule.Match("empty"); // Warm it up

            int    iter  = 10;
            string file  = Path.Combine(Environment.CurrentDirectory, @"..\..\..\UI.SyntaxBox\SyntaxRenderer.cs");
            var    input = File.ReadAllLines(file);

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            for (int i = 0; i < iter; i++)
            {
                input.SelectMany((x) => regexRule.Match(x)).ToList();
            }
            sw.Stop();

            long regex = sw.ElapsedMilliseconds;

            sw.Reset();
            sw.Start();
            for (int i = 0; i < iter; i++)
            {
                input.SelectMany((x) => acsRule.Match(x)).ToList();
            }
            sw.Stop();
            long acs = sw.ElapsedMilliseconds;

            Assert.Inconclusive($"Regex: {regex}ms, Acs: {acs}ms");
        }
예제 #4
0
        public void ParseSpecificKeyword()
        {
            var t_Rule = new KeywordRule("Jmp");

            var t_Expression = t_Rule.Match(CreateState(new Token(TokenType.Keyword, "Jmp")));

            ((KeywordExpression)t_Expression.Value.Expressions.First())
            .Keyword
            .Text
            .Should()
            .Be("Jmp");
        }
예제 #5
0
        public void ParseAddKeyword()
        {
            var t_Rule = new KeywordRule();

            var t_Expression = t_Rule.Match(CreateState(new Token(TokenType.Keyword, "Add")));

            t_Expression
            .HasError()
            .Should()
            .BeFalse();

            t_Expression.Value.Expressions.First()
            .Should()
            .BeOfType <KeywordExpression>();

            ((KeywordExpression)t_Expression.Value.Expressions.First())
            .Keyword
            .Text
            .Should()
            .Be("Add");
        }