Class: GraphQL::Language::Parser

Inherits:
Object
  • Object
show all
Includes:
EmptyObjects, Nodes
Defined in:
lib/graphql/language/parser.rb

Direct Known Subclasses

SchemaParser

Defined Under Namespace

Classes: SchemaParser

Constant Summary

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Constants included from Nodes

Nodes::NONE

Class Attribute Summary collapse

Attributes included from Nodes

#name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil) ⇒ Parser

Returns a new instance of Parser.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/language/parser.rb', line 31

def initialize(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil)
  if graphql_str.nil?
    raise GraphQL::ParseError.new("No query string was present", nil, nil, nil)
  end
  @lexer = Lexer.new(graphql_str, filename: filename, max_tokens: max_tokens)
  @graphql_str = graphql_str
  @filename = filename
  @trace = trace
  @dedup_identifiers = false
  @lines_at = nil
end

Class Attribute Details

.cacheObject

Returns the value of attribute cache.



14
15
16
# File 'lib/graphql/language/parser.rb', line 14

def cache
  @cache
end

Class Method Details

.parse(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil) ⇒ Object



16
17
18
# File 'lib/graphql/language/parser.rb', line 16

def parse(graphql_str, filename: nil, trace: Tracing::NullTrace, max_tokens: nil)
  self.new(graphql_str, filename: filename, trace: trace, max_tokens: max_tokens).parse
end

.parse_file(filename, trace: Tracing::NullTrace) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/graphql/language/parser.rb', line 20

def parse_file(filename, trace: Tracing::NullTrace)
  if cache
    cache.fetch(filename) do
      parse(File.read(filename), filename: filename, trace: trace)
    end
  else
    parse(File.read(filename), filename: filename, trace: trace)
  end
end

Instance Method Details

#column_at(pos) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/graphql/language/parser.rb', line 67

def column_at(pos)
  next_line_idx = lines_at.bsearch_index { |l| l >= pos } || 0
  if next_line_idx > 0
    line_pos = @lines_at[next_line_idx - 1]
    pos - line_pos
  else
    pos + 1
  end
end

#line_at(pos) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/graphql/language/parser.rb', line 58

def line_at(pos)
  line = lines_at.bsearch_index { |l| l >= pos }
  if line.nil?
    @lines_at.size + 1
  else
    line + 1
  end
end

#parseObject



43
44
45
46
47
48
49
50
51
# File 'lib/graphql/language/parser.rb', line 43

def parse
  @document ||= begin
    @trace.parse(query_string: @graphql_str) do
      document
    end
  rescue SystemStackError
    raise GraphQL::ParseError.new("This query is too large to execute.", nil, nil, @query_str, filename: @filename)
  end
end

#tokens_countObject



53
54
55
56
# File 'lib/graphql/language/parser.rb', line 53

def tokens_count
  parse
  @lexer.tokens_count
end