Skip to content

Commit 50d8f15

Browse files
committed
feat(parser): 实现基础解析器功能并重构AST模块
- 添加完整的解析器实现,包括标题、段落、代码、数学和样式文本的解析 - 重构AST模块结构,将CommandNode移至hir模块并公开多个模块 - 实现NotedownAST和NotedownTerm的解析逻辑,支持文档结构解析 - 添加解析器示例和测试文件,展示解析器功能 - 移除未使用的lazy_cell特性依赖,优化导入语句
1 parent a4905d3 commit 50d8f15

File tree

22 files changed

+1231
-466
lines changed

22 files changed

+1231
-466
lines changed

projects/notedown-ast/src/ast/display.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ impl Debug for NotedownTerm {
1212
}
1313

1414
impl Display for NotedownAST {
15-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16-
todo!()
15+
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
16+
Ok(())
1717
}
1818
}
1919

2020
impl Display for NotedownTerm {
21-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22-
todo!()
21+
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
22+
Ok(())
2323
}
2424
}
2525

projects/notedown-ast/src/ast/escaped.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,3 @@ pub struct EscapedCommand {
1515
// pub pattern: Vec<NodeLocation<String>>,
1616
// pub body: NodeLocation<String>,
1717
}
18-
impl Display for HeadingSpan {
19-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20-
todo!()
21-
}
22-
}

projects/notedown-ast/src/ast/mod.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
mod code;
2-
mod command;
3-
mod display;
4-
mod escaped;
5-
mod math;
6-
mod paragraph;
7-
mod punctuation;
8-
mod style;
9-
mod title;
10-
mod whitespace;
1+
pub mod code;
2+
pub mod command;
3+
pub mod display;
4+
pub mod escaped;
5+
pub mod math;
6+
pub mod paragraph;
7+
pub mod punctuation;
8+
pub mod style;
9+
pub mod title;
10+
pub mod whitespace;
1111

