public virtual void Parse(LookaheadTextReader input, ParserVisitor visitor) { var results = ParseCore(new SeekableTextReader(input)); // Replay the results on the visitor visitor.Visit(results); }
private static void AdvanceReader(int offset, LookaheadTextReader reader) { for (int i = 0; i < offset; i++) { reader.Read(); } }
private ParserResults SyncParseCore(LookaheadTextReader input) { SyntaxTreeBuilderVisitor listener = new SyntaxTreeBuilderVisitor(); Parse(input, listener); return(listener.Results); }
protected void RunBufferReadTest(Func <LookaheadTextReader, char[], int, int, int> readMethod) { // Arrange LookaheadTextReader reader = CreateReader("abcdefg"); reader.Read(); // Reader: "bcdefg" // Act char[] buffer = new char[4]; int read = -1; SourceLocation actualLocation; using (reader.BeginLookahead()) { read = readMethod(reader, buffer, 0, 4); actualLocation = reader.CurrentLocation; } // Assert Assert.AreEqual("bcde", new String(buffer), "The reader did not fill the buffer with the correct characters"); Assert.AreEqual(4, read, "The reader did not report the correct number of read characters"); Assert.AreEqual(5, actualLocation.AbsoluteIndex, "The reader did not correctly advance the raw index"); Assert.AreEqual(5, actualLocation.CharacterIndex, "The reader did not correctly advance the character index"); Assert.AreEqual(0, actualLocation.LineIndex, "The reader did not correctly advance the line index"); Assert.AreEqual(1, reader.CurrentLocation.CharacterIndex, "The reader did not correctly restore the character index when backtracking"); Assert.AreEqual(0, reader.CurrentLocation.LineIndex, "The reader did not correctly restore the line index when backtracking"); Assert.AreEqual('b', reader.Peek(), "The reader did not correctly backtrack to the appropriate character"); }
private void RunPeekOrReadTest(string input, int offset, bool isRead) { using (LookaheadTextReader reader = CreateReader(input)) { AdvanceReader(offset, reader); // Act int?actual = null; if (isRead) { actual = reader.Read(); } else { actual = reader.Peek(); } if (actual == null) { Assert.Inconclusive("Actual value was never set?!"); } // Asserts AssertReaderValueCorrect(actual.Value, input, offset, "Peek"); if (isRead) { AssertReaderValueCorrect(reader.Peek(), input, offset + 1, "Read"); } else { Assert.AreEqual(actual, reader.Peek(), "Peek moved the reader to the next character!"); } } }
protected void RunReadUntilTest(Func <LookaheadTextReader, string> readMethod, int expectedRaw, int expectedChar, int expectedLine) { // Arrange LookaheadTextReader reader = CreateReader("a\r\nbcd\r\nefg"); reader.Read(); // Reader: "\r\nbcd\r\nefg" reader.Read(); // Reader: "\nbcd\r\nefg" reader.Read(); // Reader: "bcd\r\nefg" // Act string read = null; SourceLocation actualLocation; using (reader.BeginLookahead()) { read = readMethod(reader); actualLocation = reader.CurrentLocation; } // Assert Assert.Equal(3, reader.CurrentLocation.AbsoluteIndex); Assert.Equal(0, reader.CurrentLocation.CharacterIndex); Assert.Equal(1, reader.CurrentLocation.LineIndex); Assert.Equal(expectedRaw, actualLocation.AbsoluteIndex); Assert.Equal(expectedChar, actualLocation.CharacterIndex); Assert.Equal(expectedLine, actualLocation.LineIndex); Assert.Equal('b', reader.Peek()); Assert.Equal(read, readMethod(reader)); }
protected void RunReadUntilTest(Func <LookaheadTextReader, string> readMethod, int expectedRaw, int expectedChar, int expectedLine) { // Arrange LookaheadTextReader reader = CreateReader("a\r\nbcd\r\nefg"); reader.Read(); // Reader: "\r\nbcd\r\nefg" reader.Read(); // Reader: "\nbcd\r\nefg" reader.Read(); // Reader: "bcd\r\nefg" // Act string read = null; SourceLocation actualLocation; using (reader.BeginLookahead()) { read = readMethod(reader); actualLocation = reader.CurrentLocation; } // Assert Assert.AreEqual(3, reader.CurrentLocation.AbsoluteIndex, "The reader did not correctly restore the raw index when backtracking"); Assert.AreEqual(0, reader.CurrentLocation.CharacterIndex, "The reader did not correctly restore the character index when backtracking"); Assert.AreEqual(1, reader.CurrentLocation.LineIndex, "The reader did not correctly restore the line index when backtracking"); Assert.AreEqual(expectedRaw, actualLocation.AbsoluteIndex, "The reader did not correctly advance the raw index"); Assert.AreEqual(expectedChar, actualLocation.CharacterIndex, "The reader did not correctly advance the character index"); Assert.AreEqual(expectedLine, actualLocation.LineIndex, "The reader did not correctly advance the line index"); Assert.AreEqual('b', reader.Peek(), "The reader did not correctly backtrack to the appropriate character"); Assert.AreEqual(read, readMethod(reader)); }
protected void RunBufferReadTest(Func <LookaheadTextReader, char[], int, int, int> readMethod) { // Arrange LookaheadTextReader reader = CreateReader("abcdefg"); reader.Read(); // Reader: "bcdefg" // Act char[] buffer = new char[4]; int read = -1; SourceLocation actualLocation; using (reader.BeginLookahead()) { read = readMethod(reader, buffer, 0, 4); actualLocation = reader.CurrentLocation; } // Assert Assert.Equal("bcde", new String(buffer)); Assert.Equal(4, read); Assert.Equal(5, actualLocation.AbsoluteIndex); Assert.Equal(5, actualLocation.CharacterIndex); Assert.Equal(0, actualLocation.LineIndex); Assert.Equal(1, reader.CurrentLocation.CharacterIndex); Assert.Equal(0, reader.CurrentLocation.LineIndex); Assert.Equal('b', reader.Peek()); }
private void RunPeekOrReadTest(string input, int offset, bool isRead) { using (LookaheadTextReader reader = CreateReader(input)) { AdvanceReader(offset, reader); // Act int?actual = null; if (isRead) { actual = reader.Read(); } else { actual = reader.Peek(); } Assert.NotNull(actual); // Asserts AssertReaderValueCorrect(actual.Value, input, offset, "Peek"); if (isRead) { AssertReaderValueCorrect(reader.Peek(), input, offset + 1, "Read"); } else { Assert.Equal(actual, reader.Peek()); } } }
protected void RunCancelBacktrackOutsideLookaheadTest() { // Arrange LookaheadTextReader reader = CreateReader("abcdefg"); // Act and Assert Assert.Throws <InvalidOperationException>(() => reader.CancelBacktrack(), RazorResources.CancelBacktrack_Must_Be_Called_Within_Lookahead); }
protected void RunReadToEndTest() { // Arrange LookaheadTextReader reader = CreateReader("abcdefg"); // Act string str = reader.ReadToEnd(); // Assert Assert.Equal("abcdefg", str); }
private static void RunAll( Action <StringBuilder, LookaheadTextReader>[] readerCommands, StringBuilder builder, LookaheadTextReader reader ) { foreach (Action <StringBuilder, LookaheadTextReader> readerCommand in readerCommands) { readerCommand(builder, reader); } }
protected void RunReadToEndTest() { // Arrange LookaheadTextReader reader = CreateReader("abcdefg"); // Act string str = reader.ReadToEnd(); // Assert Assert.AreEqual("abcdefg", str, "The text read from the reader did not match the text provided to it"); }
protected void RunSourceLocationTest(string input, SourceLocation expected, Action <LookaheadTextReader> readerAction) { // Arrange LookaheadTextReader reader = CreateReader(input); readerAction(reader); // Act SourceLocation actual = reader.CurrentLocation; // Assert Assert.Equal(expected, actual); }
protected void RunLookaheadTest(string input, string expected, params Action <StringBuilder, LookaheadTextReader>[] readerCommands) { // Arrange StringBuilder builder = new StringBuilder(); using (LookaheadTextReader reader = CreateReader(input)) { RunAll(readerCommands, builder, reader); } if (expected != null) { Assert.AreEqual(expected, builder.ToString(), "The reader did not backtrack correctly"); } }
protected void RunLookaheadTest(string input, string expected, params Action <StringBuilder, LookaheadTextReader>[] readerCommands) { // Arrange var builder = new StringBuilder(); using (LookaheadTextReader reader = CreateReader(input)) { RunAll(readerCommands, builder, reader); } if (expected != null) { Assert.Equal(expected, builder.ToString()); } }
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); }
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(); } }
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)); }
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(); }
public virtual ParserContext CreateParserRun(LookaheadTextReader input, ParserBase codeParser, MarkupParser markupParser, ParserVisitor listener) { return(new ParserContext(input, codeParser, markupParser, SelectActiveParser(codeParser, markupParser), listener)); }
protected void Read(StringBuilder builder, LookaheadTextReader reader) { builder.Append((char)reader.Read()); }
protected void CancelBacktrack(StringBuilder builder, LookaheadTextReader reader) { reader.CancelBacktrack(); }
public virtual ParserResults Parse(LookaheadTextReader input) { return(ParseCore(new SeekableTextReader(input))); }
public virtual ParserResults Parse(LookaheadTextReader input) { return(SyncParseCore(input)); }