static bool TryCompileImpl(
            string expression,
            NameResolver?nameResolver,
            [MaybeNullWhen(false)] out CompiledExpression result,
            [MaybeNullWhen(true)] out string error)
        {
            if (!ExpressionParser.TryParse(expression, out var root, out error))
            {
                result = null;
                return(false);
            }

            result = ExpressionCompiler.Compile(root, DefaultFunctionNameResolver.Build(nameResolver));
            error  = null;
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Construct an <see cref="ExpressionTemplate"/>.
        /// </summary>
        /// <param name="template">The template text.</param>
        /// <param name="formatProvider">Optionally, an <see cref="IFormatProvider"/> to use when formatting
        /// embedded values.</param>
        /// <param name="nameResolver">Optionally, a <see cref="NameResolver"/>
        /// with which to resolve function names that appear in the template.</param>
        public ExpressionTemplate(
            string template,
            IFormatProvider?formatProvider = null,
            NameResolver?nameResolver      = null)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            if (!TemplateParser.TryParse(template, out var parsed, out var error))
            {
                throw new ArgumentException(error);
            }

            _compiled       = TemplateCompiler.Compile(parsed, DefaultFunctionNameResolver.Build(nameResolver));
            _formatProvider = formatProvider;
        }
コード例 #3
0
        /// <summary>
        /// Construct an <see cref="ExpressionTemplate"/>.
        /// </summary>
        /// <param name="template">The template text.</param>
        /// <param name="formatProvider">Optionally, an <see cref="IFormatProvider"/> to use when formatting
        /// embedded values.</param>
        /// <param name="result">The parsed template, if successful.</param>
        /// <param name="error">A description of the error, if unsuccessful.</param>
        /// <param name="nameResolver">Optionally, a <see cref="NameResolver"/>
        /// with which to resolve function names that appear in the template.</param>
        /// <returns><c langword="true">true</c> if the template was well-formed.</returns>
        public static bool TryParse(
            string template,
            IFormatProvider?formatProvider,
            NameResolver?nameResolver,
            [MaybeNullWhen(false)] out ExpressionTemplate result,
            [MaybeNullWhen(true)] out string error)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            if (!TemplateParser.TryParse(template, out var parsed, out error))
            {
                result = null;
                return(false);
            }

            result = new ExpressionTemplate(TemplateCompiler.Compile(parsed, DefaultFunctionNameResolver.Build(nameResolver)), formatProvider);
            return(true);
        }