/// <summary>
        /// Creates an instance of a template service.
        /// </summary>
        /// <param name="language">The language the template service to support.</param>
        /// <param name="strictMode">Specifies whether parsing exceptions will be thrown.</param>
        /// <param name="markupParser">The markup parser.</param>
        /// <returns>An instance of <see cref="TemplateService"/>.</returns>
        public static TemplateService CreateTemplateService(Language language = Language.CSharp, bool strictMode = false, MarkupParser markupParser = null)
        {
            var factory = Razor.CompilerServiceFactory;
            var compilerService = factory.CreateCompilerService(language, strictMode, markupParser);

            return new TemplateService(compilerService);
        }
示例#2
0
        public RazorParser(ParserBase codeParser, MarkupParser markupParser) {
            if (codeParser == null) { throw new ArgumentNullException("codeParser"); }
            if (markupParser == null) { throw new ArgumentNullException("markupParser"); }

            MarkupParser = markupParser;
            CodeParser = codeParser;
        }
示例#3
0
        private ParserResults ParseCore(ITextDocument input)
        {
            // Setup the parser context
            ParserContext context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser)
            {
                DesignTimeMode = DesignTimeMode
            };

            MarkupParser.Context = context;
            CodeParser.Context   = context;

            // Execute the parse
            MarkupParser.ParseDocument();

            // Get the result
            ParserResults results = context.CompleteParse();

            // Rewrite whitespace if supported
            Block current = results.Document;

            foreach (ISyntaxTreeRewriter rewriter in Optimizers)
            {
                current = rewriter.Rewrite(current);
            }

            // Return the new result
            return(new ParserResults(current, results.ParserErrors));
        }
        /// <summary>
        /// Initialises a new instance of <see cref="DirectCompilerServiceBase"/>.
        /// </summary>
        /// <param name="codeLanguage">The razor code language.</param>
        /// <param name="codeDomProvider">The code dom provider used to generate code.</param>
        /// <param name="markupParser">The markup parser.</param>
        protected DirectCompilerServiceBase(RazorCodeLanguage codeLanguage, CodeDomProvider codeDomProvider, MarkupParser markupParser)
            : base(codeLanguage, markupParser)
        {
            if (codeDomProvider == null)
                throw new ArgumentNullException("codeDomProvider");

            CodeDomProvider = codeDomProvider;
        }
        /// <summary>
        /// Initialises a new instance of <see cref="CompilerServiceBase"/>
        /// </summary>
        /// <param name="codeLanguage">The code language.</param>
        /// <param name="markupParser">The markup parser.</param>
        protected CompilerServiceBase(RazorCodeLanguage codeLanguage, MarkupParser markupParser)
        {
            if (codeLanguage == null)
                throw new ArgumentNullException("codeLanguage");

            CodeLanguage = codeLanguage;
            MarkupParser = markupParser ?? new HtmlMarkupParser();
        }
        /// <summary>
        /// Creates an instance of a compiler service.
        /// </summary>
        /// <param name="language">The language to support in templates.</param>
        /// <param name="strictMode">Strict mode forces parsing exceptions to be thrown.</param>
        /// <param name="markupParser">The markup parser to use.</param>
        /// <returns>An instance of <see cref="ICompilerService"/>.</returns>
        public ICompilerService CreateCompilerService(Language language = Language.CSharp, bool strictMode = false, MarkupParser markupParser = null)
        {
            switch (language)
            {
                case Language.CSharp:
                    return new CSharpDirectCompilerService(strictMode, markupParser);
            }

            throw new ArgumentException("The language '" + language + "' is not supported.");
        }
示例#7
0
        public RazorParser(ParserBase codeParser, MarkupParser markupParser)
        {
            if (codeParser == null)
            {
                throw new ArgumentNullException("codeParser");
            }
            if (markupParser == null)
            {
                throw new ArgumentNullException("markupParser");
            }

            MarkupParser = markupParser;
            CodeParser   = codeParser;
        }
示例#8
0
        public virtual void Parse(LookaheadTextReader input, ParserVisitor visitor)
        {
            // Setup the parser context
            ParserContext context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser, visitor)
            {
                DesignTimeMode = DesignTimeMode
            };

            MarkupParser.Context = context;
            CodeParser.Context   = context;

            // Execute the context
            try {
                MarkupParser.ParseDocument();
            }
            finally {
                context.OnComplete();
            }
        }
示例#9
0
        /// <summary>
        /// Initialises a new instance of <see cref="RazorCompiler"/>
        /// </summary>
        /// <param name="provider">The language provider used to create language services.</param>
        /// <param name="baseType">[Optional] The template base type.</param>
        /// <param name="parser">[Optional] The markup parser.</param>
        public RazorCompiler(ILanguageProvider provider, Type baseType = null, MarkupParser parser = null)
        {
            if (provider == null)
                throw new ArgumentNullException("provider");

            if (baseType != null && !typeof(ITemplate).IsAssignableFrom(baseType))
                throw new ArgumentException(
                    string.Format("{0} is not a valid template base.  Templates must inherit from ITemplate.",
                                  baseType.FullName));

            // Need to initialise this type to ensure assemblies are loaded for referencing. Eugh....
            var temp1 = Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.None;

            languageProvider = provider;
            templateBaseType = baseType;

            markupParser = parser ?? new HtmlMarkupParser();

            Namespaces = new List<string> { "System", "System.Collections.Generic", "System.Linq" };
        }
