Skip to content

Remove the parser from the statements node #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/syntax_tree/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ def SClass(target, bodystmt)

# Create a new Statements node.
def Statements(body)
Statements.new(nil, body: body, location: Location.default)
Statements.new(body: body, location: Location.default)
end

# Create a new StringContent node.
Expand Down
18 changes: 7 additions & 11 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,7 @@ def initialize(
@comments = []
end

def bind(start_char, start_column, end_char, end_column)
def bind(parser, start_char, start_column, end_char, end_column)
@location =
Location.new(
start_line: location.start_line,
Expand All @@ -2289,6 +2289,7 @@ def bind(start_char, start_column, end_char, end_column)
# Here we're going to determine the bounds for the statements
consequent = rescue_clause || else_clause || ensure_clause
statements.bind(
parser,
start_char,
start_column,
consequent ? consequent.location.start_char : end_char,
Expand Down Expand Up @@ -9816,23 +9817,19 @@ def ===(other)
# propagate that onto void_stmt nodes inside the stmts in order to make sure
# all comments get printed appropriately.
class Statements < Node
# [Parser] the parser that is generating this node
attr_reader :parser

# [Array[ Node ]] the list of expressions contained within this node
attr_reader :body

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

def initialize(parser, body:, location:)
@parser = parser
def initialize(body:, location:)
@body = body
@location = location
@comments = []
end

def bind(start_char, start_column, end_char, end_column)
def bind(parser, start_char, start_column, end_char, end_column)
@location =
Location.new(
start_line: location.start_line,
Expand All @@ -9858,7 +9855,7 @@ def bind(start_char, start_column, end_char, end_column)
body[0] = VoidStmt.new(location: location)
end

attach_comments(start_char, end_char)
attach_comments(parser, start_char, end_char)
end

def bind_end(end_char, end_column)
Expand Down Expand Up @@ -9890,7 +9887,6 @@ def child_nodes
def copy(body: nil, location: nil)
node =
Statements.new(
parser,
body: body || self.body,
location: location || self.location
)
Expand All @@ -9902,7 +9898,7 @@ def copy(body: nil, location: nil)
alias deconstruct child_nodes

def deconstruct_keys(_keys)
{ parser: parser, body: body, location: location, comments: comments }
{ body: body, location: location, comments: comments }
end

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

comment_index = 0
Expand Down
46 changes: 30 additions & 16 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ def on_BEGIN(statements)

start_char = find_next_statement_start(lbrace.location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[lbrace.location.start_line - 1].start,
rbrace.location.start_char,
Expand Down Expand Up @@ -412,6 +413,7 @@ def on_END(statements)

start_char = find_next_statement_start(lbrace.location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[lbrace.location.start_line - 1].start,
rbrace.location.start_char,
Expand Down Expand Up @@ -849,6 +851,7 @@ def on_begin(bodystmt)
end

bodystmt.bind(
self,
find_next_statement_start(keyword.location.end_char),
keyword.location.end_column,
end_location.end_char,
Expand Down Expand Up @@ -960,11 +963,7 @@ def on_bodystmt(statements, rescue_clause, else_clause, ensure_clause)
# case we'll wrap it in a Statements node to be consistent.
unless statements.is_a?(Statements)
statements =
Statements.new(
self,
body: [statements],
location: statements.location
)
Statements.new(body: [statements], location: statements.location)
end

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

start_char = find_next_statement_start(location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[location.start_line - 1].start,
rbrace.location.start_char,
Expand Down Expand Up @@ -1098,6 +1098,7 @@ def on_class(constant, superclass, bodystmt)
start_char = find_next_statement_start(location.end_char)

bodystmt.bind(
self,
start_char,
start_char - line_counts[location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -1307,6 +1308,7 @@ def on_def(name, params, bodystmt)
start_char = find_next_statement_start(params.location.end_char)

bodystmt.bind(
self,
start_char,
start_char - line_counts[params.location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -1395,6 +1397,7 @@ def on_defs(target, operator, name, params, bodystmt)
start_char = find_next_statement_start(params.location.end_char)

bodystmt.bind(
self,
start_char,
start_char - line_counts[params.location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -1434,6 +1437,7 @@ def on_do_block(block_var, bodystmt)
start_char = find_next_statement_start(location.end_char)

bodystmt.bind(
self,
start_char,
start_char - line_counts[location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -1529,6 +1533,7 @@ def on_else(statements)

start_char = find_next_statement_start(keyword.location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[keyword.location.start_line - 1].start,
ending.location.start_char,
Expand All @@ -1554,6 +1559,7 @@ def on_elsif(predicate, statements, consequent)

start_char = find_next_statement_start(predicate.location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[predicate.location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -1677,6 +1683,7 @@ def on_ensure(statements)
ending = find_keyword(:end)
start_char = find_next_statement_start(keyword.location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[keyword.location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -1817,6 +1824,7 @@ def on_for(index, collection, statements)
find_next_statement_start((delimiter || collection).location.end_char)

statements.bind(
self,
start_char,
start_char -
line_counts[(delimiter || collection).location.end_line - 1].start,
Expand Down Expand Up @@ -2036,6 +2044,7 @@ def on_if(predicate, statements, consequent)
start_char =
find_next_statement_start((keyword || predicate).location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -2069,7 +2078,7 @@ def on_if_mod(predicate, statement)
IfNode.new(
predicate: predicate,
statements:
Statements.new(self, body: [statement], location: statement.location),
Statements.new(body: [statement], location: statement.location),
consequent: nil,
location: statement.location.to(predicate.location)
)
Expand Down Expand Up @@ -2121,6 +2130,7 @@ def on_in(pattern, statements, consequent)
start_char =
find_next_statement_start((token || statements_start).location.end_char)
statements.bind(
self,
start_char,
start_char -
line_counts[statements_start.location.start_line - 1].start,
Expand Down Expand Up @@ -2303,6 +2313,7 @@ def on_lambda(params, statements)

start_char = find_next_statement_start(opening.location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[opening.location.end_line - 1].start,
closing.location.start_char,
Expand Down Expand Up @@ -2587,6 +2598,7 @@ def on_module(constant, bodystmt)
start_char = find_next_statement_start(constant.location.end_char)

bodystmt.bind(
self,
start_char,
start_char - line_counts[constant.location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -2863,7 +2875,7 @@ def on_program(statements)
)

statements.body << @__end__ if @__end__
statements.bind(0, 0, source.length, last_column)
statements.bind(self, 0, 0, source.length, last_column)

program = Program.new(statements: statements, location: location)
attach_comments(program, @comments)
Expand Down Expand Up @@ -3197,6 +3209,7 @@ def on_rescue(exceptions, variable, statements, consequent)
last_node = variable || exceptions || keyword
start_char = find_next_statement_start(last_node.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[last_node.location.start_line - 1].start,
char_pos,
Expand Down Expand Up @@ -3315,6 +3328,7 @@ def on_sclass(target, bodystmt)
start_char = find_next_statement_start(target.location.end_char)

bodystmt.bind(
self,
start_char,
start_char - line_counts[target.location.start_line - 1].start,
ending.location.start_char,
Expand Down Expand Up @@ -3368,18 +3382,13 @@ def on_stmts_add(statements, statement)
statements.location.to(statement.location)
end

Statements.new(
self,
body: statements.body << statement,
location: location
)
Statements.new(body: statements.body << statement, location: location)
end

# :call-seq:
# on_stmts_new: () -> Statements
def on_stmts_new
Statements.new(
self,
body: [],
location:
Location.fixed(line: lineno, char: char_pos, column: current_column)
Expand Down Expand Up @@ -3444,6 +3453,7 @@ def on_string_embexpr(statements)
embexpr_end = consume_token(EmbExprEnd)

statements.bind(
self,
embexpr_beg.location.end_char,
embexpr_beg.location.end_column,
embexpr_end.location.start_char,
Expand Down Expand Up @@ -3794,6 +3804,7 @@ def on_unless(predicate, statements, consequent)
start_char =
find_next_statement_start((keyword || predicate).location.end_char)
statements.bind(
self,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
Expand All @@ -3816,7 +3827,7 @@ def on_unless_mod(predicate, statement)
UnlessNode.new(
predicate: predicate,
statements:
Statements.new(self, body: [statement], location: statement.location),
Statements.new(body: [statement], location: statement.location),
consequent: nil,
location: statement.location.to(predicate.location)
)
Expand All @@ -3839,6 +3850,7 @@ def on_until(predicate, statements)
find_next_statement_start((delimiter || predicate).location.end_char)

statements.bind(
self,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
Expand All @@ -3860,7 +3872,7 @@ def on_until_mod(predicate, statement)
UntilNode.new(
predicate: predicate,
statements:
Statements.new(self, body: [statement], location: statement.location),
Statements.new(body: [statement], location: statement.location),
location: statement.location.to(predicate.location)
)
end
Expand Down Expand Up @@ -3935,6 +3947,7 @@ def on_when(arguments, statements, consequent)
find_next_statement_start((token || statements_start).location.end_char)

statements.bind(
self,
start_char,
start_char -
line_counts[statements_start.location.start_line - 1].start,
Expand Down Expand Up @@ -3967,6 +3980,7 @@ def on_while(predicate, statements)
find_next_statement_start((delimiter || predicate).location.end_char)

statements.bind(
self,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
Expand All @@ -3988,7 +4002,7 @@ def on_while_mod(predicate, statement)
WhileNode.new(
predicate: predicate,
statements:
Statements.new(self, body: [statement], location: statement.location),
Statements.new(body: [statement], location: statement.location),
location: statement.location.to(predicate.location)
)
end
Expand Down
7 changes: 1 addition & 6 deletions lib/syntax_tree/yarv/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1051,17 +1051,12 @@ def visit_if_op(node)
IfNode.new(
predicate: node.predicate,
statements:
Statements.new(
nil,
body: [node.truthy],
location: Location.default
),
Statements.new(body: [node.truthy], location: Location.default),
consequent:
Else.new(
keyword: Kw.new(value: "else", location: Location.default),
statements:
Statements.new(
nil,
body: [node.falsy],
location: Location.default
),
Expand Down