graphql-c_parser 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module CParser
5
+ VERSION = "1.0.1"
6
+ end
7
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "graphql"
4
+ require "graphql/c_parser/version"
5
+ require "graphql/graphql_c_parser_ext"
6
+
7
+ module GraphQL
8
+ module CParser
9
+ def self.parse(query_str, filename: nil, trace: GraphQL::Tracing::NullTrace)
10
+ parser = Parser.new(query_str, filename, trace)
11
+ parser.result
12
+ end
13
+
14
+ def self.parse_file(filename)
15
+ contents = File.read(filename)
16
+ parse(contents, filename: filename)
17
+ end
18
+
19
+ def self.prepare_parse_error(message, parser)
20
+ if message.start_with?("memory exhausted")
21
+ return GraphQL::ParseError.new("This query is too large to execute.", nil, nil, parser.query_string, filename: parser.filename)
22
+ end
23
+ token = parser.tokens[parser.next_token_index - 1]
24
+ if token
25
+ # There might not be a token if it's a comments-only string
26
+ line = token[1]
27
+ col = token[2]
28
+ if line && col
29
+ location_str = " at [#{line}, #{col}]"
30
+ if !message.include?(location_str)
31
+ message += location_str
32
+ end
33
+ end
34
+
35
+ if !message.include?("end of file")
36
+ message.sub!(/, unexpected ([a-zA-Z ]+)(,| at)/, ", unexpected \\1 (#{token[3].inspect})\\2")
37
+ end
38
+ end
39
+
40
+ GraphQL::ParseError.new(message, line, col, parser.query_string, filename: parser.filename)
41
+ end
42
+
43
+ class Parser
44
+ def initialize(query_string, filename, trace)
45
+ if query_string.nil?
46
+ raise GraphQL::ParseError.new("No query string was present", nil, nil, query_string)
47
+ end
48
+ @query_string = query_string
49
+ @filename = filename
50
+ @tokens = nil
51
+ @next_token_index = 0
52
+ @result = nil
53
+ @trace = trace
54
+ end
55
+
56
+ def result
57
+ if @result.nil?
58
+ @tokens = @trace.lex(query_string: @query_string) do
59
+ GraphQL::CParser::Lexer.tokenize(@query_string)
60
+ end
61
+ @trace.parse(query_string: @query_string) do
62
+ c_parse
63
+ @result
64
+ end
65
+ end
66
+ @result
67
+ end
68
+
69
+ attr_reader :tokens, :next_token_index, :query_string, :filename
70
+ end
71
+ end
72
+
73
+ def self.scan_with_c(graphql_string)
74
+ GraphQL::CParser::Lexer.tokenize(graphql_string)
75
+ end
76
+
77
+ def self.parse_with_c(string, filename: nil, trace: GraphQL::Tracing::NullTrace)
78
+ if string.nil?
79
+ raise GraphQL::ParseError.new("No query string was present", nil, nil, string)
80
+ end
81
+ document = GraphQL::CParser.parse(string, filename: filename, trace: trace)
82
+ if document.definitions.size == 0
83
+ raise GraphQL::ParseError.new("Unexpected end of document", 1, 1, string)
84
+ end
85
+ document
86
+ end
87
+
88
+ self.default_parser = GraphQL::CParser
89
+ end
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ require "graphql/c_parser"
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-c_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Mosolgo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - rdmosolgo@gmail.com
30
+ executables: []
31
+ extensions:
32
+ - ext/graphql_c_parser_ext/extconf.rb
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ext/graphql_c_parser_ext/extconf.rb
36
+ - ext/graphql_c_parser_ext/graphql_c_parser_ext.c
37
+ - ext/graphql_c_parser_ext/graphql_c_parser_ext.h
38
+ - ext/graphql_c_parser_ext/lexer.c
39
+ - ext/graphql_c_parser_ext/lexer.h
40
+ - ext/graphql_c_parser_ext/lexer.rl
41
+ - ext/graphql_c_parser_ext/parser.c
42
+ - ext/graphql_c_parser_ext/parser.h
43
+ - ext/graphql_c_parser_ext/parser.y
44
+ - lib/graphql-c_parser.rb
45
+ - lib/graphql/c_parser.rb
46
+ - lib/graphql/c_parser/version.rb
47
+ homepage: https://github.com/rmosolgo/graphql-ruby
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://graphql-ruby.org
52
+ changelog_uri: https://github.com/rmosolgo/graphql-ruby/blob/master/graphql-c_parser/CHANGELOG.md
53
+ source_code_uri: https://github.com/rmosolgo/graphql-ruby
54
+ bug_tracker_uri: https://github.com/rmosolgo/graphql-ruby/issues
55
+ mailing_list_uri: https://tinyletter.com/graphql-ruby
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.4.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.4.1
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: A parser for GraphQL, implemented as a C extension
75
+ test_files: []