Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4361d34

Browse files
committed
Remove the parser from the statements node
1 parent 5a666e4 commit 4361d34

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

lib/syntax_tree/dsl.rb

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 11 deletions
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

Lines changed: 28 additions & 9 deletions
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,
@@ -961,7 +964,6 @@ def on_bodystmt(statements, rescue_clause, else_clause, ensure_clause)
961964
unless statements.is_a?(Statements)
962965
statements =
963966
Statements.new(
964-
self,
965967
body: [statements],
966968
location: statements.location
967969
)
@@ -991,6 +993,7 @@ def on_brace_block(block_var, statements)
991993

992994
start_char = find_next_statement_start(location.end_char)
993995
statements.bind(
996+
self,
994997
start_char,
995998
start_char - line_counts[location.start_line - 1].start,
996999
rbrace.location.start_char,
@@ -1098,6 +1101,7 @@ def on_class(constant, superclass, bodystmt)
10981101
start_char = find_next_statement_start(location.end_char)
10991102

11001103
bodystmt.bind(
1104+
self,
11011105
start_char,
11021106
start_char - line_counts[location.start_line - 1].start,
11031107
ending.location.start_char,
@@ -1307,6 +1311,7 @@ def on_def(name, params, bodystmt)
13071311
start_char = find_next_statement_start(params.location.end_char)
13081312

13091313
bodystmt.bind(
1314+
self,
13101315
start_char,
13111316
start_char - line_counts[params.location.start_line - 1].start,
13121317
ending.location.start_char,
@@ -1395,6 +1400,7 @@ def on_defs(target, operator, name, params, bodystmt)
13951400
start_char = find_next_statement_start(params.location.end_char)
13961401

13971402
bodystmt.bind(
1403+
self,
13981404
start_char,
13991405
start_char - line_counts[params.location.start_line - 1].start,
14001406
ending.location.start_char,
@@ -1434,6 +1440,7 @@ def on_do_block(block_var, bodystmt)
14341440
start_char = find_next_statement_start(location.end_char)
14351441

14361442
bodystmt.bind(
1443+
self,
14371444
start_char,
14381445
start_char - line_counts[location.start_line - 1].start,
14391446
ending.location.start_char,
@@ -1529,6 +1536,7 @@ def on_else(statements)
15291536

15301537
start_char = find_next_statement_start(keyword.location.end_char)
15311538
statements.bind(
1539+
self,
15321540
start_char,
15331541
start_char - line_counts[keyword.location.start_line - 1].start,
15341542
ending.location.start_char,
@@ -1554,6 +1562,7 @@ def on_elsif(predicate, statements, consequent)
15541562

15551563
start_char = find_next_statement_start(predicate.location.end_char)
15561564
statements.bind(
1565+
self,
15571566
start_char,
15581567
start_char - line_counts[predicate.location.start_line - 1].start,
15591568
ending.location.start_char,
@@ -1677,6 +1686,7 @@ def on_ensure(statements)
16771686
ending = find_keyword(:end)
16781687
start_char = find_next_statement_start(keyword.location.end_char)
16791688
statements.bind(
1689+
self,
16801690
start_char,
16811691
start_char - line_counts[keyword.location.start_line - 1].start,
16821692
ending.location.start_char,
@@ -1817,6 +1827,7 @@ def on_for(index, collection, statements)
18171827
find_next_statement_start((delimiter || collection).location.end_char)
18181828

18191829
statements.bind(
1830+
self,
18201831
start_char,
18211832
start_char -
18221833
line_counts[(delimiter || collection).location.end_line - 1].start,
@@ -2036,6 +2047,7 @@ def on_if(predicate, statements, consequent)
20362047
start_char =
20372048
find_next_statement_start((keyword || predicate).location.end_char)
20382049
statements.bind(
2050+
self,
20392051
start_char,
20402052
start_char - line_counts[predicate.location.end_line - 1].start,
20412053
ending.location.start_char,
@@ -2068,8 +2080,7 @@ def on_if_mod(predicate, statement)
20682080

20692081
IfNode.new(
20702082
predicate: predicate,
2071-
statements:
2072-
Statements.new(self, body: [statement], location: statement.location),
2083+
statements: Statements.new(body: [statement], location: statement.location),
20732084
consequent: nil,
20742085
location: statement.location.to(predicate.location)
20752086
)
@@ -2121,6 +2132,7 @@ def on_in(pattern, statements, consequent)
21212132
start_char =
21222133
find_next_statement_start((token || statements_start).location.end_char)
21232134
statements.bind(
2135+
self,
21242136
start_char,
21252137
start_char -
21262138
line_counts[statements_start.location.start_line - 1].start,
@@ -2303,6 +2315,7 @@ def on_lambda(params, statements)
23032315

23042316
start_char = find_next_statement_start(opening.location.end_char)
23052317
statements.bind(
2318+
self,
23062319
start_char,
23072320
start_char - line_counts[opening.location.end_line - 1].start,
23082321
closing.location.start_char,
@@ -2587,6 +2600,7 @@ def on_module(constant, bodystmt)
25872600
start_char = find_next_statement_start(constant.location.end_char)
25882601

25892602
bodystmt.bind(
2603+
self,
25902604
start_char,
25912605
start_char - line_counts[constant.location.start_line - 1].start,
25922606
ending.location.start_char,
@@ -2863,7 +2877,7 @@ def on_program(statements)
28632877
)
28642878

28652879
statements.body << @__end__ if @__end__
2866-
statements.bind(0, 0, source.length, last_column)
2880+
statements.bind(self, 0, 0, source.length, last_column)
28672881

28682882
program = Program.new(statements: statements, location: location)
28692883
attach_comments(program, @comments)
@@ -3197,6 +3211,7 @@ def on_rescue(exceptions, variable, statements, consequent)
31973211
last_node = variable || exceptions || keyword
31983212
start_char = find_next_statement_start(last_node.end_char)
31993213
statements.bind(
3214+
self,
32003215
start_char,
32013216
start_char - line_counts[last_node.location.start_line - 1].start,
32023217
char_pos,
@@ -3315,6 +3330,7 @@ def on_sclass(target, bodystmt)
33153330
start_char = find_next_statement_start(target.location.end_char)
33163331

33173332
bodystmt.bind(
3333+
self,
33183334
start_char,
33193335
start_char - line_counts[target.location.start_line - 1].start,
33203336
ending.location.start_char,
@@ -3369,7 +3385,6 @@ def on_stmts_add(statements, statement)
33693385
end
33703386

33713387
Statements.new(
3372-
self,
33733388
body: statements.body << statement,
33743389
location: location
33753390
)
@@ -3379,7 +3394,6 @@ def on_stmts_add(statements, statement)
33793394
# on_stmts_new: () -> Statements
33803395
def on_stmts_new
33813396
Statements.new(
3382-
self,
33833397
body: [],
33843398
location:
33853399
Location.fixed(line: lineno, char: char_pos, column: current_column)
@@ -3444,6 +3458,7 @@ def on_string_embexpr(statements)
34443458
embexpr_end = consume_token(EmbExprEnd)
34453459

34463460
statements.bind(
3461+
self,
34473462
embexpr_beg.location.end_char,
34483463
embexpr_beg.location.end_column,
34493464
embexpr_end.location.start_char,
@@ -3794,6 +3809,7 @@ def on_unless(predicate, statements, consequent)
37943809
start_char =
37953810
find_next_statement_start((keyword || predicate).location.end_char)
37963811
statements.bind(
3812+
self,
37973813
start_char,
37983814
start_char - line_counts[predicate.location.end_line - 1].start,
37993815
ending.location.start_char,
@@ -3816,7 +3832,7 @@ def on_unless_mod(predicate, statement)
38163832
UnlessNode.new(
38173833
predicate: predicate,
38183834
statements:
3819-
Statements.new(self, body: [statement], location: statement.location),
3835+
Statements.new(body: [statement], location: statement.location),
38203836
consequent: nil,
38213837
location: statement.location.to(predicate.location)
38223838
)
@@ -3839,6 +3855,7 @@ def on_until(predicate, statements)
38393855
find_next_statement_start((delimiter || predicate).location.end_char)
38403856

38413857
statements.bind(
3858+
self,
38423859
start_char,
38433860
start_char - line_counts[predicate.location.end_line - 1].start,
38443861
ending.location.start_char,
@@ -3860,7 +3877,7 @@ def on_until_mod(predicate, statement)
38603877
UntilNode.new(
38613878
predicate: predicate,
38623879
statements:
3863-
Statements.new(self, body: [statement], location: statement.location),
3880+
Statements.new(body: [statement], location: statement.location),
38643881
location: statement.location.to(predicate.location)
38653882
)
38663883
end
@@ -3935,6 +3952,7 @@ def on_when(arguments, statements, consequent)
39353952
find_next_statement_start((token || statements_start).location.end_char)
39363953

39373954
statements.bind(
3955+
self,
39383956
start_char,
39393957
start_char -
39403958
line_counts[statements_start.location.start_line - 1].start,
@@ -3967,6 +3985,7 @@ def on_while(predicate, statements)
39673985
find_next_statement_start((delimiter || predicate).location.end_char)
39683986

39693987
statements.bind(
3988+
self,
39703989
start_char,
39713990
start_char - line_counts[predicate.location.end_line - 1].start,
39723991
ending.location.start_char,
@@ -3988,7 +4007,7 @@ def on_while_mod(predicate, statement)
39884007
WhileNode.new(
39894008
predicate: predicate,
39904009
statements:
3991-
Statements.new(self, body: [statement], location: statement.location),
4010+
Statements.new(body: [statement], location: statement.location),
39924011
location: statement.location.to(predicate.location)
39934012
)
39944013
end

lib/syntax_tree/yarv/compiler.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,6 @@ def visit_if_op(node)
10521052
predicate: node.predicate,
10531053
statements:
10541054
Statements.new(
1055-
nil,
10561055
body: [node.truthy],
10571056
location: Location.default
10581057
),
@@ -1061,7 +1060,6 @@ def visit_if_op(node)
10611060
keyword: Kw.new(value: "else", location: Location.default),
10621061
statements:
10631062
Statements.new(
1064-
nil,
10651063
body: [node.falsy],
10661064
location: Location.default
10671065
),

0 commit comments

Comments
 (0)