lib-ruby-parser 0.0.1.beta1-x86_64-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +122 -0
- data/lib/lib-ruby-parser/messages.rb +813 -0
- data/lib/lib-ruby-parser/native/lib_ruby_parser.so +0 -0
- data/lib/lib-ruby-parser/nodes.rb +4871 -0
- data/lib/lib-ruby-parser/version.rb +4 -0
- data/lib/lib-ruby-parser.rb +314 -0
- data/lib-ruby-parser.gemspec +34 -0
- metadata +52 -0
@@ -0,0 +1,314 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LibRubyParser
|
4
|
+
# Representation of any location in the given input
|
5
|
+
class Loc
|
6
|
+
# Begin of the `Loc` range
|
7
|
+
#
|
8
|
+
# @return [Integer]
|
9
|
+
attr_accessor :begin
|
10
|
+
|
11
|
+
# End of the `Loc` range
|
12
|
+
#
|
13
|
+
# @return [Integer]
|
14
|
+
attr_accessor :end
|
15
|
+
|
16
|
+
# @param [Hash] options
|
17
|
+
# @option options [Integer] :begin
|
18
|
+
# @option options [Integer] :end
|
19
|
+
def initialize(**options)
|
20
|
+
@begin = options[:begin] || 0
|
21
|
+
@end = options[:end] || 0
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# A token that is emitted by a lexer and consumed by a parser
|
26
|
+
class Token
|
27
|
+
# Numeric representation of the token type, e.g. +132+ (for example) for +tINTEGER+
|
28
|
+
#
|
29
|
+
# @return [Integer]
|
30
|
+
attr_accessor :token_type
|
31
|
+
|
32
|
+
# String type of the token
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
attr_accessor :token_name
|
36
|
+
|
37
|
+
# Value of the token, e.g +“42”+ for +42+
|
38
|
+
#
|
39
|
+
# @return [String]
|
40
|
+
attr_accessor :token_value
|
41
|
+
|
42
|
+
# Lex state before token was read
|
43
|
+
#
|
44
|
+
# @return [Integer]
|
45
|
+
attr_accessor :lex_state_before
|
46
|
+
|
47
|
+
# Lex state after token was read
|
48
|
+
#
|
49
|
+
# @return [Integer]
|
50
|
+
attr_accessor :lex_state_after
|
51
|
+
|
52
|
+
# Location of the token
|
53
|
+
#
|
54
|
+
# @return [Loc]
|
55
|
+
attr_accessor :loc
|
56
|
+
|
57
|
+
# @param token_type [Integer]
|
58
|
+
# @param token_name [String]
|
59
|
+
# @param token_value [String]
|
60
|
+
# @param loc [Loc]
|
61
|
+
def initialize(token_type:, token_name:, token_value:, loc:)
|
62
|
+
@token_type = token_type
|
63
|
+
@token_name = token_name
|
64
|
+
@token_value = token_value
|
65
|
+
@lex_state_before = 0
|
66
|
+
@lex_state_after = 0
|
67
|
+
@loc = loc
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Diagnostic message that comes from the parser when there’s an error or warning
|
72
|
+
class Diagnostic
|
73
|
+
# Level of the diagnostic (+:error+ or +:warning+)
|
74
|
+
#
|
75
|
+
# @return [Symbol] +:error+ or +:warning+
|
76
|
+
attr_accessor :level
|
77
|
+
|
78
|
+
# Message of the diagnostic
|
79
|
+
#
|
80
|
+
# @return [DiagnosticMessage]
|
81
|
+
attr_accessor :message
|
82
|
+
|
83
|
+
# Location of the diagnostic
|
84
|
+
#
|
85
|
+
# @return [Loc]
|
86
|
+
attr_accessor :loc
|
87
|
+
|
88
|
+
# @param level [Symbol]
|
89
|
+
# @param message [DiagnosticMessage]
|
90
|
+
# @param loc [Loc]
|
91
|
+
def initialize(level:, message:, loc:)
|
92
|
+
@level = level
|
93
|
+
@message = message
|
94
|
+
@loc = loc
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# A class that represents a comment in Ruby
|
99
|
+
class Comment
|
100
|
+
# Location of the comment (starts with # and ends with the last char)
|
101
|
+
#
|
102
|
+
# @return [Loc]
|
103
|
+
attr_accessor :location
|
104
|
+
|
105
|
+
# Kind of the comment (+:inline+ or +:document+ or +:unknown+)
|
106
|
+
#
|
107
|
+
# @return [Symbol] +:inline+ or +:document+ or +:unknown+
|
108
|
+
attr_accessor :kind
|
109
|
+
|
110
|
+
# @param location [Loc]
|
111
|
+
# @param kind [Symbol]
|
112
|
+
def initialize(location:, kind:)
|
113
|
+
@location = location
|
114
|
+
@kind = kind
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Representation of a magic comment in Ruby
|
119
|
+
class MagicComment
|
120
|
+
# Kind of a magic comment, can be
|
121
|
+
#
|
122
|
+
# 1. +encoding+
|
123
|
+
# 2. +frozen_string_literal+
|
124
|
+
# 3. +warn_indent+
|
125
|
+
# 4. +shareable_constant_value+
|
126
|
+
#
|
127
|
+
# @return [Symbol]
|
128
|
+
attr_accessor :kind
|
129
|
+
|
130
|
+
# Location of the “key”
|
131
|
+
#
|
132
|
+
# @example
|
133
|
+
# # encoding: utf-8
|
134
|
+
# ~~~~~~~~
|
135
|
+
#
|
136
|
+
# @return [Loc]
|
137
|
+
attr_accessor :key_l
|
138
|
+
|
139
|
+
# Location of the “value”
|
140
|
+
#
|
141
|
+
# @example
|
142
|
+
# # encoding: utf-8
|
143
|
+
# ~~~~~
|
144
|
+
#
|
145
|
+
# @return [Loc]
|
146
|
+
attr_accessor :value_l
|
147
|
+
|
148
|
+
# @param kind [Symbol]
|
149
|
+
# @param key_l [Loc]
|
150
|
+
# @param value_l [Loc]
|
151
|
+
def initialize(kind:, key_l:, value_l:)
|
152
|
+
@kind = kind
|
153
|
+
@key_l = key_l
|
154
|
+
@value_l = value_l
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# Base class for all AST nodes
|
159
|
+
class Node
|
160
|
+
end
|
161
|
+
|
162
|
+
# Base class for all diagnostic messages
|
163
|
+
class DiagnosticMessage
|
164
|
+
end
|
165
|
+
|
166
|
+
# Input decoded by parser.
|
167
|
+
#
|
168
|
+
# If your code has magic encoding comment LibRubyParser
|
169
|
+
# re-encodes given input and (potentially) operates
|
170
|
+
# on a different byte sequence.
|
171
|
+
#
|
172
|
+
# This is why this input should be used to get source
|
173
|
+
# of all locations.
|
174
|
+
#
|
175
|
+
# Using initial input is wrong as you may get a different
|
176
|
+
# source range in a different byte representation.
|
177
|
+
class DecodedInput
|
178
|
+
# Name of the initial input
|
179
|
+
#
|
180
|
+
# @return [String]
|
181
|
+
attr_accessor :name
|
182
|
+
|
183
|
+
# Array of source lines
|
184
|
+
#
|
185
|
+
# @return [Array<SourceLine>]
|
186
|
+
attr_accessor :lines
|
187
|
+
|
188
|
+
# Re-encoded input string
|
189
|
+
#
|
190
|
+
# @return [String]
|
191
|
+
attr_accessor :bytes
|
192
|
+
end
|
193
|
+
|
194
|
+
# Representation of a source line in a source file
|
195
|
+
class SourceLine
|
196
|
+
# Start of the line (in bytes)
|
197
|
+
#
|
198
|
+
# @return [Integer]
|
199
|
+
attr_accessor :start
|
200
|
+
|
201
|
+
# End of the line (in bytes)
|
202
|
+
#
|
203
|
+
# @return [Integer]
|
204
|
+
attr_accessor :end
|
205
|
+
|
206
|
+
# +true+ if line ends with EOF char (which is true for the last line in the file)
|
207
|
+
#
|
208
|
+
# @return [true, false]
|
209
|
+
attr_accessor :ends_with_eof
|
210
|
+
|
211
|
+
# @param options [Hash]
|
212
|
+
# @option options [Integer] :start
|
213
|
+
# @option options [Integer] :end
|
214
|
+
# @option options [true, false] :ends_with_eof
|
215
|
+
def initialize(**options)
|
216
|
+
@start = options[:start]
|
217
|
+
@end = options[:end]
|
218
|
+
@ends_with_eof = options[:ends_with_eof]
|
219
|
+
end
|
220
|
+
|
221
|
+
# Default +==+ comparison
|
222
|
+
def ==(other)
|
223
|
+
start == other.start &&
|
224
|
+
self.end == other.end &&
|
225
|
+
ends_with_eof == other.ends_with_eof
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
# Combination of all data that +LibRubyParser+ can give you
|
230
|
+
class ParserResult
|
231
|
+
# Abstract Syntax Tree that was constructed from you code.
|
232
|
+
# +nil+ if the code gives no AST nodes
|
233
|
+
#
|
234
|
+
# @return [Node, nil]
|
235
|
+
attr_accessor :ast
|
236
|
+
|
237
|
+
# List of tokens returned by a Lexer and consumed by a Parser.
|
238
|
+
# Empty unless +:record_tokens+ is set to true.
|
239
|
+
#
|
240
|
+
# @return [Array<Token>]
|
241
|
+
attr_accessor :tokens
|
242
|
+
|
243
|
+
# List of all diagnostics (errors and warings) that
|
244
|
+
# have been recorded during lexing and parsing
|
245
|
+
#
|
246
|
+
# @return [Array<Diagnostic>]
|
247
|
+
attr_accessor :diagnostics
|
248
|
+
|
249
|
+
# List of comments extracted from the source code.
|
250
|
+
#
|
251
|
+
# @return [Array<Comment>]
|
252
|
+
attr_accessor :comments
|
253
|
+
|
254
|
+
# List of magic comments extracted from the source code.
|
255
|
+
#
|
256
|
+
# @return [Array<MagicComment>]
|
257
|
+
attr_accessor :magic_comments
|
258
|
+
|
259
|
+
# Input that was used for parsing.
|
260
|
+
#
|
261
|
+
# Note: this input is not necessary the same byte array that you passed to Parser::parse.
|
262
|
+
# If encoding of the input is not +UTF-8+ or +ASCII-8BIT/BINARY+ Parser invokes decoder
|
263
|
+
# that usually produces a different sequence of bytes.
|
264
|
+
#
|
265
|
+
# Pass this data to +Loc#source+, otherwise you’ll get incorrect source ranges.
|
266
|
+
#
|
267
|
+
# @return [DecodedInput]
|
268
|
+
attr_accessor :input
|
269
|
+
end
|
270
|
+
|
271
|
+
# Default decoder that is used to re-encode input
|
272
|
+
# from non-utf-8 encoding to utf-8
|
273
|
+
DEFAULT_DECODER = proc do |encoding, input|
|
274
|
+
encoding = Encoding.find(encoding)
|
275
|
+
input.force_encoding(encoding).encode('utf-8')
|
276
|
+
end
|
277
|
+
|
278
|
+
# Parses given input according to given options
|
279
|
+
#
|
280
|
+
# @param [String] input
|
281
|
+
# @param [Hash] options
|
282
|
+
# @option options [String] :buffer_name name of the input file
|
283
|
+
# @option options [#call] :decoder (DEFAULT_DECODER) decoder, called with +encoding+ (String) and +input+ (String)
|
284
|
+
# @option options [true, false] :record_tokens When set to true Parser records tokens. When set to false ParserResult::tokens is guaranteed to be empty. If you don’t need tokens better set it to false to speed up parsing.
|
285
|
+
#
|
286
|
+
# @return [ParserResult]
|
287
|
+
def self.parse(input, options)
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
require_relative './lib-ruby-parser/nodes'
|
292
|
+
require_relative './lib-ruby-parser/messages'
|
293
|
+
|
294
|
+
require 'rbconfig'
|
295
|
+
|
296
|
+
os = RbConfig::CONFIG['host_os']
|
297
|
+
ruby_version = Gem::Version.new(RUBY_VERSION).segments.first(2).join('.')
|
298
|
+
|
299
|
+
case ruby_version
|
300
|
+
when '3.0', '2.7', '2.6'
|
301
|
+
# ok
|
302
|
+
else
|
303
|
+
warn "[lib-ruby-parser] You are running on windows/mingw with Ruby #{ruby_version}."
|
304
|
+
warn '[lib-ruby-parser] This version has not been tested, so it may crash.'
|
305
|
+
end
|
306
|
+
|
307
|
+
case os
|
308
|
+
when /mingw/
|
309
|
+
require_relative "./lib-ruby-parser/native/#{ruby_version}/lib_ruby_parser"
|
310
|
+
when /darwin/, /linux/
|
311
|
+
require_relative './lib-ruby-parser/native/lib_ruby_parser'
|
312
|
+
else
|
313
|
+
raise "[lib-ruby-parser] Unsupported OS '#{os}'"
|
314
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/lib-ruby-parser/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'lib-ruby-parser'
|
7
|
+
spec.version = LibRubyParser::VERSION
|
8
|
+
spec.licenses = ['MIT']
|
9
|
+
spec.authors = ['Ilya Bylich']
|
10
|
+
spec.email = ['ibylich@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Ruby parser written in Rust.'
|
13
|
+
spec.description = 'Ruby bindings for lib-ruby-parser.'
|
14
|
+
spec.homepage = 'https://github.com/lib-ruby-parser/ruby-bindings'
|
15
|
+
|
16
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
|
17
|
+
spec.platform = 'x86_64-linux'
|
18
|
+
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/lib-ruby-parser/ruby-bindings'
|
21
|
+
|
22
|
+
spec.files = [
|
23
|
+
'README.md',
|
24
|
+
'lib-ruby-parser.gemspec',
|
25
|
+
'lib/lib-ruby-parser.rb',
|
26
|
+
'lib/lib-ruby-parser/version.rb',
|
27
|
+
'lib/lib-ruby-parser/messages.rb',
|
28
|
+
'lib/lib-ruby-parser/nodes.rb',
|
29
|
+
'lib/lib-ruby-parser/native/lib_ruby_parser.so',
|
30
|
+
]
|
31
|
+
spec.bindir = 'exe'
|
32
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ['lib']
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lib-ruby-parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.beta1
|
5
|
+
platform: x86_64-linux
|
6
|
+
authors:
|
7
|
+
- Ilya Bylich
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby bindings for lib-ruby-parser.
|
14
|
+
email:
|
15
|
+
- ibylich@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib-ruby-parser.gemspec
|
22
|
+
- lib/lib-ruby-parser.rb
|
23
|
+
- lib/lib-ruby-parser/messages.rb
|
24
|
+
- lib/lib-ruby-parser/native/lib_ruby_parser.so
|
25
|
+
- lib/lib-ruby-parser/nodes.rb
|
26
|
+
- lib/lib-ruby-parser/version.rb
|
27
|
+
homepage: https://github.com/lib-ruby-parser/ruby-bindings
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata:
|
31
|
+
homepage_uri: https://github.com/lib-ruby-parser/ruby-bindings
|
32
|
+
source_code_uri: https://github.com/lib-ruby-parser/ruby-bindings
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 2.6.0
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.3.1
|
47
|
+
requirements: []
|
48
|
+
rubygems_version: 3.2.32
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: Ruby parser written in Rust.
|
52
|
+
test_files: []
|