@devaloop/devalang 0.0.1-alpha.1 → 0.0.1-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.devalang +4 -0
- package/Cargo.toml +46 -45
- package/README.md +35 -10
- package/docs/CHANGELOG.md +58 -0
- package/docs/COMMANDS.md +29 -6
- package/docs/CONFIG.md +28 -0
- package/docs/ROADMAP.md +6 -2
- package/docs/TODO.md +21 -18
- package/examples/exported.deva +1 -1
- package/examples/index.deva +2 -1
- package/out-tsc/bin/devalang.exe +0 -0
- package/package.json +2 -3
- package/project-version.json +4 -4
- package/rust/cli/build.rs +99 -29
- package/rust/cli/check.rs +95 -103
- package/rust/cli/init.rs +77 -0
- package/rust/cli/mod.rs +174 -1
- package/rust/cli/template.rs +56 -0
- package/rust/config/loader.rs +14 -0
- package/rust/config/mod.rs +15 -0
- package/rust/core/builder/mod.rs +21 -27
- package/rust/core/debugger/lexer.rs +12 -0
- package/rust/core/debugger/mod.rs +12 -49
- package/rust/core/debugger/preprocessor.rs +23 -0
- package/rust/core/error/mod.rs +60 -0
- package/rust/core/lexer/handler/at.rs +21 -0
- package/rust/core/lexer/handler/brace.rs +41 -0
- package/rust/core/lexer/handler/colon.rs +21 -0
- package/rust/core/lexer/handler/comment.rs +30 -0
- package/rust/core/lexer/handler/dot.rs +21 -0
- package/rust/core/lexer/handler/equal.rs +32 -0
- package/rust/core/lexer/handler/identifier.rs +38 -0
- package/rust/core/lexer/handler/indent.rs +52 -0
- package/rust/core/lexer/handler/mod.rs +238 -0
- package/rust/core/lexer/handler/newline.rs +19 -0
- package/rust/core/lexer/handler/number.rs +31 -0
- package/rust/core/lexer/handler/string.rs +66 -0
- package/rust/core/lexer/mod.rs +16 -324
- package/rust/core/lexer/token.rs +55 -0
- package/rust/core/mod.rs +5 -2
- package/rust/core/parser/handler/at.rs +166 -0
- package/rust/core/parser/handler/bank.rs +38 -0
- package/rust/core/parser/handler/dot.rs +112 -0
- package/rust/core/parser/handler/identifier.rs +134 -0
- package/rust/core/parser/handler/loop_.rs +55 -0
- package/rust/core/parser/handler/mod.rs +6 -0
- package/rust/core/parser/handler/tempo.rs +47 -0
- package/rust/core/parser/mod.rs +204 -166
- package/rust/core/parser/statement.rs +91 -0
- package/rust/core/preprocessor/loader.rs +105 -0
- package/rust/core/preprocessor/mod.rs +2 -24
- package/rust/core/preprocessor/module.rs +37 -56
- package/rust/core/preprocessor/processor.rs +41 -0
- package/rust/core/preprocessor/resolver.rs +372 -0
- package/rust/core/shared/duration.rs +8 -0
- package/rust/core/shared/mod.rs +2 -0
- package/rust/core/shared/value.rs +18 -0
- package/rust/core/store/export.rs +28 -0
- package/rust/core/store/global.rs +39 -0
- package/rust/core/store/import.rs +28 -0
- package/rust/core/store/mod.rs +4 -0
- package/rust/core/store/variable.rs +28 -0
- package/rust/core/utils/mod.rs +2 -0
- package/rust/core/utils/validation.rs +35 -0
- package/rust/lib.rs +0 -1
- package/rust/main.rs +39 -30
- package/rust/utils/file.rs +35 -0
- package/rust/utils/logger.rs +69 -34
- package/rust/utils/mod.rs +3 -2
- package/rust/utils/watcher.rs +25 -0
- package/templates/minimal/.devalang +4 -0
- package/templates/minimal/src/index.deva +2 -0
- package/templates/welcome/.devalang +4 -0
- package/templates/welcome/README.md +185 -0
- package/templates/welcome/samples/kick-808.wav +0 -0
- package/templates/welcome/src/index.deva +13 -0
- package/templates/welcome/src/variables.deva +5 -0
- package/rust/audio/mod.rs +0 -1
- package/rust/cli/new.rs +0 -1
- package/rust/core/parser/at.rs +0 -142
- package/rust/core/parser/bank.rs +0 -42
- package/rust/core/parser/dot.rs +0 -107
- package/rust/core/parser/identifer.rs +0 -91
- package/rust/core/parser/loop_.rs +0 -62
- package/rust/core/parser/tempo.rs +0 -42
- package/rust/core/parser/variable.rs +0 -129
- package/rust/core/preprocessor/dependencies.rs +0 -54
- package/rust/core/preprocessor/resolver/at.rs +0 -24
- package/rust/core/preprocessor/resolver/bank.rs +0 -59
- package/rust/core/preprocessor/resolver/loop_.rs +0 -82
- package/rust/core/preprocessor/resolver/mod.rs +0 -113
- package/rust/core/preprocessor/resolver/tempo.rs +0 -70
- package/rust/core/preprocessor/resolver/trigger.rs +0 -176
- package/rust/core/types/cli.rs +0 -160
- package/rust/core/types/mod.rs +0 -7
- package/rust/core/types/module.rs +0 -41
- package/rust/core/types/parser.rs +0 -73
- package/rust/core/types/statement.rs +0 -105
- package/rust/core/types/store.rs +0 -116
- package/rust/core/types/token.rs +0 -83
- package/rust/core/types/variable.rs +0 -32
- package/rust/runner/executer.rs +0 -44
- package/rust/runner/mod.rs +0 -1
- /package/rust/{utils → core/utils}/path.rs +0 -0
- /package/rust/utils/{loader.rs → spinner.rs} +0 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
use crate::core::lexer::token::{ Token, TokenKind };
|
|
2
|
+
|
|
3
|
+
pub fn handle_number_lexer(
|
|
4
|
+
char: char,
|
|
5
|
+
chars: &mut std::iter::Peekable<std::str::Chars>,
|
|
6
|
+
current_indent: &mut usize,
|
|
7
|
+
indent_stack: &mut Vec<usize>,
|
|
8
|
+
tokens: &mut Vec<Token>,
|
|
9
|
+
line: &mut usize,
|
|
10
|
+
column: &mut usize
|
|
11
|
+
) {
|
|
12
|
+
let mut number = char.to_string();
|
|
13
|
+
|
|
14
|
+
while let Some(&c) = chars.peek() {
|
|
15
|
+
if c.is_ascii_digit() {
|
|
16
|
+
number.push(c);
|
|
17
|
+
chars.next();
|
|
18
|
+
*column += 1;
|
|
19
|
+
} else {
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
tokens.push(Token {
|
|
25
|
+
kind: TokenKind::Number,
|
|
26
|
+
lexeme: number,
|
|
27
|
+
line: *line,
|
|
28
|
+
column: *column,
|
|
29
|
+
indent: *current_indent,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
use crate::core::lexer::token::{Token, TokenKind};
|
|
2
|
+
|
|
3
|
+
pub fn handle_string_lexer(
|
|
4
|
+
ch: char,
|
|
5
|
+
chars: &mut std::iter::Peekable<std::str::Chars>,
|
|
6
|
+
current_indent: &mut usize,
|
|
7
|
+
indent_stack: &mut Vec<usize>,
|
|
8
|
+
tokens: &mut Vec<Token>,
|
|
9
|
+
line: &mut usize,
|
|
10
|
+
column: &mut usize
|
|
11
|
+
) {
|
|
12
|
+
let quote_char = ch;
|
|
13
|
+
|
|
14
|
+
// Position de départ du token
|
|
15
|
+
let start_column = *column;
|
|
16
|
+
let start_line = *line;
|
|
17
|
+
|
|
18
|
+
// Consommer le guillemet ouvrant
|
|
19
|
+
chars.next();
|
|
20
|
+
*column += 1;
|
|
21
|
+
|
|
22
|
+
let mut string_content = String::new();
|
|
23
|
+
|
|
24
|
+
while let Some(&next_ch) = chars.peek() {
|
|
25
|
+
if next_ch == quote_char {
|
|
26
|
+
chars.next(); // Consomme le guillemet fermant
|
|
27
|
+
*column += 1;
|
|
28
|
+
break;
|
|
29
|
+
} else if next_ch == '\\' {
|
|
30
|
+
chars.next(); // Consomme '\'
|
|
31
|
+
*column += 1;
|
|
32
|
+
|
|
33
|
+
if let Some(escaped) = chars.next() {
|
|
34
|
+
match escaped {
|
|
35
|
+
'n' => string_content.push('\n'),
|
|
36
|
+
't' => string_content.push('\t'),
|
|
37
|
+
'\\' => string_content.push('\\'),
|
|
38
|
+
'"' => string_content.push('"'),
|
|
39
|
+
'\'' => string_content.push('\''),
|
|
40
|
+
other => {
|
|
41
|
+
string_content.push('\\');
|
|
42
|
+
string_content.push(other);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
*column += 1;
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
chars.next();
|
|
49
|
+
if next_ch == '\n' {
|
|
50
|
+
*line += 1;
|
|
51
|
+
*column = 1;
|
|
52
|
+
} else {
|
|
53
|
+
*column += 1;
|
|
54
|
+
}
|
|
55
|
+
string_content.push(next_ch);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
tokens.push(Token {
|
|
60
|
+
kind: TokenKind::String,
|
|
61
|
+
lexeme: string_content,
|
|
62
|
+
indent: *current_indent,
|
|
63
|
+
line: start_line,
|
|
64
|
+
column: start_column,
|
|
65
|
+
});
|
|
66
|
+
}
|
package/rust/core/lexer/mod.rs
CHANGED
|
@@ -1,333 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
pub mod handler;
|
|
2
|
+
pub mod token;
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
use std::fs;
|
|
5
|
+
use crate::core::{ lexer::{ handler::handle_content_lexing, token::Token }, utils::path::normalize_path };
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
let mut column = 1;
|
|
7
|
+
pub struct Lexer {}
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let mut chars = input.chars().peekable();
|
|
14
|
-
|
|
15
|
-
while let Some(_) = chars.peek() {
|
|
16
|
-
if at_line_start {
|
|
17
|
-
current_indent = 0;
|
|
18
|
-
|
|
19
|
-
while let Some(&c) = chars.peek() {
|
|
20
|
-
if c == ' ' {
|
|
21
|
-
current_indent += 1;
|
|
22
|
-
chars.next();
|
|
23
|
-
column += 1;
|
|
24
|
-
} else {
|
|
25
|
-
break;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
let last_indent = *indent_stack.last().unwrap();
|
|
30
|
-
if current_indent > last_indent {
|
|
31
|
-
indent_stack.push(current_indent);
|
|
32
|
-
tokens.push(Token {
|
|
33
|
-
kind: TokenKind::Indent,
|
|
34
|
-
lexeme: String::new(),
|
|
35
|
-
line,
|
|
36
|
-
column,
|
|
37
|
-
indent: current_indent,
|
|
38
|
-
});
|
|
39
|
-
} else {
|
|
40
|
-
while current_indent < *indent_stack.last().unwrap() {
|
|
41
|
-
indent_stack.pop();
|
|
42
|
-
tokens.push(Token {
|
|
43
|
-
kind: TokenKind::Dedent,
|
|
44
|
-
lexeme: String::new(),
|
|
45
|
-
line,
|
|
46
|
-
column,
|
|
47
|
-
indent: current_indent,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
at_line_start = false;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
let Some(ch) = chars.next() else {
|
|
56
|
-
break;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
if ch == '\n' {
|
|
60
|
-
tokens.push(Token {
|
|
61
|
-
kind: TokenKind::Newline,
|
|
62
|
-
lexeme: ch.to_string(),
|
|
63
|
-
line,
|
|
64
|
-
column,
|
|
65
|
-
indent: current_indent,
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
line += 1;
|
|
69
|
-
column = 1;
|
|
70
|
-
at_line_start = true;
|
|
71
|
-
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if ch == ' ' || ch == '\t' {
|
|
76
|
-
column += if ch == '\t' { 4 } else { 1 };
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if ch == '#' {
|
|
81
|
-
let mut comment = String::new();
|
|
82
|
-
while let Some(&c) = chars.peek() {
|
|
83
|
-
if c == '\n' {
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
comment.push(c);
|
|
87
|
-
chars.next();
|
|
88
|
-
column += 1;
|
|
89
|
-
}
|
|
90
|
-
tokens.push(Token {
|
|
91
|
-
kind: TokenKind::Comment(comment.trim().to_string()),
|
|
92
|
-
lexeme: ch.to_string(),
|
|
93
|
-
line,
|
|
94
|
-
column,
|
|
95
|
-
indent: current_indent,
|
|
96
|
-
});
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if ch == ':' {
|
|
101
|
-
tokens.push(Token {
|
|
102
|
-
kind: TokenKind::Colon,
|
|
103
|
-
lexeme: ch.to_string(),
|
|
104
|
-
line,
|
|
105
|
-
column,
|
|
106
|
-
indent: current_indent,
|
|
107
|
-
});
|
|
108
|
-
column += 1;
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if ch == '=' {
|
|
113
|
-
if let Some('=') = chars.peek() {
|
|
114
|
-
chars.next();
|
|
115
|
-
tokens.push(Token {
|
|
116
|
-
kind: TokenKind::DoubleEquals,
|
|
117
|
-
lexeme: ch.to_string(),
|
|
118
|
-
line,
|
|
119
|
-
column,
|
|
120
|
-
indent: current_indent,
|
|
121
|
-
});
|
|
122
|
-
column += 2;
|
|
123
|
-
} else {
|
|
124
|
-
tokens.push(Token {
|
|
125
|
-
kind: TokenKind::Equals,
|
|
126
|
-
lexeme: ch.to_string(),
|
|
127
|
-
line,
|
|
128
|
-
column,
|
|
129
|
-
indent: current_indent,
|
|
130
|
-
});
|
|
131
|
-
column += 1;
|
|
132
|
-
}
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if ch == '[' {
|
|
137
|
-
tokens.push(Token {
|
|
138
|
-
kind: TokenKind::LBracket,
|
|
139
|
-
lexeme: ch.to_string(),
|
|
140
|
-
line,
|
|
141
|
-
column,
|
|
142
|
-
indent: current_indent,
|
|
143
|
-
});
|
|
144
|
-
column += 1;
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if ch == ']' {
|
|
149
|
-
tokens.push(Token {
|
|
150
|
-
kind: TokenKind::RBracket,
|
|
151
|
-
lexeme: ch.to_string(),
|
|
152
|
-
line,
|
|
153
|
-
column,
|
|
154
|
-
indent: current_indent,
|
|
155
|
-
});
|
|
156
|
-
column += 1;
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if ch == '{' {
|
|
161
|
-
tokens.push(Token {
|
|
162
|
-
kind: TokenKind::LBrace,
|
|
163
|
-
lexeme: ch.to_string(),
|
|
164
|
-
line,
|
|
165
|
-
column,
|
|
166
|
-
indent: current_indent,
|
|
167
|
-
});
|
|
168
|
-
column += 1;
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if ch == '}' {
|
|
173
|
-
tokens.push(Token {
|
|
174
|
-
kind: TokenKind::RBrace,
|
|
175
|
-
lexeme: ch.to_string(),
|
|
176
|
-
line,
|
|
177
|
-
column,
|
|
178
|
-
indent: current_indent,
|
|
179
|
-
});
|
|
180
|
-
column += 1;
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if ch == '"' {
|
|
185
|
-
let mut string_content = String::new();
|
|
186
|
-
column += 1; // skip the opening quote
|
|
187
|
-
|
|
188
|
-
while let Some(next_ch) = chars.next() {
|
|
189
|
-
column += 1;
|
|
190
|
-
if next_ch == '"' {
|
|
191
|
-
break; // closing quote reached
|
|
192
|
-
} else {
|
|
193
|
-
string_content.push(next_ch);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
tokens.push(Token {
|
|
198
|
-
kind: TokenKind::String,
|
|
199
|
-
lexeme: string_content,
|
|
200
|
-
line,
|
|
201
|
-
column,
|
|
202
|
-
indent: current_indent,
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if ch == '\'' {
|
|
209
|
-
let mut string_content = String::new();
|
|
210
|
-
column += 1;
|
|
211
|
-
|
|
212
|
-
while let Some(next_ch) = chars.next() {
|
|
213
|
-
column += 1;
|
|
214
|
-
if next_ch == '\'' {
|
|
215
|
-
break;
|
|
216
|
-
} else {
|
|
217
|
-
string_content.push(next_ch);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
tokens.push(Token {
|
|
222
|
-
kind: TokenKind::String,
|
|
223
|
-
lexeme: string_content,
|
|
224
|
-
line,
|
|
225
|
-
column,
|
|
226
|
-
indent: current_indent,
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if ch == '.' {
|
|
233
|
-
tokens.push(Token {
|
|
234
|
-
kind: TokenKind::Dot,
|
|
235
|
-
lexeme: ch.to_string(),
|
|
236
|
-
line,
|
|
237
|
-
column,
|
|
238
|
-
indent: current_indent,
|
|
239
|
-
});
|
|
240
|
-
column += 1;
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if ch == '@' {
|
|
245
|
-
tokens.push(Token {
|
|
246
|
-
kind: TokenKind::At,
|
|
247
|
-
lexeme: ch.to_string(),
|
|
248
|
-
line,
|
|
249
|
-
column,
|
|
250
|
-
indent: current_indent,
|
|
251
|
-
});
|
|
252
|
-
column += 1;
|
|
253
|
-
continue;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if ch.is_ascii_digit() {
|
|
257
|
-
let mut number = ch.to_string();
|
|
258
|
-
while let Some(&c) = chars.peek() {
|
|
259
|
-
if c.is_ascii_digit() {
|
|
260
|
-
number.push(c);
|
|
261
|
-
chars.next();
|
|
262
|
-
column += 1;
|
|
263
|
-
} else {
|
|
264
|
-
break;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// let value = number.parse::<f32>().unwrap();
|
|
269
|
-
tokens.push(Token {
|
|
270
|
-
kind: TokenKind::Number,
|
|
271
|
-
lexeme: number,
|
|
272
|
-
line,
|
|
273
|
-
column,
|
|
274
|
-
indent: current_indent,
|
|
275
|
-
});
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
if ch.is_ascii_alphabetic() {
|
|
280
|
-
let mut ident = ch.to_string();
|
|
281
|
-
while let Some(&c) = chars.peek() {
|
|
282
|
-
if c.is_ascii_alphanumeric() || c == '_' {
|
|
283
|
-
ident.push(c);
|
|
284
|
-
chars.next();
|
|
285
|
-
column += 1;
|
|
286
|
-
} else {
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
9
|
+
impl Lexer {
|
|
10
|
+
pub fn new() -> Self {
|
|
11
|
+
Lexer {}
|
|
12
|
+
}
|
|
290
13
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
"bpm" => TokenKind::Tempo,
|
|
294
|
-
"loop" => TokenKind::Loop,
|
|
295
|
-
_ => TokenKind::Identifier,
|
|
296
|
-
};
|
|
14
|
+
pub fn lex_tokens(&self, entrypoint: &str) -> Vec<Token> {
|
|
15
|
+
let path = normalize_path(entrypoint);
|
|
297
16
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
line,
|
|
302
|
-
column,
|
|
303
|
-
indent: current_indent,
|
|
304
|
-
});
|
|
305
|
-
continue;
|
|
306
|
-
}
|
|
17
|
+
let file_content = fs
|
|
18
|
+
::read_to_string(&path)
|
|
19
|
+
.expect("Failed to read the entrypoint file");
|
|
307
20
|
|
|
308
|
-
|
|
309
|
-
column += 1;
|
|
310
|
-
}
|
|
21
|
+
let tokens = handle_content_lexing(file_content);
|
|
311
22
|
|
|
312
|
-
|
|
313
|
-
indent_stack.pop();
|
|
314
|
-
current_indent = *indent_stack.last().unwrap();
|
|
315
|
-
tokens.push(Token {
|
|
316
|
-
kind: TokenKind::Dedent,
|
|
317
|
-
lexeme: String::new(),
|
|
318
|
-
line,
|
|
319
|
-
column,
|
|
320
|
-
indent: current_indent,
|
|
321
|
-
});
|
|
23
|
+
tokens
|
|
322
24
|
}
|
|
323
|
-
|
|
324
|
-
tokens.push(Token {
|
|
325
|
-
kind: TokenKind::EOF,
|
|
326
|
-
lexeme: String::new(),
|
|
327
|
-
line: line + 1, // EOF is considered to be on the next line
|
|
328
|
-
column: 0, // EOF has no column
|
|
329
|
-
indent: 0, // EOF has no indent
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
tokens
|
|
333
25
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
use serde::{ Deserialize, Serialize };
|
|
2
|
+
|
|
3
|
+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
4
|
+
pub struct Token {
|
|
5
|
+
pub kind: TokenKind,
|
|
6
|
+
pub lexeme: String,
|
|
7
|
+
pub indent: usize,
|
|
8
|
+
pub line: usize,
|
|
9
|
+
pub column: usize,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
impl Token {
|
|
13
|
+
pub fn is_error(&self) -> bool {
|
|
14
|
+
match &self.kind {
|
|
15
|
+
TokenKind::Error(_) => {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
_ => {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
26
|
+
pub enum TokenKind {
|
|
27
|
+
At,
|
|
28
|
+
Tempo,
|
|
29
|
+
Bank,
|
|
30
|
+
Loop,
|
|
31
|
+
Identifier,
|
|
32
|
+
Map,
|
|
33
|
+
Array,
|
|
34
|
+
Number,
|
|
35
|
+
String,
|
|
36
|
+
Boolean,
|
|
37
|
+
Colon,
|
|
38
|
+
Comma,
|
|
39
|
+
Equals,
|
|
40
|
+
DoubleEquals,
|
|
41
|
+
Dot,
|
|
42
|
+
LBrace,
|
|
43
|
+
RBrace,
|
|
44
|
+
DbQuote,
|
|
45
|
+
Quote,
|
|
46
|
+
LBracket,
|
|
47
|
+
RBracket,
|
|
48
|
+
Newline,
|
|
49
|
+
Indent,
|
|
50
|
+
Dedent,
|
|
51
|
+
Comment,
|
|
52
|
+
Unknown,
|
|
53
|
+
Error(String),
|
|
54
|
+
EOF,
|
|
55
|
+
}
|
package/rust/core/mod.rs
CHANGED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
use crate::core::{
|
|
2
|
+
lexer::token::{ Token, TokenKind },
|
|
3
|
+
parser::{
|
|
4
|
+
handler::identifier::parse_identifier_token,
|
|
5
|
+
statement::{ Statement, StatementKind },
|
|
6
|
+
Parser,
|
|
7
|
+
},
|
|
8
|
+
shared::value::Value,
|
|
9
|
+
store::global::GlobalStore,
|
|
10
|
+
};
|
|
11
|
+
pub fn parse_at_token(parser: &mut Parser, _global_store: &mut GlobalStore) -> Statement {
|
|
12
|
+
parser.advance(); // consume '@'
|
|
13
|
+
|
|
14
|
+
let Some(token) = parser.peek_clone() else {
|
|
15
|
+
return Statement::unknown();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
let keyword = token.lexeme.as_str();
|
|
19
|
+
|
|
20
|
+
match keyword {
|
|
21
|
+
"import" => {
|
|
22
|
+
parser.advance(); // consume 'import'
|
|
23
|
+
|
|
24
|
+
if !parser.match_token(TokenKind::LBrace) {
|
|
25
|
+
return Statement::error(token, "Expected '{{' after 'import'".to_string());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let mut names = Vec::new();
|
|
29
|
+
while let Some(token) = parser.peek() {
|
|
30
|
+
match &token.kind {
|
|
31
|
+
TokenKind::Identifier => {
|
|
32
|
+
names.push(token.lexeme.clone());
|
|
33
|
+
parser.advance();
|
|
34
|
+
}
|
|
35
|
+
TokenKind::Comma => {
|
|
36
|
+
parser.advance();
|
|
37
|
+
}
|
|
38
|
+
TokenKind::RBrace => {
|
|
39
|
+
parser.advance();
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
_ => {
|
|
43
|
+
let message = format!(
|
|
44
|
+
"Unexpected token in import list: {:?}",
|
|
45
|
+
token.kind.clone()
|
|
46
|
+
);
|
|
47
|
+
return Statement::error(token.clone(), message);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let Some(from_token) = parser.peek_clone() else {
|
|
53
|
+
return Statement::error(token, "Expected 'from' after import list".to_string());
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
if from_token.lexeme != "from" {
|
|
57
|
+
return Statement::error(token, "Expected keyword 'from'".to_string());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
parser.advance(); // consume 'from'
|
|
61
|
+
|
|
62
|
+
let Some(source_token) = parser.peek() else {
|
|
63
|
+
return Statement::error(token, "Expected string after 'from'".to_string());
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
if source_token.kind != TokenKind::String {
|
|
67
|
+
return Statement::error(token, "Expected string after 'from'".to_string());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let source = source_token.lexeme.clone();
|
|
71
|
+
parser.advance(); // consume string
|
|
72
|
+
|
|
73
|
+
Statement {
|
|
74
|
+
kind: StatementKind::Import { names, source },
|
|
75
|
+
value: Value::Null,
|
|
76
|
+
indent: token.indent,
|
|
77
|
+
line: token.line,
|
|
78
|
+
column: token.column,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
"export" => {
|
|
83
|
+
parser.advance(); // consume 'export'
|
|
84
|
+
|
|
85
|
+
parser.advance(); // consume '{'
|
|
86
|
+
|
|
87
|
+
let names_tokens = parser.collect_until(|t| TokenKind::RBrace == t.kind);
|
|
88
|
+
let mut names: Vec<String> = Vec::new();
|
|
89
|
+
|
|
90
|
+
for token in names_tokens {
|
|
91
|
+
if token.kind == TokenKind::Identifier {
|
|
92
|
+
names.push(token.lexeme.clone());
|
|
93
|
+
} else if token.kind == TokenKind::Comma {
|
|
94
|
+
continue; // Ignore commas
|
|
95
|
+
} else if token.kind == TokenKind::RBrace {
|
|
96
|
+
break; // Stop at the closing brace
|
|
97
|
+
} else {
|
|
98
|
+
return Statement::error(token, "Unexpected token in export list".to_string());
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
Statement {
|
|
103
|
+
kind: StatementKind::Export {
|
|
104
|
+
names: names.clone(),
|
|
105
|
+
source: parser.current_module.clone(),
|
|
106
|
+
},
|
|
107
|
+
value: Value::Null,
|
|
108
|
+
indent: token.indent,
|
|
109
|
+
line: token.line,
|
|
110
|
+
column: token.column,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
"load" => {
|
|
115
|
+
parser.advance(); // consume 'load'
|
|
116
|
+
|
|
117
|
+
// Exemple : @load "preset.mydeva"
|
|
118
|
+
let Some(path_token) = parser.peek() else {
|
|
119
|
+
return Statement::error(token, "Expected string after 'load'".to_string());
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
if path_token.kind != TokenKind::String {
|
|
123
|
+
return Statement::error(token, "Expected string after 'load'".to_string());
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
let path = path_token.lexeme.clone();
|
|
127
|
+
|
|
128
|
+
parser.advance(); // consume string
|
|
129
|
+
parser.advance(); // consume 'as'
|
|
130
|
+
|
|
131
|
+
let Some(as_token) = parser.peek_clone() else {
|
|
132
|
+
return Statement::error(
|
|
133
|
+
token,
|
|
134
|
+
"Expected 'as' after path in load statement".to_string()
|
|
135
|
+
);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
if as_token.kind != TokenKind::Identifier {
|
|
139
|
+
return Statement::error(
|
|
140
|
+
token,
|
|
141
|
+
"Expected identifier after 'as' in load statement".to_string()
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let alias = as_token.lexeme.clone();
|
|
146
|
+
|
|
147
|
+
parser.advance(); // consume identifier
|
|
148
|
+
|
|
149
|
+
Statement {
|
|
150
|
+
kind: StatementKind::Load {
|
|
151
|
+
source: path,
|
|
152
|
+
alias,
|
|
153
|
+
},
|
|
154
|
+
value: Value::Null,
|
|
155
|
+
indent: token.indent,
|
|
156
|
+
line: token.line,
|
|
157
|
+
column: token.column,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_ => {
|
|
162
|
+
let message = format!("Unknown keyword after '@' : {}", keyword);
|
|
163
|
+
Statement::error(token, message)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|