refactor(xatu): retain node parsing scaffolding
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -401,6 +401,7 @@ dependencies = [
|
|||||||
"googletest",
|
"googletest",
|
||||||
"monorepo",
|
"monorepo",
|
||||||
"rnix",
|
"rnix",
|
||||||
|
"rowan",
|
||||||
"status",
|
"status",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ rust_library(
|
|||||||
deps = [
|
deps = [
|
||||||
"//src/status:status",
|
"//src/status:status",
|
||||||
"//third_party/crates:rnix",
|
"//third_party/crates:rnix",
|
||||||
|
"//third_party/crates/rowan-0.16.1:rowan",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ path = "xatu_main.rs"
|
|||||||
clap = { workspace = true, features = ["derive"] }
|
clap = { workspace = true, features = ["derive"] }
|
||||||
monorepo = { workspace = true }
|
monorepo = { workspace = true }
|
||||||
rnix = { workspace = true }
|
rnix = { workspace = true }
|
||||||
|
rowan = { workspace = true }
|
||||||
status = { workspace = true }
|
status = { workspace = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
@@ -3,10 +3,17 @@ use crate::FileId;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ParsedFile {
|
pub struct ParsedFile {
|
||||||
pub file_id: FileId,
|
pub file_id: FileId,
|
||||||
|
pub nodes: Vec<Node>,
|
||||||
pub tokens: Vec<Token>,
|
pub tokens: Vec<Token>,
|
||||||
pub errors: Vec<ParseError>,
|
pub errors: Vec<ParseError>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct Node {
|
||||||
|
pub range: SourceRange,
|
||||||
|
pub data: NodeData,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct SourceRange {
|
pub struct SourceRange {
|
||||||
pub start: usize,
|
pub start: usize,
|
||||||
@@ -19,6 +26,45 @@ impl std::fmt::Display for SourceRange {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum NodeData {
|
||||||
|
Apply(Apply),
|
||||||
|
LetIn(LetIn),
|
||||||
|
Inherit(Inherit),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct Apply {
|
||||||
|
pub function: Expr,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum Expr {
|
||||||
|
Ident(Ident),
|
||||||
|
Select(Box<Select>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct Ident {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct Select {
|
||||||
|
pub receiver: Expr,
|
||||||
|
pub attrs: Vec<Ident>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct LetIn {
|
||||||
|
pub binding_count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct Inherit {
|
||||||
|
pub attr_count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Token {
|
pub struct Token {
|
||||||
pub range: SourceRange,
|
pub range: SourceRange,
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
|
use rnix::ast::HasEntry;
|
||||||
|
use rowan::ast::AstNode;
|
||||||
|
|
||||||
use crate::{FileId, ast};
|
use crate::{FileId, ast};
|
||||||
|
|
||||||
pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
|
pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
|
||||||
let parse = rnix::Root::parse(source);
|
let parse = rnix::Root::parse(source);
|
||||||
let source_len = source.len();
|
let source_len = source.len();
|
||||||
|
|
||||||
|
let (nodes, tokens) = convert_syntax(parse.syntax());
|
||||||
|
|
||||||
ast::ParsedFile {
|
ast::ParsedFile {
|
||||||
file_id,
|
file_id,
|
||||||
tokens: convert_tokens(parse.syntax()),
|
nodes,
|
||||||
|
tokens,
|
||||||
errors: parse
|
errors: parse
|
||||||
.errors()
|
.errors()
|
||||||
.iter()
|
.iter()
|
||||||
@@ -15,14 +21,125 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_tokens(syntax: rnix::SyntaxNode) -> Vec<ast::Token> {
|
fn convert_syntax(syntax: rnix::SyntaxNode) -> (Vec<ast::Node>, Vec<ast::Token>) {
|
||||||
syntax
|
let mut nodes = Vec::new();
|
||||||
.descendants_with_tokens()
|
let mut tokens = Vec::new();
|
||||||
.filter_map(|element| match element {
|
|
||||||
rnix::NodeOrToken::Token(token) => convert_token(token),
|
for element in syntax.descendants_with_tokens() {
|
||||||
_ => None,
|
match element {
|
||||||
|
rnix::NodeOrToken::Node(node) => nodes.extend(convert_node(node)),
|
||||||
|
rnix::NodeOrToken::Token(token) => tokens.extend(convert_token(token)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(nodes, tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||||
|
match node.kind() {
|
||||||
|
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
|
||||||
|
rnix::SyntaxKind::NODE_LET_IN => convert_let_in(node),
|
||||||
|
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||||
|
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<rnix::ast::Apply> for ast::Apply {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(apply: rnix::ast::Apply) -> Result<Self, Self::Error> {
|
||||||
|
Ok(Self {
|
||||||
|
function: apply.lambda().ok_or(())?.try_into()?,
|
||||||
})
|
})
|
||||||
.collect()
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<rnix::ast::Expr> for ast::Expr {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(expr: rnix::ast::Expr) -> Result<Self, Self::Error> {
|
||||||
|
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<rnix::ast::Ident> for ast::Ident {
|
||||||
|
fn from(ident: rnix::ast::Ident) -> Self {
|
||||||
|
Self {
|
||||||
|
name: ident.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<rnix::ast::Select> for ast::Select {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(select: rnix::ast::Select) -> Result<Self, Self::Error> {
|
||||||
|
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::<Result<Vec<_>, _>>()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_let_in(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||||
|
let let_in = rnix::ast::LetIn::cast(node)?;
|
||||||
|
|
||||||
|
Some(ast::Node {
|
||||||
|
range: let_in.syntax().text_range().into(),
|
||||||
|
data: ast::NodeData::LetIn(let_in.into()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<rnix::ast::LetIn> for ast::LetIn {
|
||||||
|
fn from(let_in: rnix::ast::LetIn) -> Self {
|
||||||
|
Self {
|
||||||
|
binding_count: let_in.attrpath_values().count()
|
||||||
|
+ let_in
|
||||||
|
.inherits()
|
||||||
|
.map(|inherit| inherit.attrs().count())
|
||||||
|
.sum::<usize>(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||||
|
let inherit = rnix::ast::Inherit::cast(node)?;
|
||||||
|
|
||||||
|
Some(ast::Node {
|
||||||
|
range: inherit.syntax().text_range().into(),
|
||||||
|
data: ast::NodeData::Inherit(inherit.into()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<rnix::ast::Inherit> for ast::Inherit {
|
||||||
|
fn from(inherit: rnix::ast::Inherit) -> Self {
|
||||||
|
Self {
|
||||||
|
attr_count: inherit.attrs().count(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
|
fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
|
||||||
|
|||||||
@@ -37,10 +37,15 @@ pub trait TokenRule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
||||||
file.tokens
|
let node_diagnostics = file.nodes.iter().flat_map(|_| std::iter::empty());
|
||||||
|
let token_diagnostics = file
|
||||||
|
.tokens
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|token| [run_token_rule::<UnquotedUri>(token)])
|
.flat_map(|token| [run_token_rule::<UnquotedUri>(token)])
|
||||||
.flatten()
|
.flatten();
|
||||||
|
|
||||||
|
node_diagnostics
|
||||||
|
.chain(token_diagnostics)
|
||||||
.map(|finding| Diagnostic {
|
.map(|finding| Diagnostic {
|
||||||
file_id: file.file_id,
|
file_id: file.file_id,
|
||||||
finding,
|
finding,
|
||||||
|
|||||||
Reference in New Issue
Block a user