示例#10
0
        private ParserResults ParseCore(ITextDocument input)
        {
            // Setup the parser context
            ParserContext context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser)
            {
                DesignTimeMode = DesignTimeMode
            };

            MarkupParser.Context = context;
            CodeParser.Context   = context;

            // Execute the parse
            MarkupParser.ParseDocument();

            // Get the result
            ParserResults results = context.CompleteParse();

            // Rewrite whitespace if supported
            Block current = results.Document;

            foreach (ISyntaxTreeRewriter rewriter in Optimizers)
            {
                current = rewriter.Rewrite(current);
            }

            // Link the leaf nodes into a chain
            Span prev = null;

            foreach (Span node in current.Flatten())
            {
                node.Previous = prev;
                if (prev != null)
                {
                    prev.Next = node;
                }
                prev = node;
            }

            // Return the new result
            return(new ParserResults(current, results.ParserErrors));
        }
示例#11
0
        public ParserContext(LookaheadTextReader source, ParserBase codeParser, MarkupParser markupParser, ParserBase activeParser, ParserVisitor visitor)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (codeParser == null)
            {
                throw new ArgumentNullException("codeParser");
            }
            if (markupParser == null)
            {
                throw new ArgumentNullException("markupParser");
            }
            if (activeParser == null)
            {
                throw new ArgumentNullException("activeParser");
            }
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }
            if (activeParser != codeParser && activeParser != markupParser)
            {
                throw new ArgumentException(RazorResources.ActiveParser_Must_Be_Code_Or_Markup_Parser, "activeParser");
            }

            CaptureOwnerTask();

            Source       = source;
            CodeParser   = codeParser;
            MarkupParser = markupParser;
            ActiveParser = activeParser;
            _visitorStack.Push(visitor);
            ResetBuffers();
        }
 protected override ParserBase SelectActiveParser(ParserBase codeParser, MarkupParser markupParser) {
     return markupParser;
 }
示例#13
0
 public virtual ParserContext CreateParserRun(LookaheadTextReader input, ParserBase codeParser, MarkupParser markupParser, ParserVisitor listener) {
     return new ParserContext(input, codeParser, markupParser, SelectActiveParser(codeParser, markupParser), listener);
 }
 private ParserContext SetupTestRun(string document, Action<TextReader> positioningAction, ParserBase codeParser, MarkupParser markupParser, ParserBase activeParser, ParserVisitor listener) {
     ParserContext context = new ParserContext(new BufferingTextReader(new StringReader(document)), codeParser, markupParser, activeParser, listener);
     positioningAction(context.Source);
     context.ResetBuffers();
     return context;
 }
示例#15
0
        public ParserContext(LookaheadTextReader source, ParserBase codeParser, MarkupParser markupParser, ParserBase activeParser, ParserVisitor visitor) {
            if (source == null) { throw new ArgumentNullException("source"); }
            if (codeParser == null) { throw new ArgumentNullException("codeParser"); }
            if (markupParser == null) { throw new ArgumentNullException("markupParser"); }
            if (activeParser == null) { throw new ArgumentNullException("activeParser"); }
            if (visitor == null) { throw new ArgumentNullException("visitor"); }
            if (activeParser != codeParser && activeParser != markupParser) {
                throw new ArgumentException(RazorResources.ActiveParser_Must_Be_Code_Or_Markup_Parser, "activeParser");
            }

            CaptureOwnerTask();

            Source = source;
            CodeParser = codeParser;
            MarkupParser = markupParser;
            ActiveParser = activeParser;
            _visitorStack.Push(visitor);
            ResetBuffers();
        }
 /// <summary>
 /// Initialises a new instance of <see cref="VBDirectCompilerService"/>.
 /// </summary>
 /// <param name="strictMode">Specifies whether the strict mode parsing is enabled.</param>
 /// <param name="markupParser">The markup parser to use.</param>
 public VBDirectCompilerService(bool strictMode = true, MarkupParser markupParser = null)
     : base(
         new VBRazorCodeLanguage(strictMode),
         new VBCodeProvider(),
         markupParser) { }
 /// <summary>
 /// Gets an instance of the markup parser and is provided an opportunity to decorate or replace it
 /// </summary>
 /// <param name="incomingMarkupParser">The markup parser</param>
 /// <returns>Either the same markup parser, after modifications, or a different markup parser</returns>
 public virtual MarkupParser DecorateMarkupParser(MarkupParser incomingMarkupParser) {
     if (incomingMarkupParser == null) { throw new ArgumentNullException("incomingMarkupParser"); }
     return incomingMarkupParser;
 }
示例#18
0
 /// <summary>
 /// Creates an instance of a <see cref="TemplateService"/>.
 /// </summary>
 /// <param name="provider">[Optional] The language provider to use.</param>
 /// <param name="templateBaseType">[Optional] The base template type.</param>
 /// <param name="parser">[Optional] The markup parser to use.</param>
 /// <returns>A new instance of <see cref="TemplateService"/>.</returns>
 public static TemplateService CreateTemplateService(ILanguageProvider provider = null, Type templateBaseType = null, MarkupParser parser = null)
 {
     return new TemplateService(provider, templateBaseType, parser);
 }
示例#19
0
 protected abstract ParserBase SelectActiveParser(ParserBase codeParser, MarkupParser markupParser);