Esempio n. 1
0
        public void TestPathLikeVariableDeclaration(InterpolationKind kind, string expected)
        {
            var statement = SyntaxFactory.PathLikeConstVariableDeclaration("foo", kind, "someLiteral");
            var text      = statement.ToDisplayString();

            Assert.Equal(expected, text);
        }
Esempio n. 2
0
        /// <summary>
        /// Returns a factory name for a given <paramref name="kind"/>.
        /// </summary>
        public static string GetIdentifierName(this InterpolationKind kind)
        {
            switch (kind)
            {
            case InterpolationKind.StringInterpolation:
                // This is a legit case
                return(string.Empty);

            case InterpolationKind.PathInterpolation:
                return(new string(Names.PathInterpolationFactory, 1));

            case InterpolationKind.FileInterpolation:
                return(new string(Names.FileInterpolationFactory, 1));

            case InterpolationKind.DirectoryInterpolation:
                return(new string(Names.DirectoryInterpolationFactory, 1));

            case InterpolationKind.PathAtomInterpolation:
                return(new string(Names.PathAtomInterpolationFactory, 1));

            case InterpolationKind.RelativePathInterpolation:
                return(new string(Names.RelativePathInterpolationFactory, 1));

            default:
                throw new ArgumentOutOfRangeException(nameof(kind), I($"Unknown interpolation kind '{kind}'."));
            }
        }
