use crate::parse::google::nodes::{
GoogleArg, GoogleAttribute, GoogleDocstring, GoogleException, GoogleMethod, GoogleReturn, GoogleSection,
GoogleSeeAlsoItem, GoogleWarning, GoogleYield,
};
use crate::parse::numpy::nodes::{
NumPyAttribute, NumPyDeprecation, NumPyDocstring, NumPyException, NumPyMethod, NumPyParameter, NumPyReference,
NumPyReturns, NumPySection, NumPySeeAlsoItem, NumPyWarning, NumPyYields,
};
use crate::parse::plain::nodes::PlainDocstring;
use crate::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};
pub trait DocstringVisitor: Sized {
type Error;
fn visit_plain_docstring(&mut self, source: &str, doc: &PlainDocstring<'_>) -> Result<(), Self::Error> {
let _ = (source, doc);
Ok(())
}
fn visit_google_docstring(&mut self, source: &str, doc: &GoogleDocstring<'_>) -> Result<(), Self::Error> {
walk_children(source, doc.syntax(), self)
}
fn visit_google_section(&mut self, source: &str, sec: &GoogleSection<'_>) -> Result<(), Self::Error> {
walk_children(source, sec.syntax(), self)
}
fn visit_google_arg(&mut self, source: &str, arg: &GoogleArg<'_>) -> Result<(), Self::Error> {
walk_children(source, arg.syntax(), self)
}
fn visit_google_return(&mut self, source: &str, rtn: &GoogleReturn<'_>) -> Result<(), Self::Error> {
walk_children(source, rtn.syntax(), self)
}
fn visit_google_yield(&mut self, source: &str, yld: &GoogleYield<'_>) -> Result<(), Self::Error> {
walk_children(source, yld.syntax(), self)
}
fn visit_google_exception(&mut self, source: &str, exc: &GoogleException<'_>) -> Result<(), Self::Error> {
walk_children(source, exc.syntax(), self)
}
fn visit_google_warning(&mut self, source: &str, wrn: &GoogleWarning<'_>) -> Result<(), Self::Error> {
walk_children(source, wrn.syntax(), self)
}
fn visit_google_see_also_item(&mut self, source: &str, sai: &GoogleSeeAlsoItem<'_>) -> Result<(), Self::Error> {
walk_children(source, sai.syntax(), self)
}
fn visit_google_attribute(&mut self, source: &str, att: &GoogleAttribute<'_>) -> Result<(), Self::Error> {
walk_children(source, att.syntax(), self)
}
fn visit_google_method(&mut self, source: &str, mtd: &GoogleMethod<'_>) -> Result<(), Self::Error> {
walk_children(source, mtd.syntax(), self)
}
fn visit_numpy_docstring(&mut self, source: &str, doc: &NumPyDocstring<'_>) -> Result<(), Self::Error> {
walk_children(source, doc.syntax(), self)
}
fn visit_numpy_deprecation(&mut self, source: &str, dep: &NumPyDeprecation<'_>) -> Result<(), Self::Error> {
walk_children(source, dep.syntax(), self)
}
fn visit_numpy_section(&mut self, source: &str, sec: &NumPySection<'_>) -> Result<(), Self::Error> {
walk_children(source, sec.syntax(), self)
}
fn visit_numpy_parameter(&mut self, source: &str, prm: &NumPyParameter<'_>) -> Result<(), Self::Error> {
walk_children(source, prm.syntax(), self)
}
fn visit_numpy_returns(&mut self, source: &str, rtn: &NumPyReturns<'_>) -> Result<(), Self::Error> {
walk_children(source, rtn.syntax(), self)
}
fn visit_numpy_yields(&mut self, source: &str, yld: &NumPyYields<'_>) -> Result<(), Self::Error> {
walk_children(source, yld.syntax(), self)
}
fn visit_numpy_exception(&mut self, source: &str, exc: &NumPyException<'_>) -> Result<(), Self::Error> {
walk_children(source, exc.syntax(), self)
}
fn visit_numpy_warning(&mut self, source: &str, wrn: &NumPyWarning<'_>) -> Result<(), Self::Error> {
walk_children(source, wrn.syntax(), self)
}
fn visit_numpy_see_also_item(&mut self, source: &str, sai: &NumPySeeAlsoItem<'_>) -> Result<(), Self::Error> {
walk_children(source, sai.syntax(), self)
}
fn visit_numpy_reference(&mut self, source: &str, r#ref: &NumPyReference<'_>) -> Result<(), Self::Error> {
walk_children(source, r#ref.syntax(), self)
}
fn visit_numpy_attribute(&mut self, source: &str, att: &NumPyAttribute<'_>) -> Result<(), Self::Error> {
walk_children(source, att.syntax(), self)
}
fn visit_numpy_method(&mut self, source: &str, mtd: &NumPyMethod<'_>) -> Result<(), Self::Error> {
walk_children(source, mtd.syntax(), self)
}
}
pub fn walk<V: DocstringVisitor>(source: &str, node: &SyntaxNode, visitor: &mut V) -> Result<(), V::Error> {
match node.kind() {
SyntaxKind::PLAIN_DOCSTRING => visitor.visit_plain_docstring(source, &PlainDocstring(node))?,
SyntaxKind::GOOGLE_DOCSTRING => visitor.visit_google_docstring(source, &GoogleDocstring(node))?,
SyntaxKind::NUMPY_DOCSTRING => visitor.visit_numpy_docstring(source, &NumPyDocstring(node))?,
SyntaxKind::GOOGLE_SECTION => visitor.visit_google_section(source, &GoogleSection(node))?,
SyntaxKind::GOOGLE_ARG => visitor.visit_google_arg(source, &GoogleArg(node))?,
SyntaxKind::GOOGLE_RETURNS => visitor.visit_google_return(source, &GoogleReturn(node))?,
SyntaxKind::GOOGLE_YIELDS => visitor.visit_google_yield(source, &GoogleYield(node))?,
SyntaxKind::GOOGLE_EXCEPTION => visitor.visit_google_exception(source, &GoogleException(node))?,
SyntaxKind::GOOGLE_WARNING => visitor.visit_google_warning(source, &GoogleWarning(node))?,
SyntaxKind::GOOGLE_SEE_ALSO_ITEM => visitor.visit_google_see_also_item(source, &GoogleSeeAlsoItem(node))?,
SyntaxKind::GOOGLE_ATTRIBUTE => visitor.visit_google_attribute(source, &GoogleAttribute(node))?,
SyntaxKind::GOOGLE_METHOD => visitor.visit_google_method(source, &GoogleMethod(node))?,
SyntaxKind::NUMPY_SECTION => visitor.visit_numpy_section(source, &NumPySection(node))?,
SyntaxKind::NUMPY_DEPRECATION => visitor.visit_numpy_deprecation(source, &NumPyDeprecation(node))?,
SyntaxKind::NUMPY_PARAMETER => visitor.visit_numpy_parameter(source, &NumPyParameter(node))?,
SyntaxKind::NUMPY_RETURNS => visitor.visit_numpy_returns(source, &NumPyReturns(node))?,
SyntaxKind::NUMPY_YIELDS => visitor.visit_numpy_yields(source, &NumPyYields(node))?,
SyntaxKind::NUMPY_EXCEPTION => visitor.visit_numpy_exception(source, &NumPyException(node))?,
SyntaxKind::NUMPY_WARNING => visitor.visit_numpy_warning(source, &NumPyWarning(node))?,
SyntaxKind::NUMPY_SEE_ALSO_ITEM => visitor.visit_numpy_see_also_item(source, &NumPySeeAlsoItem(node))?,
SyntaxKind::NUMPY_REFERENCE => visitor.visit_numpy_reference(source, &NumPyReference(node))?,
SyntaxKind::NUMPY_ATTRIBUTE => visitor.visit_numpy_attribute(source, &NumPyAttribute(node))?,
SyntaxKind::NUMPY_METHOD => visitor.visit_numpy_method(source, &NumPyMethod(node))?,
_ => {}
}
Ok(())
}
#[inline]
fn walk_children<V: DocstringVisitor>(source: &str, node: &SyntaxNode, visitor: &mut V) -> Result<(), V::Error> {
for child in node.children() {
if let SyntaxElement::Node(n) = child {
walk(source, n, visitor)?;
}
}
Ok(())
}