public BufferingTextReader(TextReader source) {
            if (source == null) { throw new ArgumentNullException("source"); }

            InnerReader = source;
            _locationTracker = new SourceLocationTracker();

            UpdateCurrentCharacter();
        }
        public void UpdateLocationDoesNotAdvanceLineIndexOnNonNewlineCharacter() {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('f', () => 'o');

            // Assert
            Assert.AreEqual(42, tracker.CurrentLocation.LineIndex);
        }
        public void UpdateLocationAdvancesAbsoluteIndexOnSlashN() {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('\n', () => 'o');

            // Assert
            Assert.AreEqual(11, tracker.CurrentLocation.AbsoluteIndex);
        }
        public void UpdateLocationAdvancesLineIndexOnSlashN()
        {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('\n', 'o');

            // Assert
            Assert.Equal(43, tracker.CurrentLocation.LineIndex);
        }
        public void UpdateLocationAdvancesCharacterIndexOnNonNewlineCharacter()
        {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('f', 'o');

            // Assert
            Assert.Equal(46, tracker.CurrentLocation.CharacterIndex);
        }
Esempio n. 6
0
 internal void CalculateStart(Span prev)
 {
     if (prev == null)
     {
         Start = SourceLocation.Zero;
     }
     else
     {
         Start = new SourceLocationTracker(prev.Start).UpdateLocation(prev.Content).CurrentLocation;
     }
 }
Esempio n. 7
0
        public BufferingTextReader(TextReader source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            InnerReader      = source;
            _locationTracker = new SourceLocationTracker();

            UpdateCurrentCharacter();
        }
        public void UpdateLocationAdvancesCorrectlyForMultiLineString()
        {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation("foo\nbar\rbaz\r\nbox");

            // Assert
            Assert.Equal(26, tracker.CurrentLocation.AbsoluteIndex);
            Assert.Equal(45, tracker.CurrentLocation.LineIndex);
            Assert.Equal(3, tracker.CurrentLocation.CharacterIndex);
        }
        public void UpdateLocationAdvancesAbsoluteIndexOnSlashRFollowedByNonNewlineCharacter()
        {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('\r', 'o');

            // Assert
            Assert.Equal(11, tracker.CurrentLocation.AbsoluteIndex);
        }
Esempio n. 10
0
 public void ChangeStart(SourceLocation newStart)
 {
     _start = newStart;
     Span current = this;
     SourceLocationTracker tracker = new SourceLocationTracker(newStart);
     tracker.UpdateLocation(Content);
     while ((current = current.Next) != null)
     {
         current._start = tracker.CurrentLocation;
         tracker.UpdateLocation(current.Content);
     }
 }
Esempio n. 11
0
 public SpanFactory()
 {
     LocationTracker = new SourceLocationTracker();
 }
Esempio n. 12
0
 private void UpdateOffset() {
     SourceLocationTracker tracker = new SourceLocationTracker();
     tracker.UpdateLocation(Content);
     Offset = tracker.CurrentLocation;
 }
Esempio n. 13
0
 private static void EvaluateSpan(ErrorCollector collector, SourceLocationTracker tracker, Span actual, Span expected) {
     if (!actual.Equals(expected)) {
         AddMismatchError(collector, tracker, actual, expected);
     }
     else {
         AddPassedMessage(collector, tracker, expected);
     }
 }
Esempio n. 14
0
 private static void AddPassedMessage(ErrorCollector collector, SourceLocationTracker tracker, SyntaxTreeNode expected) {
     collector.AddMessage("{0} - PASSED", expected);
 }
        public void UpdateLocationAdvancesCharacterIndexOnSlashRFollowedBySlashN()
        {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('\r', '\n');

            // Assert
            Assert.Equal(46, tracker.CurrentLocation.CharacterIndex);
        }
        public void UpdateLocationDoesNotAdvanceLineIndexOnSlashRFollowedBySlashN() {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('\r', () => '\n');

            // Assert
            Assert.AreEqual(42, tracker.CurrentLocation.LineIndex);
        }
        public void UpdateLocationResetsCharacterIndexOnSlashRFollowedByNonNewlineCharacter() {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('\r', () => 'o');

            // Assert
            Assert.AreEqual(0, tracker.CurrentLocation.CharacterIndex);
        }
