1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
use std::iter::Peekable;
use super::lexer::{TokenType, Token, Lexer};
use crate::types::elements;
struct Text {
token: Token,
text: String,
}
impl Into<elements::Paragraph> for &Text {
fn into(self) -> elements::Paragraph {
elements::Paragraph::new(self.text.clone())
}
}
struct Heading {
level: u8,
text: Text,
}
impl Into<elements::Heading> for &Heading {
fn into(self) -> elements::Heading {
elements::Heading::new(self.text.text.clone(), self.level)
}
}
struct Noop {}
pub trait AST {
fn convert_to_renderable(&self) -> Box<dyn elements::Renderable>;
}
impl AST for Text {
fn convert_to_renderable(&self) -> Box<dyn elements::Renderable> {
let text: elements::Paragraph = self.into();
return Box::new(text);
}
}
impl AST for Heading {
fn convert_to_renderable(&self) -> Box<dyn elements::Renderable> {
let heading: elements::Heading = self.into();
return Box::new(heading);
}
}
impl AST for Noop {
fn convert_to_renderable(&self) -> Box<dyn elements::Renderable> {
let blank = elements::Paragraph::new(String::new());
return Box::new(blank);
}
}
pub struct Exp {
pub item: Box<dyn AST>,
}
pub struct Node {
pub children: Vec<Exp>,
}
pub struct Parser {
lexer: Lexer,
tokens: Peekable<std::vec::IntoIter<Token>>,
pub tree: Node,
}
impl Parser {
pub fn new(lexer: Lexer) -> Self {
let root = Node { children: vec![] };
let input_lexer = lexer;
let input_tokens = input_lexer.tokens.clone().into_iter().peekable();
Parser { lexer: input_lexer, tokens: input_tokens, tree: root }
}
/* Parse a Text block
* TEXT
*/
fn text(&mut self) -> Text {
match &self.tokens.next() {
Some(token) => {
if token.token_type == TokenType::TEXT {
return Text { token: token.clone(), text: token.value.clone() }
}
panic!("Invalid expression for text!");
},
None => { panic!("Invalid expression for text!") }
}
}
/* Parse a Heading
* HASH heading | HASH text
*/
fn heading(&mut self) -> Heading {
let mut heading_size = 0;
while self.tokens.peek().unwrap().token_type == TokenType::HASH {
heading_size += 1;
self.tokens.next();
};
let heading_text = self.text();
return Heading { level: heading_size, text: heading_text }
}
/* exp
* text | heading
*/
fn exp(&mut self) -> Exp {
let token = self.tokens.peek();
if token.is_none() {
return Exp {item: Box::new(Noop{}) };
}
let token = token.unwrap().clone();
if token.token_type == TokenType::TEXT {
let tree = self.text();
return Exp { item: Box::new(tree) };
}
if token.token_type == TokenType::HASH {
let tree = self.heading();
return Exp { item: Box::new(tree) };
}
panic!("Invalid Exp type!")
}
/* Node
* exp | exp NEWLINE node
*/
fn node(&mut self) -> Node {
let mut node = Node { children: vec![] };
let exp = self.exp();
node.children.push(exp);
loop {
match self.tokens.peek() {
Some(token) => {
if token.token_type == TokenType::NEWLINE {
self.tokens.next();
continue
} else {
node.children.push(self.exp());
}
},
None => { break }
}
}
return node;
}
pub fn parse(&mut self) {
self.tree = self.node();
}
}
|