Esempio n. 3
0
        internal static InterpolationOperation DeserializeInterpolationOperation(JsonElement element)
        {
            Optional <InterpolationKind>     kind     = default;
            Optional <InterpolationBoundary> boundary = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("kind"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    kind = new InterpolationKind(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("boundary"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    boundary = InterpolationBoundary.DeserializeInterpolationBoundary(property.Value);
                    continue;
                }
            }
            return(new InterpolationOperation(Optional.ToNullable(kind), boundary.Value));
        }
Esempio n. 4
0
            public ProcessedTagTemplateExpression(
                ITaggedTemplateExpression taggedTemplate,
                InterpolationKind kind,
                ILiteralExpression literal,
                ITemplateLiteralFragment head,
                INodeArray <ITemplateSpan> templateSpans)
            {
                Contract.Requires(taggedTemplate != null);
                Contract.Requires(
                    kind == InterpolationKind.Unknown || (literal != null || (head != null && templateSpans != null)),
                    "If interpolation is a well-known factory method, then Literal or Head+Templates should be valid.");

                TaggedTemplate = taggedTemplate;
                Kind           = kind;
                Literal        = literal;
                Head           = head;
                TemplateSpans  = templateSpans;
            }
Esempio n. 5
0
        /// <summary> Generic interpolator - from start to end in <paramref name="t" />. </summary>
        /// <param name="start">Starting position</param>
        /// <param name="end">Ending position.</param>
        /// <param name="t">
        ///     Amount to interpolate between the two values. 0 means <paramref name="start" /> and 1 means
        ///     <paramref name="end" />. Must be between 0.0 and 1.0.
        /// </param>
        /// <param name="kind">Interpolation type.</param>
        /// <returns>Position between start and end for the given <paramref name="t" />.</returns>
        /// <exception cref="ArgumentException">Unknown interpolation kind.</exception>
        public static double Do(double start, double end, double t,
                                InterpolationKind kind = InterpolationKind.Linear)
        {
            switch (kind)
            {
            case InterpolationKind.Linear:
                break;

            case InterpolationKind.Sin:
                t = System.Math.Sin(t * PiOver2);
                break;

            case InterpolationKind.Cos:
                t = 1 - System.Math.Cos(t * PiOver2);
                break;

            case InterpolationKind.Quadratic:
                t = t * t;
                break;

            case InterpolationKind.Cubic:
                t = t * t * t;
                break;

            case InterpolationKind.Smooth:
                t = t * t * (3 - 2 * t);
                break;

            case InterpolationKind.Smoothest:
                t = t * t * t * (t * (6 * t - 15) + 10);
                break;

            default:
                throw new ArgumentException("Unknown interpolation kind.", nameof(kind));
            }

            return((end - start) * t + start);
        }
Esempio n. 6
0
        /// <summary> Generic interpolator - from start to end in <paramref name="t" />. </summary>
        /// <param name="start">Starting position</param>
        /// <param name="end">Ending position.</param>
        /// <param name="t">
        ///     Amount to interpolate between the two values. 0 means <paramref name="start" /> and 1 means
        ///     <paramref name="end" />. Must be between 0.0 and 1.0.
        /// </param>
        /// <param name="kind">Interpolation type.</param>
        /// <returns>Position between start and end for the given <paramref name="t" />.</returns>
        /// <exception cref="ArgumentException">Unknown interpolation kind.</exception>
        public static float Do(float start, float end, float t, InterpolationKind kind = InterpolationKind.Linear)
        {
            switch (kind)
            {
            case InterpolationKind.Linear:
                break;

            case InterpolationKind.Sin:
                t = (float)System.Math.Sin(t * PiOver2f);
                break;

            case InterpolationKind.Cos:
                t = (float)(1 - System.Math.Cos(t * PiOver2f));
                break;

            case InterpolationKind.Quadratic:
                t = t * t;
                break;

            case InterpolationKind.Cubic:
                t = t * t * t;
                break;

            case InterpolationKind.Smooth:
                t = t * t * (3f - 2f * t);
                break;

            case InterpolationKind.Smoothest:
                t = t * t * t * (t * (6f * t - 15f) + 10f);
                break;

            default:
                throw new ArgumentException("Unknown interpolation kind.", nameof(kind));
            }

            return((end - start) * t + start);
        }
Esempio n. 7
0
        /// <summary>
        /// Deconstructs a template expression.
        /// </summary>
        /// <remarks>
        /// "pattern matches" a tagged template expression into two cases:
        /// 1. Literal case like <code>p`string literal`</code> (in this case <paramref name="literal"/> would not be null).
        /// 2. Template expression case like <code>p`{foo}</code> (in this case <paramref name="head"/> and <paramref name="templateSpans"/> are not null).
        /// </remarks>
        public static void Deconstruct(
            [CanBeNull] this ITaggedTemplateExpression node,
            out InterpolationKind kind,
            out ILiteralExpression literal,
            out ITemplateLiteralFragment head,
            out INodeArray <ITemplateSpan> templateSpans)
        {
            kind          = InterpolationKind.Unknown;
            literal       = null;
            head          = null;
            templateSpans = null;

            if (node == null)
            {
                return;
            }

            if (node.Tag.Kind != SyntaxKind.Identifier)
            {
                // Looks like the tagged expression is invalid.
                return;
            }

            var text = node.Tag.Cast <IIdentifier>().Text;

            kind = GetInterpolationKind(text);

            if (kind == InterpolationKind.Unknown)
            {
                return;
            }

            literal = node.TemplateExpression.As <ILiteralExpression>();
            if (literal == null)
            {
                // This is another case: tagged template actually has template expressions.
                var template = node.TemplateExpression.Cast <ITemplateExpression>();
                head          = template.Head;
                templateSpans = template?.TemplateSpans;

                Contract.Assert(head != null);
                Contract.Assert(templateSpans != null);
            }

            InterpolationKind GetInterpolationKind(string factoryName)
            {
                if (factoryName.Length == 0)
                {
                    return(InterpolationKind.StringInterpolation);
                }

                var c = factoryName[0];

                switch (c)
                {
                case Names.PathInterpolationFactory:
                    return(InterpolationKind.PathInterpolation);

                case Names.DirectoryInterpolationFactory:
                    return(InterpolationKind.DirectoryInterpolation);

                case Names.FileInterpolationFactory:
                    return(InterpolationKind.FileInterpolation);

                case Names.RelativePathInterpolationFactory:
                    return(InterpolationKind.RelativePathInterpolation);

                case Names.PathAtomInterpolationFactory:
                    return(InterpolationKind.PathAtomInterpolation);

                default:
                    return(InterpolationKind.Unknown);
                }
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Creates a path-like expression with a given kind and text.
 /// </summary>
 public static ITaggedTemplateExpression PathLikeLiteral(InterpolationKind kind, IExpression firstExpression, string literalText)
 {
     return(new TaggedTemplateExpression(kind.GetIdentifierName(), firstExpression, literalText));
 }
Esempio n. 9
0
        /// <summary>
        /// Creates a variable statement with path-like interpolation literal.
        /// </summary>
        public static IStatement PathLikeConstVariableDeclaration(string variableName, InterpolationKind kind, string literal, Visibility visibility = Visibility.None)
        {
            Contract.Requires(!string.IsNullOrEmpty(variableName));
            Contract.Requires(kind != InterpolationKind.Unknown);
            Contract.Requires(!string.IsNullOrEmpty(literal));

            return(new VariableDeclarationBuilder()
                   .Const()
                   .Visibility(visibility)
                   .Name(variableName)
                   .Initializer(PathLikeLiteral(kind, literal))
                   .Build());
        }