Esempio n. 18
0
        private void HandleWhitespaceRewriting() {
            Debug.Assert(!String.IsNullOrEmpty(_nextSpanToOutput.Content));

            // Check the last span for trailing whitespace
            int endOfWhitespaceToCapture = _nextSpanToOutput.Content.Length;
            for (int i = _nextSpanToOutput.Content.Length - 1; i >= 0; i--) {
                char c = _nextSpanToOutput.Content[i];
                if (CharUtils.IsNewLine(c)) {
                    break;
                }
                else if (!Char.IsWhiteSpace(c)) {
                    return; // Saw non-whitespace before newline, markup owns this whitespace so don't touch it
                }
                else {
                    endOfWhitespaceToCapture = i;
                }
            }

            // Ok, endOfWhitespaceToCapture is now at the offset of the first whitespace character after the newline
            string oldContent = _nextSpanToOutput.Content;
            _nextSpanToOutput.Content = oldContent.Substring(0, endOfWhitespaceToCapture);

            SourceLocationTracker tracker = new SourceLocationTracker();
            tracker.CurrentLocation = _nextSpanToOutput.Start;
            tracker.UpdateLocation(_nextSpanToOutput.Content);
            Span whitespaceSpan = new CodeSpan(tracker.CurrentLocation, oldContent.Substring(endOfWhitespaceToCapture));

            Visitor.VisitSpan(_nextSpanToOutput);
            _nextSpanToOutput = whitespaceSpan;
        }
Esempio n. 19
0
 private static void AddNullActualError(ErrorCollector collector, SourceLocationTracker tracker, SyntaxTreeNode actual, SyntaxTreeNode expected) {
     collector.AddError("{0} - FAILED :: Actual: << Null >>", expected);
 }
Esempio n. 20
0
 private static void AddMismatchError(ErrorCollector collector, SourceLocationTracker tracker, SyntaxTreeNode actual, SyntaxTreeNode expected) {
     collector.AddError("{0} - FAILED :: Actual: {1}", expected, actual);
 }
Esempio n. 21
0
        private static void EvaluateSyntaxTreeNode(ErrorCollector collector, SourceLocationTracker tracker, SyntaxTreeNode actual, SyntaxTreeNode expected) {
            if (actual == null) {
                AddNullActualError(collector, tracker, actual, expected);
            }

            if (actual.IsBlock != expected.IsBlock) {
                AddMismatchError(collector, tracker, actual, expected);
            }
            else {
                if (expected.IsBlock) {
                    EvaluateBlock(collector, tracker, (Block)actual, (Block)expected);
                }
                else {
                    EvaluateSpan(collector, tracker, (Span)actual, (Span)expected);
                }
            }
        }
Esempio n. 22
0
 private static void EvaluateBlock(ErrorCollector collector, SourceLocationTracker tracker, Block actual, Block expected) {
     if (actual.Type != expected.Type) {
         AddMismatchError(collector, tracker, actual, expected);
     }
     else {
         AddPassedMessage(collector, tracker, expected);
         using (collector.Indent()) {
             IEnumerator<SyntaxTreeNode> expectedNodes = expected.Children.GetEnumerator();
             IEnumerator<SyntaxTreeNode> actualNodes = actual.Children.GetEnumerator();
             while (expectedNodes.MoveNext()) {
                 if (!actualNodes.MoveNext()) {
                     collector.AddError("{0} - FAILED :: No more elements at this node", expectedNodes.Current);
                 }
                 else {
                     EvaluateSyntaxTreeNode(collector, tracker, actualNodes.Current, expectedNodes.Current);
                 }
             }
             while (actualNodes.MoveNext()) {
                 collector.AddError("End of Node - FAILED :: Found Node: {0}", actualNodes.Current);
             }
         }
     }
 }
        public void UpdateLocationResetsCharacterIndexOnSlashN()
        {
            // Arrange
            SourceLocationTracker tracker = new SourceLocationTracker(TestStartLocation);

            // Act
            tracker.UpdateLocation('\n', 'o');

            // Assert
            Assert.Equal(0, tracker.CurrentLocation.CharacterIndex);
        }
Esempio n. 24
0
        public static void EvaluateParseTree(TestContext context, Block actualRoot, Block expectedRoot) {
            // Evaluate the result
            ErrorCollector collector = new ErrorCollector();
            SourceLocationTracker tracker = new SourceLocationTracker();

            // Link all the nodes
            Span first = null;
            Span previous = null;
            foreach (Span span in expectedRoot.Flatten()) {
                if(first == null) {
                    first = span;
                }
                span.Previous = previous;
                if (previous != null) {
                    previous.Next = span;
                }
                previous = span;
            }
            Span.ClearCachedStartPoints(first);

            if (expectedRoot == null) {
                Assert.IsNull(actualRoot, "Expected an empty document.  Actual: {0}", actualRoot);
            }
            else {
                Assert.IsNotNull(actualRoot, "Expected a valid document, but it was empty");
                EvaluateSyntaxTreeNode(collector, tracker, actualRoot, expectedRoot);
                if (collector.Success) {
                    if (context != null) {
                        context.WriteLine("Parse Tree Validation Succeeded:\r\n{0}", collector.Message);
                    }
                }
                else {
                    Assert.Fail("\r\n{0}", collector.Message);
                }
            }
        }