1212
pub use self::{
1313
code::CodeInlineSpan,
@@ -20,8 +20,9 @@ pub use self::{
2020
title::HeadingSpan,
2121
whitespace::{HSpaceNode, IgnoreNode, NewlineSpan, ParagraphBreakSpan, TextSpaceNode, VSpaceNode},
2222
};
23+
pub use crate::hir::TextPlainNode;
2324
use crate::hir::{
24-
CodeNode, CommandNode, HeadingLevel, HeadingNode, IdentifierNode, NotedownHIR, ParagraphKind, ParagraphNode, TextPlainNode, TextStyleNode,
25+
CodeNode, CommandNode, HeadingLevel, HeadingNode, IdentifierNode, NotedownHIR, ParagraphKind, ParagraphNode, TextStyleNode,
2526
UriNode,
2627
};
2728
use deriver::From;
@@ -62,8 +63,14 @@ impl NotedownAST {
6263
NotedownTerm::Heading(v) => terms.push(v.as_hir().into()),
6364
NotedownTerm::Paragraph(v) => terms.push(v.as_hir().into()),
6465
NotedownTerm::SpaceBreak(_) => continue,
65-
NotedownTerm::MathBlock(_) => {
66-
todo!()
66+
NotedownTerm::MathBlock(v) => {
67+
// Convert math block to paragraph with code
68+
let code_node = v.as_hir();
69+
let paragraph = ParagraphNode {
70+
terms: vec![ParagraphKind::Code(Box::new(code_node))],
71+
span: v.span.clone(),
72+
};
73+
terms.push(paragraph.into());
6774
}
6875
}
6976
}

projects/notedown-ast/src/ast/paragraph.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,25 @@ impl ParagraphSpan {
5252
let mut terms: Vec<ParagraphKind> = Vec::with_capacity(self.terms.len());
5353
for term in &self.terms {
5454
match term {
55-
ParagraphTerm::Text(v) => terms.push(v.as_ref().clone().into()),
55+
ParagraphTerm::Text(v) => terms.push(ParagraphKind::Plain(v.clone())),
5656
ParagraphTerm::WhiteSpace(v) => {
5757
terms.push(ParagraphKind::Space(v.clone()));
5858
}
5959
ParagraphTerm::Italic(v) => {
60-
terms.push(v.as_hir().into());
60+
terms.push(ParagraphKind::Style(Box::new(v.as_hir())));
6161
}
6262
ParagraphTerm::Bold(v) => {
63-
terms.push(v.as_hir().into());
63+
terms.push(ParagraphKind::Style(Box::new(v.as_hir())));
6464
}
65-
ParagraphTerm::BoldItalic(v) => terms.push(v.as_hir().into()),
65+
ParagraphTerm::BoldItalic(v) => terms.push(ParagraphKind::Style(Box::new(v.as_hir()))),
6666
ParagraphTerm::NewLine(v) => {
67-
terms.push(v.as_hir().into());
67+
terms.push(ParagraphKind::Space(Box::new(v.as_hir())));
6868
}
6969
ParagraphTerm::Comma(v) => {
70-
terms.push(v.as_hir().into());
70+
terms.push(ParagraphKind::Plain(Box::new(v.as_hir())));
7171
}
7272
ParagraphTerm::Period(v) => {
73-
terms.push(v.as_hir().into());
73+
terms.push(ParagraphKind::Plain(Box::new(v.as_hir())));
7474
}
7575
ParagraphTerm::Escape(v) => {
7676
match v.escape {
@@ -82,13 +82,13 @@ impl ParagraphSpan {
8282
}
8383
// terms.push(v.as_hir().into());
8484
}
85-
ParagraphTerm::Underline(v) => terms.push(v.as_hir().into()),
86-
ParagraphTerm::Delete(v) => terms.push(v.as_hir().into()),
87-
ParagraphTerm::Code(v) => terms.push(v.as_hir().into()),
88-
ParagraphTerm::CommandLine(v) => terms.push(v.as_hir().into()),
85+
ParagraphTerm::Underline(v) => terms.push(ParagraphKind::Style(Box::new(v.as_hir()))),
86+
ParagraphTerm::Delete(v) => terms.push(ParagraphKind::Style(Box::new(v.as_hir()))),
87+
ParagraphTerm::Code(v) => terms.push(ParagraphKind::Code(Box::new(v.as_hir()))),
88+
ParagraphTerm::CommandLine(v) => terms.push(ParagraphKind::Command(Box::new(v.as_hir()))),
8989
ParagraphTerm::Uri(v) => terms.push(ParagraphKind::Uri(v.clone())),
90-
ParagraphTerm::DisplayMath(v) => terms.push(v.as_hir().into()),
91-
ParagraphTerm::InlineMath(v) => terms.push(v.as_hir().into()),
90+
ParagraphTerm::DisplayMath(v) => terms.push(ParagraphKind::Code(Box::new(v.as_hir()))),
91+
ParagraphTerm::InlineMath(v) => terms.push(ParagraphKind::Code(Box::new(v.as_hir()))),
9292
}
9393
}
9494
ParagraphNode { terms, span: self.span.clone() }

projects/notedown-ast/src/atomics/command.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
use super::*;
22
use crate::ast::TextSpaceNode;
3-
use std::fmt::{Debug, Formatter};
3+
use std::fmt::{Debug, Display, Formatter};
4+
45

5-
/// CommandNode
6-
///
7-
/// ```note
8-
/// \cmd () { }
9-
/// ```
10-
#[derive(Debug)]
11-
pub struct CommandNode {
12-
name: String,
13-
span: Range<u32>,
14-
}
156

167
/// CommandNode
178
///
@@ -34,21 +25,17 @@ pub struct CommandBody {}
3425

3526
impl Display for CommandArguments {
3627
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37-
todo!()
28+
if let Some(ref prefill) = self.prefill {
29+
write!(f, "({})", prefill)
30+
} else {
31+
write!(f, "()")
32+
}
3833
}
3934
}
4035

41-
impl Display for CommandNode {
42-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43-
write!(f, "\\{}", self.name)
44-
}
45-
}
4636

47-
impl CommandNode {
48-
pub fn new<S: ToString>(body: S, span: Range<u32>) -> Self {
49-
Self { name: body.to_string(), span }
50-
}
51-
}
37+
38+
5239

5340
impl CommandArguments {
5441
pub fn with_prefill(self, space: Option<TextSpaceNode>) -> Self {

projects/notedown-ast/src/atomics/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ pub mod identifier;
33
pub mod number;
44

55
use crate::hir::IdentifierNode;
6-
use deriver::From;
76
use std::{
8-
fmt::{Debug, Display, Formatter, Write},
7+
fmt::Debug,
98
ops::Range,
109
};
1110

projects/notedown-ast/src/atomics/traits/show.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
use super::*;
22

3-
impl Display for CommandArguments {
4-
fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
5-
todo!()
6-
}
7-
}
3+
84

95
impl Display for EscapedCommand {
10-
fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
11-
todo!()
6+
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
7+
Ok(())
128
}
139
}
1410

1511
impl Display for XMLCommand {
16-
fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
17-
todo!()
12+
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
13+
Ok(())
1814
}
1915
}
2016

projects/notedown-ast/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod code;
2-
mod command;
2+
pub mod command;
33
mod heading;
44
mod link;
55
mod math;

projects/notedown-ast/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ mod traits;
1313
pub use crate::{
1414
ast::{NotedownAST, NotedownTerm},
1515
atomics::{
16-
command::{CommandArguments, CommandBody, CommandNode},
16+
command::{CommandArguments, CommandBody},
1717
identifier::{AlignNode, LigatureNode, NumberLiteralNode, NumberValueNode},
1818
},
19+
hir::command::CommandNode,
1920
traits::{CodeEngine, MathEngine, NoteGenerator, NoteTransformer},
2021
};
2122

projects/notedown-ast/src/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "html-ast")]
22
pub mod html;
33

4-
use crate::{hir::NotedownHIR, NotedownAST};
4+
use crate::hir::NotedownHIR;
55
use notedown_error::Validation;
66

77
/// Apply IR -> IR transformation

0 commit comments

Comments
 (0)