예제 #1
0
        public CsdlSyntax(bool rightToLeftParsing, bool perfectMatchOnly, CsdlToken[] tokens)
        {
            RightToLeftParsing = rightToLeftParsing;
            PerfectMatchOnly = perfectMatchOnly;

            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            Tokens = tokens;

            var syntaxTextBuilder = new StringBuilder();

            foreach (var csdlToken in tokens)
            {
                syntaxTextBuilder.AppendFormat("{0} ", csdlToken.TokenText);
            }

            SyntaxText = syntaxTextBuilder.ToString().Trim();

            if (PerfectMatchOnly)
            {
                SyntaxText = $"[{SyntaxText}]";
            }

            if (RightToLeftParsing)
            {
                SyntaxText = $"{SyntaxText}^";
            }
        }
예제 #2
0
        /// <summary>
        /// Creates a simple CSDL string for a method.
        /// </summary>
        public static CsdlSyntax CreateBasicSyntaxForMethod(MethodInfo methodInfo, bool rightToLeftParsing, bool perfectMatchOnly)
        {
            if (methodInfo == null)
            {
                throw new ArgumentNullException(nameof(methodInfo));
            }

            var csdlTokenList = new List <CsdlToken>();

            foreach (var parameter in methodInfo.GetParameters())
            {
                var tokenTypeType =
                    TokenTypeDictionary
                    .FirstOrDefault(v => TypeUtil.GetGenericTokenTypeParameterType(v.Value) == parameter.ParameterType);

                if (tokenTypeType.Key != null)
                {
                    var csdlToken = new CsdlToken(parameter.Name, tokenTypeType.Key,
                                                  TypeUtil.IsNullable(parameter.ParameterType), false, null);
                    csdlTokenList.Add(csdlToken);
                }
                else if (parameter.ParameterType != typeof(IRequestContext) &&
                         parameter.ParameterType != typeof(Expression))
                {
                    throw new ArgumentException(
                              $"There's no registered token type for type '{parameter.ParameterType}'");
                }
            }

            var csdlSyntax = new CsdlSyntax(rightToLeftParsing, perfectMatchOnly, csdlTokenList.ToArray());

            return(csdlSyntax);
        }
예제 #3
0
        public CsdlSyntax(string syntaxText)
        {
            if (string.IsNullOrWhiteSpace(syntaxText))
            {
                throw new ArgumentNullException(nameof(syntaxText));
            }

            SyntaxText = syntaxText;
            syntaxText = syntaxText.Trim();

            // Parse anchor (direction)
            if (syntaxText[0] == ANCHOR_TOKEN)
            {
                RightToLeftParsing = false;
                syntaxText         = syntaxText.TrimStart(ANCHOR_TOKEN);
            }
            else if (syntaxText[syntaxText.Length - 1] == ANCHOR_TOKEN)
            {
                RightToLeftParsing = true;
                syntaxText         = syntaxText.TrimEnd(ANCHOR_TOKEN);
            }

            // Check for perfect match
            if (syntaxText.Length > 0 &&
                syntaxText[0] == PERFECT_MATCH_START &&
                syntaxText[syntaxText.Length - 1] == PERFECT_MATCH_END)
            {
                PerfectMatchOnly = true;
                syntaxText       = syntaxText.TrimStart(PERFECT_MATCH_START).TrimEnd(PERFECT_MATCH_END);
            }

            Tokens = CsdlToken.GetTokensFromPattern(syntaxText.Trim());
        }
예제 #4
0
        /// <summary>
        /// Gets the tokens from pattern.
        /// </summary>
        /// <param name="syntaxPattern">The syntax pattern.</param>
        /// <returns></returns>
        public static CsdlToken[] GetTokensFromPattern(string syntaxPattern)
        {
            var matches = Regex.Matches(syntaxPattern, TOKEN_PATTERN, RegexOptions.ExplicitCapture);
            var tokens  = new CsdlToken[matches.Count];

            for (var i = 0; i < matches.Count; i++)
            {
                tokens[i] = new CsdlToken(matches[i].Value, $"t{i}");
            }

            return(tokens);
        }
예제 #5
0
        /// <summary>
        /// Gets the tokens from pattern.
        /// </summary>
        /// <param name="syntaxPattern">The syntax pattern.</param>
        /// <returns></returns>
        public static CsdlToken[] GetTokensFromPattern(string syntaxPattern)
        {
            var matches = Regex.Matches(syntaxPattern, TOKEN_PATTERN, RegexOptions.ExplicitCapture);
            var tokens = new CsdlToken[matches.Count];

            for (var i = 0; i < matches.Count; i++)
            {
                tokens[i] = new CsdlToken(matches[i].Value, $"t{i}");
            }

            return tokens;
        }