#![allow(dead_code)]
macro_rules! byte_gate {
($name:ident, $test:path) => {
fn $name(node: &$crate::Node<'_>, code: &[u8]) -> bool {
$test($crate::lang_helpers::doc_comment::node_bytes(node, code))
}
};
}
pub(crate) use byte_gate;
#[cfg(any(test, feature = "test-support"))]
use crate::{Ancestors, LANG, Node, SpaceKind};
pub mod bash;
pub mod c;
pub mod c_family;
pub mod ccomment;
pub(crate) mod control_flow;
pub mod cpp;
pub mod csharp;
pub mod elixir;
pub mod go;
pub mod groovy;
pub mod irules;
pub mod java;
pub mod javascript;
pub mod js_family;
pub mod kotlin;
pub mod lua;
pub mod mozcpp;
pub mod mozjs;
pub mod objc;
pub mod perl;
pub mod php;
pub mod preproc;
pub mod python;
pub mod ruby;
pub mod rust;
pub mod tcl;
pub mod tcl_family;
pub mod tsx;
pub mod typescript;
#[cfg(test)]
mod space_tree_tests;
#[cfg(test)]
mod call_string_tests;
#[cfg(test)]
mod control_flow_tests;
#[cfg(test)]
mod doc_tests;
#[cfg(doctest)]
mod parent_list_doctests;
#[cfg(all(
test,
any(
feature = "bash",
feature = "c",
feature = "c-family-helpers",
feature = "cpp",
feature = "csharp",
feature = "elixir",
feature = "go",
feature = "groovy",
feature = "irules",
feature = "java",
feature = "javascript",
feature = "kotlin",
feature = "lua",
feature = "mozcpp",
feature = "mozjs",
feature = "objc",
feature = "perl",
feature = "php",
feature = "python",
feature = "ruby",
feature = "rust",
feature = "tcl",
feature = "typescript",
)
))]
mod derived_parity_tests;
#[doc(hidden)]
#[must_use]
pub const fn kinds_overlap(a: &[u16], b: &[u16]) -> bool {
let mut i = 0;
while i < a.len() {
let mut j = 0;
while j < b.len() {
if a[i] == b[j] {
return true;
}
j += 1;
}
i += 1;
}
false
}
#[doc(hidden)]
#[must_use]
pub const fn kinds_repeat(a: &[u16]) -> bool {
let mut i = 0;
while i < a.len() {
let mut j = i + 1;
while j < a.len() {
if a[i] == a[j] {
return true;
}
j += 1;
}
i += 1;
}
false
}
#[doc(hidden)]
#[must_use]
pub const fn count_some(marked: &[Option<u16>]) -> usize {
let mut n = 0;
let mut i = 0;
while i < marked.len() {
if marked[i].is_some() {
n += 1;
}
i += 1;
}
n
}
#[doc(hidden)]
#[must_use]
pub const fn some_kinds<const N: usize>(marked: &[Option<u16>]) -> [u16; N] {
let mut kinds = [0; N];
let (mut i, mut j) = (0, 0);
while i < marked.len() {
if let Some(kind) = marked[i] {
kinds[j] = kind;
j += 1;
}
i += 1;
}
kinds
}
#[doc(hidden)]
#[must_use]
pub const fn excluded_kinds<const N: usize>(excluded: &[(u16, &str)]) -> [u16; N] {
let mut kinds = [0; N];
let mut i = 0;
while i < N {
kinds[i] = excluded[i].0;
i += 1;
}
kinds
}
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
pub type RowPred = for<'a> fn(&Node<'a>, &[u8], Ancestors<'a, '_>) -> bool;
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
pub type RowKindPred = for<'a> fn(u16, &Node<'a>, &[u8], Ancestors<'a, '_>) -> bool;
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct DeclaredRow {
pub role: &'static str,
pub owners: &'static [u16],
pub payers: &'static [u16],
pub leaf_kinds: &'static [u16],
pub callable: &'static [u16],
pub holds_payer: &'static [u16],
pub wraps: &'static [u16],
pub excluded: &'static [(u16, &'static str)],
pub text_gated: bool,
pub chain_gated: bool,
pub pays_never: bool,
pub mode: &'static str,
pub pays_at_wrapper: RowPred,
pub owns_kind: RowKindPred,
pub pays_at_leaf_kind: RowKindPred,
pub pays_at_token_kind: RowKindPred,
pub is_callable_kind: RowKindPred,
pub lists_kind: fn(u16) -> bool,
pub project: for<'a> fn(&Node<'a>) -> Option<Node<'a>>,
}
#[cfg(any(test, feature = "test-support"))]
impl DeclaredRow {
pub(crate) fn candidate_kinds(&self) -> impl Iterator<Item = u16> + '_ {
let mut seen = std::collections::BTreeSet::new();
self.owners
.iter()
.chain(self.payers)
.chain(self.leaf_kinds)
.copied()
.filter(move |k| seen.insert(*k))
}
}
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
#[derive(Clone)]
pub enum RowView {
Declared(Box<DeclaredRow>),
Absent {
role: &'static str,
why: &'static str,
},
Pending {
role: &'static str,
package: &'static str,
},
}
#[cfg(any(test, feature = "test-support"))]
impl RowView {
#[must_use]
pub fn role(&self) -> &'static str {
match self {
Self::Declared(row) => row.role,
Self::Absent { role, .. } | Self::Pending { role, .. } => role,
}
}
#[must_use]
pub fn declared(&self) -> Option<&DeclaredRow> {
match self {
Self::Declared(row) => Some(row),
_ => None,
}
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! role_table {
(lang: $lang:ident; $($role:ident { $($row:tt)* })*) => {
$( $crate::role_table!(@row $lang $role { $($row)* }); )*
#[cfg(test)]
pub(crate) fn canonical() -> String {
let mut out = format!("lang {}\n", stringify!($lang));
$( out.push_str(&$crate::role_table!(@canonical $role)); out.push('\n'); )*
out
}
#[cfg(any(test, feature = "test-support"))]
pub(crate) fn rows() -> Vec<$crate::roles::RowView> {
vec![ $( $crate::role_table!(@view $role), )* ]
}
pub fn opens_space<'a>(
node: &$crate::Node<'a>,
code: &[u8],
ancestors: $crate::Ancestors<'a, '_>,
) -> $crate::SpaceKind {
let kind = node.kind_id();
$( $crate::role_table!(@opens [$lang kind node code ancestors] { $($row)* }); )*
let _ = (kind, node, code, ancestors);
$crate::SpaceKind::Unknown
}
};
(@row $lang:ident $role:ident { absent $why:literal }) => {
$crate::role_table!(@named $role {
pub const ABSENT: &str = $why;
pub const OWNERS: &[u16] = &[];
pub const PAYERS: &[u16] = &[];
pub const LEAF_KINDS: &[u16] = &[];
pub const TEXT_GATED: bool = false;
pub const CHAIN_GATED: bool = false;
$crate::role_table!(@absent_predicate owns);
$crate::role_table!(@absent_predicate pays_at_leaf);
$crate::role_table!(@absent_predicate pays_at_wrapper);
$crate::role_table!(@absent_predicate is_callable);
$crate::role_table!(@absent_predicate_kind is_callable_kind);
#[inline]
#[must_use]
pub fn lists_kind(_kind: u16) -> bool {
false
}
$crate::role_table!(@absent_predicate_kind owns_kind);
$crate::role_table!(@absent_predicate_kind pays_at_leaf_kind);
$crate::role_table!(@absent_predicate_kind pays_at_token_kind);
#[cfg(test)]
pub(crate) fn canonical() -> String {
format!("{} absent {}", stringify!($role), ABSENT)
}
#[cfg(any(test, feature = "test-support"))]
pub(crate) fn view() -> $crate::roles::RowView {
$crate::roles::RowView::Absent { role: stringify!($role), why: ABSENT }
}
});
};
(@absent_predicate $name:ident) => {
#[inline]
pub fn $name<'a>(
_node: &$crate::Node<'a>,
_code: &[u8],
_ancestors: $crate::Ancestors<'a, '_>,
) -> bool {
false
}
};
(@absent_predicate_kind $name:ident) => {
#[inline]
pub fn $name<'a>(
_kind: u16,
_node: &$crate::Node<'a>,
_code: &[u8],
_ancestors: $crate::Ancestors<'a, '_>,
) -> bool {
false
}
};
(@row $lang:ident $role:ident { pending $package:ident }) => {
$crate::role_table!(@named $role {
pub const PENDING: &str = stringify!($package);
#[cfg(test)]
pub(crate) fn canonical() -> String {
format!("{} pending {}", stringify!($role), PENDING)
}
#[cfg(any(test, feature = "test-support"))]
pub(crate) fn view() -> $crate::roles::RowView {
$crate::roles::RowView::Pending { role: stringify!($role), package: PENDING }
}
});
};
(@row $lang:ident $role:ident { owns $owners:tt pays never $($tail:tt)* }) => {
$crate::role_table!(@splice $lang $role [never] [] $owners { pays [] $($tail)* });
};
(@row $lang:ident $role:ident { owns $owners:tt pays [ $($payer:tt)* ] $($tail:tt)* }) => {
$crate::role_table!(@splice $lang $role [split] [] $owners { pays [ $($payer)* ] $($tail)* });
};
(@row $lang:ident $role:ident { owns $owners:tt $($tail:tt)* }) => {
$crate::role_table!(@splice $lang $role [owners] [] $owners { pays [] $($tail)* });
};
(@splice $lang:ident $role:ident $mode:tt [$($acc:tt)*] [ $($entries:tt)* ] $tail:tt) => {
$crate::role_table!(@owners [$lang $role $mode $tail] [$($acc)*] $($entries)*);
};
(@owners $cont:tt [$($acc:tt)*] ..$frag:ident!() $(, $($rest:tt)*)?) => {
$frag!(@splice $cont [$($acc)*] $($($rest)*)?);
};
(@owners $cont:tt [$($acc:tt)*] $k:ident gated_by $g:ident $(, $($rest:tt)*)?) => {
$crate::role_table!(@owners $cont [$($acc)* $k gated_by $g,] $($($rest)*)?);
};
(@owners $cont:tt [$($acc:tt)*] $k:ident text_gated_by $g:ident $(, $($rest:tt)*)?) => {
$crate::role_table!(@owners $cont [$($acc)* $k gated_by $g,] $($($rest)*)?);
};
(@owners $cont:tt [$($acc:tt)*] $k:ident under [ $($p:ident),+ $(,)? ] $(, $($rest:tt)*)?) => {
$crate::role_table!(@owners $cont [$($acc)* $k under [$($p),*],] $($($rest)*)?);
};
(@owners $cont:tt [$($acc:tt)*] $k:ident under $p:ident $(, $($rest:tt)*)?) => {
$crate::role_table!(@owners $cont [$($acc)* $k under [$p],] $($($rest)*)?);
};
(@owners $cont:tt [$($acc:tt)*] $k:ident $(( $($attr:tt)* ))? $(, $($rest:tt)*)?) => {
$crate::role_table!(@owners $cont [$($acc)* $k $(( $($attr)* ))?,] $($($rest)*)?);
};
(@owners [$lang:ident $role:ident $mode:tt { pays [ $($payers:tt)* ] $($tail:tt)* }]
[$($acc:tt)*]) => {
$crate::role_table!(
@owners [pays $lang $role $mode [$($acc)*] { $($tail)* }] [] $($payers)*
);
};
(@owners [pays $lang:ident $role:ident $mode:tt [$($owners:tt)*] { $($tail:tt)* }]
[$($acc:tt)*]) => {
$crate::role_table!(
@row_body $lang $role $mode { owns [ $($owners)* ] pays [ $($acc)* ] $($tail)* }
);
};
(@owners [$l:ident $k:ident $n:ident $c:ident $a:ident]
[ $($owner:ident $(( $($owner_attr:tt)* ))?,)* ]) => {
$( $crate::role_table!(
@scan opens_done [] [$l $k $n $c $a] $owner [] [] [] [] $($($owner_attr)*)?
); )*
};
(@row_body $lang:ident $role:ident [$mode:ident] {
owns [ $($owner:ident $(( $($owner_attr:tt)* ))?),* $(,)? ]
pays [ $($payer:ident $(gated_by $payer_gate:ident)? $(under [$($payer_parent:ident),+ $(,)?])?),* $(,)? ]
$(project $field:literal)?
$(wraps [ $($wrapped:ident),* $(,)? ])?
$(excludes [ $($excluded:ident $excluded_why:literal),* $(,)? ])?
}) => {
$crate::role_table!(@named $role {
use $crate::{Ancestors, Node};
#[allow(unused_imports)]
use super::*;
pub const OWNERS: &[u16] = &[ $( $lang::$owner as u16, )* ];
pub const PAYERS: &[u16] = &[ $( $lang::$payer as u16, )* ];
pub const EXCLUDED: &[(u16, &str)] =
&[ $($( ($lang::$excluded as u16, $excluded_why), )*)? ];
pub const WRAPS: &[u16] = &[ $($( $lang::$wrapped as u16, )*)? ];
pub const PAYS_NEVER: bool = $crate::role_table!(@mode $mode never);
#[cfg(any(test, feature = "test-support"))]
pub(crate) const MODE: &str = stringify!($mode);
const OWNERS_PAY: bool = $crate::role_table!(@mode $mode owners);
#[cfg(test)]
pub(crate) fn canonical() -> String {
let ids = |kinds: &[u16]| {
kinds.iter().map(u16::to_string).collect::<Vec<_>>().join(",")
};
let excluded = EXCLUDED
.iter()
.map(|(k, why)| format!("{k}={why}"))
.collect::<Vec<_>>()
.join(";");
format!(
"{} rows owners=[{}] payers=[{}] wraps=[{}] excluded=[{}] mode={} \
text_gated={} chain_gated={}",
stringify!($role),
ids(OWNERS),
ids(PAYERS),
ids(WRAPS),
excluded,
stringify!($mode),
TEXT_GATED,
CHAIN_GATED
)
}
const _: () = assert!(
!$crate::roles::kinds_overlap(OWNERS, PAYERS),
"a kind cannot be both an owner and a payer of one row; \
declare a tokenless owner once as `pays_itself`"
);
const _: () = assert!(
!$crate::roles::kinds_repeat(OWNERS) && !$crate::roles::kinds_repeat(PAYERS),
"a kind is declared twice in one list of this row; the second \
clause could never fire"
);
const _: () = {
let excluded =
$crate::roles::excluded_kinds::<{ EXCLUDED.len() }>(EXCLUDED);
assert!(
!$crate::roles::kinds_overlap(&excluded, OWNERS)
&& !$crate::roles::kinds_overlap(&excluded, PAYERS),
"a kind this row excludes is also one it owns or pays for"
);
};
const _: () = {
$( $crate::role_table!(
@scan opens_role [$role] [] $owner [] [] [] [] $($($owner_attr)*)?
); )*
};
#[inline]
pub fn owns<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
owns_kind(node.kind_id(), node, code, ancestors)
}
#[inline]
#[must_use]
pub fn lists_kind(kind: u16) -> bool {
let _ = kind;
false $(|| kind == $lang::$owner as u16)*
}
#[inline]
pub fn owns_kind<'a>(
kind: u16,
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
$( $crate::role_table!(
@scan owner_done [] [$lang kind node code ancestors] $owner
[] [] [] [] $($($owner_attr)*)?
); )*
let _ = (kind, node, code, ancestors);
false
}
#[inline]
pub fn pays_at_leaf<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
pays_at_leaf_kind(node.kind_id(), node, code, ancestors)
}
#[inline]
pub fn pays_at_leaf_kind<'a>(
kind: u16,
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
$( $crate::role_table!(
@clause_gated [$lang kind node code ancestors] $payer
[$($payer_gate)? $(under [$($payer_parent),*])?]
); )*
$( $crate::role_table!(
@scan self_payer_done [$mode] [$lang kind node code ancestors] $owner
[] [] [] [] $($($owner_attr)*)?
); )*
OWNERS_PAY && owns_kind(kind, node, code, ancestors)
}
#[inline]
pub fn pays_at_token_kind<'a>(
kind: u16,
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
let is_payer = false $(|| kind == $lang::$payer as u16)*;
is_payer && pays_at_leaf_kind(kind, node, code, ancestors)
}
pub const TEXT_GATED: bool = false $( || $crate::role_table!(
@scan text_gated_kind [] [] $owner [] [] [] [] $($($owner_attr)*)?
) )*;
pub const CHAIN_GATED: bool = false $( || $crate::role_table!(
@scan chain_gated_kind [] [] $owner [] [] [] [] $($($owner_attr)*)?
) )*;
pub const LEAF_KINDS: &[u16] = &{
const MARKED: &[Option<u16>] = &[
$( Some($lang::$payer as u16), )*
$( $crate::role_table!(
@scan self_payer_kind [$mode] [$lang] $owner [] [] [] [] $($($owner_attr)*)?
), )*
];
$crate::roles::some_kinds::<{ $crate::roles::count_some(MARKED) }>(MARKED)
};
pub const CALLABLE: &[u16] = &{
const MARKED: &[Option<u16>] = &[ $( $crate::role_table!(
@scan callable_kind [] [$lang] $owner [] [] [] [] $($($owner_attr)*)?
), )* ];
$crate::roles::some_kinds::<{ $crate::roles::count_some(MARKED) }>(MARKED)
};
#[cfg(any(test, feature = "test-support"))]
pub(crate) const HOLDS_PAYER: &[u16] = &{
const MARKED: &[Option<u16>] = &[ $( $crate::role_table!(
@scan holds_payer_kind [] [$lang] $owner [] [] [] [] $($($owner_attr)*)?
), )* ];
$crate::roles::some_kinds::<{ $crate::roles::count_some(MARKED) }>(MARKED)
};
#[inline]
pub fn is_callable<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
is_callable_kind(node.kind_id(), node, code, ancestors)
}
#[inline]
pub fn is_callable_kind<'a>(
kind: u16,
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
$( $crate::role_table!(
@scan callable_done [] [$lang kind node code ancestors] $owner
[] [] [] [] $($($owner_attr)*)?
); )*
let _ = (kind, node, code, ancestors);
false
}
#[inline]
pub fn pays_at_wrapper<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
!PAYS_NEVER && owns(node, code, ancestors)
}
pub const PROJECT: Option<&str> = $crate::role_table!(@project $($field)?);
pub fn project<'a>(owner: &Node<'a>) -> Option<Node<'a>> {
$crate::role_table!(@projected owner $($field)?).or_else(|| {
owner
.children()
.find(|child| PAYERS.contains(&child.kind_id()))
})
}
pub fn owns_via<'a>(
payer: u16,
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
owns(node, code, ancestors)
&& project(node).is_some_and(|child| child.kind_id() == payer)
}
#[cfg(any(test, feature = "test-support"))]
pub(crate) fn view() -> $crate::roles::RowView {
$crate::roles::RowView::Declared(Box::new($crate::roles::DeclaredRow {
role: stringify!($role),
owners: OWNERS,
payers: PAYERS,
leaf_kinds: LEAF_KINDS,
callable: CALLABLE,
holds_payer: HOLDS_PAYER,
wraps: WRAPS,
excluded: EXCLUDED,
text_gated: TEXT_GATED,
chain_gated: CHAIN_GATED,
pays_never: PAYS_NEVER,
mode: MODE,
pays_at_wrapper,
owns_kind,
pays_at_leaf_kind,
pays_at_token_kind,
is_callable_kind,
lists_kind,
project,
}))
}
});
};
(@project) => { None };
(@project $field:literal) => { Some($field) };
(@projected $owner:ident) => { None::<Node<'a>> };
(@projected $owner:ident $field:literal) => {{
static FIELD: ::std::sync::OnceLock<Option<u16>> = ::std::sync::OnceLock::new();
FIELD
.get_or_init(|| $owner.field_id($field))
.and_then(|field| $owner.child_by_field_id(field))
}};
(@mode never never) => { true };
(@mode owners owners) => { true };
(@mode $have:ident $want:ident) => { false };
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt $p:tt $s:tt $c:tt) => {
$crate::role_table!(@$d $m $ctx $k $g $p $s $c)
};
(@scan text_gated_kind $m:tt $ctx:tt $k:ident $g:tt $p:tt $s:tt $c:tt $(holds_payer)? text_gated_by $f:ident $($r:tt)*) => {
true
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt holds_payer gated_by $g:ident no_chain $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [holds_payer $g no_chain] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt holds_payer text_gated_by $g:ident no_chain $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [holds_payer $g no_chain] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt gated_by $g:ident no_chain $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [$g no_chain] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt text_gated_by $g:ident no_chain $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [$g no_chain] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt $p:tt [] $c:tt opens_by $f:ident no_chain $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k $g $p [by $f no_chain] $c $($r)*)
};
(@scan chain_gated_kind $m:tt $ctx:tt $k:ident $g:tt $p:tt $s:tt $c:tt $(holds_payer)? gated_by $f:ident $($r:tt)*) => {
true
};
(@scan chain_gated_kind $m:tt $ctx:tt $k:ident $g:tt $p:tt $s:tt $c:tt $(holds_payer)? text_gated_by $f:ident $($r:tt)*) => {
true
};
(@scan chain_gated_kind $m:tt $ctx:tt $k:ident $g:tt $p:tt $s:tt $c:tt opens_by $f:ident $($r:tt)*) => {
true
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt holds_payer gated_by $g:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [holds_payer $g] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt holds_payer text_gated_by $g:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [holds_payer $g] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt holds_payer $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [holds_payer] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [$g:ident] $p:tt $s:tt $c:tt holds_payer $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [holds_payer $g] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [$g:ident no_chain] $p:tt $s:tt $c:tt holds_payer $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [holds_payer $g no_chain] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt gated_by $g:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [$g] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt text_gated_by $g:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [$g] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt under [$($u:ident),+ $(,)?] $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [under [$($u),+]] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident [] $p:tt $s:tt $c:tt not_under [$($u:ident),+ $(,)?] $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k [not_under [$($u),+]] $p $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt [] $s:tt $c:tt pays_itself $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k $g [pays_itself] $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt [] $s:tt $c:tt pays_itself_when $w:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k $g [pays_itself $w] $s $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt $p:tt [] $c:tt opens $s:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k $g $p [$s] $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt $p:tt [] $c:tt opens_by $f:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k $g $p [by $f] $c $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt $p:tt $s:tt [] callable $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k $g $p $s [callable] $($r)*)
};
(@scan $d:ident $m:tt $ctx:tt $k:ident $g:tt $p:tt $s:tt [] callable_when $w:ident $($r:tt)*) => {
$crate::role_table!(@scan $d $m $ctx $k $g $p $s [callable $w] $($r)*)
};
(@owner_done [] $ctx:tt $k:ident [holds_payer $($g:ident $($nc:ident)?)?] $p:tt $s:tt $c:tt) => {
$crate::role_table!(@clause_payer_gated $ctx $k [$($g $($nc)?)?]);
};
(@owner_done [] $ctx:tt $k:ident $g:tt $p:tt $s:tt $c:tt) => {
$crate::role_table!(@clause_gated $ctx $k $g);
};
(@text_gated_kind [] [] $k:ident $g:tt $p:tt $s:tt $c:tt) => {
false
};
(@chain_gated_kind [] [] $k:ident [$rel:ident [$($q:ident),+]] $p:tt $s:tt $c:tt) => {
true
};
(@chain_gated_kind [] [] $k:ident $g:tt $p:tt $s:tt $c:tt) => {
false
};
(@opens_role [$role:ident] [] $k:ident $g:tt $p:tt [] $c:tt) => {};
(@opens_role [FuncOpener] [] $k:ident $g:tt $p:tt $s:tt $c:tt) => {};
(@opens_role [Container] [] $k:ident $g:tt $p:tt $s:tt $c:tt) => {};
(@opens_role [$role:ident] [] $k:ident $g:tt $p:tt $s:tt $c:tt) => {
compile_error!(concat!(
"`opens` / `opens_by` on a `",
stringify!($role),
"` row: only `FuncOpener` and `Container` open a space (#1793)"
));
};
(@self_payer_kind [owners] [$l:ident] $k:ident $g:tt $p:tt $s:tt $c:tt) => {
Some($l::$k as u16)
};
(@self_payer_kind [split] [$l:ident] $k:ident $g:tt [pays_itself $($w:ident)?] $s:tt $c:tt) => {
Some($l::$k as u16)
};
(@self_payer_kind [$mode:ident] [$l:ident] $k:ident $g:tt $p:tt $s:tt $c:tt) => {
None
};
(@holds_payer_kind [] [$l:ident] $k:ident [holds_payer $($g:ident $($nc:ident)?)?] $p:tt $s:tt $c:tt) => {
Some($l::$k as u16)
};
(@holds_payer_kind [] [$l:ident] $k:ident $g:tt $p:tt $s:tt $c:tt) => {
None
};
(@callable_kind [] [$l:ident] $k:ident $g:tt $p:tt $s:tt []) => {
None
};
(@callable_kind [] [$l:ident] $k:ident $g:tt $p:tt $s:tt [callable $($w:ident)?]) => {
Some($l::$k as u16)
};
(@callable_done [] $ctx:tt $k:ident $g:tt $p:tt $s:tt []) => {};
(@callable_done [] $ctx:tt $k:ident $g:tt $p:tt $s:tt [callable]) => {
$crate::role_table!(@clause_gated $ctx $k $g);
};
(@callable_done [] $ctx:tt $k:ident $g:tt $p:tt $s:tt [callable $w:ident]) => {
$crate::role_table!(@clause_when_gated $ctx $k $w $g);
};
(@self_payer_done [$mode:ident] $ctx:tt $k:ident $g:tt [] $s:tt $c:tt) => {};
(@self_payer_done [split] $ctx:tt $k:ident $g:tt [pays_itself] $s:tt $c:tt) => {
$crate::role_table!(@clause_gated $ctx $k $g);
};
(@self_payer_done [split] $ctx:tt $k:ident $g:tt [pays_itself $w:ident] $s:tt $c:tt) => {
$crate::role_table!(@clause_when_gated $ctx $k $w $g);
};
(@self_payer_done [never] $ctx:tt $k:ident $g:tt [pays_itself $($w:ident)?] $s:tt $c:tt) => {
compile_error!("`pays_itself` contradicts `pays never`: the row says nothing pays");
};
(@self_payer_done [owners] $ctx:tt $k:ident $g:tt [pays_itself $($w:ident)?] $s:tt $c:tt) => {
compile_error!(
"`pays_itself` is redundant without a `pays` list: every owner already pays"
);
};
(@opens_done [] $ctx:tt $k:ident $g:tt $p:tt [] $c:tt) => {};
(@opens_done [] $ctx:tt $k:ident $g:tt $p:tt [by $f:ident $($fnc:ident)?] $c:tt) => {
$crate::role_table!(@space_clause_by_gated $ctx $k [$f $($fnc)?] $g);
};
(@opens_done [] $ctx:tt $k:ident $g:tt $p:tt [$s:ident] $c:tt) => {
$crate::role_table!(@space_clause_gated $ctx $k $s $g);
};
(@opens $ctx:tt { absent $why:literal }) => {};
(@opens $ctx:tt { pending $package:ident }) => {};
(@opens $ctx:tt { owns [ $($entries:tt)* ] $($r:tt)* }) => {
$crate::role_table!(@owners $ctx [] $($entries)*);
};
(@gate [$l:ident $kind:ident $n:ident $c:ident $a:ident] []) => {
true
};
(@gate [$l:ident $kind:ident $n:ident $c:ident $a:ident] [$g:ident]) => {
$g($n, $c, $a)
};
(@gate [$l:ident $kind:ident $n:ident $c:ident $a:ident] [$g:ident no_chain]) => {
$g($n, $c)
};
(@gate [$l:ident $kind:ident $n:ident $c:ident $a:ident] [under [$($p:ident),*]]) => {
$a.parent($n).is_some_and(|parent| {
let parent = parent.kind_id();
false $(|| parent == $l::$p as u16)*
})
};
(@gate [$l:ident $kind:ident $n:ident $c:ident $a:ident] [not_under [$($p:ident),*]]) => {
!$crate::role_table!(@gate [$l $kind $n $c $a] [under [$($p),*]])
};
(@clause_gated [$l:ident $kind:ident $n:ident $c:ident $a:ident] $k:ident $g:tt) => {
if $kind == $l::$k as u16 {
return $crate::role_table!(@gate [$l $kind $n $c $a] $g);
}
};
(@clause_payer_gated [$l:ident $kind:ident $n:ident $c:ident $a:ident] $k:ident $g:tt) => {
if $kind == $l::$k as u16 {
return $crate::role_table!(@gate [$l $kind $n $c $a] $g)
&& project($n).is_some_and(|child| PAYERS.contains(&child.kind_id()));
}
};
(@clause_when_gated [$l:ident $kind:ident $n:ident $c:ident $a:ident] $k:ident $w:ident $g:tt) => {
if $kind == $l::$k as u16 {
return $crate::role_table!(@gate [$l $kind $n $c $a] $g) && $w($n, $c, $a);
}
};
(@space_clause_gated [$l:ident $kind:ident $n:ident $c:ident $a:ident] $k:ident $s:ident $g:tt) => {
if $kind == $l::$k as u16 && $crate::role_table!(@gate [$l $kind $n $c $a] $g) {
return $crate::SpaceKind::$s;
}
};
(@space_clause_by_gated [$l:ident $kind:ident $n:ident $c:ident $a:ident] $k:ident $f:tt $g:tt) => {
if $kind == $l::$k as u16 && $crate::role_table!(@gate [$l $kind $n $c $a] $g) {
return $crate::role_table!(@gate [$l $kind $n $c $a] $f);
}
};
(@canonical Call) => { call::canonical() };
(@canonical StringLit) => { string_lit::canonical() };
(@canonical Comparison) => { comparison::canonical() };
(@canonical ShortCircuit) => { short_circuit::canonical() };
(@canonical EagerLogical) => { eager_logical::canonical() };
(@canonical Ternary) => { ternary::canonical() };
(@canonical NullSafe) => { null_safe::canonical() };
(@canonical CaseArm) => { case_arm::canonical() };
(@canonical Guard) => { guard::canonical() };
(@canonical Branch) => { branch::canonical() };
(@canonical Switch) => { switch::canonical() };
(@canonical Loop) => { r#loop::canonical() };
(@canonical Catch) => { catch::canonical() };
(@canonical ElseIf) => { else_if::canonical() };
(@canonical Exit) => { exit::canonical() };
(@canonical Modifier) => { modifier::canonical() };
(@canonical DeclKeyword) => { decl_keyword::canonical() };
(@canonical Primitive) => { primitive::canonical() };
(@canonical FuncOpener) => { func_opener::canonical() };
(@canonical Closure) => { closure::canonical() };
(@canonical Container) => { container::canonical() };
(@canonical Member) => { member::canonical() };
(@canonical Comment) => { comment::canonical() };
(@canonical DocComment) => { doc_comment::canonical() };
(@canonical Docstring) => { docstring::canonical() };
(@canonical DocTarget) => { doc_target::canonical() };
(@canonical DocRunOpener) => { doc_run_opener::canonical() };
(@canonical DocRunDirective) => { doc_run_directive::canonical() };
(@canonical DocBodyResidual) => { doc_body_residual::canonical() };
(@view Call) => { call::view() };
(@view StringLit) => { string_lit::view() };
(@view Comparison) => { comparison::view() };
(@view ShortCircuit) => { short_circuit::view() };
(@view EagerLogical) => { eager_logical::view() };
(@view Ternary) => { ternary::view() };
(@view NullSafe) => { null_safe::view() };
(@view CaseArm) => { case_arm::view() };
(@view Guard) => { guard::view() };
(@view Branch) => { branch::view() };
(@view Switch) => { switch::view() };
(@view Loop) => { r#loop::view() };
(@view Catch) => { catch::view() };
(@view ElseIf) => { else_if::view() };
(@view Exit) => { exit::view() };
(@view Modifier) => { modifier::view() };
(@view DeclKeyword) => { decl_keyword::view() };
(@view Primitive) => { primitive::view() };
(@view FuncOpener) => { func_opener::view() };
(@view Closure) => { closure::view() };
(@view Container) => { container::view() };
(@view Member) => { member::view() };
(@view Comment) => { comment::view() };
(@view DocComment) => { doc_comment::view() };
(@view Docstring) => { docstring::view() };
(@view DocTarget) => { doc_target::view() };
(@view DocRunOpener) => { doc_run_opener::view() };
(@view DocRunDirective) => { doc_run_directive::view() };
(@view DocBodyResidual) => { doc_body_residual::view() };
(@named Call { $($b:tt)* }) => {
pub mod call { $($b)* }
};
(@named StringLit { $($b:tt)* }) => {
pub mod string_lit { $($b)* }
};
(@named Comparison { $($b:tt)* }) => {
pub mod comparison { $($b)* }
};
(@named ShortCircuit { $($b:tt)* }) => {
pub mod short_circuit { $($b)* }
};
(@named EagerLogical { $($b:tt)* }) => {
pub mod eager_logical { $($b)* }
};
(@named Ternary { $($b:tt)* }) => {
pub mod ternary { $($b)* }
};
(@named NullSafe { $($b:tt)* }) => {
pub mod null_safe { $($b)* }
};
(@named CaseArm { $($b:tt)* }) => {
pub mod case_arm { $($b)* }
};
(@named Guard { $($b:tt)* }) => {
pub mod guard { $($b)* }
};
(@named Branch { $($b:tt)* }) => {
pub mod branch { $($b)* }
};
(@named Switch { $($b:tt)* }) => {
pub mod switch { $($b)* }
};
(@named Loop { $($b:tt)* }) => {
pub mod r#loop { $($b)* }
};
(@named Catch { $($b:tt)* }) => {
pub mod catch { $($b)* }
};
(@named ElseIf { $($b:tt)* }) => {
pub mod else_if { $($b)* }
};
(@named Exit { $($b:tt)* }) => {
pub mod exit { $($b)* }
};
(@named Modifier { $($b:tt)* }) => {
pub mod modifier { $($b)* }
};
(@named DeclKeyword { $($b:tt)* }) => {
pub mod decl_keyword { $($b)* }
};
(@named Primitive { $($b:tt)* }) => {
pub mod primitive { $($b)* }
};
(@named FuncOpener { $($b:tt)* }) => {
pub mod func_opener { $($b)* }
};
(@named Closure { $($b:tt)* }) => {
pub mod closure { $($b)* }
};
(@named Container { $($b:tt)* }) => {
pub mod container { $($b)* }
};
(@named Member { $($b:tt)* }) => {
pub mod member { $($b)* }
};
(@named Comment { $($b:tt)* }) => {
pub mod comment { $($b)* }
};
(@named DocComment { $($b:tt)* }) => {
pub mod doc_comment { $($b)* }
};
(@named Docstring { $($b:tt)* }) => {
pub mod docstring { $($b)* }
};
(@named DocTarget { $($b:tt)* }) => {
pub mod doc_target { $($b)* }
};
(@named DocRunOpener { $($b:tt)* }) => {
pub mod doc_run_opener { $($b)* }
};
(@named DocRunDirective { $($b:tt)* }) => {
pub mod doc_run_directive { $($b)* }
};
(@named DocBodyResidual { $($b:tt)* }) => {
pub mod doc_body_residual { $($b)* }
};
}
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct LanguageTable {
pub lang: LANG,
pub name: &'static str,
pub rows: fn() -> Vec<RowView>,
pub opens_space: for<'a> fn(&Node<'a>, &[u8], Ancestors<'a, '_>) -> SpaceKind,
pub kind_name: fn(u16) -> &'static str,
#[cfg(test)]
pub(crate) canonical: fn() -> String,
}
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
#[must_use]
pub fn tables() -> &'static [LanguageTable] {
macro_rules! tables {
($($lang:ident : $module:ident),* $(,)?) => {
&[ $( LanguageTable {
lang: LANG::$lang,
name: stringify!($lang),
rows: $module::rows,
opens_space: $module::opens_space,
kind_name: |id| crate::$lang::from(id).into(),
#[cfg(test)]
canonical: $module::canonical,
}, )* ]
};
}
tables![
Bash: bash,
C: c,
Ccomment: ccomment,
Cpp: cpp,
Csharp: csharp,
Elixir: elixir,
Go: go,
Groovy: groovy,
Irules: irules,
Java: java,
Javascript: javascript,
Kotlin: kotlin,
Lua: lua,
Mozcpp: mozcpp,
Mozjs: mozjs,
Objc: objc,
Perl: perl,
Php: php,
Preproc: preproc,
Python: python,
Ruby: ruby,
Rust: rust,
Tcl: tcl,
Tsx: tsx,
Typescript: typescript,
]
}
#[cfg(any(test, feature = "test-support"))]
pub(crate) const ROLES: &[&str] = &[
"Call",
"StringLit",
"Comparison",
"ShortCircuit",
"EagerLogical",
"Ternary",
"NullSafe",
"CaseArm",
"Guard",
"Branch",
"Switch",
"Loop",
"Catch",
"ElseIf",
"Exit",
"Modifier",
"DeclKeyword",
"Primitive",
"FuncOpener",
"Closure",
"Container",
"Member",
"Comment",
"DocComment",
"Docstring",
"DocTarget",
"DocRunOpener",
"DocRunDirective",
"DocBodyResidual",
];
#[cfg(test)]
mod tests {
use crate::{Ancestors, Java, Node};
#[test]
fn count_some_and_some_kinds_compact_the_marked_owners_in_order() {
use super::{count_some, some_kinds};
const NONE: &[Option<u16>] = &[None, None, None];
const ALL: &[Option<u16>] = &[Some(3), Some(1), Some(2)];
const ENDS: &[Option<u16>] = &[Some(7), None, None, Some(9)];
const MIDDLE: &[Option<u16>] = &[None, Some(5), None];
const EMPTY: &[Option<u16>] = &[];
assert_eq!(count_some(NONE), 0);
assert_eq!(count_some(ALL), 3);
assert_eq!(count_some(ENDS), 2);
assert_eq!(count_some(MIDDLE), 1);
assert_eq!(count_some(EMPTY), 0);
assert_eq!(some_kinds::<0>(NONE), [0u16; 0]);
assert_eq!(some_kinds::<3>(ALL), [3, 1, 2], "list order, not sorted");
assert_eq!(some_kinds::<2>(ENDS), [7, 9]);
assert_eq!(some_kinds::<1>(MIDDLE), [5]);
assert_eq!(some_kinds::<0>(EMPTY), [0u16; 0]);
}
#[test]
fn an_absent_rows_lists_kind_is_false_for_every_kind() {
use crate::roles::java::{call, null_safe};
assert_eq!(null_safe::OWNERS, &[] as &[u16]);
for kind in [0u16, 1, Java::MethodInvocation as u16, u16::MAX] {
assert!(!null_safe::lists_kind(kind), "absent row, kind {kind}");
}
assert!(call::lists_kind(Java::MethodInvocation as u16));
assert!(!call::lists_kind(Java::ClassBody as u16));
}
type Canonical = fn() -> String;
#[test]
fn tables_name_every_language() {
let tables = super::tables();
let listed: std::collections::BTreeSet<String> =
tables.iter().map(|t| t.name.to_owned()).collect();
let known: std::collections::BTreeSet<String> = crate::LANG::into_enum_iter()
.map(|l| format!("{l:?}"))
.collect();
pretty_assertions::assert_eq!(listed, known);
assert_eq!(tables.len(), known.len(), "no language listed twice");
}
#[test]
fn every_production_table_declares_every_role_once() {
let want: std::collections::BTreeSet<&str> = super::ROLES.iter().copied().collect();
assert_eq!(want.len(), super::ROLES.len(), "ROLES lists a role twice");
for table in super::tables() {
let rows = (table.rows)();
let roles: Vec<&str> = rows.iter().map(super::RowView::role).collect();
let got: std::collections::BTreeSet<&str> = roles.iter().copied().collect();
assert_eq!(
roles.len(),
got.len(),
"{}: a role declared twice",
table.lang
);
pretty_assertions::assert_eq!(got, want, "{}", table.lang);
}
}
#[test]
fn rows_agree_with_canonical_row_for_row() {
use super::RowView;
for table in super::tables() {
let canonical = (table.rows)();
let lines: Vec<String> = (table.canonical)()
.lines()
.skip(1)
.map(str::to_owned)
.collect();
assert_eq!(lines.len(), canonical.len(), "{}: row count", table.name);
for (line, row) in lines.iter().zip(&canonical) {
let ids = |kinds: &[u16]| {
kinds
.iter()
.map(u16::to_string)
.collect::<Vec<_>>()
.join(",")
};
let want = match row {
RowView::Absent { role, why } => format!("{role} absent {why}"),
RowView::Pending { role, package } => format!("{role} pending {package}"),
RowView::Declared(r) => {
let excluded = r
.excluded
.iter()
.map(|(k, why)| format!("{k}={why}"))
.collect::<Vec<_>>()
.join(";");
format!(
"{} rows owners=[{}] payers=[{}] wraps=[{}] excluded=[{}] mode={} \
text_gated={} chain_gated={}",
r.role,
ids(r.owners),
ids(r.payers),
ids(r.wraps),
excluded,
r.mode,
r.text_gated,
r.chain_gated
)
}
};
pretty_assertions::assert_eq!(line, &want, "{}", table.name);
}
}
}
#[cfg(not(windows))]
#[test]
fn python_reader_agrees_with_the_compiled_tables() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("the ast crate sits under the workspace root");
let tables: Vec<(String, Canonical)> = super::tables()
.iter()
.map(|t| (t.name.to_lowercase(), t.canonical))
.collect();
let listed: std::collections::BTreeSet<String> =
tables.iter().map(|(s, _)| s.clone()).collect();
let known: std::collections::BTreeSet<String> = crate::LANG::into_enum_iter()
.map(|l| format!("{l:?}").to_lowercase())
.collect();
pretty_assertions::assert_eq!(listed, known, "every LANG has a table listed here");
for (stem, canonical) in &tables {
let path = root.join(format!("big-code-analysis-ast/src/roles/{stem}.rs"));
let out = std::process::Command::new("python3")
.arg("-B")
.arg(root.join("utils/role_tables.py"))
.arg("--dump")
.arg(&path)
.output()
.expect("python3 is on PATH: the role gates need it, so this test does too");
assert!(
out.status.success(),
"{stem}: role_tables.py --dump failed:\n{}",
String::from_utf8_lossy(&out.stderr)
);
let printed = String::from_utf8(out.stdout).expect("dump is UTF-8");
pretty_assertions::assert_eq!(printed, canonical(), "{stem}");
}
}
fn never<'a>(_: &Node<'a>, _: &[u8], _: Ancestors<'a, '_>) -> bool {
false
}
fn always<'a>(_: &Node<'a>, _: &[u8], _: Ancestors<'a, '_>) -> bool {
true
}
fn always_no_chain(_: &Node<'_>, _: &[u8]) -> bool {
true
}
crate::observation::counter!(recording_gate_calls, cfg(test));
fn recording<'a>(_: &Node<'a>, _: &[u8], _: Ancestors<'a, '_>) -> bool {
recording_gate_calls::record();
true
}
mod gated_self_payer {
use super::never;
use crate::Java;
role_table! {
lang: Java;
Comparison {
owns [InstanceofExpression(pays_itself gated_by never)]
pays [EQEQ]
}
}
}
mod dsl_gates {
use super::{always, class_or_interface, never, recording};
use crate::Java;
role_table! {
lang: Java;
ElseIf {
owns [
WhileStatement(pays_itself),
IfStatement(gated_by recording),
DoStatement(pays_itself gated_by never),
]
pays []
}
Comparison {
owns [BinaryExpression(holds_payer)]
pays [EQEQ, LT under BinaryExpression, GT under [TernaryExpression, BinaryExpression]]
project "operator"
}
ShortCircuit {
owns [BinaryExpression(holds_payer gated_by always)]
pays [AMPAMP under TernaryExpression]
project "operator"
}
Ternary {
owns [TernaryExpression(gated_by never), InstanceofExpression(pays_itself gated_by always)]
pays [QMARK]
project "operator"
}
Guard { owns [Guard] pays never }
FuncOpener {
owns [
MethodDeclaration(callable opens Function),
StaticInitializer(opens Function),
ConstructorDeclaration(opens Function gated_by never callable),
LambdaExpression(opens Function callable_when never),
CompactConstructorDeclaration(opens Function callable_when always),
RecordDeclaration(opens Function callable text_gated_by always),
]
}
Container { owns [ClassDeclaration(opens_by class_or_interface)] }
Member { owns [FieldDeclaration(text_gated_by always)] }
Call {
owns [MethodInvocation(holds_payer text_gated_by always)]
pays [LPAREN]
}
}
}
mod dsl_no_chain {
use super::{always, always_no_chain, class_or_interface_no_chain};
use crate::Java;
role_table! {
lang: Java;
Container {
owns [
ClassDeclaration(opens_by class_or_interface_no_chain no_chain gated_by always_no_chain no_chain),
InterfaceDeclaration(gated_by always_no_chain no_chain opens_by class_or_interface_no_chain no_chain),
]
}
FuncOpener {
owns [LambdaExpression(opens Function callable text_gated_by always_no_chain no_chain)]
}
Member { owns [FieldDeclaration(holds_payer text_gated_by always_no_chain no_chain)] pays [EQEQ] }
Exit {
owns [ReturnStatement(gated_by always_no_chain no_chain), ThrowStatement(gated_by always_no_chain no_chain pays_itself)]
pays [Return]
}
Catch { owns [CatchClause(text_gated_by always_no_chain no_chain)] }
Loop { owns [WhileStatement(holds_payer gated_by always)] pays [While] }
Branch { owns [IfStatement(text_gated_by always)] }
}
}
mod dsl_two_fn_gate_marked {
use super::{always_no_chain, class_or_interface};
use crate::Java;
role_table! {
lang: Java;
Container { owns [ClassDeclaration(opens_by class_or_interface gated_by always_no_chain no_chain)] }
FuncOpener { owns [EnumDeclaration(gated_by always_no_chain no_chain opens_by class_or_interface)] }
}
}
mod dsl_two_fn_opens_by_marked {
use super::{always, class_or_interface_no_chain};
use crate::Java;
role_table! {
lang: Java;
Container { owns [ClassDeclaration(opens_by class_or_interface_no_chain no_chain gated_by always)] }
FuncOpener { owns [EnumDeclaration(gated_by always opens_by class_or_interface_no_chain no_chain)] }
}
}
mod dsl_parent_gates {
use super::{always, class_or_interface_no_chain, never};
use crate::Java;
role_table! {
lang: Java;
Branch { owns [IfStatement(under [ClassBody, Block])] }
Loop { owns [WhileStatement(not_under [ClassBody, Block])] }
ElseIf {
owns [
IfStatement(pays_itself not_under [ClassBody, Block]),
DoStatement(pays_itself_when always under [ClassBody, Block]),
WhileStatement(pays_itself_when never under [ClassBody, Block]),
]
pays []
}
FuncOpener {
owns [
ConstructorDeclaration(opens Function callable_when never under [Program, ClassBody]),
MethodDeclaration(callable opens Function under [Program, ClassBody]),
LambdaExpression(opens Function callable_when always not_under [ClassBody, VariableDeclarator]),
]
}
Container {
owns [ClassDeclaration(opens_by class_or_interface_no_chain no_chain under [InterfaceBody, Program])]
}
}
}
pub(super) fn class_or_interface<'a>(
node: &Node<'a>,
code: &[u8],
_: Ancestors<'a, '_>,
) -> crate::SpaceKind {
if node
.child_by_field_name("name")
.and_then(|n| n.utf8_text(code))
== Some("I")
{
crate::SpaceKind::Interface
} else {
crate::SpaceKind::Class
}
}
pub(super) fn class_or_interface_no_chain(node: &Node<'_>, code: &[u8]) -> crate::SpaceKind {
class_or_interface(node, code, Ancestors::known(&[]))
}
mod spliced {
use super::never;
use crate::Java;
macro_rules! jvm_openers {
(@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
$crate::role_table!(
@owners $cont [$($acc)* MethodDeclaration(opens Function), ConstructorDeclaration(opens Function),]
$($rest)*
);
};
}
macro_rules! equality_tokens {
(@splice $cont:tt [$($acc:tt)*] $($rest:tt)*) => {
$crate::role_table!(
@owners $cont [$($acc)* EQEQ, BANGEQ gated_by never,]
$($rest)*
);
};
}
role_table! {
lang: Java;
FuncOpener { owns [..jvm_openers!(), StaticInitializer(opens Function)] }
Container { owns [..jvm_openers!()] pays [ReturnStatement] }
Comparison { owns [BinaryExpression] pays [..equality_tokens!(), LT gated_by never] }
Guard { pending R99 }
}
}
#[cfg(feature = "javascript")]
#[test]
fn an_absent_row_answers_every_predicate_false() {
use crate::JavascriptCode;
use crate::roles::javascript::primitive;
use crate::test_support::for_each_node_with_chain;
let source = b"let x = 1;\n";
let mut asked = 0;
for_each_node_with_chain::<JavascriptCode>(source, |node, chain| {
let a = Ancestors::known(chain);
let kind = node.kind_id();
assert!(!primitive::owns(node, source, a));
assert!(!primitive::pays_at_leaf(node, source, a));
assert!(!primitive::pays_at_wrapper(node, source, a));
assert!(!primitive::owns_kind(kind, node, source, a));
assert!(!primitive::pays_at_leaf_kind(kind, node, source, a));
assert!(!primitive::pays_at_token_kind(kind, node, source, a));
asked += 1;
});
assert!(asked > 1);
assert_eq!(primitive::ABSENT, "JavaScript has no type names");
assert_eq!((primitive::OWNERS, primitive::PAYERS), (&[][..], &[][..]));
}
#[test]
fn leaf_kinds_follow_the_row_mode() {
assert_eq!(dsl_gates::guard::LEAF_KINDS, &[] as &[u16], "pays never");
assert_eq!(
spliced::container::LEAF_KINDS,
&[Java::ReturnStatement as u16],
"split, no self-payers"
);
assert_eq!(
spliced::func_opener::LEAF_KINDS,
spliced::func_opener::OWNERS,
"plain: every owner"
);
assert_eq!(
dsl_gates::ternary::LEAF_KINDS,
&[Java::QMARK as u16, Java::InstanceofExpression as u16]
);
assert_eq!(
gated_self_payer::comparison::LEAF_KINDS,
&[Java::EQEQ as u16, Java::InstanceofExpression as u16]
);
assert_eq!(
dsl_gates::else_if::LEAF_KINDS,
&[Java::WhileStatement as u16, Java::DoStatement as u16],
"split-empty: the marked owners only, the unmarked one not"
);
assert_eq!(
crate::roles::java::null_safe::LEAF_KINDS,
&[] as &[u16],
"absent"
);
}
#[test]
fn a_fragment_splices_its_kinds_before_the_rows_own() {
assert_eq!(
spliced::func_opener::OWNERS,
&[
Java::MethodDeclaration as u16,
Java::ConstructorDeclaration as u16,
Java::StaticInitializer as u16
]
);
assert_eq!(
spliced::container::OWNERS,
&[
Java::MethodDeclaration as u16,
Java::ConstructorDeclaration as u16
]
);
assert_eq!(spliced::container::PAYERS, &[Java::ReturnStatement as u16]);
assert_eq!(
spliced::comparison::PAYERS,
&[Java::EQEQ as u16, Java::BANGEQ as u16, Java::LT as u16]
);
}
#[cfg(feature = "java")]
#[test]
fn a_payer_fragment_keeps_its_inline_gates() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
let source = b"class A { boolean f(int a, int b) { return a == b || a != b || a < b; } }\n";
let mut paid = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
if spliced::comparison::pays_at_leaf(node, source, Ancestors::known(chain)) {
paid.push(node.kind());
}
});
assert_eq!(paid, ["=="]);
}
#[cfg(feature = "java")]
#[test]
fn a_fragment_reaches_the_opens_space_fold() {
use crate::test_support::for_each_node_with_chain;
use crate::{JavaCode, SpaceKind};
let source = b"class A { A() { } void m() { } static { } }\n";
let mut seen = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let k = spliced::opens_space(node, source, Ancestors::known(chain));
if k != SpaceKind::Unknown {
seen.push((node.kind(), k));
}
});
assert_eq!(
seen,
[
("constructor_declaration", SpaceKind::Function),
("method_declaration", SpaceKind::Function),
("static_initializer", SpaceKind::Function),
]
);
}
#[cfg(feature = "java")]
#[test]
fn a_gated_self_payer_pays_at_the_leaf_under_its_gate() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
let source = b"class A { boolean f(Object x) { return x instanceof String; } }\n";
let mut seen = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
if node.kind_id() == Java::InstanceofExpression as u16 {
let ancestors = Ancestors::known(chain);
seen.push((
gated_self_payer::comparison::owns(node, source, ancestors),
gated_self_payer::comparison::pays_at_leaf(node, source, ancestors),
));
}
});
assert_eq!(seen, [(false, false)]);
}
#[cfg(feature = "java")]
#[test]
fn holds_payer_and_under_gate_through_the_projection_and_the_parent() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
use dsl_gates::{comparison, short_circuit};
let source = b"class A { boolean f(int a, int b, java.util.List<String> l) { return a < b && a + b > 0; } }\n";
let (mut owns, mut chains, mut pays) = (Vec::new(), Vec::new(), Vec::new());
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let ancestors = Ancestors::known(chain);
let text = node.utf8_text(source).expect("fixture is UTF-8");
if comparison::owns(node, source, ancestors) {
owns.push(text.to_owned());
}
if short_circuit::owns(node, source, ancestors) {
chains.push(text.to_owned());
}
if comparison::pays_at_leaf(node, source, ancestors)
|| short_circuit::pays_at_leaf(node, source, ancestors)
{
pays.push(text.to_owned());
}
});
assert_eq!(owns, ["a < b", "a + b > 0"]);
assert_eq!(chains, ["a < b && a + b > 0"]);
assert_eq!(pays, ["<", ">"]);
}
#[cfg(feature = "java")]
#[test]
fn a_gated_self_payer_pays_where_its_gate_admits_it() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
use dsl_gates::ternary;
let source =
b"class A { int f(Object o, boolean a) { return o instanceof String ? 1 : 2; } }\n";
let (mut owns, mut pays) = (Vec::new(), Vec::new());
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let ancestors = Ancestors::known(chain);
let text = node.utf8_text(source).expect("fixture is UTF-8");
if ternary::owns(node, source, ancestors) {
owns.push(text.to_owned());
}
if ternary::pays_at_leaf(node, source, ancestors) {
pays.push(text.to_owned());
}
});
assert_eq!(owns, ["o instanceof String"]);
assert_eq!(pays, ["o instanceof String", "?"]);
}
#[cfg(feature = "java")]
#[test]
fn a_split_row_with_no_payers_pays_only_at_its_self_paying_owners() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
use dsl_gates::else_if;
let source =
b"class A { void f(boolean a) { if (a) { } while (a) { } do { } while (a); } }\n";
let mut seen = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let kind: Java = node.kind_id().into();
if !matches!(
kind,
Java::IfStatement | Java::WhileStatement | Java::DoStatement
) {
return;
}
let ancestors = Ancestors::known(chain);
let before = recording_gate_calls::observed();
let owns = else_if::owns(node, source, ancestors);
let during_owns = recording_gate_calls::observed() - before;
let before = recording_gate_calls::observed();
let leaf = else_if::pays_at_leaf(node, source, ancestors);
let during_leaf = recording_gate_calls::observed() - before;
let wrapper = else_if::pays_at_wrapper(node, source, ancestors);
seen.push((kind, owns, leaf, wrapper, during_owns, during_leaf));
});
assert_eq!(
seen,
[
(Java::IfStatement, true, false, true, 1, 0),
(Java::WhileStatement, true, true, true, 0, 0),
(Java::DoStatement, false, false, false, 0, 0),
]
);
assert_eq!(
(else_if::PAYERS, else_if::PAYS_NEVER),
(&[] as &[u16], false)
);
assert!(!else_if::lists_kind(Java::ForStatement as u16));
assert_eq!(
else_if::canonical(),
"ElseIf rows owners=[206,205,190] payers=[] wraps=[] excluded=[] mode=split \
text_gated=false chain_gated=true"
);
}
#[cfg(feature = "java")]
const PARENT_LIST_FIXTURE: &[u8] = b"class A {\n\
A() { }\n\
void m(boolean a) {\n\
if (a) { } else if (a) { }\n\
while (a) while (a) { }\n\
do { } while (a);\n\
if (a) do { } while (a);\n\
Runnable r = () -> { };\n\
run(() -> { });\n\
}\n\
class B { }\n\
}\n\
interface I { void g(); }\n";
#[cfg(feature = "java")]
#[test]
fn a_parent_list_gates_an_owner_on_its_parents_kind() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
use dsl_parent_gates::{branch, container, r#loop};
let source = PARENT_LIST_FIXTURE;
let (mut branches, mut loops) = (Vec::new(), Vec::new());
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let a = Ancestors::known(chain);
let parent = chain.last().map_or("", |p| p.kind());
if node.kind_id() == Java::IfStatement as u16 {
branches.push((parent, branch::owns(node, source, a)));
}
if node.kind_id() == Java::WhileStatement as u16 {
loops.push((parent, r#loop::owns(node, source, a)));
}
});
assert_eq!(
branches,
[("block", true), ("if_statement", false), ("block", true)]
);
assert_eq!(loops, [("block", false), ("while_statement", true)]);
assert_eq!(branch::OWNERS, &[Java::IfStatement as u16]);
assert_eq!(container::OWNERS, &[Java::ClassDeclaration as u16]);
}
#[cfg(feature = "java")]
#[test]
fn a_parent_list_reads_a_parentless_node_as_under_nothing() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
use dsl_parent_gates::{branch, r#loop};
let source = PARENT_LIST_FIXTURE;
let none = Ancestors::known(&[]);
let (mut branches, mut loops) = (0, 0);
for_each_node_with_chain::<JavaCode>(source, |node, _| {
if node.kind_id() == Java::IfStatement as u16 {
assert!(
!branch::owns(node, source, none),
"`under` admitted a parentless `if`"
);
branches += 1;
}
if node.kind_id() == Java::WhileStatement as u16 {
assert!(
r#loop::owns(node, source, none),
"`not_under` rejected a parentless `while`"
);
loops += 1;
}
});
assert_eq!((branches, loops), (3, 2));
}
#[cfg(feature = "java")]
#[test]
fn a_parent_list_gates_a_self_paying_owner_at_the_leaf() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
use dsl_parent_gates::else_if;
let source = PARENT_LIST_FIXTURE;
let mut chained = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let a = Ancestors::known(chain);
if matches!(
node.kind_id().into(),
Java::IfStatement | Java::WhileStatement | Java::DoStatement
) {
chained.push((
node.kind(),
chain.last().map_or("", |p| p.kind()),
else_if::owns(node, source, a),
else_if::pays_at_leaf(node, source, a),
));
}
});
assert_eq!(
chained,
[
("if_statement", "block", false, false),
("if_statement", "if_statement", true, true),
("while_statement", "block", true, false),
("while_statement", "while_statement", false, false),
("do_statement", "block", true, true),
("if_statement", "block", false, false),
("do_statement", "if_statement", false, false),
]
);
}
#[cfg(feature = "java")]
#[test]
fn a_parent_list_gates_callables_and_the_opens_space_fold() {
use crate::test_support::for_each_node_with_chain;
use crate::{JavaCode, SpaceKind};
use dsl_parent_gates::{func_opener, opens_space};
let source = PARENT_LIST_FIXTURE;
let mut openers = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let a = Ancestors::known(chain);
if matches!(
node.kind_id().into(),
Java::ClassDeclaration
| Java::ConstructorDeclaration
| Java::MethodDeclaration
| Java::LambdaExpression
) {
openers.push((
node.kind(),
chain.last().map_or("", |p| p.kind()),
func_opener::owns(node, source, a),
func_opener::is_callable(node, source, a),
opens_space(node, source, a),
));
}
});
assert_eq!(
openers,
[
(
"class_declaration",
"program",
false,
false,
SpaceKind::Class
),
(
"constructor_declaration",
"class_body",
true,
false,
SpaceKind::Function
),
(
"method_declaration",
"class_body",
true,
true,
SpaceKind::Function
),
(
"lambda_expression",
"variable_declarator",
false,
false,
SpaceKind::Unknown
),
(
"lambda_expression",
"argument_list",
true,
true,
SpaceKind::Function
),
(
"class_declaration",
"class_body",
false,
false,
SpaceKind::Unknown
),
(
"method_declaration",
"interface_body",
false,
false,
SpaceKind::Unknown
),
]
);
}
#[test]
fn canonical_names_each_paying_mode() {
use dsl_gates::{comparison, else_if, guard, member};
let mode_of = |row: String| {
let (_, tail) = row
.rsplit_once(" mode=")
.expect("every declared row carries its mode");
let (mode, _) = tail
.split_once(' ')
.expect("the context flags follow the mode");
mode.to_owned()
};
assert_eq!(
[
mode_of(comparison::canonical()),
mode_of(else_if::canonical()),
mode_of(member::canonical()),
mode_of(guard::canonical()),
],
["split", "split", "owners", "never"]
);
assert_eq!(
member::canonical(),
"Member rows owners=[249] payers=[] wraps=[] excluded=[] mode=owners \
text_gated=true chain_gated=true"
);
}
#[test]
fn rows_enumerate_what_the_invocation_declared_in_its_order() {
use super::RowView;
let one = gated_self_payer::rows();
assert_eq!(one.len(), 1);
let row = one[0].declared().expect("the one row is declared");
assert_eq!(row.role, "Comparison");
assert_eq!(row.owners, gated_self_payer::comparison::OWNERS);
assert_eq!(row.payers, gated_self_payer::comparison::PAYERS);
assert_eq!(row.leaf_kinds, gated_self_payer::comparison::LEAF_KINDS);
assert_eq!(
(row.mode, row.pays_never, row.text_gated),
("split", false, false)
);
assert_eq!(
row.candidate_kinds().collect::<Vec<_>>(),
[Java::InstanceofExpression as u16, Java::EQEQ as u16],
"owners, then payers, then leaf kinds, each once"
);
assert!((row.lists_kind)(Java::InstanceofExpression as u16));
assert!(!(row.lists_kind)(Java::EQEQ as u16));
let four: Vec<&str> = spliced::rows().iter().map(RowView::role).collect();
assert_eq!(
four,
["FuncOpener", "Container", "Comparison", "Guard"],
"declaration order"
);
let pending = spliced::rows().into_iter().nth(3).expect("four rows");
assert!(matches!(
&pending,
RowView::Pending {
role: "Guard",
package: "R99"
}
));
assert!(pending.declared().is_none());
assert_eq!(spliced::guard::canonical(), "Guard pending R99");
let java = super::java::rows();
let absent = java
.iter()
.find(|r| r.role() == "NullSafe")
.expect("Java declares NullSafe absent");
assert!(
matches!(absent, RowView::Absent { why, .. } if *why == super::java::null_safe::ABSENT)
);
assert!(absent.declared().is_none());
}
#[test]
fn text_gated_is_true_exactly_for_rows_whose_owners_read_bytes() {
use crate::roles::{cpp, csharp, elixir, groovy, java, javascript, kotlin, ruby};
let rows: &[(&str, bool, bool)] = &[
(
"elixir FuncOpener `text_gated_by method_macro`",
elixir::func_opener::TEXT_GATED,
true,
),
(
"elixir Member `text_gated_by struct_macro`",
elixir::member::TEXT_GATED,
true,
),
(
"groovy Primitive `text_gated_by spells_primitive_type`",
groovy::primitive::TEXT_GATED,
true,
),
(
"groovy DeclKeyword `text_gated_by spells_var`",
groovy::decl_keyword::TEXT_GATED,
true,
),
(
"csharp Modifier `text_gated_by aliases_a_modifier_keyword`",
csharp::modifier::TEXT_GATED,
true,
),
(
"java FuncOpener, kind-only owners `callable` and `opens`",
java::func_opener::TEXT_GATED,
false,
),
("java NullSafe, absent", java::null_safe::TEXT_GATED, false),
(
"java Container `gated_by has_class_body` reads the node",
java::container::TEXT_GATED,
false,
),
(
"java Comparison `holds_payer` reads the chain",
java::comparison::TEXT_GATED,
false,
),
(
"ruby FuncOpener `gated_by not_a_lambda_body` reads the chain",
ruby::func_opener::TEXT_GATED,
false,
),
(
"javascript FuncOpener `callable_when` reads the chain",
javascript::func_opener::TEXT_GATED,
false,
),
(
"kotlin Container `opens_by` reads the node",
kotlin::container::TEXT_GATED,
false,
),
(
"cpp Container `gated_by has_body` reads the node",
cpp::container::TEXT_GATED,
false,
),
];
for (row, text_gated, want) in rows {
assert_eq!(text_gated, want, "{row}");
}
let synthetic: &[(&str, bool)] = &[
("text_gated_by alone", dsl_gates::member::TEXT_GATED),
("after holds_payer", dsl_gates::call::TEXT_GATED),
(
"after opens and callable",
dsl_gates::func_opener::TEXT_GATED,
),
("gated_by, not text", dsl_gates::short_circuit::TEXT_GATED),
];
for (order, text_gated) in synthetic {
assert_eq!(*text_gated, *order != "gated_by, not text", "{order}");
}
let marked: &[(&str, bool, bool)] = &[
("under, alone", dsl_parent_gates::branch::TEXT_GATED, false),
(
"text_gated_by … no_chain",
dsl_no_chain::catch::TEXT_GATED,
true,
),
(
"holds_payer text_gated_by … no_chain",
dsl_no_chain::member::TEXT_GATED,
true,
),
("gated_by … no_chain", dsl_no_chain::exit::TEXT_GATED, false),
];
for (order, got, want) in marked {
assert_eq!(got, want, "{order}");
}
}
#[test]
fn text_and_chain_flags_are_independent_on_the_production_rows() {
use crate::roles::{groovy, java, javascript, kotlin, python, ruby};
type Flags = (bool, bool);
let rows: &[(&str, Flags, Flags)] = &[
(
"java DocComment `text_gated_by opens_with_javadoc_marker no_chain`: bytes, no chain",
(
java::doc_comment::TEXT_GATED,
java::doc_comment::CHAIN_GATED,
),
(true, false),
),
(
"groovy DeclKeyword `text_gated_by spells_var`: bytes and chain",
(
groovy::decl_keyword::TEXT_GATED,
groovy::decl_keyword::CHAIN_GATED,
),
(true, true),
),
(
"java Container `gated_by has_class_body no_chain`: neither",
(java::container::TEXT_GATED, java::container::CHAIN_GATED),
(false, false),
),
(
"ruby FuncOpener `gated_by not_a_lambda_body`: chain, no bytes",
(
ruby::func_opener::TEXT_GATED,
ruby::func_opener::CHAIN_GATED,
),
(false, true),
),
(
"kotlin Container `opens_by class_or_interface no_chain`",
(
kotlin::container::TEXT_GATED,
kotlin::container::CHAIN_GATED,
),
(false, false),
),
(
"javascript FuncOpener: `callable_when` reads the chain and is not covered",
(
javascript::func_opener::TEXT_GATED,
javascript::func_opener::CHAIN_GATED,
),
(false, false),
),
(
"java NullSafe, absent",
(java::null_safe::TEXT_GATED, java::null_safe::CHAIN_GATED),
(false, false),
),
(
"python Guard `under [CaseClause]`, its only owner: chain, no bytes",
(python::guard::TEXT_GATED, python::guard::CHAIN_GATED),
(false, true),
),
(
"java DeclKeyword `not_under [ClassLiteral]`, its only gate: chain, no bytes",
(
java::decl_keyword::TEXT_GATED,
java::decl_keyword::CHAIN_GATED,
),
(false, true),
),
(
"java Exit, kind-only",
(java::exit::TEXT_GATED, java::exit::CHAIN_GATED),
(false, false),
),
];
for (row, got, want) in rows {
assert_eq!(got, want, "{row}");
}
}
#[test]
fn chain_gated_is_true_exactly_for_rows_whose_owner_fns_take_the_chain() {
let synthetic: &[(&str, bool, bool)] = &[
(
"gated_by alone, and before pays_itself",
dsl_no_chain::exit::CHAIN_GATED,
false,
),
(
"text_gated_by after opens and callable",
dsl_no_chain::func_opener::CHAIN_GATED,
false,
),
(
"holds_payer text_gated_by",
dsl_no_chain::member::CHAIN_GATED,
false,
),
(
"text_gated_by alone",
dsl_no_chain::catch::CHAIN_GATED,
false,
),
(
"both fns marked, both orders",
dsl_no_chain::container::CHAIN_GATED,
false,
),
(
"holds_payer gated_by, unmarked",
dsl_no_chain::r#loop::CHAIN_GATED,
true,
),
(
"text_gated_by, unmarked",
dsl_no_chain::branch::CHAIN_GATED,
true,
),
(
"opens_by unmarked, written first",
dsl_two_fn_gate_marked::container::CHAIN_GATED,
true,
),
(
"opens_by unmarked, written second",
dsl_two_fn_gate_marked::func_opener::CHAIN_GATED,
true,
),
(
"gate unmarked, written second",
dsl_two_fn_opens_by_marked::container::CHAIN_GATED,
true,
),
(
"gate unmarked, written first",
dsl_two_fn_opens_by_marked::func_opener::CHAIN_GATED,
true,
),
(
"gated_by unmarked on the proof table",
dsl_gates::ternary::CHAIN_GATED,
true,
),
(
"opens_by unmarked on the proof table",
dsl_gates::container::CHAIN_GATED,
true,
),
(
"callable_when and a gated callable: the gate, not the when",
dsl_gates::func_opener::CHAIN_GATED,
true,
),
("kind-only owners", dsl_gates::guard::CHAIN_GATED, false),
("under alone", dsl_parent_gates::branch::CHAIN_GATED, true),
(
"not_under alone",
dsl_parent_gates::r#loop::CHAIN_GATED,
true,
),
(
"a list beside pays_itself and pays_itself_when",
dsl_parent_gates::else_if::CHAIN_GATED,
true,
),
(
"a list beside callable and callable_when",
dsl_parent_gates::func_opener::CHAIN_GATED,
true,
),
(
"a list beside a no_chain opens_by",
dsl_parent_gates::container::CHAIN_GATED,
true,
),
];
for (order, got, want) in synthetic {
assert_eq!(got, want, "{order}");
}
}
#[test]
fn callable_when_is_outside_chain_gated() {
use crate::roles::{javascript, mozjs, tsx, typescript};
for (lang, chain_gated, callable) in [
(
"javascript",
javascript::func_opener::CHAIN_GATED,
javascript::func_opener::CALLABLE.len(),
),
(
"mozjs",
mozjs::func_opener::CHAIN_GATED,
mozjs::func_opener::CALLABLE.len(),
),
(
"tsx",
tsx::func_opener::CHAIN_GATED,
tsx::func_opener::CALLABLE.len(),
),
(
"typescript",
typescript::func_opener::CHAIN_GATED,
typescript::func_opener::CALLABLE.len(),
),
] {
assert!(
callable >= 6,
"{lang}: the `callable_when` owners are declared"
);
assert!(
!chain_gated,
"{lang}: `callable_when` is not a chain gate of the row"
);
}
}
#[cfg(feature = "java")]
#[test]
fn a_callable_owner_is_told_from_an_opener_that_merely_opens() {
use crate::JavaCode;
use crate::SpaceKind;
use crate::test_support::for_each_node_with_chain;
use dsl_gates::{func_opener, opens_space};
let source = b"record R(int a) { R { } }\nclass A { A() { } void m() { Runnable r = () -> { }; } static { } }\n";
let mut seen = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let ancestors = Ancestors::known(chain);
let owns = func_opener::owns(node, source, ancestors);
let callable = func_opener::is_callable(node, source, ancestors);
let space = opens_space(node, source, ancestors);
if owns || callable || space != SpaceKind::Unknown {
seen.push((node.kind(), owns, callable, space));
}
});
assert_eq!(
seen,
[
("record_declaration", true, true, SpaceKind::Function),
(
"compact_constructor_declaration",
true,
true,
SpaceKind::Function
),
("class_declaration", false, false, SpaceKind::Class),
("method_declaration", true, true, SpaceKind::Function),
("lambda_expression", true, false, SpaceKind::Function),
("static_initializer", true, false, SpaceKind::Function),
]
);
assert_eq!(func_opener::OWNERS.len(), 6);
assert_eq!(
func_opener::CALLABLE,
&[
Java::MethodDeclaration as u16,
Java::ConstructorDeclaration as u16,
Java::LambdaExpression as u16,
Java::CompactConstructorDeclaration as u16,
Java::RecordDeclaration as u16,
]
);
assert_eq!(dsl_gates::guard::CALLABLE, &[] as &[u16]);
}
#[test]
fn holds_payer_lists_the_marked_owners_in_every_spelling() {
assert_eq!(
dsl_gates::comparison::HOLDS_PAYER,
&[Java::BinaryExpression as u16],
"bare"
);
assert_eq!(
dsl_gates::short_circuit::HOLDS_PAYER,
&[Java::BinaryExpression as u16],
"beside gated_by"
);
assert_eq!(
dsl_gates::call::HOLDS_PAYER,
&[Java::MethodInvocation as u16],
"beside text_gated_by"
);
assert_eq!(
dsl_gates::ternary::HOLDS_PAYER,
&[] as &[u16],
"unmarked owners"
);
}
#[cfg(feature = "java")]
#[test]
fn an_opens_by_owner_opens_what_its_fn_answers() {
use crate::JavaCode;
use crate::SpaceKind;
use crate::test_support::for_each_node_with_chain;
use dsl_gates::opens_space;
let source = b"class I { }\nclass A { }\n";
let mut seen = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let kind = opens_space(node, source, Ancestors::known(chain));
if kind != SpaceKind::Unknown {
seen.push(kind);
}
});
assert_eq!(seen, [SpaceKind::Interface, SpaceKind::Class]);
}
#[cfg(feature = "java")]
#[test]
fn a_pays_never_row_owns_and_pays_nowhere() {
use crate::JavaCode;
use crate::test_support::for_each_node_with_chain;
use dsl_gates::guard;
let source = b"class A { int f(Object o) { return switch (o) { case Integer i when i > 0 -> 1; default -> 0; }; } }\n";
let mut seen = Vec::new();
for_each_node_with_chain::<JavaCode>(source, |node, chain| {
let ancestors = Ancestors::known(chain);
if node.kind_id() == Java::Guard as u16 {
seen.push((
guard::owns(node, source, ancestors),
guard::pays_at_leaf(node, source, ancestors),
guard::pays_at_wrapper(node, source, ancestors),
guard::pays_at_token_kind(node.kind_id(), node, source, ancestors),
));
}
});
assert_eq!(seen, [(true, false, false, false)]);
assert_eq!(
guard::canonical(),
"Guard rows owners=[184] payers=[] wraps=[] excluded=[] mode=never \
text_gated=false chain_gated=false"
);
}
}