1use std::io;
2
3use tyd::*;
4
5#[derive(Debug, Clone)]
6pub enum Token {
7 Struct(String),
8 Use(String),
9 Derive(String),
10 Module(String),
11 None,
12}
13
14fn find_use() {}
15
16fn parse_nested_use(s: &String, i: usize, line: &str, depth: usize) -> Line<Token> {
17 let line = line.trim();
18
19 if line.is_empty() {
20 return Line::LineDepth(i, Token::Use(s.to_string()), State::Continue, depth);
21 }
22
23 let s = s.to_string() + line;
24 if ends_with(line, &["}", "},", ",}"]) {
25 let depth_count = rcount_ends_with(line, &["}", "},", ",}"]);
26 if depth > 0 {
27 if depth - (depth_count - 1) == 0 {
28 return Line::LineDepth(
29 i,
30 Token::Use(s.to_string()),
31 State::End,
32 depth - (depth_count - 1),
33 );
34 }
35
36 return Line::LineDepth(
37 i,
38 Token::Use(s.to_string()),
39 State::Continue,
40 depth - depth_count,
41 );
42 } else {
43 return Line::LineDepth(i, Token::Use(s.to_string()), State::End, 0);
44 }
45 } else if ends_with(line, &["{", "{,", ",{"]) {
46 let depth_count = rcount_ends_with(line, &["{", "{,", ",{"]);
47 return Line::LineDepth(
48 i,
49 Token::Use(s.to_string()),
50 State::Continue,
51 depth + depth_count,
52 );
53 } else {
54 return Line::LineDepth(i, Token::Use(s), State::Continue, depth);
55 }
56}
57
58fn parse_derive(s: &String, i: usize, line: &str) -> Line<Token> {
59 Line::Skip
60}
61
62fn parse_nested_module(s: &String, i: usize, line: &str, depth: usize) -> Line<Token> {
63
64 let line = line.trim();
65
66 if line.is_empty() {
67 return Line::LineDepth(i, Token::Module(s.to_string()), State::Continue, depth);
68 }
69
70 let s = s.to_string() + line;
71 if ends_with(line, &["}"]) {
72 let depth_count = rcount_ends_with(line, &["}"]);
73 if depth > 0 {
76 if depth - (depth_count - 1) == 0 {
77 return Line::LineDepth(
78 i,
79 Token::Module(s.to_string()),
80 State::End,
81 depth - (depth_count - 1),
82 );
83 }
84
85 return Line::LineDepth(
86 i,
87 Token::Module(s.to_string()),
88 State::Continue,
89 depth - depth_count,
90 );
91 } else {
92 return Line::LineDepth(i, Token::Module(s.to_string()), State::End, 0);
93 }
94 } else if ends_with(line, &["{", "{,", ",{"]) {
95 return line_starts_with(i, line, depth + 1);
96
97 } else {
107 return line_starts_with(i, line, depth + 1);
108 }
110}
111
112fn parse_struct(i: usize, line: &str) -> Line<Token> {
113 Line::Line(i, Token::Struct(line.to_string()), State::End)
114}
115
116fn line_starts_with(i: usize, line: &str, depth: usize) -> Line<Token> {
117 if starts_with(line, &[":use {", ":use{"]) && line.ends_with('}') {
118 return Line::LineDepth(i, Token::Use(String::from(line)), State::End, 0);
119 } else if starts_with(line, &[":use {", ":use{"]) {
120 return Line::LineDepth(i, Token::Use(String::new()), State::Begin, 0);
121 } else if line.starts_with(":derive") && line.ends_with(')') {
122 return Line::Line(i, Token::Derive(line[7..].trim().to_string()), State::End);
123 } else if line.starts_with(":derive") {
124 return Line::Line(i, Token::Derive(String::from(line)), State::Begin);
125 } else if line.starts_with('%') && line.ends_with('}') {
126 return Line::LineDepth(i, Token::Module(String::from(&line[1..])), State::End, depth);
127 } else if line.starts_with('%') {
128 return Line::LineDepth(i, Token::Module(String::from(&line[1..])), State::Begin, depth);
129 } else {
130 if !line.is_empty() {
131 return parse_struct(i, line);
132 }
133 }
134
135 Line::Skip
136}
137
138fn parse_line(old: Option<&Line<Token>>, i: usize, line: &str) -> Line<Token> {
139 if let Some(last) = old {
143 return match last {
144 Line::Line(_, token, state) => {
145 match state {
146 State::Begin => {}
147 State::Continue => {}
148 State::End => {
149 return line_starts_with(i, line, 0);
150 }
151 _ => {}
152 }
153
154 match token {
155 Token::Derive(v) => parse_derive(v, i, line),
156 _ => Line::Skip,
157 }
158 }
159 Line::LineDepth(_, token, state, depth) => {
160 match state {
161 State::Begin => {}
162 State::Continue => {}
163 State::End => {
164 return line_starts_with(i, line, 0);
165 }
166 _ => {}
167 }
168
169 match token {
170 Token::Use(v) => parse_nested_use(v, i, line, *depth),
171 Token::Module(v) => parse_nested_module(v, i, line, *depth),
172 _ => Line::Skip
173 }
174 }
175 _ => {
176 return line_starts_with(i, line, 0);
177 }
178 }
179 } else {
180 return line_starts_with(i, line, 0);
181 }
182}
183
184fn main() -> io::Result<()> {
185 let raw = std::fs::read("examples/components")?;
194 let data = String::from_utf8_lossy(&raw);
195
196 let default_uses = String::new();
197
198 let mut parser = tyd::parser(&parse_line);
199 for (state, line) in parser.iter(&data) {
200 println!("{:?}", state)
201 }
202
203 Ok(())
204}