Skip to content

Parser locations #311

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 4 commits into from
Feb 19, 2023
Merged
Changes from 1 commit
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
Next Next commit
Even more parser gem locations
  • Loading branch information
kddnewton committed Feb 18, 2023
commit f5f8b6a8dcbf499db95d2c3f8c13ff57a4782bcc
129 changes: 67 additions & 62 deletions lib/syntax_tree/translation/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1287,35 +1287,13 @@ def visit_ident(node)

# Visit an IfNode node.
def visit_if(node)
predicate =
case node.predicate
when RangeNode
type =
node.predicate.operator.value == ".." ? :iflipflop : :eflipflop
s(type, visit(node.predicate).children, nil)
when RegexpLiteral
s(:match_current_line, [visit(node.predicate)], nil)
when Unary
if node.predicate.operator.value == "!" &&
node.predicate.statement.is_a?(RegexpLiteral)
s(
:send,
[
s(:match_current_line, [visit(node.predicate.statement)]),
:!
],
nil
)
else
visit(node.predicate)
end
else
visit(node.predicate)
end

s(
:if,
[predicate, visit(node.statements), visit(node.consequent)],
[
visit_predicate(node.predicate),
visit(node.statements),
visit(node.consequent)
],
if node.modifier?
smap_keyword_bare(
srange_find_between(node.statements, node.predicate, "if"),
Expand Down Expand Up @@ -2376,22 +2354,42 @@ def visit_tstring_content(node)
# Visit a Unary node.
def visit_unary(node)
# Special handling here for flipflops
if node.statement.is_a?(Paren) &&
node.statement.contents.is_a?(Statements) &&
node.statement.contents.body.length == 1 &&
(range = node.statement.contents.body.first).is_a?(RangeNode) &&
if (paren = node.statement).is_a?(Paren) &&
paren.contents.is_a?(Statements) &&
paren.contents.body.length == 1 &&
(range = paren.contents.body.first).is_a?(RangeNode) &&
node.operator == "!"
type = range.operator.value == ".." ? :iflipflop : :eflipflop
return(
s(
:send,
[s(:begin, [s(type, visit(range).children, nil)], nil), :!],
nil
s(
:send,
[
s(
:begin,
[
s(
range.operator.value == ".." ? :iflipflop : :eflipflop,
visit(range).children,
smap_operator(
srange_node(range.operator),
srange_node(range)
)
)
],
smap_collection(
srange_length(paren.start_char, 1),
srange_length(paren.end_char, -1),
srange_node(paren)
)
),
:!
],
smap_send_bare(
srange_length(node.start_char, 1),
srange_node(node)
)
)
else
visit(canonical_unary(node))
end

visit(canonical_unary(node))
end

# Visit an Undef node.
Expand All @@ -2408,31 +2406,13 @@ def visit_undef(node)

# Visit an UnlessNode node.
def visit_unless(node)
predicate =
case node.predicate
when RegexpLiteral
s(:match_current_line, [visit(node.predicate)], nil)
when Unary
if node.predicate.operator.value == "!" &&
node.predicate.statement.is_a?(RegexpLiteral)
s(
:send,
[
s(:match_current_line, [visit(node.predicate.statement)]),
:!
],
nil
)
else
visit(node.predicate)
end
else
visit(node.predicate)
end

s(
:if,
[predicate, visit(node.consequent), visit(node.statements)],
[
visit_predicate(node.predicate),
visit(node.consequent),
visit(node.statements)
],
if node.modifier?
smap_keyword_bare(
srange_find_between(node.statements, node.predicate, "unless"),
Expand Down Expand Up @@ -3014,6 +2994,31 @@ def srange_node(node)
location = node.location
srange(location.start_char, location.end_char)
end

def visit_predicate(node)
case node
when RangeNode
s(
node.operator.value == ".." ? :iflipflop : :eflipflop,
visit(node).children,
smap_operator(srange_node(node.operator), srange_node(node))
)
when RegexpLiteral
s(:match_current_line, [visit(node)], smap(srange_node(node)))
when Unary
if node.operator.value == "!" && node.statement.is_a?(RegexpLiteral)
s(
:send,
[s(:match_current_line, [visit(node.statement)]), :!],
smap_send_bare(srange_node(node.operator), srange_node(node))
)
else
visit(node)
end
else
visit(node)
end
end
end
end
end