Method: GraphQL::Language::Lexer#advance

Defined in:
lib/graphql/language/lexer.rb

#advanceObject



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
106
107
108
109
# File 'lib/graphql/language/lexer.rb', line 30

def advance
  @scanner.skip(IGNORE_REGEXP)
  if @scanner.eos?
    @finished = true
    return false
  end
  @tokens_count += 1
  if @tokens_count > @max_tokens
    raise_parse_error("This query is too large to execute.")
  end
  @pos = @scanner.pos
  next_byte = @string.getbyte(@pos)
  next_byte_is_for = FIRST_BYTES[next_byte]
  case next_byte_is_for
  when ByteFor::PUNCTUATION
    @scanner.pos += 1
    PUNCTUATION_NAME_FOR_BYTE[next_byte]
  when ByteFor::NAME
    if len = @scanner.skip(KEYWORD_REGEXP)
      case len
      when 2
        :ON
      when 12
        :SUBSCRIPTION
      else
        pos = @pos

        # Use bytes 2 and 3 as a unique identifier for this keyword
        bytes = (@string.getbyte(pos + 2) << 8) | @string.getbyte(pos + 1)
        KEYWORD_BY_TWO_BYTES[_hash(bytes)]
      end
    else
      @scanner.skip(IDENTIFIER_REGEXP)
      :IDENTIFIER
    end
  when ByteFor::IDENTIFIER
    @scanner.skip(IDENTIFIER_REGEXP)
    :IDENTIFIER
  when ByteFor::NUMBER
    if len = @scanner.skip(NUMERIC_REGEXP)

      if GraphQL.reject_numbers_followed_by_names
        new_pos = @scanner.pos
        peek_byte = @string.getbyte(new_pos)
        next_first_byte = FIRST_BYTES[peek_byte]
        if next_first_byte == ByteFor::NAME || next_first_byte == ByteFor::IDENTIFIER
          number_part = token_value
          name_part = @scanner.scan(IDENTIFIER_REGEXP)
          raise_parse_error("Name after number is not allowed (in `#{number_part}#{name_part}`)")
        end
      end
      # Check for a matched decimal:
      @scanner[1] ? :FLOAT : :INT
    else
      # Attempt to find the part after the `-`
      value = @scanner.scan(/-\s?[a-z0-9]*/i)
      invalid_byte_for_number_error_message = "Expected type 'number', but it was malformed#{value.nil? ? "" : ": #{value.inspect}"}."
      raise_parse_error(invalid_byte_for_number_error_message)
    end
  when ByteFor::ELLIPSIS
    if @string.getbyte(@pos + 1) != 46 || @string.getbyte(@pos + 2) != 46
      raise_parse_error("Expected `...`, actual: #{@string[@pos..@pos + 2].inspect}")
    end
    @scanner.pos += 3
    :ELLIPSIS
  when ByteFor::STRING
    if @scanner.skip(BLOCK_STRING_REGEXP) || @scanner.skip(QUOTED_STRING_REGEXP)
      :STRING
    else
      raise_parse_error("Expected string or block string, but it was malformed")
    end
  else
    @scanner.pos += 1
    :UNKNOWN_CHAR
  end
rescue ArgumentError => err
  if err.message == "invalid byte sequence in UTF-8"
    raise_parse_error("Parse error on bad Unicode escape sequence", nil, nil)
  end
end