Esempio n. 1
0
 internal InternalError(Maybe <TToken> unexpected, bool eof, SourcePos errorPos, string message)
 {
     Unexpected = unexpected;
     EOF        = eof;
     ErrorPos   = errorPos;
     Message    = message;
 }
Esempio n. 2
0
        // postcondition: bufferedLength >= _currentIndex + min(readAhead, AmountLeft(_stream))
        private void Buffer(int readAhead)
        {
            var readAheadTo = _currentIndex + readAhead;

            if (readAheadTo >= _bufferedCount)
            {
                // we're about to read past the end of the current chunk. Pull a new chunk from the stream
                var keepSeenLength = _bookmarks.Count > 0
                    ? Location - _bookmarks[0]
                    : 0;
                var keepFrom        = _currentIndex - keepSeenLength;
                var keepLength      = _bufferedCount - keepFrom;
                var amountToRead    = Math.Max(_bufferChunkSize, readAheadTo - keepFrom);
                var newBufferLength = _bufferedCount + amountToRead;

                //                  _currentIndex
                //                        |
                //                        | _bufferedCount
                //              keepFrom  |      |
                //                 |      |      | readAheadTo
                //                 |      |      |    |
                //              abcdefghijklmnopqrstuvwxyz
                //       readAhead        |-----------|
                //  keepSeenLength |------|
                //      keepLength |-------------|
                //    amountToRead               |----|
                // newBufferLength |------------------|


                for (var i = 0; i < keepFrom; i++)
                {
                    _bufferStartSourcePos = _posCalculator(_buffer[i], _bufferStartSourcePos);
                }

                if (newBufferLength > _buffer.Length)
                {
                    // grow the buffer
                    var newBuffer = ArrayPool <TToken> .Shared.Rent(Math.Max(newBufferLength, _buffer.Length * 2));

                    Array.Copy(_buffer, keepFrom, newBuffer, 0, keepLength);

                    ArrayPool <TToken> .Shared.Return(_buffer);

                    _buffer = newBuffer;
                }
                else if (keepFrom != 0 && keepLength != 0)
                {
                    // move the buffer's contents to the start

                    // todo: find out how expensive this Copy tends to be.
                    // Could prevent it by using a ring buffer, but might make reads slower
                    Array.Copy(_buffer, keepFrom, _buffer, 0, keepLength);
                }
                _bufferStartLocation += keepFrom;
                _currentIndex         = keepSeenLength;
                _bufferedCount        = keepLength;
                _bufferedCount       += _stream.ReadInto(_buffer, _bufferedCount, amountToRead);
            }
        }
Esempio n. 3
0
 internal ParseError <TToken> WithErrorPos(SourcePos errorPos)
 => new ParseError <TToken>(
     Unexpected,
     EOF,
     Expected,
     errorPos,
     Message
     );
Esempio n. 4
0
 internal ParseError(Maybe <TToken> unexpected, bool eof, IEnumerable <Expected <TToken> > expected, SourcePos errorPos, string message)
 {
     Unexpected = unexpected;
     EOF        = eof;
     Expected   = expected;
     ErrorPos   = errorPos;
     Message    = message;
 }
Esempio n. 5
0
 internal ParseError(Maybe <TToken> unexpected, bool eof, Expected <TToken>[] expected, SourcePos errorPos, string?message)
 {
     Unexpected = unexpected;
     EOF        = eof;
     Expected   = expected;
     ErrorPos   = errorPos;
     Message    = message;
 }
Esempio n. 6
0
 public ParseState(Func <TToken, SourcePos, SourcePos> posCalculator, ITokenStream <TToken> stream)
 {
     _posCalculator = posCalculator;
     _bookmarks     = new Stack <Bookmark>();
     _stream        = stream;
     _consumedCount = 0;
     _hasCurrent    = false;
     SourcePos      = new SourcePos(1, 1);
     Error          = default;
 }
Esempio n. 7
0
        public ParseState(Func <TToken, SourcePos, SourcePos> posCalculator, ITokenStream <TToken> stream)
        {
            _posCalculator = posCalculator;
            _bookmarks     = new PooledList <Bookmark>();
            _stream        = stream;
            _consumedCount = 0;
            SourcePos      = new SourcePos(1, 1);

            _bufferChunkSize = stream.ChunkSizeHint;
            _buffer          = ArrayPool <TToken> .Shared.Rent(_bufferChunkSize);

            _currentIndex  = 0;
            _bufferedCount = 0;

            _eof               = default;
            _unexpected        = default;
            _errorPos          = default;
            _message           = default;
            _expecteds         = new PooledList <Expected <TToken> >();
            _expectedBookmarks = new PooledList <int>();

            Buffer(0);
        }
Esempio n. 8
0
 public Bookmark(int value, SourcePos sourcePos)
 {
     Value = value;
     Pos   = sourcePos;
 }