public AntlrToExpressionTreeCompiler(AntlrVelocityParser parser, IImmutableList <CustomDirectiveBuilder> customDirectives, VelocityExpressionFactory expressionFactory, bool reduceWhitespace)
 {
     _parser            = parser;
     _customDirectives  = customDirectives ?? ImmutableList <CustomDirectiveBuilder> .Empty;
     _expressionFactory = expressionFactory;
     ReduceWhitespace   = reduceWhitespace;
 }
示例#2
0
        protected T Parse <T>(string input, Func <VelocityParser, T> parseFunc, int?lexerMode = null)
            where T : RuleContext
        {
            var inputStream       = new AntlrInputStream(input);
            var expressionFactory = new VelocityExpressionFactory(new ReusableBinderFactory(new BinderFactory()));

            return(new AntlrVelocityParser(null, expressionFactory)
                   .ParseTemplate(inputStream, Utility.GetName(), parseFunc, lexerMode));
        }
        public void TemplateCompilationTests(string path, string assemblyName)
        {
            using (var file = File.OpenRead(path))
                using (var reader = new StreamReader(file))
                {
                    var antlrDirectives = BlockDirectives
                                          .Select(x => new AntlrBlockDirectiveBuilder(x))
                                          .Concat(new CustomDirectiveBuilder[] { new ForeachDirectiveBuilder() })
                                          .ToImmutableList();

                    var expressionTreeFactory = new VelocityExpressionFactory(_binderFactory);
                    var parser = new AntlrVelocityParser(antlrDirectives, expressionTreeFactory);


                    var expressionTree = parser.Parse(reader, assemblyName);
                    if (Compile || ExecuteTemplate)
                    {
                        VelocityDiskCompiler diskCompiler = null;
                        VelocityCompiler     compiler;

                        if (SaveDlls || SaveIl)
                        {
                            diskCompiler = new VelocityDiskCompiler(new AssemblyName(assemblyName), OutputDir);
                            compiler     = diskCompiler;
                        }
                        else
                        {
                            compiler = new VelocityCompiler();
                        }

                        var result = compiler.CompileWithSymbols(expressionTree, assemblyName, path);

                        if (ExecuteTemplate)
                        {
                            var context = new VelocityContext();
                            using (var writer = new StringWriter())
                            {
                                var output = new VelocityOutput(writer);
                                result(context, output);
                            }
                        }

                        if (SaveDlls || SaveIl)
                        {
                            diskCompiler.SaveDll();

                            if (SaveIl)
                            {
                                diskCompiler.SaveIl();
                            }
                        }
                    }
                }
        }
示例#4
0
        public AntlrVelocityParser(IImmutableList <CustomDirectiveBuilder> customDirectives, VelocityExpressionFactory expressionFactory, bool reduceWhitespace = false)
        {
            if (expressionFactory == null)
            {
                throw new ArgumentNullException(nameof(expressionFactory));
            }

            _customDirectives     = customDirectives ?? ImmutableList <CustomDirectiveBuilder> .Empty;
            _expressionFactory    = expressionFactory;
            _reduceWhitespace     = reduceWhitespace;
            _blockDirectives      = _customDirectives.Where(x => x.IsBlockDirective).Select(x => x.Name).ToImmutableList();
            _singleLineDirectives = _customDirectives.Where(x => !x.IsBlockDirective).Select(x => x.Name).ToImmutableList();
        }