use crate::lang_helpers::doc_comment::go_directive;
use crate::role_table;
use crate::{Ancestors, FoldLane, Go, Node};
fn outside_function_body<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
ancestors.fold_down(
node,
0,
FoldLane::DocScope,
0,
|outside, ancestor, _, _| match ancestor.kind_id().into() {
Go::SourceFile => 1,
Go::StatementList | Go::Block => 0,
_ => outside,
},
) == 1
}
fn interface_element_opens_with_a_name<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
ancestors.parent_has_kind(node, Go::InterfaceType as u16)
&& node.child(0).is_some_and(|first| {
matches!(
first.kind_id().into(),
Go::TypeIdentifier | Go::QualifiedType | Go::GenericType
)
})
&& outside_function_body(node, ancestors)
}
crate::roles::byte_gate!(is_go_directive, go_directive);
fn parent_is_if<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
ancestors.parent_has_kind(node, Go::IfStatement as u16)
}
fn calls_panic(node: &Node<'_>, code: &[u8]) -> bool {
node.child_by_field_name("function")
.is_some_and(|f| f.kind_id() == Go::Identifier as u16 && f.utf8_text(code) == Some("panic"))
}
role_table! {
lang: Go;
Call { owns [CallExpression] }
StringLit { owns [InterpretedStringLiteral, RawStringLiteral] }
Comparison {
owns [BinaryExpression(holds_payer)]
pays [
EQEQ, BANGEQ, LTEQ, GTEQ,
LT under BinaryExpression, GT under BinaryExpression,
]
project "operator"
}
ShortCircuit {
owns [BinaryExpression(holds_payer)]
pays [AMPAMP, PIPEPIPE]
project "operator"
}
EagerLogical { absent "Go's `&` / `|` / `^` are bitwise" }
Ternary { absent "Go has no ternary expression" }
NullSafe { absent "Go has no optional navigation or null coalescing" }
CaseArm {
owns [ExpressionCase, TypeCase, CommunicationCase]
excludes [
DefaultCase "the fallback arm, no decision (lesson 11, #469)",
Case "the arm's keyword, billed as such; the arm pays",
Default "the fallback label's keyword",
]
}
Guard { absent "Go has no guard clause on a case or a function head" }
Branch { owns [IfStatement] }
Switch { owns [ExpressionSwitchStatement, TypeSwitchStatement, SelectStatement] }
Loop { owns [ForStatement] }
Catch { absent "Go has no exception handling; `recover` is a call" }
ElseIf { owns [IfStatement(gated_by parent_is_if)] pays [] }
Exit { owns [ReturnStatement, CallExpression(text_gated_by calls_panic no_chain)] }
Modifier { absent "Go has no modifier keywords; visibility is the identifier's case" }
DeclKeyword { owns [Func, Type, Struct, Interface, Const, Var, Package, Import] }
Primitive { absent "Go's built-in types (`int`, `string`) are ordinary `type_identifier`s, \
operands like every other type name" }
FuncOpener {
owns [
FunctionDeclaration(callable opens Function),
MethodDeclaration(callable opens Function),
FuncLiteral(opens Function),
]
}
Closure { owns [FuncLiteral] }
Container { owns [SourceFile(opens Unit)] }
Member { owns [FieldDeclaration] }
Comment { owns [Comment] }
DocComment { absent "go/doc reads a comment as documentation by its position above a declaration, never by a marker" }
Docstring { absent "Go documents with comments, not string literals" }
DocTarget {
owns [
PackageClause,
FunctionDeclaration,
MethodDeclaration,
TypeDeclaration(gated_by outside_function_body),
VarDeclaration(gated_by outside_function_body),
ConstDeclaration(gated_by outside_function_body),
TypeSpec(gated_by outside_function_body),
TypeAlias(gated_by outside_function_body),
VarSpec(gated_by outside_function_body),
ConstSpec(gated_by outside_function_body),
FieldDeclaration(gated_by outside_function_body),
MethodElem(gated_by outside_function_body),
TypeElem(gated_by interface_element_opens_with_a_name),
]
}
DocRunOpener { absent "a go/doc comment documents by its position above a declaration, never by its own content" }
DocRunDirective { owns [Comment(text_gated_by is_go_directive no_chain)] }
DocBodyResidual { absent "go/doc reads no comment inside a function body; nothing beyond the function withholds one" }
}