예제 #1
0
        private Tuple <bool, Expr> IsMatch(FluentPluginHelper helper, Type type, Token klass, Token instance, Token prop, Token method)
        {
            var memberName = string.Empty;
            var rootVar    = string.Empty;
            var match      = false;
            var nameToken  = klass;

            // 1. Class property
            if (klass != null && prop != null)
            {
                rootVar = klass.Text;
                if (helper.IsClassProp(type, prop.Text))
                {
                    memberName = prop.Text;
                    match      = true;
                }
            }
            // 2. Class method
            else if (klass != null && method != null)
            {
                rootVar = type.Name;
                if (helper.IsClassMethod(type, method.Text))
                {
                    memberName = method.Text;
                    match      = true;
                }
            }
            // 3. Instance property
            else if (instance != null && prop != null)
            {
                rootVar = instance.Text;
                if (helper.IsInstanceProp(type, prop.Text))
                {
                    memberName = prop.Text;
                    match      = true;
                    nameToken  = instance;
                }
            }
            // 4. Instance method
            else if (instance != null && method != null)
            {
                rootVar = instance.Text;
                if (helper.IsInstanceMethod(type, method.Text))
                {
                    memberName = method.Text;
                    match      = true;
                    nameToken  = instance;
                }
            }
            if (!match)
            {
                return(new Tuple <bool, Expr>(false, null));
            }

            var varExp = Exprs.Ident(rootVar, null);
            var memExp = Exprs.MemberAccess(varExp, memberName, false, null);

            return(new Tuple <bool, Expr>(memberName != null, memExp));
        }
예제 #2
0
        private Tuple <Type, Expr> Match(string groupName, FluentPart part)
        {
            var  group  = _matches[groupName];
            var  ctx    = _parser.Context;
            var  helper = new FluentPluginHelper(ctx);
            Expr expr   = null;
            Type type   = null;

            // 1. Go through all the matches in group. e.g. "class"
            // Note: This is at most 4 possible groups ( class, instance, property, method )
            for (int ndx = 0; ndx < group.Count; ndx++)
            {
                var   match = group[ndx];
                Token klass = null, instance = null, method = null, prop = null;
                var   token = _tokenIt.NextToken.Token;

                // 2. Go through all the matches in each group
                // Note: There are at most 4 matches.
                int lastTokenPeek = 0;
                for (int ndxT = 0; ndxT < match.Count; ndxT++)
                {
                    part = match[ndxT];
                    if (part == FluentPart.Class)
                    {
                        klass = token;
                    }
                    else if (part == FluentPart.Instance)
                    {
                        instance = token;
                    }
                    else if (part == FluentPart.Method)
                    {
                        method = token;
                    }
                    else if (part == FluentPart.Prop)
                    {
                        prop = token;
                    }

                    lastTokenPeek = ndxT;
                    token         = _tokenIt.Peek(lastTokenPeek + 1, false).Token;
                }
                // Check if match.
                type = helper.GetType(klass, instance);
                if (type != null)
                {
                    var result = IsMatch(helper, type, klass, instance, prop, method);

                    // Success
                    if (result.Item1)
                    {
                        expr = result.Item2;
                        _tokenIt.Advance(lastTokenPeek + 1);
                        break;
                    }
                }
            }
            return(new Tuple <Type, Expr>(type, expr));
        }
예제 #3
0
        /* ****************************************************************************************
         * The following syntax can be supported via this Fluent expression combinator
         *
         * 1. activate user.
         * 2. move file "c:\temp.txt".
         * 3. file "c:\temp.txt" exists.
         * 3. run program "msbuild.exe", solution: 'comlib.sln', mode: "debug", style: "rebuild all".
         *
         * 1. <method> <class> .
         * 2. <method> <class> <arg1> <arg2> .
         * 3. <class>  <arg1>  <method> .
         * 3. <method> <class> <arg1>, <arg1_name> : <arg1_value>, <arg2_name> : <arg2_value> .
         *
         ***************************************************************************************** */
        /// <summary>
        /// Parses the fluent expression.
        /// </summary>
        /// <returns></returns>
        public override Expr Parse()
        {
            var  ctx    = _parser.Context;
            var  helper = new FluentPluginHelper(ctx);
            var  token  = _tokenIt.NextToken;
            Expr mexp   = null;
            Tuple <Type, Expr> matchResult = null;

            // 1. Check if instance variable.
            if (helper.IsInstance(token.Token))
            {
                matchResult = Match("instance", FluentPart.Instance);
                mexp        = matchResult.Item2;
            }
            // 2. Check if class
            else if (helper.IsClass(token.Token))
            {
                matchResult = Match("class", FluentPart.Class);
                mexp        = matchResult.Item2;
            }
            // 3. Has to be method or prop
            else
            {
                matchResult = Match("method", FluentPart.Method);
                mexp        = matchResult.Item2;
                if (mexp == null)
                {
                    matchResult = Match("prop", FluentPart.Prop);
                    mexp        = matchResult.Item2;
                }
            }
            if (mexp != null)
            {
                // TODO: Performance improvement.
                var memExp = mexp as MemberAccessExpr;
                if (helper.IsClassMethod(matchResult.Item1, memExp.MemberName) || helper.IsInstanceMethod(matchResult.Item1, memExp.MemberName))
                {
                    mexp = ParseParams(mexp);
                }
            }
            return(mexp);
        }
