From 7d9f9be43469748148da5cdf516cd8b32238e1f5 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 23 Apr 2026 18:58:41 +0800 Subject: 重构AST抽象 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ast.rs | 155 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 113 insertions(+), 42 deletions(-) (limited to 'src/ast.rs') diff --git a/src/ast.rs b/src/ast.rs index 49cfd6f..3f8882d 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -3,93 +3,164 @@ use std::collections::HashMap; pub mod parser; pub mod renderer; +/// 语法树 pub struct MarkdownAST { - pub root: Layer, + /// 块列表 + pub blocks: Vec, } +/// 层级 pub type Level = u8; + +/// 语言(用于代码块) pub type Lang = String; + +/// 标题(用于 Headings) pub type Title = String; + +/// 链接(用于 Link) pub type Url = String; -pub struct Layer { - pub range_row_begin: u32, - pub range_row_end: u32, - pub lines: Vec, +/// 块 +pub struct Block { + /// 块开始行 + pub begin_row: u32, + + /// 块结束行 + pub end_row: u32, + + /// 块数据 + pub data: BlockData, +} + +/// 块数据 +pub enum BlockData { + // 头 + Heading(Level, Line), + + // 列表 + UnorderedList(Vec, UnorderedListPrefix), + OrderedList(Vec, u32), + + /// 普通行 + Lines(Vec), + + /// 表格 + Table(Table), + + /// 代码块 + Code(Fragment, Lang), + + // 引用 + Blockquotes(Vec), + + // 分割线 + HorizontalRule(HorizontalRuleStyle), } +/// 下划线风格 +pub enum HorizontalRuleStyle { + Stars, + Dashes, + Underscores, +} + +/// 无序列表前缀 +pub enum UnorderedListPrefix { + /// 星号 "*" + Star, + + /// 减号 "-" + Dash, + + /// 加号 "+" + Plus, +} + +/// 行 pub struct Line { + /// 行所在位置 pub row: u32, + + /// 行内记录的词 pub tokens: Vec, } +/// 词 pub struct Token { + /// 开始行 pub begin_row: u32, + + /// 开始列 pub begin_col: u16, + + /// 结束行 pub end_row: u32, + + /// 结束列 pub end_col: u16, - pub token: TokenData, + + /// 词数据 + pub data: TokenData, } +/// 词数据 pub enum TokenData { + /// 普通 - 记录片段 Normal(Fragment), - Newline, - Newlayer(Layer), - - // Headings - Heading(Level, Line, Layer), - - // Emphasis - Emphasis(Vec, EmphasisStyle), - - // Lists - UnorderedList(Line, UnorderedListPrefix), - OrderedList(Line, u32), - - // Links - Link(Vec, Url, Option, LinkType), - - // Code - InlineCode(Vec<Fragment>), - CodeBlock(Vec<Line>, Option<Lang>), - // Blockquotes - Blockquotes(Layer), + // 强调 - 记录词列表(嵌套)和强调样式 + Emphasis(Vec<Token>, EmphasisFormat), - // HorizontalRule - HorizontalRule(HorizontalRuleType), + // 链接 - 记录词列表(嵌套)、链接、标题、链接类型 + Link(Vec<Token>, Url, Option<Title>, LinkType), - // Table - Table(Table), + // 内联代码 - 记录词列表(嵌套) + InlineCode(Vec<Token>), } +/// 片段 +#[derive(Debug, Default)] pub struct Fragment { + /// 片段文本 pub str: String, } -pub struct EmphasisStyle { +/// 强调格式 +pub struct EmphasisFormat { + /// 强调样式 + pub style: EmphasisStyle, + + /// 是否为粗体 pub bold: bool, + + /// 是否为斜体 pub italic: bool, - pub strikethrough: bool, } -pub enum UnorderedListPrefix { +/// 强调样式 +pub enum EmphasisStyle { + /// 星号 "*" Star, - Dash, - Plus, + + /// 下划线 "_" + Underline, } +/// 链接类型 pub enum LinkType { + /// 是否为图像链接 Image, + + /// 是否为网页链接 Url, -} -pub enum HorizontalRuleType { - Stars, - Dashes, - Underscores, + /// 是否为段落链接 + Section, } +/// 表格 pub struct Table { - pub contents: HashMap<(u32, u32), Vec<Fragment>>, + /// 位置 映射 词列表 + pub contents: HashMap<(u32, u32), Vec<Token>>, } -- cgit