diff --git a/src/xatu/parser.rs b/src/xatu/parser.rs index 581e19a..300fc10 100644 --- a/src/xatu/parser.rs +++ b/src/xatu/parser.rs @@ -21,6 +21,91 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile { } } +fn convert_node(node: rnix::SyntaxNode) -> Option { + match node.kind() { + rnix::SyntaxKind::NODE_APPLY => convert_apply(node), + rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node), + _ => None, + } +} + +fn convert_apply(node: rnix::SyntaxNode) -> Option { + let apply = rnix::ast::Apply::cast(node)?; + let range = apply.syntax().text_range().into(); + let data = apply.try_into().ok()?; + + Some(ast::Node { + range, + data: ast::NodeData::Apply(data), + }) +} + +impl TryFrom for ast::Apply { + type Error = (); + + fn try_from(apply: rnix::ast::Apply) -> Result { + Ok(Self { + function: apply.lambda().ok_or(())?.try_into()?, + }) + } +} + +impl TryFrom for ast::Expr { + type Error = (); + + fn try_from(expr: rnix::ast::Expr) -> Result { + match expr { + rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.into())), + rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))), + _ => Err(()), + } + } +} + +impl From for ast::Ident { + fn from(ident: rnix::ast::Ident) -> Self { + Self { + name: ident.to_string(), + } + } +} + +impl TryFrom for ast::Select { + type Error = (); + + fn try_from(select: rnix::ast::Select) -> Result { + Ok(Self { + receiver: select.expr().ok_or(())?.try_into()?, + attrs: select + .attrpath() + .ok_or(())? + .attrs() + .map(|attr| match attr { + rnix::ast::Attr::Ident(ident) => Ok(ident.into()), + _ => Err(()), + }) + .collect::, _>>()?, + }) + } +} + +fn convert_inherit(node: rnix::SyntaxNode) -> Option { + let inherit = rnix::ast::Inherit::cast(node)?; + + Some(ast::Node { + range: inherit.syntax().text_range().into(), + data: ast::NodeData::Inherit(inherit.into()), + }) +} + +impl From for ast::Inherit { + fn from(inherit: rnix::ast::Inherit) -> Self { + Self { + attr_count: inherit.attrs().count(), + } + } +} + fn convert_parse_error(error: &rnix::ParseError, source_len: usize) -> ast::ParseError { ast::ParseError { range: parse_error_range(error, source_len), @@ -50,93 +135,6 @@ fn parse_error_range(error: &rnix::ParseError, source_len: usize) -> ast::Source } } -fn convert_node(node: rnix::SyntaxNode) -> Option { - match node.kind() { - rnix::SyntaxKind::NODE_APPLY => convert_apply(node), - rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node), - _ => None, - } -} - -fn convert_apply(node: rnix::SyntaxNode) -> Option { - let apply = rnix::ast::Apply::cast(node)?; - let range = apply.syntax().text_range().into(); - let data = apply.try_into().ok()?; - - Some(ast::Node { - range, - data: ast::NodeData::Apply(data), - }) -} - -fn convert_inherit(node: rnix::SyntaxNode) -> Option { - let inherit = rnix::ast::Inherit::cast(node)?; - - Some(ast::Node { - range: inherit.syntax().text_range().into(), - data: ast::NodeData::Inherit(inherit.into()), - }) -} - -impl TryFrom for ast::Apply { - type Error = (); - - fn try_from(apply: rnix::ast::Apply) -> Result { - Ok(Self { - function: apply.lambda().ok_or(())?.try_into()?, - }) - } -} - -impl TryFrom for ast::Expr { - type Error = (); - - fn try_from(expr: rnix::ast::Expr) -> Result { - match expr { - rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.try_into()?)), - rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))), - _ => Err(()), - } - } -} - -impl TryFrom for ast::Ident { - type Error = (); - - fn try_from(ident: rnix::ast::Ident) -> Result { - Ok(Self { - name: ident.ident_token().ok_or(())?.text().to_owned(), - }) - } -} - -impl TryFrom for ast::Select { - type Error = (); - - fn try_from(select: rnix::ast::Select) -> Result { - Ok(Self { - receiver: select.expr().ok_or(())?.try_into()?, - attrs: select - .attrpath() - .ok_or(())? - .attrs() - .map(|attr| match attr { - rnix::ast::Attr::Ident(ident) => ident.try_into(), - _ => Err(()), - }) - .collect::, _>>()?, - }) - } -} - -impl From for ast::Inherit { - fn from(inherit: rnix::ast::Inherit) -> Self { - Self { - attr_count: inherit.attrs().count(), - } - } -} - impl From for ast::SourceRange { fn from(range: rnix::TextRange) -> Self { Self {