Exemplo n.º 1
0
        internal virtual void RunParseTest(string document, Func<ParserBase, Action> parserActionSelector, Block expectedRoot, IList<RazorError> expectedErrors, bool designTimeParser) {
            // Create the source
            using (StringTextBuffer reader = new StringTextBuffer(document)) {
                ParserResults results = null;
                try {
                    ParserBase codeParser = CreateCodeParser();
                    MarkupParser markupParser = CreateMarkupParser();
                    SyntaxTreeBuilderVisitor listener = new SyntaxTreeBuilderVisitor();
                    ParserContext context = CreateParserRun(new TextBufferReader(reader), codeParser, markupParser, listener);
                    context.DesignTimeMode = designTimeParser;

                    codeParser.Context = context;
                    markupParser.Context = context;

                    // Run the parser
                    parserActionSelector(context.ActiveParser)();
                    context.OnComplete();

                    // Collect the results
                    results = listener.Results;
                    EvaluateResults(TestContext, results, expectedRoot, expectedErrors);
                }
                finally {
                    if (TestContext != null && results != null && results.Document != null) {
                        TestContext.WriteLine(String.Empty);
                        TestContext.WriteLine("Actual Parse Tree:");
                        WriteNode(0, TestContext, results.Document);
                    }
                }
            }
        }
        protected internal virtual ParserResults ParseTemplateCore(LookaheadTextReader input, CancellationToken? cancelToken) {
            // Setup the consumer
            SyntaxTreeBuilderVisitor treeBuilder = new SyntaxTreeBuilderVisitor() { CancelToken = cancelToken };

            // Construct the parser
            RazorParser parser = CreateParser();
            Debug.Assert(parser != null);
            parser.Parse(input, treeBuilder);

            // Return the results
            return treeBuilder.Results;
        }
Exemplo n.º 3
0
        public void ParseMethodUsesProvidedParserListenerIfSpecified() {
            // Arrange
            RazorParser parser = new RazorParser(new CSharpCodeParser(), new HtmlMarkupParser());
            SyntaxTreeBuilderVisitor builder = new SyntaxTreeBuilderVisitor();

            // Act
            parser.Parse(new StringReader("foo @bar baz"), builder);

            // Assert
            ParserTestBase.EvaluateResults(TestContext,
                                           builder.Results,
                                           new MarkupBlock(
                                            new MarkupSpan("foo "),
                                            new ExpressionBlock(
                                                new TransitionSpan(RazorParser.TransitionString, hidden: false, acceptedCharacters: AcceptedCharacters.None),
                                                new ImplicitExpressionSpan("bar", CSharpCodeParser.DefaultKeywords, acceptTrailingDot: false, acceptedCharacters: AcceptedCharacters.NonWhiteSpace)
                                            ),
                                            new MarkupSpan(" baz")
                                           ));
        }
Exemplo n.º 4
0
 private ParserResults SyncParseCore(LookaheadTextReader input) {
     SyntaxTreeBuilderVisitor listener = new SyntaxTreeBuilderVisitor();
     Parse(input, listener);
     return listener.Results;
 }
        protected internal virtual GeneratorResults GenerateCodeCore(LookaheadTextReader input, string className, string rootNamespace, string sourceFileName, CancellationToken? cancelToken) {
            className = className ?? Host.DefaultClassName;
            rootNamespace = rootNamespace ?? Host.DefaultNamespace;

            // Set up the code generator
            RazorCodeGenerator generator = CreateCodeGenerator(className, rootNamespace, sourceFileName);
            generator.DesignTimeMode = Host.DesignTimeMode;

            // Setup the consumers
            SyntaxTreeBuilderVisitor treeBuilder = new SyntaxTreeBuilderVisitor() { CancelToken = cancelToken };
            VisitorPair consumer = new VisitorPair(treeBuilder, generator);

            // Run the parser
            RazorParser parser = CreateParser();
            Debug.Assert(parser != null);
            parser.Parse(input, consumer);

            // Post process code
            Host.PostProcessGeneratedCode(generator.GeneratedCode, generator.GeneratedNamespace, generator.GeneratedClass, generator.GeneratedExecuteMethod);

            // Extract design-time mappings
            IDictionary<int, GeneratedCodeMapping> designTimeLineMappings = null;
            if (Host.DesignTimeMode) {
                designTimeLineMappings = generator.CodeMappings;
            }

            // Collect results and return
            return new GeneratorResults(treeBuilder.Results, generator.GeneratedCode, designTimeLineMappings);
        }