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
7 changes: 7 additions & 0 deletions crates/plotnik-lib/src/bytecode/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ pub const STEP_SIZE: usize = 8;
/// instead of comparing type IDs. This distinguishes `(_)` (any named)
/// from `_` (any node including anonymous).
pub const NAMED_WILDCARD: u16 = 0xFFFF;

/// Maximum payload slots for Match instructions.
///
/// Match64 (the largest variant) supports up to 28 u16 slots for
/// effects, neg_fields, and successors combined. When an epsilon
/// transition needs more successors, it must be split into a cascade.
pub const MAX_MATCH_PAYLOAD_SLOTS: usize = 28;
4 changes: 3 additions & 1 deletion crates/plotnik-lib/src/bytecode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ mod nav;
mod sections;
mod type_meta;

pub use constants::{MAGIC, NAMED_WILDCARD, SECTION_ALIGN, STEP_SIZE, VERSION};
pub use constants::{
MAGIC, MAX_MATCH_PAYLOAD_SLOTS, NAMED_WILDCARD, SECTION_ALIGN, STEP_SIZE, VERSION,
};

pub use ids::{QTypeId, StringId};

Expand Down
29 changes: 29 additions & 0 deletions crates/plotnik-lib/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,33 @@ mod tests {

assert!(!result.instructions.is_empty());
}

#[test]
fn compile_large_tagged_alternation() {
// Regression test: alternations with 30+ branches should compile
// by splitting epsilon transitions into a cascade.
let branches: String = (0..30)
.map(|i| format!("A{i}: (identifier) @x{i}"))
.collect::<Vec<_>>()
.join(" ");
let query_str = format!("Q = [{branches}]");

let query = QueryBuilder::one_liner(&query_str)
.parse()
.unwrap()
.analyze();

let mut strings = StringTableBuilder::new();
let result = Compiler::compile(
query.interner(),
query.type_context(),
&query.symbol_table,
&mut strings,
None,
None,
)
.unwrap();

assert!(!result.instructions.is_empty());
}
}
49 changes: 39 additions & 10 deletions crates/plotnik-lib/src/compile/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,17 +484,46 @@ impl Compiler<'_> {
}

/// Emit an epsilon transition (no node interaction).
///
/// If there are more successors than fit in a single Match instruction,
/// this creates a cascade of epsilon transitions to preserve NFA semantics.
pub(super) fn emit_epsilon(&mut self, label: Label, successors: Vec<Label>) {
self.instructions.push(Instruction::Match(MatchIR {
label,
nav: Nav::Stay,
node_type: None,
node_field: None,
pre_effects: vec![],
neg_fields: vec![],
post_effects: vec![],
successors,
}));
use crate::bytecode::MAX_MATCH_PAYLOAD_SLOTS;

if successors.len() <= MAX_MATCH_PAYLOAD_SLOTS {
self.instructions.push(Instruction::Match(MatchIR {
label,
nav: Nav::Stay,
node_type: None,
node_field: None,
pre_effects: vec![],
neg_fields: vec![],
post_effects: vec![],
successors,
}));
} else {
// Split: first (MAX-1) successors + intermediate for rest.
// This preserves priority order: VM tries s0, s1, ..., then intermediate.
let split_at = MAX_MATCH_PAYLOAD_SLOTS - 1;
let (first_batch, rest) = successors.split_at(split_at);

let intermediate = self.fresh_label();
self.emit_epsilon(intermediate, rest.to_vec());

let mut batch = first_batch.to_vec();
batch.push(intermediate);

self.instructions.push(Instruction::Match(MatchIR {
label,
nav: Nav::Stay,
node_type: None,
node_field: None,
pre_effects: vec![],
neg_fields: vec![],
post_effects: vec![],
successors: batch,
}));
}
}

/// Emit a wildcard navigation step that accepts any node.
Expand Down