prism 0.26.0 → 0.27.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -1
- data/Makefile +3 -2
- data/config.yml +278 -5
- data/ext/prism/api_node.c +70 -72
- data/ext/prism/extconf.rb +23 -4
- data/ext/prism/extension.c +11 -6
- data/ext/prism/extension.h +1 -1
- data/include/prism/ast.h +271 -4
- data/include/prism/diagnostic.h +3 -0
- data/include/prism/version.h +2 -2
- data/lib/prism/ffi.rb +1 -1
- data/lib/prism/lex_compat.rb +18 -1
- data/lib/prism/node.rb +1772 -265
- data/lib/prism/parse_result/newlines.rb +0 -2
- data/lib/prism/parse_result.rb +59 -13
- data/lib/prism/reflection.rb +18 -28
- data/lib/prism/serialize.rb +7 -4
- data/lib/prism/translation/parser/compiler.rb +32 -13
- data/lib/prism/translation/ripper.rb +59 -59
- data/lib/prism/translation/ruby_parser.rb +66 -28
- data/lib/prism.rb +1 -1
- data/prism.gemspec +1 -1
- data/rbi/prism/node.rbi +5 -2
- data/rbi/prism/parse_result.rbi +62 -34
- data/rbi/prism/reflection.rbi +7 -13
- data/rbi/prism.rbi +9 -9
- data/sig/prism/node.rbs +4 -1
- data/sig/prism/parse_result.rbs +30 -10
- data/sig/prism/reflection.rbs +2 -8
- data/sig/prism/serialize.rbs +2 -3
- data/sig/prism.rbs +9 -9
- data/src/diagnostic.c +7 -1
- data/src/prism.c +16 -3
- data/src/util/pm_integer.c +9 -2
- metadata +2 -2
@@ -58,8 +58,6 @@ module Prism
|
|
58
58
|
|
59
59
|
# Walk the tree and mark nodes that are on a new line.
|
60
60
|
def mark_newlines!
|
61
|
-
value = self.value
|
62
|
-
raise "This method should only be called on a parse result that contains a node" unless Node === value
|
63
61
|
value.accept(Newlines.new(Array.new(1 + source.offsets.size, false))) # steep:ignore
|
64
62
|
end
|
65
63
|
end
|
data/lib/prism/parse_result.rb
CHANGED
@@ -438,14 +438,9 @@ module Prism
|
|
438
438
|
end
|
439
439
|
|
440
440
|
# This represents the result of a call to ::parse or ::parse_file. It contains
|
441
|
-
# the
|
442
|
-
# encountered.
|
443
|
-
class
|
444
|
-
# The value that was generated by parsing. Normally this holds the AST, but
|
445
|
-
# it can sometimes how a list of tokens or other results passed back from
|
446
|
-
# the parser.
|
447
|
-
attr_reader :value
|
448
|
-
|
441
|
+
# the requested structure, any comments that were encounters, and any errors
|
442
|
+
# that were encountered.
|
443
|
+
class Result
|
449
444
|
# The list of comments that were encountered during parsing.
|
450
445
|
attr_reader :comments
|
451
446
|
|
@@ -466,9 +461,8 @@ module Prism
|
|
466
461
|
# A Source instance that represents the source code that was parsed.
|
467
462
|
attr_reader :source
|
468
463
|
|
469
|
-
# Create a new
|
470
|
-
def initialize(
|
471
|
-
@value = value
|
464
|
+
# Create a new result object with the given values.
|
465
|
+
def initialize(comments, magic_comments, data_loc, errors, warnings, source)
|
472
466
|
@comments = comments
|
473
467
|
@magic_comments = magic_comments
|
474
468
|
@data_loc = data_loc
|
@@ -477,9 +471,9 @@ module Prism
|
|
477
471
|
@source = source
|
478
472
|
end
|
479
473
|
|
480
|
-
# Implement the hash pattern matching interface for
|
474
|
+
# Implement the hash pattern matching interface for Result.
|
481
475
|
def deconstruct_keys(keys)
|
482
|
-
{
|
476
|
+
{ comments: comments, magic_comments: magic_comments, data_loc: data_loc, errors: errors, warnings: warnings }
|
483
477
|
end
|
484
478
|
|
485
479
|
# Returns the encoding of the source code that was parsed.
|
@@ -500,6 +494,58 @@ module Prism
|
|
500
494
|
end
|
501
495
|
end
|
502
496
|
|
497
|
+
# This is a result specific to the `parse` and `parse_file` methods.
|
498
|
+
class ParseResult < Result
|
499
|
+
# The syntax tree that was parsed from the source code.
|
500
|
+
attr_reader :value
|
501
|
+
|
502
|
+
# Create a new parse result object with the given values.
|
503
|
+
def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
|
504
|
+
@value = value
|
505
|
+
super(comments, magic_comments, data_loc, errors, warnings, source)
|
506
|
+
end
|
507
|
+
|
508
|
+
# Implement the hash pattern matching interface for ParseResult.
|
509
|
+
def deconstruct_keys(keys)
|
510
|
+
super.merge!(value: value)
|
511
|
+
end
|
512
|
+
end
|
513
|
+
|
514
|
+
# This is a result specific to the `lex` and `lex_file` methods.
|
515
|
+
class LexResult < Result
|
516
|
+
# The list of tokens that were parsed from the source code.
|
517
|
+
attr_reader :value
|
518
|
+
|
519
|
+
# Create a new lex result object with the given values.
|
520
|
+
def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
|
521
|
+
@value = value
|
522
|
+
super(comments, magic_comments, data_loc, errors, warnings, source)
|
523
|
+
end
|
524
|
+
|
525
|
+
# Implement the hash pattern matching interface for LexResult.
|
526
|
+
def deconstruct_keys(keys)
|
527
|
+
super.merge!(value: value)
|
528
|
+
end
|
529
|
+
end
|
530
|
+
|
531
|
+
# This is a result specific to the `parse_lex` and `parse_lex_file` methods.
|
532
|
+
class ParseLexResult < Result
|
533
|
+
# A tuple of the syntax tree and the list of tokens that were parsed from
|
534
|
+
# the source code.
|
535
|
+
attr_reader :value
|
536
|
+
|
537
|
+
# Create a new parse lex result object with the given values.
|
538
|
+
def initialize(value, comments, magic_comments, data_loc, errors, warnings, source)
|
539
|
+
@value = value
|
540
|
+
super(comments, magic_comments, data_loc, errors, warnings, source)
|
541
|
+
end
|
542
|
+
|
543
|
+
# Implement the hash pattern matching interface for ParseLexResult.
|
544
|
+
def deconstruct_keys(keys)
|
545
|
+
super.merge!(value: value)
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
503
549
|
# This represents a token from the Ruby source.
|
504
550
|
class Token
|
505
551
|
# The Source object that represents the source this token came from.
|
data/lib/prism/reflection.rb
CHANGED
@@ -73,14 +73,16 @@ module Prism
|
|
73
73
|
class OptionalLocationField < Field
|
74
74
|
end
|
75
75
|
|
76
|
-
#
|
77
|
-
#
|
78
|
-
|
76
|
+
# An integer field represents an integer value. It is used to represent the
|
77
|
+
# value of an integer literal, the depth of local variables, and the number
|
78
|
+
# of a numbered reference. It resolves to an Integer in Ruby.
|
79
|
+
class IntegerField < Field
|
79
80
|
end
|
80
81
|
|
81
|
-
# A
|
82
|
-
#
|
83
|
-
|
82
|
+
# A float field represents a double-precision floating point value. It is
|
83
|
+
# used exclusively to represent the value of a floating point literal. It
|
84
|
+
# resolves to a Float in Ruby.
|
85
|
+
class FloatField < Field
|
84
86
|
end
|
85
87
|
|
86
88
|
# A flags field represents a bitset of flags on a node. It resolves to an
|
@@ -98,18 +100,6 @@ module Prism
|
|
98
100
|
end
|
99
101
|
end
|
100
102
|
|
101
|
-
# An integer field represents an arbitrarily-sized integer value. It is used
|
102
|
-
# exclusively to represent the value of an integer literal. It resolves to
|
103
|
-
# an Integer in Ruby.
|
104
|
-
class IntegerField < Field
|
105
|
-
end
|
106
|
-
|
107
|
-
# A double field represents a double-precision floating point value. It is
|
108
|
-
# used exclusively to represent the value of a floating point literal. It
|
109
|
-
# resolves to a Float in Ruby.
|
110
|
-
class DoubleField < Field
|
111
|
-
end
|
112
|
-
|
113
103
|
# Returns the fields for the given node.
|
114
104
|
def self.fields_for(node)
|
115
105
|
case node.type
|
@@ -220,7 +210,7 @@ module Prism
|
|
220
210
|
when :flip_flop_node
|
221
211
|
[FlagsField.new(:flags, [:exclude_end?]), OptionalNodeField.new(:left), OptionalNodeField.new(:right), LocationField.new(:operator_loc)]
|
222
212
|
when :float_node
|
223
|
-
[
|
213
|
+
[FloatField.new(:value)]
|
224
214
|
when :for_node
|
225
215
|
[NodeField.new(:index), NodeField.new(:collection), OptionalNodeField.new(:statements), LocationField.new(:for_keyword_loc), LocationField.new(:in_keyword_loc), OptionalLocationField.new(:do_keyword_loc), LocationField.new(:end_keyword_loc)]
|
226
216
|
when :forwarding_arguments_node
|
@@ -276,7 +266,7 @@ module Prism
|
|
276
266
|
when :instance_variable_write_node
|
277
267
|
[ConstantField.new(:name), LocationField.new(:name_loc), NodeField.new(:value), LocationField.new(:operator_loc)]
|
278
268
|
when :integer_node
|
279
|
-
[FlagsField.new(:flags, [:binary?, :decimal?, :octal?, :hexadecimal?]),
|
269
|
+
[FlagsField.new(:flags, [:binary?, :decimal?, :octal?, :hexadecimal?]), Integer.new(:value)]
|
280
270
|
when :interpolated_match_last_line_node
|
281
271
|
[FlagsField.new(:flags, [:ignore_case?, :extended?, :multi_line?, :once?, :euc_jp?, :ascii_8bit?, :windows_31j?, :utf_8?, :forced_utf8_encoding?, :forced_binary_encoding?, :forced_us_ascii_encoding?]), LocationField.new(:opening_loc), NodeListField.new(:parts), LocationField.new(:closing_loc)]
|
282
272
|
when :interpolated_regular_expression_node
|
@@ -296,17 +286,17 @@ module Prism
|
|
296
286
|
when :lambda_node
|
297
287
|
[ConstantListField.new(:locals), LocationField.new(:operator_loc), LocationField.new(:opening_loc), LocationField.new(:closing_loc), OptionalNodeField.new(:parameters), OptionalNodeField.new(:body)]
|
298
288
|
when :local_variable_and_write_node
|
299
|
-
[LocationField.new(:name_loc), LocationField.new(:operator_loc), NodeField.new(:value), ConstantField.new(:name),
|
289
|
+
[LocationField.new(:name_loc), LocationField.new(:operator_loc), NodeField.new(:value), ConstantField.new(:name), Integer.new(:depth)]
|
300
290
|
when :local_variable_operator_write_node
|
301
|
-
[LocationField.new(:name_loc), LocationField.new(:operator_loc), NodeField.new(:value), ConstantField.new(:name), ConstantField.new(:operator),
|
291
|
+
[LocationField.new(:name_loc), LocationField.new(:operator_loc), NodeField.new(:value), ConstantField.new(:name), ConstantField.new(:operator), Integer.new(:depth)]
|
302
292
|
when :local_variable_or_write_node
|
303
|
-
[LocationField.new(:name_loc), LocationField.new(:operator_loc), NodeField.new(:value), ConstantField.new(:name),
|
293
|
+
[LocationField.new(:name_loc), LocationField.new(:operator_loc), NodeField.new(:value), ConstantField.new(:name), Integer.new(:depth)]
|
304
294
|
when :local_variable_read_node
|
305
|
-
[ConstantField.new(:name),
|
295
|
+
[ConstantField.new(:name), Integer.new(:depth)]
|
306
296
|
when :local_variable_target_node
|
307
|
-
[ConstantField.new(:name),
|
297
|
+
[ConstantField.new(:name), Integer.new(:depth)]
|
308
298
|
when :local_variable_write_node
|
309
|
-
[ConstantField.new(:name),
|
299
|
+
[ConstantField.new(:name), Integer.new(:depth), LocationField.new(:name_loc), NodeField.new(:value), LocationField.new(:operator_loc)]
|
310
300
|
when :match_last_line_node
|
311
301
|
[FlagsField.new(:flags, [:ignore_case?, :extended?, :multi_line?, :once?, :euc_jp?, :ascii_8bit?, :windows_31j?, :utf_8?, :forced_utf8_encoding?, :forced_binary_encoding?, :forced_us_ascii_encoding?]), LocationField.new(:opening_loc), LocationField.new(:content_loc), LocationField.new(:closing_loc), StringField.new(:unescaped)]
|
312
302
|
when :match_predicate_node
|
@@ -330,9 +320,9 @@ module Prism
|
|
330
320
|
when :no_keywords_parameter_node
|
331
321
|
[LocationField.new(:operator_loc), LocationField.new(:keyword_loc)]
|
332
322
|
when :numbered_parameters_node
|
333
|
-
[
|
323
|
+
[Integer.new(:maximum)]
|
334
324
|
when :numbered_reference_read_node
|
335
|
-
[
|
325
|
+
[Integer.new(:number)]
|
336
326
|
when :optional_keyword_parameter_node
|
337
327
|
[FlagsField.new(:flags, [:repeated_parameter?]), ConstantField.new(:name), LocationField.new(:name_loc), NodeField.new(:value)]
|
338
328
|
when :optional_parameter_node
|
data/lib/prism/serialize.rb
CHANGED
@@ -18,7 +18,7 @@ module Prism
|
|
18
18
|
|
19
19
|
# The minor version of prism that we are expecting to find in the serialized
|
20
20
|
# strings.
|
21
|
-
MINOR_VERSION =
|
21
|
+
MINOR_VERSION = 27
|
22
22
|
|
23
23
|
# The patch version of prism that we are expecting to find in the serialized
|
24
24
|
# strings.
|
@@ -260,6 +260,8 @@ module Prism
|
|
260
260
|
:invalid_character,
|
261
261
|
:invalid_encoding_magic_comment,
|
262
262
|
:invalid_float_exponent,
|
263
|
+
:invalid_local_variable_read,
|
264
|
+
:invalid_local_variable_write,
|
263
265
|
:invalid_multibyte_char,
|
264
266
|
:invalid_multibyte_character,
|
265
267
|
:invalid_multibyte_escape,
|
@@ -332,6 +334,7 @@ module Prism
|
|
332
334
|
:pattern_hash_key,
|
333
335
|
:pattern_hash_key_duplicate,
|
334
336
|
:pattern_hash_key_label,
|
337
|
+
:pattern_hash_key_locals,
|
335
338
|
:pattern_ident_after_hrocket,
|
336
339
|
:pattern_label_after_comma,
|
337
340
|
:pattern_rest,
|
@@ -426,7 +429,7 @@ module Prism
|
|
426
429
|
length = load_varuint
|
427
430
|
lex_state = load_varuint
|
428
431
|
location = Location.new(@source, start, length)
|
429
|
-
tokens << [
|
432
|
+
tokens << [Token.new(source, type, location.slice, location), lex_state]
|
430
433
|
end
|
431
434
|
|
432
435
|
tokens
|
@@ -441,7 +444,7 @@ module Prism
|
|
441
444
|
tokens.each { |token,| token.value.force_encoding(encoding) }
|
442
445
|
|
443
446
|
raise "Expected to consume all bytes while deserializing" unless @io.eof?
|
444
|
-
|
447
|
+
LexResult.new(tokens, comments, magic_comments, data_loc, errors, warnings, @source)
|
445
448
|
end
|
446
449
|
|
447
450
|
def load_nodes
|
@@ -460,7 +463,7 @@ module Prism
|
|
460
463
|
|
461
464
|
def load_result
|
462
465
|
node, comments, magic_comments, data_loc, errors, warnings = load_nodes
|
463
|
-
|
466
|
+
ParseResult.new(node, comments, magic_comments, data_loc, errors, warnings, @source)
|
464
467
|
end
|
465
468
|
|
466
469
|
private
|
@@ -839,7 +839,7 @@ module Prism
|
|
839
839
|
token(node.in_loc),
|
840
840
|
pattern,
|
841
841
|
guard,
|
842
|
-
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset
|
842
|
+
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset, [";", "then"]),
|
843
843
|
visit(node.statements)
|
844
844
|
)
|
845
845
|
end
|
@@ -1030,6 +1030,12 @@ module Prism
|
|
1030
1030
|
end
|
1031
1031
|
end
|
1032
1032
|
|
1033
|
+
# -> { it }
|
1034
|
+
# ^^^^^^^^^
|
1035
|
+
def visit_it_parameters_node(node)
|
1036
|
+
builder.args(nil, [], nil, false)
|
1037
|
+
end
|
1038
|
+
|
1033
1039
|
# foo(bar: baz)
|
1034
1040
|
# ^^^^^^^^
|
1035
1041
|
def visit_keyword_hash_node(node)
|
@@ -1052,13 +1058,14 @@ module Prism
|
|
1052
1058
|
# ^^^^^
|
1053
1059
|
def visit_lambda_node(node)
|
1054
1060
|
parameters = node.parameters
|
1061
|
+
implicit_parameters = parameters.is_a?(NumberedParametersNode) || parameters.is_a?(ItParametersNode)
|
1055
1062
|
|
1056
1063
|
builder.block(
|
1057
1064
|
builder.call_lambda(token(node.operator_loc)),
|
1058
1065
|
[node.opening, srange(node.opening_loc)],
|
1059
1066
|
if parameters.nil?
|
1060
1067
|
builder.args(nil, [], nil, false)
|
1061
|
-
elsif
|
1068
|
+
elsif implicit_parameters
|
1062
1069
|
visit(node.parameters)
|
1063
1070
|
else
|
1064
1071
|
builder.args(
|
@@ -1068,7 +1075,7 @@ module Prism
|
|
1068
1075
|
false
|
1069
1076
|
)
|
1070
1077
|
end,
|
1071
|
-
node.body&.accept(copy_compiler(forwarding:
|
1078
|
+
node.body&.accept(copy_compiler(forwarding: implicit_parameters ? [] : find_forwarding(parameters&.parameters))),
|
1072
1079
|
[node.closing, srange(node.closing_loc)]
|
1073
1080
|
)
|
1074
1081
|
end
|
@@ -1076,7 +1083,14 @@ module Prism
|
|
1076
1083
|
# foo
|
1077
1084
|
# ^^^
|
1078
1085
|
def visit_local_variable_read_node(node)
|
1079
|
-
|
1086
|
+
name = node.name
|
1087
|
+
|
1088
|
+
# This is just a guess. parser doesn't have support for the implicit
|
1089
|
+
# `it` variable yet, so we'll probably have to visit this once it
|
1090
|
+
# does.
|
1091
|
+
name = :it if name == :"0it"
|
1092
|
+
|
1093
|
+
builder.ident([name, srange(node.location)]).updated(:lvar)
|
1080
1094
|
end
|
1081
1095
|
|
1082
1096
|
# foo = 1
|
@@ -1665,7 +1679,7 @@ module Prism
|
|
1665
1679
|
end
|
1666
1680
|
|
1667
1681
|
# until foo; bar end
|
1668
|
-
#
|
1682
|
+
# ^^^^^^^^^^^^^^^^^^
|
1669
1683
|
#
|
1670
1684
|
# bar until foo
|
1671
1685
|
# ^^^^^^^^^^^^^
|
@@ -1698,7 +1712,7 @@ module Prism
|
|
1698
1712
|
if node.then_keyword_loc
|
1699
1713
|
token(node.then_keyword_loc)
|
1700
1714
|
else
|
1701
|
-
srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset
|
1715
|
+
srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset, [";"])
|
1702
1716
|
end,
|
1703
1717
|
visit(node.statements)
|
1704
1718
|
)
|
@@ -1857,12 +1871,16 @@ module Prism
|
|
1857
1871
|
|
1858
1872
|
# Constructs a new source range by finding the given tokens between the
|
1859
1873
|
# given start offset and end offset. If the needle is not found, it
|
1860
|
-
# returns nil.
|
1874
|
+
# returns nil. Importantly it does not search past newlines or comments.
|
1875
|
+
#
|
1876
|
+
# Note that end_offset is allowed to be nil, in which case this will
|
1877
|
+
# search until the end of the string.
|
1861
1878
|
def srange_find(start_offset, end_offset, tokens)
|
1862
|
-
tokens.
|
1863
|
-
|
1864
|
-
|
1865
|
-
|
1879
|
+
if (match = source_buffer.source.byteslice(start_offset...end_offset).match(/(\s*)(#{tokens.join("|")})/))
|
1880
|
+
_, whitespace, token = *match
|
1881
|
+
token_offset = start_offset + whitespace.bytesize
|
1882
|
+
|
1883
|
+
[token, Range.new(source_buffer, offset_cache[token_offset], offset_cache[token_offset + token.bytesize])]
|
1866
1884
|
end
|
1867
1885
|
end
|
1868
1886
|
|
@@ -1875,13 +1893,14 @@ module Prism
|
|
1875
1893
|
def visit_block(call, block)
|
1876
1894
|
if block
|
1877
1895
|
parameters = block.parameters
|
1896
|
+
implicit_parameters = parameters.is_a?(NumberedParametersNode) || parameters.is_a?(ItParametersNode)
|
1878
1897
|
|
1879
1898
|
builder.block(
|
1880
1899
|
call,
|
1881
1900
|
token(block.opening_loc),
|
1882
1901
|
if parameters.nil?
|
1883
1902
|
builder.args(nil, [], nil, false)
|
1884
|
-
elsif
|
1903
|
+
elsif implicit_parameters
|
1885
1904
|
visit(parameters)
|
1886
1905
|
else
|
1887
1906
|
builder.args(
|
@@ -1896,7 +1915,7 @@ module Prism
|
|
1896
1915
|
false
|
1897
1916
|
)
|
1898
1917
|
end,
|
1899
|
-
block.body&.accept(copy_compiler(forwarding:
|
1918
|
+
block.body&.accept(copy_compiler(forwarding: implicit_parameters ? [] : find_forwarding(parameters&.parameters))),
|
1900
1919
|
token(block.closing_loc)
|
1901
1920
|
)
|
1902
1921
|
else
|
@@ -19,31 +19,31 @@ module Prism
|
|
19
19
|
# The main known difference is that we may omit dispatching some events in
|
20
20
|
# some cases. This impacts the following events:
|
21
21
|
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
22
|
+
# - on_assign_error
|
23
|
+
# - on_comma
|
24
|
+
# - on_ignored_nl
|
25
|
+
# - on_ignored_sp
|
26
|
+
# - on_kw
|
27
|
+
# - on_label_end
|
28
|
+
# - on_lbrace
|
29
|
+
# - on_lbracket
|
30
|
+
# - on_lparen
|
31
|
+
# - on_nl
|
32
|
+
# - on_op
|
33
|
+
# - on_operator_ambiguous
|
34
|
+
# - on_rbrace
|
35
|
+
# - on_rbracket
|
36
|
+
# - on_rparen
|
37
|
+
# - on_semicolon
|
38
|
+
# - on_sp
|
39
|
+
# - on_symbeg
|
40
|
+
# - on_tstring_beg
|
41
|
+
# - on_tstring_end
|
42
42
|
#
|
43
43
|
class Ripper < Compiler
|
44
44
|
# Parses the given Ruby program read from +src+.
|
45
45
|
# +src+ must be a String or an IO or a object with a #gets method.
|
46
|
-
def
|
46
|
+
def self.parse(src, filename = "(ripper)", lineno = 1)
|
47
47
|
new(src, filename, lineno).parse
|
48
48
|
end
|
49
49
|
|
@@ -54,22 +54,22 @@ module Prism
|
|
54
54
|
# By default, this method does not handle syntax errors in +src+,
|
55
55
|
# use the +raise_errors+ keyword to raise a SyntaxError for an error in +src+.
|
56
56
|
#
|
57
|
-
#
|
58
|
-
#
|
57
|
+
# require "ripper"
|
58
|
+
# require "pp"
|
59
59
|
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
60
|
+
# pp Ripper.lex("def m(a) nil end")
|
61
|
+
# #=> [[[1, 0], :on_kw, "def", FNAME ],
|
62
|
+
# [[1, 3], :on_sp, " ", FNAME ],
|
63
|
+
# [[1, 4], :on_ident, "m", ENDFN ],
|
64
|
+
# [[1, 5], :on_lparen, "(", BEG|LABEL],
|
65
|
+
# [[1, 6], :on_ident, "a", ARG ],
|
66
|
+
# [[1, 7], :on_rparen, ")", ENDFN ],
|
67
|
+
# [[1, 8], :on_sp, " ", BEG ],
|
68
|
+
# [[1, 9], :on_kw, "nil", END ],
|
69
|
+
# [[1, 12], :on_sp, " ", END ],
|
70
|
+
# [[1, 13], :on_kw, "end", END ]]
|
71
71
|
#
|
72
|
-
def
|
72
|
+
def self.lex(src, filename = "-", lineno = 1, raise_errors: false)
|
73
73
|
result = Prism.lex_compat(src, filepath: filename, line: lineno)
|
74
74
|
|
75
75
|
if result.failure? && raise_errors
|
@@ -368,17 +368,17 @@ module Prism
|
|
368
368
|
# returning +nil+ in such cases. Use the +raise_errors+ keyword
|
369
369
|
# to raise a SyntaxError for an error in +src+.
|
370
370
|
#
|
371
|
-
#
|
372
|
-
#
|
371
|
+
# require "ripper"
|
372
|
+
# require "pp"
|
373
373
|
#
|
374
|
-
#
|
375
|
-
#
|
376
|
-
#
|
377
|
-
#
|
378
|
-
#
|
379
|
-
#
|
374
|
+
# pp Ripper.sexp("def m(a) nil end")
|
375
|
+
# #=> [:program,
|
376
|
+
# [[:def,
|
377
|
+
# [:@ident, "m", [1, 4]],
|
378
|
+
# [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil, nil, nil, nil]],
|
379
|
+
# [:bodystmt, [[:var_ref, [:@kw, "nil", [1, 9]]]], nil, nil, nil]]]]
|
380
380
|
#
|
381
|
-
def
|
381
|
+
def self.sexp(src, filename = "-", lineno = 1, raise_errors: false)
|
382
382
|
builder = SexpBuilderPP.new(src, filename, lineno)
|
383
383
|
sexp = builder.parse
|
384
384
|
if builder.error?
|
@@ -397,23 +397,23 @@ module Prism
|
|
397
397
|
# returning +nil+ in such cases. Use the +raise_errors+ keyword
|
398
398
|
# to raise a SyntaxError for an error in +src+.
|
399
399
|
#
|
400
|
-
#
|
401
|
-
#
|
400
|
+
# require "ripper"
|
401
|
+
# require "pp"
|
402
402
|
#
|
403
|
-
#
|
404
|
-
#
|
405
|
-
#
|
406
|
-
#
|
407
|
-
#
|
408
|
-
#
|
409
|
-
#
|
410
|
-
#
|
411
|
-
#
|
412
|
-
#
|
413
|
-
#
|
414
|
-
#
|
403
|
+
# pp Ripper.sexp_raw("def m(a) nil end")
|
404
|
+
# #=> [:program,
|
405
|
+
# [:stmts_add,
|
406
|
+
# [:stmts_new],
|
407
|
+
# [:def,
|
408
|
+
# [:@ident, "m", [1, 4]],
|
409
|
+
# [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil]],
|
410
|
+
# [:bodystmt,
|
411
|
+
# [:stmts_add, [:stmts_new], [:var_ref, [:@kw, "nil", [1, 9]]]],
|
412
|
+
# nil,
|
413
|
+
# nil,
|
414
|
+
# nil]]]]
|
415
415
|
#
|
416
|
-
def
|
416
|
+
def self.sexp_raw(src, filename = "-", lineno = 1, raise_errors: false)
|
417
417
|
builder = SexpBuilder.new(src, filename, lineno)
|
418
418
|
sexp = builder.parse
|
419
419
|
if builder.error?
|