예제 #4
0
        private Tuple<bool, Expr> IsMatch(FluentPluginHelper helper, Type type, Token klass, Token instance, Token prop, Token method)
        {
            var memberName = string.Empty;
            var rootVar = string.Empty;
            var match = false;
            var nameToken = klass;

            // 1. Class property
            if (klass != null && prop != null)
            {
                rootVar = klass.Text;
                if (helper.IsClassProp(type, prop.Text))
                {
                    memberName = prop.Text;
                    match = true;
                }
            }
            // 2. Class method
            else if (klass != null && method != null)
            {
                rootVar = type.Name;
                if (helper.IsClassMethod(type, method.Text))
                {   
                    memberName = method.Text;
                    match = true;
                }
            }
            // 3. Instance property
            else if (instance != null && prop != null)
            {
                rootVar = instance.Text;
                if (helper.IsInstanceProp(type, prop.Text))
                {
                    memberName = prop.Text;
                    match = true;
                    nameToken = instance;
                }
            }
            // 4. Instance method
            else if (instance != null && method != null)
            {
                rootVar = instance.Text;
                if (helper.IsInstanceMethod(type, method.Text))
                {
                    memberName = method.Text;
                    match = true;
                    nameToken = instance;
                }
            }
            if (!match)
                return new Tuple<bool, Expr>(false, null);

            var varExp = Exprs.Ident(rootVar, null);
            var memExp = Exprs.MemberAccess(varExp, memberName, false, null);
            return new Tuple<bool, Expr>(memberName != null, memExp);
        }
예제 #5
0
        private Tuple<Type, Expr>  Match(string groupName, FluentPart part)
        {
            var group = _matches[groupName];            
            var ctx = _parser.Context;
            var helper = new FluentPluginHelper(ctx);
            Expr expr = null;
            Type type = null;

            // 1. Go through all the matches in group. e.g. "class" 
            // Note: This is at most 4 possible groups ( class, instance, property, method )
            for (int ndx = 0; ndx < group.Count; ndx++)
            {
                var match = group[ndx];
                Token klass = null, instance = null, method = null, prop = null;
                var token = _tokenIt.NextToken.Token;
            
                // 2. Go through all the matches in each group
                // Note: There are at most 4 matches.
                int lastTokenPeek = 0;
                for (int ndxT = 0; ndxT < match.Count; ndxT++)
                {
                    part = match[ndxT];
                    if (part == FluentPart.Class) klass = token;
                    else if (part == FluentPart.Instance) instance = token;
                    else if (part == FluentPart.Method) method = token;
                    else if (part == FluentPart.Prop) prop = token;

                    lastTokenPeek = ndxT;
                    token = _tokenIt.Peek(lastTokenPeek + 1, false).Token;
                }
                // Check if match.
                type = helper.GetType(klass, instance);
                if (type != null)
                {
                    var result = IsMatch(helper, type, klass, instance, prop, method);

                    // Success
                    if (result.Item1)
                    {
                        expr = result.Item2;
                        _tokenIt.Advance(lastTokenPeek + 1);
                        break;
                    }
                }
            }
            return new Tuple<Type, Expr>(type, expr);
        }
예제 #6
0
        /* ****************************************************************************************
         * The following syntax can be supported via this Fluent expression combinator
         * 
         * 1. activate user.         
         * 2. move file "c:\temp.txt".
         * 3. file "c:\temp.txt" exists.
         * 3. run program "msbuild.exe", solution: 'comlib.sln', mode: "debug", style: "rebuild all".
         * 
         * 1. <method> <class> .
         * 2. <method> <class> <arg1> <arg2> .
         * 3. <class>  <arg1>  <method> .
         * 3. <method> <class> <arg1>, <arg1_name> : <arg1_value>, <arg2_name> : <arg2_value> .
         * 
        ***************************************************************************************** */
        /// <summary>
        /// Parses the fluent expression.
        /// </summary>
        /// <returns></returns>
        public override Expr Parse()
        {
            var ctx = _parser.Context;
            var helper = new FluentPluginHelper(ctx);
            var token = _tokenIt.NextToken;
            Expr mexp = null;
            Tuple<Type, Expr> matchResult = null;

            // 1. Check if instance variable.
            if (helper.IsInstance(token.Token))
            {
                matchResult = Match("instance", FluentPart.Instance);
                mexp = matchResult.Item2;
            }
            // 2. Check if class 
            else if (helper.IsClass(token.Token))
            {
                matchResult = Match("class", FluentPart.Class);
                mexp = matchResult.Item2;
            }
            // 3. Has to be method or prop
            else
            {
                matchResult = Match("method", FluentPart.Method);
                mexp = matchResult.Item2;
                if(mexp == null)
                {
                    matchResult = Match("prop", FluentPart.Prop);
                    mexp = matchResult.Item2;
                }
            }
            if (mexp != null )
            {
                // TODO: Performance improvement.
                var memExp = mexp as MemberAccessExpr;
                if (helper.IsClassMethod(matchResult.Item1, memExp.MemberName) || helper.IsInstanceMethod(matchResult.Item1, memExp.MemberName))
                {
                    mexp = ParseParams(mexp);    
                }                
            }
            return mexp;
        }