private void ReadNumberToken(byte firstCode)
        {
            int  start   = _position;
            byte code    = firstCode;
            var  isFloat = false;

            if (code == GraphQLConstants.Minus)
            {
                code = _graphQLData[++_position];
            }

            if (code == GraphQLConstants.Zero && !IsEndOfStream(_position + 1))
            {
                code = _graphQLData[++_position];
                if (GraphQLConstants.IsDigit(code))
                {
                    throw new SyntaxException(this,
                                              "Invalid number, unexpected digit after 0: " +
                                              $"`{(char)code}` ({code}).");
                }
            }
            else
            {
                code = ReadDigits(code);
            }

            if (code == GraphQLConstants.Dot)
            {
                isFloat      = true;
                _floatFormat = Language.FloatFormat.FixedPoint;
                code         = _graphQLData[++_position];
                code         = ReadDigits(code);
            }

            if ((code | 0x20) == GraphQLConstants.E)
            {
                isFloat      = true;
                _floatFormat = Language.FloatFormat.Exponential;
                code         = _graphQLData[++_position];
                if (code == GraphQLConstants.Plus ||
                    code == GraphQLConstants.Minus)
                {
                    code = _graphQLData[++_position];
                }
                ReadDigits(code);
            }

            _kind  = isFloat ? TokenKind.Float : TokenKind.Integer;
            _start = start;
            _end   = _position;
            _value = _graphQLData.Slice(start, _position - start);
        }
