forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from Shopify/Alex/translate-pattern-vars
Implement Prism -> Sorbet translation for variable binding in patterns
- Loading branch information
Showing
3 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
test/prism_regression/case_match_variable_binding.parse-tree.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
CaseMatch { | ||
expr = Send { | ||
receiver = NULL | ||
method = <U foo> | ||
args = [ | ||
] | ||
} | ||
inBodies = [ | ||
InPattern { | ||
pattern = ArrayPattern { | ||
elts = [ | ||
MatchVar { | ||
name = <U x> | ||
} | ||
] | ||
} | ||
guard = NULL | ||
body = DString { | ||
nodes = [ | ||
String { | ||
val = <U An Array-like thing that only contains > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U x> | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
InPattern { | ||
pattern = HashPattern { | ||
pairs = [ | ||
Pair { | ||
key = Symbol { | ||
val = <U k> | ||
} | ||
value = MatchVar { | ||
name = <U x> | ||
} | ||
} | ||
] | ||
} | ||
guard = NULL | ||
body = DString { | ||
nodes = [ | ||
String { | ||
val = <U A Hash-like whose key `:k` has value > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U x> | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
InPattern { | ||
pattern = ArrayPattern { | ||
elts = [ | ||
ArrayPattern { | ||
elts = [ | ||
MatchVar { | ||
name = <U value> | ||
} | ||
] | ||
} | ||
MatchRest { | ||
var = MatchVar { | ||
name = <U tail> | ||
} | ||
} | ||
] | ||
} | ||
guard = NULL | ||
body = DString { | ||
nodes = [ | ||
String { | ||
val = <U An array-like thing that starts with a one-element Array containing > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U value> | ||
} | ||
] | ||
} | ||
String { | ||
val = <U , and ends with > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U tail> | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
InPattern { | ||
pattern = HashPattern { | ||
pairs = [ | ||
Pair { | ||
key = Symbol { | ||
val = <U k> | ||
} | ||
value = ArrayPattern { | ||
elts = [ | ||
MatchVar { | ||
name = <U value> | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
guard = NULL | ||
body = DString { | ||
nodes = [ | ||
String { | ||
val = <U A hash-like whose key `:k` has a one-element Array value containing > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U value> | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
InPattern { | ||
pattern = ArrayPattern { | ||
elts = [ | ||
HashPattern { | ||
pairs = [ | ||
Pair { | ||
key = Symbol { | ||
val = <U k> | ||
} | ||
value = MatchVar { | ||
name = <U value> | ||
} | ||
} | ||
] | ||
} | ||
MatchRest { | ||
var = MatchVar { | ||
name = <U tail> | ||
} | ||
} | ||
] | ||
} | ||
guard = NULL | ||
body = DString { | ||
nodes = [ | ||
String { | ||
val = <U An array-like thing that starts with a one-element Hash containing > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U value> | ||
} | ||
] | ||
} | ||
String { | ||
val = <U , and ends with > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U tail> | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
InPattern { | ||
pattern = HashPattern { | ||
pairs = [ | ||
Pair { | ||
key = Symbol { | ||
val = <U k> | ||
} | ||
value = HashPattern { | ||
pairs = [ | ||
Pair { | ||
key = Symbol { | ||
val = <U k2> | ||
} | ||
value = MatchVar { | ||
name = <U value> | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
guard = NULL | ||
body = DString { | ||
nodes = [ | ||
String { | ||
val = <U A hash-like whose key `:k` has a one-element Hash value containing k2: > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U value> | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
InPattern { | ||
pattern = MatchVar { | ||
name = <U x> | ||
} | ||
guard = NULL | ||
body = DString { | ||
nodes = [ | ||
String { | ||
val = <U Some other value: > | ||
} | ||
Begin { | ||
stmts = [ | ||
LVar { | ||
name = <U x> | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
] | ||
elseBody = NULL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# typed: false | ||
|
||
case foo | ||
in [x] # Variable binding nested in an Array pattern | ||
"An Array-like thing that only contains #{x}" | ||
in { k: x } # Variable binding nested in a Hash pattern | ||
"A Hash-like whose key `:k` has value #{x}" | ||
|
||
in [[value], *tail] # Array pattern inside an Array pattern | ||
"An array-like thing that starts with a one-element Array containing #{value}, and ends with #{tail}" | ||
in { k: [value] } # Array pattern inside a Hash pattern | ||
"A hash-like whose key `:k` has a one-element Array value containing #{value}" | ||
|
||
in [{ k: value }, *tail] # A Hash pattern inside an Array pattern | ||
"An array-like thing that starts with a one-element Hash containing #{value}, and ends with #{tail}" | ||
in { k: { k2: value } } # A Hash pattern inside a Hash pattern | ||
"A hash-like whose key `:k` has a one-element Hash value containing k2: #{value}" | ||
|
||
in x | ||
"Some other value: #{x}" | ||
end |