use crate::lang_helpers::doc_comment::{
documents_where_it_sits, ldoc_opener, lua_group_head_above, node_bytes,
};
use crate::role_table;
use crate::{Ancestors, Lua, LuaCode, Node};
fn assigns_one_name(node: &Node<'_>) -> bool {
node.child(0).is_some_and(|targets| {
let mut names = targets.children().filter(Node::is_named);
names.next().is_some_and(|name| is_dotted_name(&name)) && names.next().is_none()
})
}
fn is_dotted_name(node: &Node) -> bool {
node.preorder().filter(Node::is_named).all(|part| {
matches!(
part.kind_id().into(),
Lua::Identifier | Lua::DotIndexExpression | Lua::DotIndexExpression2
)
})
}
fn declares_one_name(node: &Node<'_>) -> bool {
node.children().any(|child| {
matches!(
child.kind_id().into(),
Lua::AssignmentStatement | Lua::AssignmentStatement2
) && assigns_one_name(&child)
})
}
fn field_has_a_name(node: &Node<'_>) -> bool {
node.child(0)
.is_some_and(|name| name.kind_id() == Lua::Identifier)
&& node.child(1).is_some_and(|eq| eq.kind_id() == Lua::EQ)
}
fn returns_a_function_or_table(node: &Node<'_>) -> bool {
node.children()
.find(|child| {
matches!(
child.kind_id().into(),
Lua::ExpressionList | Lua::ExpressionList2
)
})
.and_then(|list| list.children().find(Node::is_named))
.is_some_and(|value| {
matches!(
value.kind_id().into(),
Lua::FunctionDefinition | Lua::TableConstructor
)
})
}
fn opens_an_ldoc_block_where_ldoc_reads<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
ldoc_opener(node_bytes(node, code))
&& documents_where_it_sits::<LuaCode>(node, code, ancestors)
&& lua_group_head_above::<LuaCode>(node, code, ancestors)
.is_none_or(|head| ldoc_opener(node_bytes(&head, code)))
}
fn error_or_os_exit(node: &Node<'_>, code: &[u8]) -> bool {
node.child_by_field_name("name")
.is_some_and(|name| matches!(name.utf8_text(code), Some("error" | "os.exit")))
}
role_table! {
lang: Lua;
Call { owns [FunctionCall] }
StringLit { owns [String] }
Comparison {
owns [BinaryExpression(holds_payer)]
pays [
EQEQ, TILDEEQ, LTEQ, GTEQ,
LT under BinaryExpression, GT under BinaryExpression,
]
project "operator"
}
ShortCircuit {
owns [BinaryExpression(holds_payer)]
pays [And, Or]
project "operator"
}
EagerLogical { absent "Lua's `and` / `or` are lazy; it has no eager boolean operator" }
Ternary { absent "Lua has no ternary expression; `a and b or c` is a chain" }
NullSafe { absent "Lua has no optional navigation or null coalescing" }
CaseArm { absent "Lua has no switch-like construct" }
Guard { absent "Lua has no guard clause" }
Branch { owns [IfStatement] }
Switch { absent "Lua has no switch-like construct" }
Loop { owns [ForStatement, WhileStatement, RepeatStatement] }
Catch { absent "Lua has no exception handling; `pcall` is a call" }
ElseIf { owns [ElseifStatement(pays_itself)] pays [] }
Exit { owns [ReturnStatement, FunctionCall(text_gated_by error_or_os_exit no_chain)] }
Modifier { absent "Lua has no modifier keywords; `local` is a declaration" }
DeclKeyword { owns [Local, Function] }
Primitive { absent "Lua has no type names" }
FuncOpener {
owns [
FunctionDeclaration(callable opens Function),
FunctionDeclaration2(callable opens Function),
FunctionDeclaration3(callable opens Function),
FunctionDefinition(opens Function),
]
}
Closure { owns [FunctionDefinition] }
Container { owns [Chunk(opens Unit)] }
Member { absent "Lua has no container to hold a member" }
Comment { owns [Comment] }
DocComment { absent "an LDoc block is documentation as a run opened by `---`, never one comment by a marker" }
Docstring { absent "Lua documents with LDoc comments, not string literals" }
DocTarget {
owns [
FunctionDeclaration,
FunctionDeclaration2,
FunctionDeclaration3,
AssignmentStatement(gated_by assigns_one_name no_chain),
AssignmentStatement2(gated_by assigns_one_name no_chain),
VariableDeclaration(gated_by declares_one_name no_chain),
VariableDeclaration2(gated_by declares_one_name no_chain),
Field(gated_by field_has_a_name no_chain),
ReturnStatement(gated_by returns_a_function_or_table no_chain),
]
}
DocRunOpener { owns [Comment(text_gated_by opens_an_ldoc_block_where_ldoc_reads)] }
DocRunDirective { absent "LDoc reads no directive comment" }
DocBodyResidual { absent "only the functions hold an LDoc block in a body; the trait default" }
}