Skip to content
Merged
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
97 changes: 72 additions & 25 deletions src/forroute.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
// "blast radius" with no who-calls and no where-used word is TRANSITIVE (→ --impact); with one it stays
// direct-radius (→ --uses). Off (kBareBlastRadiusOn = false): every "blast radius" is direct-radius, as RT2 had it.
// A vetoed question is answered as every unrouted question is: the --for answer, byte for byte.
// R1 (lane R1, 2026-11-23) — an apply/invoke word ("apply", "invoke" and their forms) is a where-used cue unless "how do|does|did"
// leads it in its sentence: "how does rake invoke the task … `multitask` …" asks how the actor works, and the backticked symbol is
// not what is invoked. "where is `X` invoked", "what invokes `X`", a passive "how is `X` applied" still route (howDoBefore).
#pragma once

#include <algorithm>
Expand Down Expand Up @@ -247,15 +250,83 @@ template <class OkAt>
} );
}

// Where the sentence that `from` sits in ends: the first '?', or the first '.' followed by whitespace or the end — a '.' inside a
// dotted name (`Application.initialize`) does not end it (Python's (?:[^?.]|\.(?!\s|$))*). npos: the text's end.
[[nodiscard]] inline bool endsSentenceAt( std::string_view lq, std::size_t i ) noexcept
{
const bool spaceNext = i + 1 == lq.size() || lq[ i + 1 ] == ' ' || lq[ i + 1 ] == '\t' || lq[ i + 1 ] == '\n' || lq[ i + 1 ] == '\r'
|| lq[ i + 1 ] == '\f' || lq[ i + 1 ] == '\v';
return lq[ i ] == '?' || ( lq[ i ] == '.' && spaceNext );
}
[[nodiscard]] inline std::size_t sentenceEnd( std::string_view lq, std::size_t from )
{
for( std::size_t i = from; i < lq.size(); ++i )
{
if( endsSentenceAt( lq, i ) )
{
return i;
}
}
return std::string_view::npos;
}
// Where the sentence that `at` sits in begins: just after the last sentence end (sentenceEnd's reading) before `at`; 0 if none.
[[nodiscard]] inline std::size_t sentenceStart( std::string_view lq, std::size_t at )
{
for( std::size_t i = at; i > 0; --i )
{
if( endsSentenceAt( lq, i - 1 ) )
{
return i;
}
}
return 0;
}

// `word`, bounded on both sides, at an offset in [from, the end of from's sentence).
[[nodiscard]] inline bool wordInSentence( std::string_view lq, std::size_t from, std::string_view word )
{
const std::size_t stop = sentenceEnd( lq, from );
const std::string_view span = lq.substr( from, stop == std::string_view::npos ? std::string_view::npos : stop - from );
return occursWhere( span, word, [ & ]( std::size_t at ) { return isBoundary( lq, from + at ) && isBoundary( lq, from + at + word.size() ); } );
}

// R1 (the rake-12 everyday question): "how does rake invoke the task … `multitask` …" asks how the ACTOR works; the backticked
// symbol is not what gets invoked, so --uses on it drops the answer. True when "how" + do|does|did precedes `at` in its sentence;
// a passive "how is `X` invoked", "where is `X` applied" and "what invokes `X`" stay where-used.
[[nodiscard]] inline bool howDoBefore( std::string_view lq, std::size_t at )
{
const std::size_t from = sentenceStart( lq, at );
const std::string_view head = lq.substr( from, at - from );
return occursWhere( head, "how", [ & ]( std::size_t h )
{
const std::size_t k = afterBoundedWord( head, h, 3 );
if( k == std::string_view::npos || k == h + 3 )
{
return false; // not the word "how", or no space after it
}
static constexpr std::string_view kAux[] = { "does", "did", "do" };
return std::ranges::any_of( kAux, [ & ]( std::string_view aux ) { return head.substr( k, aux.size() ) == aux && isBoundary( head, k + aux.size() ); } );
} );
}

// rule R2's where-used cues (RT2; see the header for the pattern this transcribes).
[[nodiscard]] inline bool isWhereUsed( std::string_view lq )
{
constexpr std::string_view kWords[] = { "usage", "usages", "reference", "references", "referenced", "referencing", "pass", "passes",
"passed", "passing", "apply", "applies", "applied", "invoke", "invokes", "invoked" };
"passed", "passing" };
if( std::ranges::any_of( kWords, [ & ]( std::string_view w ) { return hasBoundedWord( lq, w, true ); } ) )
{
return true;
}
// R1: an apply/invoke word cues where-used unless "how do|does|did" leads it in its sentence (howDoBefore).
constexpr std::string_view kActWords[] = { "apply", "applies", "applied", "invoke", "invokes", "invoked" };
if( std::ranges::any_of( kActWords, [ & ]( std::string_view w )
{
return occursWhere( lq, w, [ & ]( std::size_t at ) { return isBoundary( lq, at ) && isBoundary( lq, at + w.size() ) && !howDoBefore( lq, at ); } );
} ) )
{
return true;
}
if( occursWhere( lq, "decorat", [ & ]( std::size_t at ) { return isBoundary( lq, at ); } ) )
{
return true;
Expand All @@ -278,30 +349,6 @@ template <class OkAt>
} );
}

