use crate::{ContainerBody, Import, Language, LanguageSymbols};
use tree_sitter::Node;
pub struct TlaPlus;
impl Language for TlaPlus {
fn name(&self) -> &'static str {
"TLA+"
}
fn extensions(&self) -> &'static [&'static str] {
&["tla"]
}
fn grammar_name(&self) -> &'static str {
"tlaplus"
}
fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
Some(self)
}
fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
if node.kind() != "extends" {
return Vec::new();
}
let text = &content[node.byte_range()];
vec![Import {
module: text.trim().to_string(),
names: Vec::new(),
alias: None,
is_wildcard: false,
is_relative: false,
line: node.start_position().row + 1,
}]
}
fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
format!("EXTENDS {}", import.module)
}
fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
let name = symbol.name.as_str();
match symbol.kind {
crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
crate::SymbolKind::Module => name == "tests" || name == "test",
_ => false,
}
}
fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
Some(*node)
}
fn analyze_container_body(
&self,
body_node: &Node,
content: &str,
inner_indent: &str,
) -> Option<ContainerBody> {
let start = body_node.start_byte();
let end = body_node.end_byte();
let bytes = content.as_bytes();
let mut content_start = start;
while content_start < end && bytes[content_start] != b'\n' {
content_start += 1;
}
if content_start < end {
content_start += 1; }
let mut content_end = end;
if end >= 4 && bytes.get(end - 4..end) == Some(b"====") {
content_end = end - 4;
while content_end > content_start
&& matches!(bytes[content_end - 1], b' ' | b'\t' | b'\n')
{
content_end -= 1;
}
}
let is_empty = content[content_start..content_end].trim().is_empty();
Some(ContainerBody {
content_start,
content_end,
inner_indent: inner_indent.to_string(),
is_empty,
})
}
}
impl LanguageSymbols for TlaPlus {}
#[cfg(test)]
mod tests {
use super::*;
use crate::validate_unused_kinds_audit;
#[test]
fn unused_node_kinds_audit() {
#[rustfmt::skip]
let documented_unused: &[&str] = &[
"constant_declaration", "variable_declaration", "operator_declaration",
"local_definition", "function_definition", "module_definition",
"recursive_declaration", "operator_args",
"definition_proof_step", "case_proof_step", "use_body", "use_body_def",
"use_body_expr", "statement_level",
"forall", "quantifier_bound", "tuple_of_identifiers",
"unbounded_quantification", "bounded_quantification", "temporal_forall",
"case_arm", "case_arrow", "case_box",
"function_literal", "function_evaluation", "set_of_functions",
"except", "except_update", "except_update_specifier",
"except_update_fn_appl", "except_update_record_field",
"pcal_algorithm_body", "pcal_definitions", "pcal_if", "pcal_end_if",
"pcal_while", "pcal_end_while", "pcal_with", "pcal_end_with",
"pcal_await", "pcal_return",
"identifier", "identifier_ref", "module_ref",
"block_comment", "block_comment_text",
"lambda", "iff", "format", "subexpression",
"case", "if_then_else",
];
validate_unused_kinds_audit(&TlaPlus, documented_unused)
.expect("TLA+ unused node kinds audit failed");
}
}