Skip to content

Commit

Permalink
Expose tokens_count to understand existing sizes before applying a ma…
Browse files Browse the repository at this point in the history
…x_tokens
  • Loading branch information
ravangen committed Aug 13, 2024
1 parent d9bd9cf commit 6ccbcb5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions graphql-c_parser/lib/graphql/c_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/language/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def eos?
@scanner.eos?
end

attr_reader :pos
attr_reader :pos, :tokens_count

def advance
@scanner.skip(IGNORE_REGEXP)
Expand Down
5 changes: 5 additions & 0 deletions lib/graphql/language/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
7 changes: 7 additions & 0 deletions spec/graphql/language/clexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down

0 comments on commit 6ccbcb5

Please sign in to comment.