[ruby/prism] Track parentheses in patterns
https://github.com/ruby/prism/commit/62db99f156
This commit is contained in:
parent
3f8ef7ff7c
commit
d1eaa97ec3
@ -583,13 +583,23 @@ module Prism
|
|||||||
# foo => bar | baz
|
# foo => bar | baz
|
||||||
# ^^^^^^^^^
|
# ^^^^^^^^^
|
||||||
def visit_alternation_pattern_node(node)
|
def visit_alternation_pattern_node(node)
|
||||||
left = visit(node.left)
|
left = visit_pattern_node(node.left)
|
||||||
right = visit(node.right)
|
right = visit_pattern_node(node.right)
|
||||||
|
|
||||||
bounds(node.location)
|
bounds(node.location)
|
||||||
on_binary(left, :|, right)
|
on_binary(left, :|, right)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Visit a pattern within a pattern match. This is used to bypass the
|
||||||
|
# parenthesis node that can be used to wrap patterns.
|
||||||
|
private def visit_pattern_node(node)
|
||||||
|
if node.is_a?(ParenthesesNode)
|
||||||
|
visit(node.body)
|
||||||
|
else
|
||||||
|
visit(node)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# a and b
|
# a and b
|
||||||
# ^^^^^^^
|
# ^^^^^^^
|
||||||
def visit_and_node(node)
|
def visit_and_node(node)
|
||||||
@ -1952,7 +1962,7 @@ module Prism
|
|||||||
# This is a special case where we're not going to call on_in directly
|
# This is a special case where we're not going to call on_in directly
|
||||||
# because we don't have access to the consequent. Instead, we'll return
|
# because we don't have access to the consequent. Instead, we'll return
|
||||||
# the component parts and let the parent node handle it.
|
# the component parts and let the parent node handle it.
|
||||||
pattern = visit(node.pattern)
|
pattern = visit_pattern_node(node.pattern)
|
||||||
statements =
|
statements =
|
||||||
if node.statements.nil?
|
if node.statements.nil?
|
||||||
bounds(node.location)
|
bounds(node.location)
|
||||||
@ -2389,7 +2399,7 @@ module Prism
|
|||||||
# ^^^^^^^^^^
|
# ^^^^^^^^^^
|
||||||
def visit_match_predicate_node(node)
|
def visit_match_predicate_node(node)
|
||||||
value = visit(node.value)
|
value = visit(node.value)
|
||||||
pattern = on_in(visit(node.pattern), nil, nil)
|
pattern = on_in(visit_pattern_node(node.pattern), nil, nil)
|
||||||
|
|
||||||
on_case(value, pattern)
|
on_case(value, pattern)
|
||||||
end
|
end
|
||||||
@ -2398,7 +2408,7 @@ module Prism
|
|||||||
# ^^^^^^^^^^
|
# ^^^^^^^^^^
|
||||||
def visit_match_required_node(node)
|
def visit_match_required_node(node)
|
||||||
value = visit(node.value)
|
value = visit(node.value)
|
||||||
pattern = on_in(visit(node.pattern), nil, nil)
|
pattern = on_in(visit_pattern_node(node.pattern), nil, nil)
|
||||||
|
|
||||||
on_case(value, pattern)
|
on_case(value, pattern)
|
||||||
end
|
end
|
||||||
|
@ -14883,14 +14883,20 @@ parse_pattern_primitives(pm_parser_t *parser, pm_diagnostic_id_t diag_id) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PM_TOKEN_PARENTHESIS_LEFT: {
|
case PM_TOKEN_PARENTHESIS_LEFT: {
|
||||||
|
pm_token_t opening = parser->current;
|
||||||
parser_lex(parser);
|
parser_lex(parser);
|
||||||
if (node != NULL) {
|
|
||||||
pm_node_destroy(parser, node);
|
|
||||||
}
|
|
||||||
node = parse_pattern(parser, false, PM_ERR_PATTERN_EXPRESSION_AFTER_PAREN);
|
|
||||||
|
|
||||||
|
pm_node_t *body = parse_pattern(parser, false, PM_ERR_PATTERN_EXPRESSION_AFTER_PAREN);
|
||||||
accept1(parser, PM_TOKEN_NEWLINE);
|
accept1(parser, PM_TOKEN_NEWLINE);
|
||||||
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_PATTERN_TERM_PAREN);
|
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_PATTERN_TERM_PAREN);
|
||||||
|
pm_node_t *right = (pm_node_t *) pm_parentheses_node_create(parser, &opening, body, &parser->previous);
|
||||||
|
|
||||||
|
if (node == NULL) {
|
||||||
|
node = right;
|
||||||
|
} else {
|
||||||
|
node = (pm_node_t *) pm_alternation_pattern_node_create(parser, node, right, &operator);
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
@ -186,16 +186,20 @@
|
|||||||
│ │ ├── closing_loc: ∅
|
│ │ ├── closing_loc: ∅
|
||||||
│ │ └── unescaped: "a"
|
│ │ └── unescaped: "a"
|
||||||
│ ├── conditions: (length: 1)
|
│ ├── conditions: (length: 1)
|
||||||
│ │ └── @ InNode (location: (22,0)-(22,9))
|
│ │ └── @ InNode (location: (22,0)-(22,10))
|
||||||
│ │ ├── pattern:
|
│ │ ├── pattern:
|
||||||
│ │ │ @ RangeNode (location: (22,4)-(22,9))
|
│ │ │ @ ParenthesesNode (location: (22,3)-(22,10))
|
||||||
│ │ │ ├── flags: exclude_end
|
│ │ │ ├── body:
|
||||||
│ │ │ ├── left: ∅
|
│ │ │ │ @ RangeNode (location: (22,4)-(22,9))
|
||||||
│ │ │ ├── right:
|
│ │ │ │ ├── flags: exclude_end
|
||||||
│ │ │ │ @ IntegerNode (location: (22,7)-(22,9))
|
│ │ │ │ ├── left: ∅
|
||||||
│ │ │ │ ├── flags: decimal
|
│ │ │ │ ├── right:
|
||||||
│ │ │ │ └── value: 10
|
│ │ │ │ │ @ IntegerNode (location: (22,7)-(22,9))
|
||||||
│ │ │ └── operator_loc: (22,4)-(22,7) = "..."
|
│ │ │ │ │ ├── flags: decimal
|
||||||
|
│ │ │ │ │ └── value: 10
|
||||||
|
│ │ │ │ └── operator_loc: (22,4)-(22,7) = "..."
|
||||||
|
│ │ │ ├── opening_loc: (22,3)-(22,4) = "("
|
||||||
|
│ │ │ └── closing_loc: (22,9)-(22,10) = ")"
|
||||||
│ │ ├── statements: ∅
|
│ │ ├── statements: ∅
|
||||||
│ │ ├── in_loc: (22,0)-(22,2) = "in"
|
│ │ ├── in_loc: (22,0)-(22,2) = "in"
|
||||||
│ │ └── then_loc: ∅
|
│ │ └── then_loc: ∅
|
||||||
@ -211,16 +215,20 @@
|
|||||||
│ │ ├── closing_loc: ∅
|
│ │ ├── closing_loc: ∅
|
||||||
│ │ └── unescaped: "a"
|
│ │ └── unescaped: "a"
|
||||||
│ ├── conditions: (length: 1)
|
│ ├── conditions: (length: 1)
|
||||||
│ │ └── @ InNode (location: (26,0)-(26,8))
|
│ │ └── @ InNode (location: (26,0)-(26,9))
|
||||||
│ │ ├── pattern:
|
│ │ ├── pattern:
|
||||||
│ │ │ @ RangeNode (location: (26,4)-(26,8))
|
│ │ │ @ ParenthesesNode (location: (26,3)-(26,9))
|
||||||
│ │ │ ├── flags: ∅
|
│ │ │ ├── body:
|
||||||
│ │ │ ├── left: ∅
|
│ │ │ │ @ RangeNode (location: (26,4)-(26,8))
|
||||||
│ │ │ ├── right:
|
│ │ │ │ ├── flags: ∅
|
||||||
│ │ │ │ @ IntegerNode (location: (26,6)-(26,8))
|
│ │ │ │ ├── left: ∅
|
||||||
│ │ │ │ ├── flags: decimal
|
│ │ │ │ ├── right:
|
||||||
│ │ │ │ └── value: 10
|
│ │ │ │ │ @ IntegerNode (location: (26,6)-(26,8))
|
||||||
│ │ │ └── operator_loc: (26,4)-(26,6) = ".."
|
│ │ │ │ │ ├── flags: decimal
|
||||||
|
│ │ │ │ │ └── value: 10
|
||||||
|
│ │ │ │ └── operator_loc: (26,4)-(26,6) = ".."
|
||||||
|
│ │ │ ├── opening_loc: (26,3)-(26,4) = "("
|
||||||
|
│ │ │ └── closing_loc: (26,8)-(26,9) = ")"
|
||||||
│ │ ├── statements: ∅
|
│ │ ├── statements: ∅
|
||||||
│ │ ├── in_loc: (26,0)-(26,2) = "in"
|
│ │ ├── in_loc: (26,0)-(26,2) = "in"
|
||||||
│ │ └── then_loc: ∅
|
│ │ └── then_loc: ∅
|
||||||
@ -236,16 +244,20 @@
|
|||||||
│ │ ├── closing_loc: ∅
|
│ │ ├── closing_loc: ∅
|
||||||
│ │ └── unescaped: "a"
|
│ │ └── unescaped: "a"
|
||||||
│ ├── conditions: (length: 1)
|
│ ├── conditions: (length: 1)
|
||||||
│ │ └── @ InNode (location: (30,0)-(30,8))
|
│ │ └── @ InNode (location: (30,0)-(30,9))
|
||||||
│ │ ├── pattern:
|
│ │ ├── pattern:
|
||||||
│ │ │ @ RangeNode (location: (30,4)-(30,8))
|
│ │ │ @ ParenthesesNode (location: (30,3)-(30,9))
|
||||||
│ │ │ ├── flags: exclude_end
|
│ │ │ ├── body:
|
||||||
│ │ │ ├── left:
|
│ │ │ │ @ RangeNode (location: (30,4)-(30,8))
|
||||||
│ │ │ │ @ IntegerNode (location: (30,4)-(30,5))
|
│ │ │ │ ├── flags: exclude_end
|
||||||
│ │ │ │ ├── flags: decimal
|
│ │ │ │ ├── left:
|
||||||
│ │ │ │ └── value: 1
|
│ │ │ │ │ @ IntegerNode (location: (30,4)-(30,5))
|
||||||
│ │ │ ├── right: ∅
|
│ │ │ │ │ ├── flags: decimal
|
||||||
│ │ │ └── operator_loc: (30,5)-(30,8) = "..."
|
│ │ │ │ │ └── value: 1
|
||||||
|
│ │ │ │ ├── right: ∅
|
||||||
|
│ │ │ │ └── operator_loc: (30,5)-(30,8) = "..."
|
||||||
|
│ │ │ ├── opening_loc: (30,3)-(30,4) = "("
|
||||||
|
│ │ │ └── closing_loc: (30,8)-(30,9) = ")"
|
||||||
│ │ ├── statements: ∅
|
│ │ ├── statements: ∅
|
||||||
│ │ ├── in_loc: (30,0)-(30,2) = "in"
|
│ │ ├── in_loc: (30,0)-(30,2) = "in"
|
||||||
│ │ └── then_loc: ∅
|
│ │ └── then_loc: ∅
|
||||||
@ -261,19 +273,23 @@
|
|||||||
│ │ ├── closing_loc: ∅
|
│ │ ├── closing_loc: ∅
|
||||||
│ │ └── unescaped: "a"
|
│ │ └── unescaped: "a"
|
||||||
│ ├── conditions: (length: 1)
|
│ ├── conditions: (length: 1)
|
||||||
│ │ └── @ InNode (location: (34,0)-(34,9))
|
│ │ └── @ InNode (location: (34,0)-(34,10))
|
||||||
│ │ ├── pattern:
|
│ │ ├── pattern:
|
||||||
│ │ │ @ RangeNode (location: (34,4)-(34,9))
|
│ │ │ @ ParenthesesNode (location: (34,3)-(34,10))
|
||||||
│ │ │ ├── flags: exclude_end
|
│ │ │ ├── body:
|
||||||
│ │ │ ├── left:
|
│ │ │ │ @ RangeNode (location: (34,4)-(34,9))
|
||||||
│ │ │ │ @ IntegerNode (location: (34,4)-(34,5))
|
│ │ │ │ ├── flags: exclude_end
|
||||||
│ │ │ │ ├── flags: decimal
|
│ │ │ │ ├── left:
|
||||||
│ │ │ │ └── value: 1
|
│ │ │ │ │ @ IntegerNode (location: (34,4)-(34,5))
|
||||||
│ │ │ ├── right:
|
│ │ │ │ │ ├── flags: decimal
|
||||||
│ │ │ │ @ IntegerNode (location: (34,8)-(34,9))
|
│ │ │ │ │ └── value: 1
|
||||||
│ │ │ │ ├── flags: decimal
|
│ │ │ │ ├── right:
|
||||||
│ │ │ │ └── value: 3
|
│ │ │ │ │ @ IntegerNode (location: (34,8)-(34,9))
|
||||||
│ │ │ └── operator_loc: (34,5)-(34,8) = "..."
|
│ │ │ │ │ ├── flags: decimal
|
||||||
|
│ │ │ │ │ └── value: 3
|
||||||
|
│ │ │ │ └── operator_loc: (34,5)-(34,8) = "..."
|
||||||
|
│ │ │ ├── opening_loc: (34,3)-(34,4) = "("
|
||||||
|
│ │ │ └── closing_loc: (34,9)-(34,10) = ")"
|
||||||
│ │ ├── statements: ∅
|
│ │ ├── statements: ∅
|
||||||
│ │ ├── in_loc: (34,0)-(34,2) = "in"
|
│ │ ├── in_loc: (34,0)-(34,2) = "in"
|
||||||
│ │ └── then_loc: ∅
|
│ │ └── then_loc: ∅
|
||||||
@ -289,11 +305,15 @@
|
|||||||
│ │ ├── closing_loc: ∅
|
│ │ ├── closing_loc: ∅
|
||||||
│ │ └── unescaped: "a"
|
│ │ └── unescaped: "a"
|
||||||
│ ├── conditions: (length: 1)
|
│ ├── conditions: (length: 1)
|
||||||
│ │ └── @ InNode (location: (38,0)-(38,6))
|
│ │ └── @ InNode (location: (38,0)-(38,7))
|
||||||
│ │ ├── pattern:
|
│ │ ├── pattern:
|
||||||
│ │ │ @ IntegerNode (location: (38,4)-(38,6))
|
│ │ │ @ ParenthesesNode (location: (38,3)-(38,7))
|
||||||
│ │ │ ├── flags: decimal
|
│ │ │ ├── body:
|
||||||
│ │ │ └── value: 42
|
│ │ │ │ @ IntegerNode (location: (38,4)-(38,6))
|
||||||
|
│ │ │ │ ├── flags: decimal
|
||||||
|
│ │ │ │ └── value: 42
|
||||||
|
│ │ │ ├── opening_loc: (38,3)-(38,4) = "("
|
||||||
|
│ │ │ └── closing_loc: (38,6)-(38,7) = ")"
|
||||||
│ │ ├── statements: ∅
|
│ │ ├── statements: ∅
|
||||||
│ │ ├── in_loc: (38,0)-(38,2) = "in"
|
│ │ ├── in_loc: (38,0)-(38,2) = "in"
|
||||||
│ │ └── then_loc: ∅
|
│ │ └── then_loc: ∅
|
||||||
|
Loading…
x
Reference in New Issue
Block a user