use std::collections::HashMap;
use std::sync::Arc;
use super::Passes;
use crate::analysis::Bytes;
use crate::ir::{self, CharSet, Reserved};
use crate::tree::{FieldId, Kind, KindId, Names};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Inst {
Char(u8),
Str(u32),
StrFold(u32),
Class(u32),
Any,
Span {
class: u32,
min: u32,
},
Test {
test: u32,
target: u32,
},
TestFail(u32),
SeedOpen,
SeedKeep,
SeedUse,
SeedGrow(u32),
SeedClose,
Choice(u32),
Commit(u32),
LoopCommit(u32),
RepOpen {
end: u32,
bounds: u32,
},
RepNext(u32),
PredChoice(u32),
BackCommit(u32),
FailTwice,
ErrorAttempt(u32),
TestEnd,
LabelChoice(u32),
Throw(u32),
Recover {
label: u32,
field: FieldId,
},
Fail,
Cut,
Call(u32),
MemoCall(u32),
LrCall(u32),
Sub(u32),
Ret,
Skip,
End,
QuietOn,
QuietOff,
TokenOpen(KindId),
TokenClose {
hidden: bool,
},
Open,
CloseNode(KindId),
CloseTransparent(KindId),
CloseField(FieldId),
WordEnd,
Mark,
Unreserved,
FlagOn(u8),
FlagOff(u8),
FlagsPop,
FlagTest {
flag: u8,
negated: bool,
},
Indent {
skip: bool,
},
IndentEnd,
Same {
skip: bool,
},
AtEnd {
skip: bool,
},
DefOpen {
skip: bool,
},
DefClose(u16),
Ref(u16),
CaptureMark,
CaptureKeep,
CaptureDrop,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct ByteTest {
pub bytes: Bytes,
pub after_skip: bool,
pub pop: bool,
pub kinds: Box<[KindId]>,
}
#[derive(Clone, Debug)]
pub(crate) struct Class {
ascii: [u64; 2],
beyond: Beyond,
pub set: CharSet,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Beyond {
None,
All,
Some,
}
impl Class {
pub fn new(set: &CharSet) -> Class {
let mut ascii = [0u64; 2];
for byte in 0..0x80u8 {
if set.contains(char::from(byte)) {
ascii[usize::from(byte >> 6)] |= 1 << (byte & 63);
}
}
let only_ascii = set.items.iter().all(|item| match *item {
crate::grammar::ClassItem::Range(_, last) => last.is_ascii(),
crate::grammar::ClassItem::Property { .. } => false,
});
let beyond = match (only_ascii, set.negated) {
(true, false) => Beyond::None,
(true, true) => Beyond::All,
(false, _) => Beyond::Some,
};
Class {
ascii,
beyond,
set: set.clone(),
}
}
pub fn matches(&self, rest: &str, steps: &mut u64) -> Option<usize> {
let &first = rest.as_bytes().first()?;
if first.is_ascii() {
let bit = self.ascii[usize::from(first >> 6)] >> (first & 63) & 1;
return (bit == 1).then_some(1);
}
let c = rest.chars().next()?;
let contained = match self.beyond {
Beyond::None => false,
Beyond::All => true,
Beyond::Some => {
*steps += self.set.items.len() as u64;
self.set.contains(c)
}
};
contained.then_some(c.len_utf8())
}
}
#[derive(Clone, Debug)]
pub(crate) struct RuleInfo {
pub name: String,
pub pc: u32,
pub memo: bool,
pub recursive: bool,
}
impl Program {
pub fn skip_return(&self) -> Option<u32> {
let skip = self.skip?;
let rest = self.code.get(skip as usize..)?;
let ret = rest.iter().position(|inst| matches!(inst, Inst::Ret))?;
Some(skip + ret as u32)
}
pub fn rule_at(&self, pc: u32) -> u32 {
let after = self.rules.partition_point(|rule| rule.pc <= pc);
after.saturating_sub(1) as u32
}
}
#[derive(Debug)]
pub(crate) struct Program {
pub code: Vec<Inst>,
pub strings: Vec<Box<[u8]>>,
pub classes: Vec<Class>,
pub tests: Vec<ByteTest>,
pub bounds: Vec<(u32, u32)>,
pub rules: Vec<RuleInfo>,
#[cfg(feature = "reference")]
pub shared: Vec<u32>,
pub skip: Option<u32>,
pub skip_class: Option<u32>,
pub entries: HashMap<String, u32>,
pub word: Class,
pub reserved: Reserved,
pub names: Arc<Names>,
pub kind_display: Vec<String>,
pub labels: Vec<ir::Label>,
pub recoveries: Vec<Option<u32>>,
pub error_messages: Vec<String>,
pub error_ranks: Vec<u32>,
pub error_skips: Vec<bool>,
pub skip_test: Option<u32>,
pub error_tests: Vec<Option<u32>>,
pub explains: bool,
pub kinds: Vec<Kind>,
pub single_item: HashMap<String, bool>,
pub passes: Passes,
pub tab_width: u32,
pub skip_context: bool,
pub skip_first: Option<Bytes>,
}