-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fernando Corrêa de Oliveira
committed
Sep 2, 2024
1 parent
4b1b734
commit 754dc67
Showing
5 changed files
with
177 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,84 @@ | ||
use QueryStorage; | ||
unit class Event is Any; | ||
unit class Event is Capture is Any; | ||
|
||
my QueryStorage $storage .= new; | ||
|
||
has Event $.parent; | ||
has Str $.rule = 'TOP'; | ||
has @.pos; | ||
has UInt $.index = 0; | ||
has $.hash-event; | ||
has $.received-event; | ||
has QueryStorage $.storage = $storage; | ||
has $.actions; | ||
has $.made; | ||
has Capture $.args; | ||
|
||
sub run(\match ( :$rule, :$args, | )) { | ||
match."$rule"(|$args.Capture) | ||
my sub to-map(\match --> Map()) { | ||
to-list match | ||
} | ||
my sub to-list(\match) { | ||
match.^attributes.grep(*.has_accessor).map(-> $a { | ||
$a.name.substr(2) => .<> with $a.get_value(match) | ||
}) | ||
} | ||
|
||
multi method raku(::?CLASS:U:) {self.^name} | ||
|
||
multi method raku(::?CLASS:D:) { | ||
"{ self.^name }.new({ to-list(self).grep(*.key ne 'storage')>>.raku.join: ', ' })" | ||
} | ||
|
||
sub run(\match) { | ||
my Str $rule = match.rule // "TOP"; | ||
my Capture $args = match.args // \(); | ||
do with $args { | ||
match."$rule"(|.Capture) | ||
} else { | ||
match."$rule"() | ||
} | ||
} | ||
|
||
method parse(Supply $supply, Str :$rule = "TOP", |args) { | ||
my $match := run self.new: :$rule, :args(args); | ||
say $match; | ||
my $match = run self.new: :$rule, :args(args), :pos[0], :0index; | ||
say $?LINE; | ||
supply { | ||
whenever $supply -> $event { | ||
for $storage.search: $event -> \match { | ||
say match; | ||
run match; | ||
run match.clone: :received-event($event); | ||
} | ||
} | ||
} | ||
} | ||
|
||
method clone(*%p) { | ||
my sub to-map(\match --> Map()) { | ||
match.^attributes.map(-> $a { | ||
$a.name.substr(2) => .<> with $a.get_value(match) | ||
}) | ||
} | ||
|
||
$.new: |to-map(self), |%p | ||
} | ||
|
||
use Pattern; | ||
my $pattern = Pattern.bless: :name<event>; | ||
$pattern.add-step: my method (*%pars) { | ||
say 'step: 1'; | ||
my $pattern = Pattern.bless: :name<event>, :source("event(...)"); | ||
$pattern.add-step: sub add-query-to-storage($match, *%pars) { | ||
my %query is Map = %pars.kv.map: -> $key, $value { | ||
do if $value !~~ Associative { | ||
$key => %("==" => $value) | ||
} else { | ||
$key => $value | ||
} | ||
} | ||
HandledReturn::AddToStorage.new: :match(self), :$!storage, :%query | ||
my $storage = $match.storage; | ||
HandledReturn::AddToStorage.new: :$storage, :%query | ||
} | ||
|
||
$pattern.add-step: my method (*%pars) { | ||
say 'step: 2'; | ||
note "RUNNING: {self.gist}" | ||
$pattern.add-step: sub query-matched ($match, *%pars) { | ||
HandledReturn::Next | ||
} | ||
::?CLASS.^add_method: 'event', $pattern; | ||
say $pattern; | ||
|
||
multi method gist(::?CLASS:D:) { | ||
[ | ||
"{$!rule}: 「" ~ (.gist with $!received-event) ~ '」' ~ " - ({ .subst(/\n/, "").subst(/\t/, "␉") with $.^find_method($!rule).?source })", | ||
do for |@.list.kv, |%.hash.kv -> $i, $submatch { | ||
"$i => $submatch.gist()" | ||
}.indent: 4 | ||
].join: "\n" | ||
} | ||
|
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
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
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
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