// Where the sentence that `from` sits in ends: the first '?', or the first '.' followed by whitespace or the end — a '.' inside a
// dotted name (`Application.initialize`) does not end it (Python's (?:[^?.]|\.(?!\s|$))*). npos: the text's end.
[[nodiscard]] inline std::size_t sentenceEnd( std::string_view lq, std::size_t from )
{
for( std::size_t i = from; i < lq.size(); ++i )
{
const bool spaceNext = i + 1 == lq.size() || lq[ i + 1 ] == ' ' || lq[ i + 1 ] == '\t' || lq[ i + 1 ] == '\n' || lq[ i + 1 ] == '\r'
|| lq[ i + 1 ] == '\f' || lq[ i + 1 ] == '\v';
if( lq[ i ] == '?' || ( lq[ i ] == '.' && spaceNext ) )
{
return i;
}
}
return std::string_view::npos;
}

// `word`, bounded on both sides, at an offset in [from, the end of from's sentence).
[[nodiscard]] inline bool wordInSentence( std::string_view lq, std::size_t from, std::string_view word )
{
const std::size_t stop = sentenceEnd( lq, from );
const std::string_view span = lq.substr( from, stop == std::string_view::npos ? std::string_view::npos : stop - from );
return occursWhere( span, word, [ & ]( std::size_t at ) { return isBoundary( lq, from + at ) && isBoundary( lq, from + at + word.size() ); } );
}

// \b(does|do|did)\b S \bcall\b starting at `from`, within one sentence.
[[nodiscard]] inline bool auxThenCall( std::string_view lq, std::size_t from )
{
Expand Down
23 changes: 23 additions & 0 deletions test/forverbroutecheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -591,5 +591,28 @@ say $? "(39) [N] an unrouted --for --offset is the --for file page byte for byte
run f40 wide --for='Where is `target_fn` used?' --offset=100 --no-verb-route
if grep -q '<route ' "$DIR/f40.out"; then no "(40) [N] --no-verb-route --offset routed"; else ok "(40) [N] --no-verb-route --offset is the --for file page, never routed"; fi

# ── R1 arms (41–42): an apply/invoke word led by "how do|does|did" is not a where-used cue (forroute.h howDoBefore) ──────
# The rake-12 everyday shape ("how does rake invoke the task … `multitask` …") asks how the actor works; RT2 routed it to
# --uses on the backticked symbol and the answer lost the mechanism. (41) and (41b) are RED on the RT2/RT3 binary (it routed).
r1Unrouted(){ local n="$1" q="$2"
if run "r$n" plain --for="$q" && run "r${n}o" plain --for="$q" --no-verb-route; then
cmp -s "$DIR/r$n.out" "$DIR/r${n}o.out" && ! grep -q '<route ' "$DIR/r$n.out"
say $? "($n) [P] R1: '$q' stays on --for (the unrouted answer, byte for byte)" "($n) [P] R1: '$q' was routed: '$( firstChild "$DIR/r$n.out" )'"
else
no "($n) [P] a run exited non-zero: $( head -c 200 "$DIR/r${n}o.err" )"
fi; }
r1Unrouted 41 'When I run the program, how does `run_main` invoke the work, and how is `compute_total` added up?'
r1Unrouted 41b 'How does the build apply `compute_total` to each input?'
# (42) [N] the where-used forms R1 must NOT touch: still kind=where-used, verb=--uses (non-vacuity for (41): the cue is live)
for q in 'Where is `compute_total` invoked?' 'What invokes `compute_total`?' 'How is `compute_total` invoked?' 'Where is `compute_total` applied?'; do
if run r42 plain --for="$q"; then
F="$( firstChild "$DIR/r42.out" )"
[ "$( attr "$F" kind )" = where-used ] && [ "$( attr "$F" verb )" = "--uses=compute_total" ]
say $? "(42) [N] R1: '$q' still routes where-used → --uses" "(42) [N] R1: '$q' route reads '$F'"
else
no "(42) [N] a run exited non-zero: $( head -c 200 "$DIR/r42.err" )"
fi
done

if [ "$fail" = 0 ]; then echo "ALL PASS"; else echo "FAILURES ABOVE"; fi
exit "$fail"
Loading