Beispiel #1
0
        public static IFluidTemplate Parse(this FluidParser parser, string template)
        {
            var context = new FluidParseContext(template);

            var success = parser.Grammar.TryParse(context, out var statements, out var parlotError);

            if (parlotError != null)
            {
                // Extract line with error
                var start = parlotError.Position.Offset - 1;
                var end   = parlotError.Position.Offset;
                while (start > 0 && template[start] != '\n' && template[start] != '\r')
                {
                    start--;
                }
                while (end < template.Length && template[end] != '\n')
                {
                    end++;
                }
                var source = template.Substring(start, end - start).Trim('\n', '\r');

                throw new ParseException($"{parlotError.Message} at {parlotError.Position}\nSource:\n{source}");
            }

            if (!success)
            {
                return(null);
            }

            return(new FluidTemplate(statements));
        }
Beispiel #2
0
        /// <summary>
        /// Parse the supplied template into a collection of tokens that are recognized by the Fluid parser.
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        public static List <string> ParseToTokens(string template)
        {
            var tokens = new List <string>();

            var context = new FluidParseContext(template);

            var lavaTokens = LavaTokensListParser.Parse(template, context).Select(x => x.ToString()).ToList();

            // If the template contains only literal text, add the entire content as a single text token.
            if (!lavaTokens.Any() &&
                !string.IsNullOrEmpty(template))
            {
                lavaTokens.Add(template);
            }

            return(lavaTokens);
        }
Beispiel #3
0
        public static IFluidTemplate Parse(this FluidParser parser, string template)
        {
            var context = new FluidParseContext(template);

            var success = parser.Grammar.TryParse(context, out var statements, out var parlotError);

            if (parlotError != null)
            {
                throw new ParseException($"{parlotError.Message} at {parlotError.Position}");
            }

            if (!success)
            {
                return(null);
            }

            return(new FluidTemplate(statements));
        }