394 std::size_t end, std::string_view program_lower,
396 std::vector<Diagnostic> &diagnostics)
398 std::size_t at = begin + 3;
410 const bool initial =
word_at(source, at,
"initial_step");
411 if(initial ||
word_at(source, at,
"step")) {
412 const std::size_t item = at;
413 at += initial ? 12 : 4;
419 if(at >= end || source[at] !=
':') {
423 const std::size_t content = ++at;
424 const std::size_t close =
find_word(source,
"end_step", at);
425 if(close == std::string_view::npos || close > end ||
427 content, source,
step, diagnostics,
441 if(
word_at(source, at,
"transition")) {
442 const std::size_t item = at;
443 const std::size_t close =
find_word(source,
"end_transition", at);
444 if(close == std::string_view::npos || close > end) {
448 const std::size_t from =
find_word(source,
"from", at + 10);
449 const std::size_t to =
find_word(source,
"to", from + 4);
451 if(from == std::string_view::npos || to == std::string_view::npos ||
458 source.substr(from + 4, to - from - 4));
459 std::string target_text =
trim(
460 source.substr(to + 2,
assign - to - 2));
461 const std::size_t simultaneous =
463 transition.simultaneous = simultaneous != std::string_view::npos;
465 target_text.erase(simultaneous, 12);
468 const std::size_t condition_end = semi != std::string_view::npos &&
471 const std::size_t condition_begin =
474 condition_end -
assign - 2),
490 const std::set<std::string> unique_sources(
492 const std::set<std::string> unique_targets(
494 if(unique_sources.size() !=
transition.sources.size() ||
495 unique_targets.size() !=
transition.targets.size()) {
508 std::string_view::npos) {
523 if(
word_at(source, at,
"action")) {
524 const std::size_t item = at;
530 if(at >= end || source[at] !=
':') {
534 const std::size_t body = ++at;
535 const std::size_t close =
find_word(source,
"end_action", at);
536 if(close == std::string_view::npos || close > end) {
542 const std::size_t body_begin =
543 body +
skip_space(source.substr(body, close - body), 0);
544 action.body =
trim(source.substr(body, close - body));
561 std::vector<Diagnostic> &diagnostics)
564 std::map<std::string, std::size_t> steps;
565 std::size_t initial_count = 0;
566 for(std::size_t index = 0; index < network.
steps.size(); ++index) {
567 if(!steps.emplace(network.
steps[index].lower, index).second) {
569 source, draft.
begin);
572 if(network.
steps[index].initial) ++initial_count;
574 if(initial_count != 1 || network.
steps.empty()) {
576 source, draft.
begin);
579 std::vector<std::vector<std::size_t>> edges(network.
steps.size());
580 std::vector<bool> outgoing(network.
steps.size(),
false);
582 for(
const std::string &source_name :
transition.sources) {
583 if(steps.find(source_name) == steps.end()) {
585 source, draft.
begin);
588 outgoing[steps[source_name]] =
true;
589 for(
const std::string &target_name :
transition.targets) {
590 if(steps.find(target_name) == steps.end()) {
592 source, draft.
begin);
595 edges[steps[source_name]].push_back(steps[target_name]);
598 for(
const std::string &name :
transition.sources) {
602 source, draft.
begin);
607 for(std::size_t index = 0; index < network.
steps.size(); ++index) {
608 if(!network.
steps[index].terminal && !outgoing[index]) {
610 source, draft.
begin);
614 std::vector<bool> reachable(network.
steps.size(),
false);
615 std::vector<std::size_t> pending;
616 for(std::size_t index = 0; index < network.
steps.size(); ++index) {
617 if(network.
steps[index].initial) pending.push_back(index);
619 while(!pending.empty()) {
620 const std::size_t current = pending.back();
622 if(reachable[current])
continue;
623 reachable[current] =
true;
624 for(std::size_t next : edges[current]) pending.push_back(next);
626 if(std::find(reachable.begin(), reachable.end(),
false) != reachable.end()) {
628 source, draft.
begin);
631 std::vector<std::vector<std::size_t>> branch_edges(network.
steps.size());
633 for(
const std::string &source_name :
transition.sources)
634 for(
const std::string &target_name :
transition.targets)
635 branch_edges[steps[source_name]].push_back(steps[target_name]);
637 std::vector<bool> matched_convergence(network.
transitions.size(),
false);
638 const auto reaches = [&](std::size_t from, std::size_t to,
639 const std::vector<std::size_t> &stops) {
640 std::vector<bool> seen(network.
steps.size(),
false);
641 std::vector<std::size_t> work{from};
642 while(!work.empty()) {
643 const std::size_t current = work.back();
645 if(current == to)
return true;
646 if(seen[current])
continue;
647 seen[current] =
true;
648 if(std::find(stops.begin(), stops.end(), current) != stops.end())
650 for(std::size_t next : branch_edges[current]) work.push_back(next);
654 for(std::size_t divergence_index = 0;
655 divergence_index < network.
transitions.size(); ++divergence_index) {
662 bool all_terminal =
true;
663 for(
const std::string &target : divergence.
targets)
664 all_terminal = all_terminal && network.
steps[steps[target]].terminal;
670 std::size_t selected = std::numeric_limits<std::size_t>::max();
671 std::vector<std::size_t> selected_exits;
672 for(std::size_t candidate = 0;
673 candidate < network.
transitions.size(); ++candidate) {
674 if(candidate <= divergence_index || matched_convergence[candidate])
680 convergence.
sources.size() < 2)
continue;
681 std::vector<bool> used(convergence.
sources.size(),
false);
682 std::vector<std::size_t> exits;
683 std::vector<std::size_t> stops;
684 stops.reserve(convergence.
sources.size());
685 for(
const std::string &name : convergence.
sources)
686 stops.push_back(steps[name]);
688 for(
const std::string &entry_name : divergence.
targets) {
689 std::size_t match = std::numeric_limits<std::size_t>::max();
690 for(std::size_t source_index = 0;
691 source_index < convergence.
sources.size(); ++source_index) {
692 if(!reaches(steps[entry_name],
693 steps[convergence.
sources[source_index]],
696 if(match != std::numeric_limits<std::size_t>::max()) {
700 match = source_index;
702 if(!unique || match == std::numeric_limits<std::size_t>::max() ||
708 exits.push_back(steps[convergence.
sources[match]]);
710 if(!unique)
continue;
711 if(selected != std::numeric_limits<std::size_t>::max()) {
713 source, draft.
begin);
716 selected = candidate;
717 selected_exits =
static_cast<std::vector<std::size_t> &&
>(exits);
719 if(selected == std::numeric_limits<std::size_t>::max()) {
721 source, draft.
begin);
725 region.
exits =
static_cast<std::vector<std::size_t> &&
>(selected_exits);
726 matched_convergence[selected] =
true;
730 for(std::size_t index = 0; index < network.
transitions.size(); ++index) {
733 !matched_convergence[index]) {
735 source, draft.
begin);
739 std::vector<std::vector<bool>> can_coexist(
740 network.
steps.size(), std::vector<bool>(network.
steps.size(),
false));
741 std::vector<std::vector<std::vector<bool>>> region_branch_steps;
745 std::vector<std::vector<bool>> branch_steps;
746 for(std::size_t branch = 0; branch < divergence.
targets.size(); ++branch) {
747 std::vector<bool> seen(network.
steps.size(),
false);
748 std::vector<std::size_t> work{steps[divergence.
targets[branch]]};
749 while(!work.empty()) {
750 const std::size_t current = work.back();
752 bool another_entry =
false;
753 for(std::size_t other = 0; other < divergence.
targets.size();
755 another_entry = another_entry ||
757 current == steps[divergence.
targets[other]]);
758 if(another_entry || seen[current])
continue;
759 seen[current] =
true;
760 if(std::find(region.
exits.begin(), region.
exits.end(), current) !=
761 region.
exits.end())
continue;
762 for(std::size_t next : branch_edges[current]) work.push_back(next);
764 branch_steps.push_back(
static_cast<std::vector<bool> &&
>(seen));
766 for(std::size_t
left = 0;
left < branch_steps.size(); ++
left)
769 for(std::size_t a = 0; a < network.
steps.size(); ++a)
770 if(branch_steps[
left][a])
771 for(std::size_t b = 0; b < network.
steps.size(); ++b)
772 if(branch_steps[
right][b]) {
773 can_coexist[a][b] =
true;
774 can_coexist[b][a] =
true;
776 region_branch_steps.push_back(
777 static_cast<std::vector<std::vector<bool>
> &&>(branch_steps));
779 const std::size_t no_region = std::numeric_limits<std::size_t>::max();
782 for(std::size_t child = 0; child < draft.
parallel_regions.size(); ++child) {
785 std::size_t best_size = std::numeric_limits<std::size_t>::max();
786 for(std::size_t candidate = 0;
788 if(candidate == child ||
791 for(std::size_t branch = 0;
792 branch < region_branch_steps[candidate].size(); ++branch) {
793 bool contains =
true;
794 for(
const std::string &source_name : child_divergence.
sources)
795 contains = contains &&
796 region_branch_steps[candidate][branch][steps[source_name]];
797 if(!contains)
continue;
798 const std::size_t size =
static_cast<std::size_t
>(std::count(
799 region_branch_steps[candidate][branch].begin(),
800 region_branch_steps[candidate][branch].end(),
true));
801 if(size < best_size) {
803 parent[child] = candidate;
804 parent_branch[child] = branch;
809 std::function<std::uint32_t(std::size_t)> region_peak =
810 [&](std::size_t region_index) {
813 std::vector<std::uint32_t> branch_peak(
814 divergence.
targets.size(), 1U);
815 for(std::size_t child = 0; child < parent.size(); ++child) {
816 if(parent[child] != region_index)
continue;
817 branch_peak[parent_branch[child]] = std::max(
818 branch_peak[parent_branch[child]], region_peak(child));
820 std::uint32_t peak = 0;
821 for(std::uint32_t branch : branch_peak) peak += branch;
825 for(std::size_t region = 0; region < parent.size(); ++region)
826 if(parent[region] == no_region)
834 bool concurrently_enabled =
true;
835 for(
const std::string &a_source : a.
sources)
836 for(
const std::string &b_source : b.
sources)
837 concurrently_enabled = concurrently_enabled &&
838 can_coexist[steps[a_source]][steps[b_source]];
839 if(!concurrently_enabled)
continue;
840 bool conflict =
false;
841 for(
const std::string &a_source : a.
sources)
842 conflict = conflict ||
845 for(
const std::string &b_source : b.
sources)
846 conflict = conflict ||
851 source, draft.
begin);
860 source, draft.
begin);
868 source, draft.
begin);