Skip to content

Instantly share code, notes, and snippets.

@alabamenhu
Last active January 11, 2021 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alabamenhu/1b502c4ad8b4fa26c30f858e32e44938 to your computer and use it in GitHub Desktop.
Save alabamenhu/1b502c4ad8b4fa26c30f858e32e44938 to your computer and use it in GitHub Desktop.
Slang term
#unit module BX;
sub EXPORT(|) {
use QAST:from<NQP>;
use nqp;
role RevAccept {
multi method ACCEPTS (\a) { a.ACCEPTS(self) }
}
sub xpslang-xp (Str $a) { $a does RevAccept }
role BX::Grammar {
token term:sym<xpath> { <sym> '|' $<xpathS>=.+? '|' }
}
role BX::Actions {
sub lk(Mu \h, \k) {
nqp::atkey(nqp::findmethod(h, 'hash')(h), k)
}
method term:sym<xpath>(Mu $/) {
my $xpath := lk($/, 'xpathS');
my $block := QAST::Op.new(
:op<call>,
:name<&xpslang-xp>,
QAST::SVal.new(:value($xpath.Str))
);
$/.make($block);
}
}
$ = $*LANG.define_slang(
'MAIN',
$*LANG.slang_grammar('MAIN').^mixin(BX::Grammar),
$*LANG.actions.^mixin(BX::Actions)
);
Map.new: "&xpslang-xp" => &xpslang-xp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment