Example #1
0
        internal static bool IsExpression(string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return(false);
            }

            var stream = new ExpressionStream(expression);

            return(stream.Start() && !stream.Start() && stream.SkipToEnd());
        }
Example #2
0
        /// <summary>
        /// Tokenize an expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>A stream of tokens representing the expression.</returns>
        /// <example>
        /// [parameters('vnetName')]
        /// [concat('route-', parameters('subnets')[copyIndex('routeIndex')].name)]
        /// [concat(split(parameters('addressPrefix')[0], '/')[0], '/27')]
        /// </example>
        internal static TokenStream Parse(string expression)
        {
            var output = new TokenStream();

            if (string.IsNullOrEmpty(expression))
            {
                return(output);
            }

            var stream    = new ExpressionStream(expression);
            var processed = false;

            stream.Start();
            while (!stream.End())
            {
                processed = false;
                if (stream.TryElement(out string element))
                {
                    stream.Separator();
                    if (int.TryParse(element, out int numeric))
                    {
                        output.Numeric(numeric);
                    }
                    else
                    {
                        output.Function(element);
                        Function(stream, output);
                    }
                    processed = true;
                }
                if (Index(stream, output))
                {
                    stream.Separator();
                    processed = true;
                }
                if (stream.CaptureProperty(out string propertyName))
                {
                    output.Property(propertyName);
                    stream.Separator();
                    processed = true;
                }
                if (stream.CaptureString(out string literal))
                {
                    output.String(literal);
                    stream.Separator();
                    processed = true;
                }
                if (!processed)
                {
                    throw new ExpressionParseException(expression, string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.ExpressionInvalid, expression));
                }
            }
            output.MoveTo(0);
            return(output);
        }