Files
marqdo-lint/src/alignment/mod.rs
T

124 lines
2.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
mod semantic;
mod source;
use crate::model::{
statements, AnalysisFunction, Carrier, MarkdownTree, NodeId, Span, StatementKind,
};
pub use crate::model::node_span;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ControlKind {
Branch,
Loop,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArmRole {
If,
ElseIf,
Else,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BodyOwner {
pub control: usize,
pub arm: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct BranchArm {
pub role: ArmRole,
pub carrier: Carrier,
pub analysis_index: Option<usize>,
pub(crate) body: Vec<BodyStatement>,
}
#[derive(Debug, Clone)]
pub(crate) struct BodyStatement {
pub kind: StatementKind,
pub carrier: Carrier,
}
#[derive(Debug, Clone)]
pub struct ControlLayout {
pub kind: ControlKind,
pub function: NodeId,
pub heading: NodeId,
pub opener: Carrier,
pub arms: Vec<BranchArm>,
pub end: Option<Carrier>,
pub parent: Option<BodyOwner>,
pub children: Vec<usize>,
pub semantic_span: Option<Span>,
pub(crate) body: Vec<BodyStatement>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JumpKind {
Break,
Continue,
}
#[derive(Debug, Clone)]
pub struct JumpLayout {
pub kind: JumpKind,
pub carrier: Carrier,
pub parent: BodyOwner,
pub target_loop: usize,
}
#[derive(Debug, Clone)]
pub struct ReadableControlTree {
pub function: Option<NodeId>,
pub function_name: String,
pub controls: Vec<ControlLayout>,
pub jumps: Vec<JumpLayout>,
pub complete: bool,
pub issues: Vec<String>,
}
impl ReadableControlTree {
pub fn control(&self, index: usize) -> &ControlLayout {
&self.controls[index]
}
}
pub fn align_function(markdown: &MarkdownTree, analysis: &AnalysisFunction) -> ReadableControlTree {
let mut tree = ReadableControlTree {
function: None,
function_name: analysis.local_name.clone(),
controls: Vec::new(),
jumps: Vec::new(),
complete: false,
issues: Vec::new(),
};
let function = match markdown.function_heading(&analysis.id) {
Some(node) => node,
None => {
tree.issues.push(at(
analysis.heading_span,
"Analysis function_id 无法对应 Exec H2",
));
return tree;
}
};
tree.function = Some(function);
let source = source::scan(markdown, function, &mut tree);
if tree.issues.is_empty() {
if let Err(issue) = semantic::align(&analysis.body, &source, &mut tree) {
tree.issues.push(issue);
}
}
tree.complete = tree.issues.is_empty();
tree
}
fn at(span: Span, issue: &str) -> String {
format!("字节 [{}, {}):{issue}", span.start, span.end)
}
pub fn statement_count(analysis: &AnalysisFunction) -> usize {
statements(&analysis.body).count()
}