diff --git a/graphql-c_parser/lib/graphql/c_parser.rb b/graphql-c_parser/lib/graphql/c_parser.rb index 363fe7cf4a..3deec946f1 100644 --- a/graphql-c_parser/lib/graphql/c_parser.rb +++ b/graphql-c_parser/lib/graphql/c_parser.rb @@ -123,6 +123,11 @@ def result @result end + def tokens_count + result + @tokens.length + end + attr_reader :tokens, :next_token_index, :query_string, :filename end diff --git a/lib/graphql/language/lexer.rb b/lib/graphql/language/lexer.rb index 3bb362e61a..f78ffa06a7 100644 --- a/lib/graphql/language/lexer.rb +++ b/lib/graphql/language/lexer.rb @@ -19,7 +19,7 @@ def eos? @scanner.eos? end - attr_reader :pos + attr_reader :pos, :tokens_count def advance @scanner.skip(IGNORE_REGEXP) diff --git a/lib/graphql/language/parser.rb b/lib/graphql/language/parser.rb index 9c138c7178..280a76f5b7 100644 --- a/lib/graphql/language/parser.rb +++ b/lib/graphql/language/parser.rb @@ -49,6 +49,11 @@ def parse end end + def tokens_count + parse + @lexer.tokens_count + end + def line_at(pos) line = lines_at.bsearch_index { |l| l >= pos } if line.nil? diff --git a/spec/graphql/language/clexer_spec.rb b/spec/graphql/language/clexer_spec.rb index ffae05b7bf..e97a428a90 100644 --- a/spec/graphql/language/clexer_spec.rb +++ b/spec/graphql/language/clexer_spec.rb @@ -54,6 +54,13 @@ def assert_bad_unicode(string, _message = nil) refute default_ast.definitions.first.name.frozen? end + it "exposes tokens_count" do + str = "type Query { f1: Int }" + parser = GraphQL::CParser::Parser.new(str, nil, GraphQL::Tracing::NullTrace, nil) + + assert_equal 7, parser.tokens_count + end + include LexerExamples end end diff --git a/spec/graphql/language/parser_spec.rb b/spec/graphql/language/parser_spec.rb index c0e1a55c55..860e5a3460 100644 --- a/spec/graphql/language/parser_spec.rb +++ b/spec/graphql/language/parser_spec.rb @@ -506,6 +506,15 @@ end end + describe "#tokens_count" do + it "counts parsed token" do + str = "type Query { f1: Int }" + parser = GraphQL::Language::Parser.new(str) + + assert_equal 7, parser.tokens_count + end + end + module ParserTrace TRACES = [] def parse(query_string:)