Skip to content
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

[WIP] Implement pattern matching #456

Draft
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions fixtures/small/aryptn_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
case ["I will arise", "and go now", "and go to Innisfree"]
in String, String
"and a small cabin build there, of clay and wattles made"
in SmallCabin["clay", "wattles"]
"Nine bean-rows will I have there, a hive for the honey-bee,"
in BeeLoudGlade[String, *, String]
"And live alone in the bee-loud glade"
end

case ["And I shall have some peace there", "for peace", "comes dropping slow"]
in String,;
"Dropping from the veils of the morning to where the cricket sings;"
in Midnight[*glow]
"There midnight's all a glimmer, and noon a purple glow,"
in 1, *, 2
"And evening full of the linnet's wings."
end

case []
in [*]
0
in []
1
else
2
end
26 changes: 26 additions & 0 deletions fixtures/small/aryptn_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
case ["I will arise", "and go now", "and go to Innisfree"]
in [String, String]
"and a small cabin build there, of clay and wattles made"
in SmallCabin["clay", "wattles"]
"Nine bean-rows will I have there, a hive for the honey-bee,"
in BeeLoudGlade[String, *, String]
"And live alone in the bee-loud glade"
end

case ["And I shall have some peace there", "for peace", "comes dropping slow"]
in [String, *]
"Dropping from the veils of the morning to where the cricket sings;"
in Midnight[*glow]
"There midnight's all a glimmer, and noon a purple glow,"
in [1, *, 2]
"And evening full of the linnet's wings."
end

case []
in [*]
0
in []
1
else
2
end
7 changes: 7 additions & 0 deletions fixtures/small/fndptn_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
case ["I will arise", "and go now", "for always night and day"]
in Lake[*, "I hear lake water lapping",with_low_sounds_by_the_shore, *waves]
<<~YEATS
While I stand on the roadway, or on the pavements grey,
I hear it in the deep heart's core.
YEATS
end
7 changes: 7 additions & 0 deletions fixtures/small/fndptn_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
case ["I will arise", "and go now", "for always night and day"]
in Lake[*, "I hear lake water lapping", with_low_sounds_by_the_shore, *waves]
<<~YEATS
While I stand on the roadway, or on the pavements grey,
I hear it in the deep heart's core.
YEATS
end
22 changes: 22 additions & 0 deletions fixtures/small/hshptn_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
case bees!
in {}
()
in { bees: "bzzz" }
()
in { bees: "bzzz", **nil }
()
in bees:
()
in bees: this_is_a_bee
()
in Beehive(queen_bee:, drones:)
()
in Beehive(queen_bee:beyonce, drones:)
()
in Beehive(queen_bee:, **nil)
()
in Beehive(queen_bee:, **rest_of_the_hive)
()
in Beehive(queen_bee:, **)
()
end
22 changes: 22 additions & 0 deletions fixtures/small/hshptn_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
case bees!
in {}
()
in {bees: "bzzz"}
()
in {bees: "bzzz", **nil}
()
in {bees:}
()
in {bees: this_is_a_bee}
()
in Beehive(queen_bee:, drones:)
()
in Beehive(queen_bee: beyonce, drones:)
()
in Beehive(queen_bee:, **nil)
()
in Beehive(queen_bee:, **rest_of_the_hive)
()
in Beehive(queen_bee:)
()
end
8 changes: 8 additions & 0 deletions fixtures/small/multiline_aryptn_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
case []
in [first,
*,
last]
puts "bees!!!"
else
"no bees"
end
10 changes: 10 additions & 0 deletions fixtures/small/multiline_aryptn_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
case []
in [
first,
*,
last
]
puts("bees!!!")
else
"no bees"
end
7 changes: 7 additions & 0 deletions fixtures/small/multiline_fndptn_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
case []
in *, "bees are present",
*; then
puts "bees!!!"
else
"no bees"
end
10 changes: 10 additions & 0 deletions fixtures/small/multiline_fndptn_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
case []
in [
*,
"bees are present",
*
]
puts("bees!!!")
else
"no bees"
end
23 changes: 23 additions & 0 deletions fixtures/small/multiline_hshptn_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
case bees!
in {
bees: "bzzz"
}
()
in {
bees: "bzzz",
**nil }
()
in bees:,
more_bees: aaaahhhh,;
()
in bees: this_is_a_bee
()
in Beehive(
queen_bee:,
drones:)
()
in Beehive(
queen_bee:,
**nil)
()
end
28 changes: 28 additions & 0 deletions fixtures/small/multiline_hshptn_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
case bees!
in {
bees: "bzzz"
}
()
in {
bees: "bzzz",
**nil
}
()
in {
bees:,
more_bees: aaaahhhh
}
()
in {bees: this_is_a_bee}
()
in Beehive(
queen_bee:,
drones:
)
()
in Beehive(
queen_bee:,
**nil
)
()
end
44 changes: 44 additions & 0 deletions librubyfmt/rubyfmt_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def initialize(file_data)
"next" => [],
"return" => [],
"when" => [],
"in" => [],
"case" => [],
"yield" => [],
"break" => [],
Expand Down Expand Up @@ -374,6 +375,10 @@ def on_when(cond, body, tail)
[:when, cond, body, tail, start_end_for_keyword('when')]
end

def on_in(cond, body, tail)
[:in, cond, body, tail, start_end_for_keyword('in')]
end

def on_case(cond, body)
[:case, cond, body, start_end_for_keyword('case')]
end
Expand All @@ -386,6 +391,45 @@ def on_break(arg)
[:break, arg, start_end_for_keyword('break')]
end

def on_var_field(ident)
# `ident` is `nil` for **nil in hshptn
line = if ident && ident != :nil
# ident.last = [line, col]
ident.last.first
else
# When ident is nil, this represents a "*" pattern, which
# is lexed as an op. *However*, this can also happen during
# an "implicit" splat, e.g. `in String,` (with a trailing comma),
# in which case there's not a last op_location either, so at that
# point we give up because `lineno` would be incorrect
#
# Important to note: in this case, we must `shift` and not `pop`,
# because in the case of fndptn, this isn't run until after both
# splats have been lexed, so their op locations will be in the reverse order
@op_locations.shift
end

start_end = if line
[[line, line]]
else
[nil]
end

super + start_end
end

def on_aryptn(*_)
with_lineno { super }
end

def on_fndptn(*_)
with_lineno { super }
end

def on_hshptn(*_)
with_lineno { super }
end

def on_tlambda(*args)
@tlambda_stack << lineno
super
Expand Down
Loading
Loading