summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.Razor/Text/TextBufferReader.cs
blob: 941ef8a107882de00a27c1799599b66e07c53e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Web.Razor.Resources;
using System.Web.Razor.Utils;

namespace System.Web.Razor.Text
{
    public class TextBufferReader : LookaheadTextReader
    {
        private Stack<BacktrackContext> _bookmarks = new Stack<BacktrackContext>();
        private SourceLocationTracker _tracker = new SourceLocationTracker();

        public TextBufferReader(ITextBuffer buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            InnerBuffer = buffer;
        }

        internal ITextBuffer InnerBuffer { get; private set; }

        public override SourceLocation CurrentLocation
        {
            get { return _tracker.CurrentLocation; }
        }

        public override int Peek()
        {
            return InnerBuffer.Peek();
        }

        public override int Read()
        {
            int read = InnerBuffer.Read();
            if (read != -1)
            {
                char nextChar = '\0';
                int next = Peek();
                if (next != -1)
                {
                    nextChar = (char)next;
                }
                _tracker.UpdateLocation((char)read, nextChar);
            }
            return read;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                IDisposable disposable = InnerBuffer as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        public override IDisposable BeginLookahead()
        {
            BacktrackContext context = new BacktrackContext() { Location = CurrentLocation };
            _bookmarks.Push(context);
            return new DisposableAction(() =>
            {
                EndLookahead(context);
            });
        }

        public override void CancelBacktrack()
        {
            if (_bookmarks.Count == 0)
            {
                throw new InvalidOperationException(RazorResources.CancelBacktrack_Must_Be_Called_Within_Lookahead);
            }
            _bookmarks.Pop();
        }

        private void EndLookahead(BacktrackContext context)
        {
            if (_bookmarks.Count > 0 && ReferenceEquals(_bookmarks.Peek(), context))
            {
                // Backtrack wasn't cancelled, so pop it
                _bookmarks.Pop();

                // Set the new current location
                _tracker.CurrentLocation = context.Location;
                InnerBuffer.Position = context.Location.AbsoluteIndex;
            }
        }

        /// <summary>
        /// Need a class for reference equality to support cancelling backtrack.
        /// </summary>
        private class BacktrackContext
        {
            public SourceLocation Location { get; set; }
        }
    }
}