Ejemplo n.º 1
0
 public Interpreter(
     string text,
     TemplateCache cache = null)
 {
     Cache = cache ?? new TemplateCache();
     var stream = new ANTLRStringStream(text);
     var lexer = TemplateLexer.Create(stream);
     var tokenStream = new CommonTokenStream(lexer);
     var parser = new TemplateParser(tokenStream)
     {
         TraceDestination = Console.Out,
         Errors = _parserErrors
     };
     var docResult = parser.document();
     _ast = docResult.Tree;
 }
Ejemplo n.º 2
0
        private ErrorList InternalCompile(ICharStream stream)
        {
            var errors = new ErrorList();
            var lexer = TemplateLexer.Create(stream);
            var tStream = new CommonTokenStream(lexer);
            var parser = new TemplateParser(tStream) {Errors = errors};
            var docResult = parser.document();

            _template = new DynamicMethod(
                string.Format("__template_{0}", Name),
                typeof (void),
                new[] {typeof (VirtualTemplate)},
                typeof (VirtualTemplate),
                true);

            var emit = _template.GetILGenerator();
            var ctx = new Context(emit) {OptimizeLevel = OptimizeLevel};
            var doc = new Document(docResult.Tree);

            try
            {
                var e = doc.Generate(ctx);
                errors.AddRange(e);
                _image = (RunTemplate) _template.CreateDelegate(typeof (RunTemplate));
                Assembly = ctx.Sink.Build();
            }
            catch (Exception e)
            {
                errors.ErrorUnhandledCompileError(e);
            }

            stream.Seek(0);
            Source = stream.ToString();

            return errors;
        }