Skip to content

Commit 9663fec

Browse files
authored
Merge pull request #307 from ruby-syntax-tree/remove-parser
Remove the parser from the statements node
2 parents 5a666e4 + cd882e8 commit 9663fec

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

lib/syntax_tree/dsl.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def SClass(target, bodystmt)
791791

792792
# Create a new Statements node.
793793
def Statements(body)
794-
Statements.new(nil, body: body, location: Location.default)
794+
Statements.new(body: body, location: Location.default)
795795
end
796796

797797
# Create a new StringContent node.

lib/syntax_tree/node.rb

+7-11
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ def initialize(
22752275
@comments = []
22762276
end
22772277

2278-
def bind(start_char, start_column, end_char, end_column)
2278+
def bind(parser, start_char, start_column, end_char, end_column)
22792279
@location =
22802280
Location.new(
22812281
start_line: location.start_line,
@@ -2289,6 +2289,7 @@ def bind(start_char, start_column, end_char, end_column)
22892289
# Here we're going to determine the bounds for the statements
22902290
consequent = rescue_clause || else_clause || ensure_clause
22912291
statements.bind(
2292+
parser,
22922293
start_char,
22932294
start_column,
22942295
consequent ? consequent.location.start_char : end_char,
@@ -9816,23 +9817,19 @@ def ===(other)
98169817
# propagate that onto void_stmt nodes inside the stmts in order to make sure
98179818
# all comments get printed appropriately.
98189819
class Statements < Node
9819-
# [Parser] the parser that is generating this node
9820-
attr_reader :parser
9821-
98229820
# [Array[ Node ]] the list of expressions contained within this node
98239821
attr_reader :body
98249822

98259823
# [Array[ Comment | EmbDoc ]] the comments attached to this node
98269824
attr_reader :comments
98279825

9828-
def initialize(parser, body:, location:)
9829-
@parser = parser
9826+
def initialize(body:, location:)
98309827
@body = body
98319828
@location = location
98329829
@comments = []
98339830
end
98349831

9835-
def bind(start_char, start_column, end_char, end_column)
9832+
def bind(parser, start_char, start_column, end_char, end_column)
98369833
@location =
98379834
Location.new(
98389835
start_line: location.start_line,
@@ -9858,7 +9855,7 @@ def bind(start_char, start_column, end_char, end_column)
98589855
body[0] = VoidStmt.new(location: location)
98599856
end
98609857

9861-
attach_comments(start_char, end_char)
9858+
attach_comments(parser, start_char, end_char)
98629859
end
98639860

98649861
def bind_end(end_char, end_column)
@@ -9890,7 +9887,6 @@ def child_nodes
98909887
def copy(body: nil, location: nil)
98919888
node =
98929889
Statements.new(
9893-
parser,
98949890
body: body || self.body,
98959891
location: location || self.location
98969892
)
@@ -9902,7 +9898,7 @@ def copy(body: nil, location: nil)
99029898
alias deconstruct child_nodes
99039899

99049900
def deconstruct_keys(_keys)
9905-
{ parser: parser, body: body, location: location, comments: comments }
9901+
{ body: body, location: location, comments: comments }
99069902
end
99079903

99089904
def format(q)
@@ -9962,7 +9958,7 @@ def ===(other)
99629958
# As efficiently as possible, gather up all of the comments that have been
99639959
# found while this statements list was being parsed and add them into the
99649960
# body.
9965-
def attach_comments(start_char, end_char)
9961+
def attach_comments(parser, start_char, end_char)
99669962
parser_comments = parser.comments
99679963

99689964
comment_index = 0

lib/syntax_tree/parser.rb

+30-16
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ def on_BEGIN(statements)
374374

375375
start_char = find_next_statement_start(lbrace.location.end_char)
376376
statements.bind(
377+
self,
377378
start_char,
378379
start_char - line_counts[lbrace.location.start_line - 1].start,
379380
rbrace.location.start_char,
@@ -412,6 +413,7 @@ def on_END(statements)
412413

413414
start_char = find_next_statement_start(lbrace.location.end_char)
414415
statements.bind(
416+
self,
415417
start_char,
416418
start_char - line_counts[lbrace.location.start_line - 1].start,
417419
rbrace.location.start_char,
@@ -849,6 +851,7 @@ def on_begin(bodystmt)
849851
end
850852

851853
bodystmt.bind(
854+
self,
852855
find_next_statement_start(keyword.location.end_char),
853856
keyword.location.end_column,
854857
end_location.end_char,
@@ -960,11 +963,7 @@ def on_bodystmt(statements, rescue_clause, else_clause, ensure_clause)
960963
# case we'll wrap it in a Statements node to be consistent.
961964
unless statements.is_a?(Statements)
962965
statements =
963-
Statements.new(
964-
self,
965-
body: [statements],
966-
location: statements.location
967-
)
966+
Statements.new(body: [statements], location: statements.location)
968967
end
969968

970969
parts = [statements, rescue_clause, else_clause, ensure_clause].compact
@@ -991,6 +990,7 @@ def on_brace_block(block_var, statements)
991990

992991
start_char = find_next_statement_start(location.end_char)
993992
statements.bind(
993+
self,
994994
start_char,
995995
start_char - line_counts[location.start_line - 1].start,
996996
rbrace.location.start_char,
@@ -1098,6 +1098,7 @@ def on_class(constant, superclass, bodystmt)
10981098
start_char = find_next_statement_start(location.end_char)
10991099

11001100
bodystmt.bind(
1101+
self,
11011102
start_char,
11021103
start_char - line_counts[location.start_line - 1].start,
11031104
ending.location.start_char,
@@ -1307,6 +1308,7 @@ def on_def(name, params, bodystmt)
13071308
start_char = find_next_statement_start(params.location.end_char)
13081309

13091310
bodystmt.bind(
1311+
self,
13101312
start_char,
13111313
start_char - line_counts[params.location.start_line - 1].start,
13121314
ending.location.start_char,
@@ -1395,6 +1397,7 @@ def on_defs(target, operator, name, params, bodystmt)
13951397
start_char = find_next_statement_start(params.location.end_char)
13961398

13971399
bodystmt.bind(
1400+
self,
13981401
start_char,
13991402
start_char - line_counts[params.location.start_line - 1].start,
14001403
ending.location.start_char,
@@ -1434,6 +1437,7 @@ def on_do_block(block_var, bodystmt)
14341437
start_char = find_next_statement_start(location.end_char)
14351438

14361439
bodystmt.bind(
1440+
self,
14371441
start_char,
14381442
start_char - line_counts[location.start_line - 1].start,
14391443
ending.location.start_char,
@@ -1529,6 +1533,7 @@ def on_else(statements)
15291533

15301534
start_char = find_next_statement_start(keyword.location.end_char)
15311535
statements.bind(
1536+
self,
15321537
start_char,
15331538
start_char - line_counts[keyword.location.start_line - 1].start,
15341539
ending.location.start_char,
@@ -1554,6 +1559,7 @@ def on_elsif(predicate, statements, consequent)
15541559

15551560
start_char = find_next_statement_start(predicate.location.end_char)
15561561
statements.bind(
1562+
self,
15571563
start_char,
15581564
start_char - line_counts[predicate.location.start_line - 1].start,
15591565
ending.location.start_char,
@@ -1677,6 +1683,7 @@ def on_ensure(statements)
16771683
ending = find_keyword(:end)
16781684
start_char = find_next_statement_start(keyword.location.end_char)
16791685
statements.bind(
1686+
self,
16801687
start_char,
16811688
start_char - line_counts[keyword.location.start_line - 1].start,
16821689
ending.location.start_char,
@@ -1817,6 +1824,7 @@ def on_for(index, collection, statements)
18171824
find_next_statement_start((delimiter || collection).location.end_char)
18181825

18191826
statements.bind(
1827+
self,
18201828
start_char,
18211829
start_char -
18221830
line_counts[(delimiter || collection).location.end_line - 1].start,
@@ -2036,6 +2044,7 @@ def on_if(predicate, statements, consequent)
20362044
start_char =
20372045
find_next_statement_start((keyword || predicate).location.end_char)
20382046
statements.bind(
2047+
self,
20392048
start_char,
20402049
start_char - line_counts[predicate.location.end_line - 1].start,
20412050
ending.location.start_char,
@@ -2069,7 +2078,7 @@ def on_if_mod(predicate, statement)
20692078
IfNode.new(
20702079
predicate: predicate,
20712080
statements:
2072-
Statements.new(self, body: [statement], location: statement.location),
2081+
Statements.new(body: [statement], location: statement.location),
20732082
consequent: nil,
20742083
location: statement.location.to(predicate.location)
20752084
)
@@ -2121,6 +2130,7 @@ def on_in(pattern, statements, consequent)
21212130
start_char =
21222131
find_next_statement_start((token || statements_start).location.end_char)
21232132
statements.bind(
2133+
self,
21242134
start_char,
21252135
start_char -
21262136
line_counts[statements_start.location.start_line - 1].start,
@@ -2303,6 +2313,7 @@ def on_lambda(params, statements)
23032313

23042314
start_char = find_next_statement_start(opening.location.end_char)
23052315
statements.bind(
2316+
self,
23062317
start_char,
23072318
start_char - line_counts[opening.location.end_line - 1].start,
23082319
closing.location.start_char,
@@ -2587,6 +2598,7 @@ def on_module(constant, bodystmt)
25872598
start_char = find_next_statement_start(constant.location.end_char)
25882599

25892600
bodystmt.bind(
2601+
self,
25902602
start_char,
25912603
start_char - line_counts[constant.location.start_line - 1].start,
25922604
ending.location.start_char,
@@ -2863,7 +2875,7 @@ def on_program(statements)
28632875
)
28642876

28652877
statements.body << @__end__ if @__end__
2866-
statements.bind(0, 0, source.length, last_column)
2878+
statements.bind(self, 0, 0, source.length, last_column)
28672879

28682880
program = Program.new(statements: statements, location: location)
28692881
attach_comments(program, @comments)
@@ -3197,6 +3209,7 @@ def on_rescue(exceptions, variable, statements, consequent)
31973209
last_node = variable || exceptions || keyword
31983210
start_char = find_next_statement_start(last_node.end_char)
31993211
statements.bind(
3212+
self,
32003213
start_char,
32013214
start_char - line_counts[last_node.location.start_line - 1].start,
32023215
char_pos,
@@ -3315,6 +3328,7 @@ def on_sclass(target, bodystmt)
33153328
start_char = find_next_statement_start(target.location.end_char)
33163329

33173330
bodystmt.bind(
3331+
self,
33183332
start_char,
33193333
start_char - line_counts[target.location.start_line - 1].start,
33203334
ending.location.start_char,
@@ -3368,18 +3382,13 @@ def on_stmts_add(statements, statement)
33683382
statements.location.to(statement.location)
33693383
end
33703384

3371-
Statements.new(
3372-
self,
3373-
body: statements.body << statement,
3374-
location: location
3375-
)
3385+
Statements.new(body: statements.body << statement, location: location)
33763386
end
33773387

33783388
# :call-seq:
33793389
# on_stmts_new: () -> Statements
33803390
def on_stmts_new
33813391
Statements.new(
3382-
self,
33833392
body: [],
33843393
location:
33853394
Location.fixed(line: lineno, char: char_pos, column: current_column)
@@ -3444,6 +3453,7 @@ def on_string_embexpr(statements)
34443453
embexpr_end = consume_token(EmbExprEnd)
34453454

34463455
statements.bind(
3456+
self,
34473457
embexpr_beg.location.end_char,
34483458
embexpr_beg.location.end_column,
34493459
embexpr_end.location.start_char,
@@ -3794,6 +3804,7 @@ def on_unless(predicate, statements, consequent)
37943804
start_char =
37953805
find_next_statement_start((keyword || predicate).location.end_char)
37963806
statements.bind(
3807+
self,
37973808
start_char,
37983809
start_char - line_counts[predicate.location.end_line - 1].start,
37993810
ending.location.start_char,
@@ -3816,7 +3827,7 @@ def on_unless_mod(predicate, statement)
38163827
UnlessNode.new(
38173828
predicate: predicate,
38183829
statements:
3819-
Statements.new(self, body: [statement], location: statement.location),
3830+
Statements.new(body: [statement], location: statement.location),
38203831
consequent: nil,
38213832
location: statement.location.to(predicate.location)
38223833
)
@@ -3839,6 +3850,7 @@ def on_until(predicate, statements)
38393850
find_next_statement_start((delimiter || predicate).location.end_char)
38403851

38413852
statements.bind(
3853+
self,
38423854
start_char,
38433855
start_char - line_counts[predicate.location.end_line - 1].start,
38443856
ending.location.start_char,
@@ -3860,7 +3872,7 @@ def on_until_mod(predicate, statement)
38603872
UntilNode.new(
38613873
predicate: predicate,
38623874
statements:
3863-
Statements.new(self, body: [statement], location: statement.location),
3875+
Statements.new(body: [statement], location: statement.location),
38643876
location: statement.location.to(predicate.location)
38653877
)
38663878
end
@@ -3935,6 +3947,7 @@ def on_when(arguments, statements, consequent)
39353947
find_next_statement_start((token || statements_start).location.end_char)
39363948

39373949
statements.bind(
3950+
self,
39383951
start_char,
39393952
start_char -
39403953
line_counts[statements_start.location.start_line - 1].start,
@@ -3967,6 +3980,7 @@ def on_while(predicate, statements)
39673980
find_next_statement_start((delimiter || predicate).location.end_char)
39683981

39693982
statements.bind(
3983+
self,
39703984
start_char,
39713985
start_char - line_counts[predicate.location.end_line - 1].start,
39723986
ending.location.start_char,
@@ -3988,7 +4002,7 @@ def on_while_mod(predicate, statement)
39884002
WhileNode.new(
39894003
predicate: predicate,
39904004
statements:
3991-
Statements.new(self, body: [statement], location: statement.location),
4005+
Statements.new(body: [statement], location: statement.location),
39924006
location: statement.location.to(predicate.location)
39934007
)
39944008
end

lib/syntax_tree/yarv/compiler.rb

+1-6
Original file line numberDiff line numberDiff line change
@@ -1051,17 +1051,12 @@ def visit_if_op(node)
10511051
IfNode.new(
10521052
predicate: node.predicate,
10531053
statements:
1054-
Statements.new(
1055-
nil,
1056-
body: [node.truthy],
1057-
location: Location.default
1058-
),
1054+
Statements.new(body: [node.truthy], location: Location.default),
10591055
consequent:
10601056
Else.new(
10611057
keyword: Kw.new(value: "else", location: Location.default),
10621058
statements:
10631059
Statements.new(
1064-
nil,
10651060
body: [node.falsy],
10661061
location: Location.default
10671062
),

0 commit comments

Comments
 (0)