Beispiel #2
0
        private void ReadNumberToken(byte firstCode)
        {
            var start   = _position;
            var code    = firstCode;
            var isFloat = false;

            if (code is GraphQLConstants.Minus)
            {
                code = _graphQLData[++_position];
            }

            if (code is GraphQLConstants.Zero && !IsEndOfStream(_position + 1))
            {
                code = _graphQLData[++_position];
                if (code.IsDigit())
                {
                    throw new SyntaxException(this, UnexpectedDigit, (char)code, code);
                }
            }
            else
            {
                code = ReadDigits(code);
            }

            if (code is GraphQLConstants.Dot)
            {
                isFloat      = true;
                _floatFormat = Language.FloatFormat.FixedPoint;
                code         = _graphQLData[++_position];
                code         = ReadDigits(code);
            }

            if ((code | 0x20) is GraphQLConstants.E)
            {
                isFloat      = true;
                _floatFormat = Language.FloatFormat.Exponential;
                code         = _graphQLData[++_position];
                if (code is GraphQLConstants.Plus or GraphQLConstants.Minus)
                {
                    code = _graphQLData[++_position];
                }
                ReadDigits(code);
            }

            _kind  = isFloat ? TokenKind.Float : TokenKind.Integer;
            _start = start;
            _end   = _position;
            _value = _graphQLData.Slice(start, _position - start);
        }
 public Utf8GraphQLReader(ReadOnlySpan <byte> graphQLData)
 {
     _kind         = TokenKind.StartOfFile;
     _start        = 0;
     _end          = 0;
     _lineStart    = 0;
     _line         = 1;
     _column       = 1;
     _graphQLData  = graphQLData;
     _length       = graphQLData.Length;
     _nextNewLines = 0;
     _position     = 0;
     _value        = null;
     _floatFormat  = null;
 }
        private IValueNode ParseScalarValue()
        {
            if (TokenHelper.IsString(in _reader))
            {
                return(ParseStringLiteral());
            }

            TokenInfo start = Start();
            TokenKind kind  = _reader.Kind;

            if (!TokenHelper.IsScalarValue(in _reader))
            {
                throw new SyntaxException(_reader,
                                          string.Format(CultureInfo.InvariantCulture,
                                                        LangResources.Parser_InvalidScalarToken,
                                                        _reader.Kind));
            }

            ReadOnlyMemory <byte> value  = _reader.Value.ToArray();
            FloatFormat?          format = _reader.FloatFormat;

            MoveNext();

            Location?location = CreateLocation(in start);

            if (kind == TokenKind.Float)
            {
                return(new FloatValueNode
                       (
                           location,
                           value,
                           format ?? FloatFormat.FixedPoint
                       ));
            }

            if (kind == TokenKind.Integer)
            {
                return(new IntValueNode
                       (
                           location,
                           value
                       ));
            }

            throw Unexpected(kind);
        }
        public Utf8GraphQLReader(ReadOnlySpan <byte> graphQLData)
        {
            if (graphQLData.Length == 0)
            {
                throw new ArgumentException("The graphQLData is empty.", nameof(graphQLData));
            }

            _kind         = TokenKind.StartOfFile;
            _start        = 0;
            _end          = 0;
            _lineStart    = 0;
            _line         = 1;
            _column       = 1;
            _graphQLData  = graphQLData;
            _length       = graphQLData.Length;
            _nextNewLines = 0;
            _position     = 0;
            _value        = null;
            _floatFormat  = null;
        }
        public StringGraphQLReader(ReadOnlySpan <char> graphQLData)
        {
            if (graphQLData.Length == 0)
            {
                throw new ArgumentException("The graphQLData is empty.", nameof(graphQLData));
            }

            _kind         = TokenKind.StartOfFile;
            _start        = 0;
            _end          = 0;
            _lineStart    = 0;
            _line         = 1;
            _column       = 1;
            _token        = new SyntaxToken(TokenKind.StartOfFile, 0, 0, 1, 1, null, null);
            _graphQLData  = graphQLData;
            _length       = graphQLData.Length;
            _nextNewLines = 0;
            _position     = 0;
            _value        = null;
            _floatFormat  = null;
        }
        private unsafe IValueNode ParseNonStringScalarValue()
        {
            ISyntaxToken start = _reader.Token;
            TokenKind    kind  = _reader.Kind;

            fixed(char *c = _reader.Value)
            {
                string      value  = new string(c, 0, _reader.Value.Length);
                FloatFormat?format = _reader.FloatFormat;

                MoveNext();

                var location = new Location(start, _reader.Token);

                if (kind == TokenKind.Float)
                {
                    return(new FloatValueNode
                           (
                               location,
                               value,
                               format ?? FloatFormat.FixedPoint
                           ));
                }

                if (kind == TokenKind.Integer)
                {
                    return(new IntValueNode
                           (
                               location,
                               value
                           ));
                }

                throw Unexpected(kind);
            }
        }
        public bool Read()
        {
            _floatFormat = null;

            if (_position == 0)
            {
                SkipBoml();
            }

            SkipWhitespaces();
            UpdateColumn();

            if (IsEndOfStream())
            {
                _start = _position;
                _end   = _position;
                _kind  = TokenKind.EndOfFile;
                _value = null;
                return(false);
            }

            byte code = _graphQLData[_position];

            if (GraphQLConstants.IsPunctuator(code))
            {
                ReadPunctuatorToken(code);
                return(true);
            }

            if (GraphQLConstants.IsLetterOrUnderscore(code))
            {
                ReadNameToken();
                return(true);
            }

            if (GraphQLConstants.IsDigitOrMinus(code))
            {
                ReadNumberToken(code);
                return(true);
            }

            if (code == GraphQLConstants.Hash)
            {
                ReadCommentToken();
                return(true);
            }

            if (code == GraphQLConstants.Quote)
            {
                if (_length > _position + 2 &&
                    _graphQLData[_position + 1] == GraphQLConstants.Quote &&
                    _graphQLData[_position + 2] == GraphQLConstants.Quote)
                {
                    _position += 2;
                    ReadBlockStringToken();
                }
                else
                {
                    ReadStringValueToken();
                }
                return(true);
            }

            throw new SyntaxException(this,
                                      $"Unexpected character `{(char)code}` ({code}).");
        }
        public bool Read()
        {
            _floatFormat = null;

            SkipWhitespaces();
            UpdateColumn();

            if (IsEndOfStream())
            {
                _start = _position;
                _end   = _position;
                _kind  = TokenKind.EndOfFile;
                _value = null;
                var token = new SyntaxToken(
                    TokenKind.EndOfFile, _position, _position,
                    _line, _column, null, _token);
                _token.Next = token;
                _token      = token;
                return(false);
            }

            char code = _graphQLData[_position];

            if (_isPunctuator[code])
            {
                ReadPunctuatorToken(code);
                CreateToken();
                return(true);
            }

            if (_isLetterOrDigitOrUnderscore[code])
            {
                ReadNameToken();
                CreateToken();
                return(true);
            }

            if (_isDigitOrMinus[code])
            {
                ReadNumberToken(code);
                CreateToken();
                return(true);
            }

            if (code == GraphQLConstants.Hash)
            {
                ReadCommentToken();
                CreateToken();
                return(true);
            }

            if (code == GraphQLConstants.Quote)
            {
                if (_length > _position + 2 &&
                    _graphQLData[_position + 1] == GraphQLConstants.Quote &&
                    _graphQLData[_position + 2] == GraphQLConstants.Quote)
                {
                    _position += 2;
                    ReadBlockStringToken();
                }
                else
                {
                    ReadStringValueToken();
                }
                CreateToken();
                return(true);
            }

            throw new SyntaxException(this,
                                      $"Unexpected character `{(char)code}` ({code}).");
        }