예제 #1
0
        private List <Expression> EnumerateTaggedExpressionsForStringInterpolation(ITemplateExpression template, FunctionScope escapes, QualifierSpaceId currentQualifierSpaceId)
        {
            // Creating a list that will hold all potential expressions
            List <Expression> result = new List <Expression>((template.TemplateSpans.Length * 2) + 1);

            var head = template.Head.Text;

            if (!string.IsNullOrEmpty(head))
            {
                result.Add(LiteralConverter.ConvertStringLiteral(head, Location(template.Head)));
            }

            for (int i = 0; i < template.TemplateSpans.Length; i++)
            {
                var span = template.TemplateSpans[i];
                if (span.Expression != null)
                {
                    var convertedExpression = m_converter.ConvertExpression(span.Expression, escapes, currentQualifierSpaceId);
                    if (convertedExpression != null)
                    {
                        result.Add(convertedExpression);
                    }
                }

                var fragment = span.Literal.Text;

                if (!string.IsNullOrEmpty(fragment))
                {
                    result.Add(LiteralConverter.ConvertStringLiteral(fragment, Location(template.Head)));
                }
            }

            return(result);
        }
예제 #2
0
 public static void Validation(this ITemplateExpression expression, string s)
 {
     if (!Validation(s))
     {
         throw new Exception(string.Format("配置键和配置值均不能包含{0}字符", SPLITESPACER));
     }
 }
예제 #3
0
 private void AnalyzeTemplateExpression(ITemplateExpression source, int idx)
 {
     foreach (var t in source.Cast <ITemplateExpression>().TemplateSpans.AsStructEnumerable())
     {
         AnalyzeExpression(t.Expression, idx);
     }
 }
예제 #4
0
 public static void FromValues(this ITemplateExpression expression, IEnumerable <string> values)
 {
     foreach (string value in values)
     {
         Validation(value);
     }
     expression.Value = string.Join(SPLITESPACER, values.ToArray());
 }
예제 #5
0
 public static void FromKeys(this ITemplateExpression expression, IEnumerable <string> keys)
 {
     foreach (string key in keys)
     {
         Validation(key);
     }
     expression.Expression = string.Join(SPLITESPACER, keys.ToArray());
 }
예제 #6
0
        private static int ComparePathTemplateExpression(ITemplateExpression left, ITemplateExpression right)
        {
            Contract.Requires(left != null);
            Contract.Requires(right != null);

            var leftSteps  = new List <Tuple <PathStepKind, string> >();
            var rightSteps = new List <Tuple <PathStepKind, string> >();

            CollectSteps(left, leftSteps);
            CollectSteps(right, rightSteps);

            return(ComparePathSteps(leftSteps, rightSteps));
        }
예제 #7
0
        private static int CompareTemplateExpression(ITemplateExpression left, ITemplateExpression right, bool usePathCompareSemantics = false)
        {
            // Standard left,right and null checks
            if (left == null && right == null)
            {
                return(0);
            }
            if (left == null)
            {
                return(1);
            }
            if (right == null)
            {
                return(-1);
            }

            if (usePathCompareSemantics)
            {
                return(ComparePathTemplateExpression(left, right));
            }

            int result = 0;

            if (left.Head != null)
            {
                if (right.Head != null)
                {
                    result = CompareStringLiteralExpression(left.Head.Cast <ILiteralExpression>(), right.Head.Cast <ILiteralExpression>(), usePathCompareSemantics);
                }
                else
                {
                    return(-1); // Literals win over expressions;
                }
            }
            else if (right.Head == null)
            {
                return(1); // Literals win over expressions
            }

            if (result != 0)
            {
                return(result);
            }

            return(CompareNodeArrays(left.TemplateSpans, right.TemplateSpans, (l, r) => CompareTemplateSpan(l, r, usePathCompareSemantics)));
        }
예제 #8
0
        public static void AppendConfig(this ITemplateExpression expression, string Key, string Value)
        {
            if (string.IsNullOrEmpty(Key))
            {
                throw new Exception("配置键不能为空");
            }
            expression.Validation(Key);
            expression.Validation(Value);
            Dictionary <string, string> dic = new Dictionary <string, string>();

            foreach (KeyValuePair <string, string> pair in expression.ToKeyValuePairs())
            {
                dic.Add(pair.Key, pair.Value);
            }
            dic.Add(Key, Value);
            expression.FromKeys(dic.Keys);
            expression.FromValues(dic.Values);
        }
예제 #9
0
        /// <summary>
        /// Converts string interpolation expression like <code>let x = `${foo}`;</code>
        /// </summary>
        public Expression ConvertStringInterpolation(ITemplateExpression source, FunctionScope escapes, QualifierSpaceId currentQualifierSpaceId)
        {
            var taggedExpressions = EnumerateTaggedExpressionsForStringInterpolation(source, escapes, currentQualifierSpaceId);

            // There is one corner cases here:
            // If tagged expression is just a string literal, but has no expressions, we can just return it
            if (taggedExpressions.Count == 1)
            {
                if (taggedExpressions[0] is StringLiteral stringLiteral)
                {
                    return(stringLiteral);
                }
            }

            var applyExpression = ApplyExpression.Create(m_stringInterpolationSelectorExpression, taggedExpressions.ToArray(), LineInfo(source));

            return(new StringLiteralExpression(applyExpression, applyExpression.Location));
        }
예제 #10
0
        private static void CollectSteps(ITemplateExpression template, List <Tuple <PathStepKind, string> > steps)
        {
            if (template.Head != null)
            {
                CollectSteps(template.Head.Text, steps);
            }

            foreach (var span in template.TemplateSpans.AsStructEnumerable())
            {
                if (span.Expression != null)
                {
                    steps.Add(Tuple.Create(PathStepKind.Expression, span.Expression.ToDisplayString()));
                }

                if (span.Literal != null)
                {
                    CollectSteps(span.Literal.Text, steps);
                }
            }
        }
예제 #11
0
        public static IEnumerable <KeyValuePair <string, string> > ToKeyValuePairs(this ITemplateExpression expression)
        {
            Dictionary <string, string> dic = new Dictionary <string, string>();

            string[] exs = expression.Expression.Split(new string[] { SPLITESPACER }, StringSplitOptions.RemoveEmptyEntries);
            string[] vas = expression.Value.Split(new string[] { SPLITESPACER }, StringSplitOptions.None);
            if (exs.Length > 0)
            {
                if (exs.Length != vas.Length)
                {
                    throw new Exception("表达式跟值不匹配,请确保他们的元素一样多");
                }

                for (int i = 0; i < exs.Length; i++)
                {
                    dic[exs[i]] = vas[i];
                }
            }
            return(dic);
        }
예제 #12
0
 private static int FitsOnOneLine(ITemplateExpression expression, int remainingSpace)
 {
     // This is a recursion on the pretty printer which is not nice, but it is the simplest for now
     return(remainingSpace - expression.GetFormattedText().Length);
 }