42 / 43 lines covered (97.7%)
1use nqp;2use BDD::Behave::Callsite;34# When a `let(:NAME, { ... })` statement (or let-bang / subject) is parsed,5# install a lexical accessor NAME in the enclosing scope that resolves to6# $*LET-RUNTIME.value('NAME') at runtime.7#8# engage-let-slang must be called from the EXPORT sub so that $*LANG and the9# compiler's world ($*W) or resolver ($*R, under RakuAST) refer to the spec's10# compilation.1112sub has-lexical(Str $symbol --> Bool) {13 with $*R {14 return so nqp::isconcrete($*R.resolve-lexical($symbol, :current-scope-only));15 }1617 so $*W.cur_lexpad.symbol($symbol).elems;18}1920sub install-lexical(Str $symbol, $value) {21 with $*R {22 my $declaration := ::('RakuAST::VarDeclaration::Implicit::Constant').new(:name($symbol), :$value);23 $*R.current-scope.merge-generated-lexical-declaration($declaration, :resolver($*R), :force);24 return;25 }2627 $*W.install_lexical_symbol($*W.cur_lexpad, $symbol, $value);28}2930# Under RakuAST a block reports the line of its first statement rather than the31# line it opens on, so a direct call to one of these is told the line it starts32# on. The legacy frontend reports the opening line, so it needs no argument.33constant DECLARED-LINE is export = 'declared-at-line';3435my constant LINE-AWARE-CALLS = set <36 describe context fdescribe xdescribe fcontext xcontext37 it fit xit specify example pending let let-bang shared-examples38>;3940# A let named after one of these, or a sub of the spec's own, takes the name41# over, so the argument goes only to a call that still reaches behave's routine.42sub calls-behave-routine(Str $name --> Bool) {43 my $declaration := $*R.resolve-lexical('&' ~ $name);44 return False unless nqp::isconcrete($declaration);4546 my $routine := $declaration.compile-time-value;4748 so $routine ~~ Routine && $routine.name eq $name && is-behave-source($routine.file);49}5051sub add-declared-line(Mu $match, Str $text) {52 return without $*R;5354 my $statement := nqp::findmethod($match, 'ast')($match);55 return unless $statement ~~ ::('RakuAST::Statement::Expression');5657 my $call := $statement.expression;58 return unless $call ~~ ::('RakuAST::Call::Name');59 return unless $call.name.is-identifier;6061 my $name = $call.name.simple-identifier;62 return unless $name (elem) LINE-AWARE-CALLS && calls-behave-routine($name);6364 my $start = nqp::findmethod($match, 'from')($match) + ($text ~~ /^ \s*/).chars;65 my $line = nqp::getcomp('Raku').lineof(nqp::findmethod($match, 'orig')($match), $start, :cache(1));6667 $call.args.push: ::('RakuAST::ColonPair::Value').new(68 :key(DECLARED-LINE),69 :value(::('RakuAST::IntLiteral').new($line)),70 );71}7273sub engage-let-slang() is export {74 my $original-actions := $*LANG.slang_actions('MAIN');75 my %installed-names;7677 my role LetSlangActions {78 method statement(Mu $cursor is raw) {79 my $match := nqp::decont($cursor);80 nqp::findmethod($original-actions, 'statement')(self, $match);8182 my $text = nqp::box_s(nqp::findmethod($match, 'Str')($match), Str);8384 add-declared-line($match, $text);8586 if $text ~~ /^ \s* ['let-bang' | 'let' | 'subject'] \s* '(' \s*87 [ ':' $<name>=<[\w\-]>+88 | <['"]> $<name>=<-['"]>+ <['"]> ] / {89 my $name = ~$<name>;9091 if !%installed-names{$name} && has-lexical('&' ~ $name) {92 note "[BDD::Behave] let(:$name) shadows an existing '$name' in the same scope";93 }9495 %installed-names{$name} = True;9697 install-lexical('&' ~ $name, anon sub () { $*LET-RUNTIME.value($name) });98 }99 }100 }101102 my $grammar := $*LANG.slang_grammar('MAIN');103 my $actions := $original-actions.^mixin(LetSlangActions);104 $*LANG.define_slang('MAIN', $grammar, $actions);105}