@devaloop/devalang 0.0.1-alpha.1 → 0.0.1-alpha.11
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 +9 -0
- package/Cargo.toml +15 -6
- package/README.md +79 -81
- package/docs/CHANGELOG.md +213 -0
- package/docs/ROADMAP.md +11 -8
- package/docs/TODO.md +32 -29
- package/examples/bank.deva +9 -0
- package/examples/condition.deva +20 -0
- package/examples/duration.deva +9 -0
- package/examples/group.deva +12 -0
- package/examples/index.deva +12 -5
- package/examples/loop.deva +16 -0
- package/examples/samples/hat-808.wav +0 -0
- package/examples/synth.deva +14 -0
- package/examples/variables.deva +9 -0
- package/out-tsc/bin/devalang.exe +0 -0
- package/out-tsc/scripts/version/fetch.js +1 -5
- package/package.json +5 -4
- package/project-version.json +3 -3
- package/rust/cli/bank.rs +455 -0
- package/rust/cli/build.rs +114 -28
- package/rust/cli/check.rs +96 -103
- package/rust/cli/driver.rs +280 -0
- package/rust/cli/init.rs +79 -0
- package/rust/cli/install.rs +17 -0
- package/rust/cli/mod.rs +8 -1
- package/rust/cli/play.rs +193 -0
- package/rust/cli/template.rs +57 -0
- package/rust/cli/update.rs +4 -0
- package/rust/common/cdn.rs +11 -0
- package/rust/common/mod.rs +1 -0
- package/rust/config/driver.rs +76 -0
- package/rust/config/loader.rs +110 -0
- package/rust/config/mod.rs +2 -0
- package/rust/core/audio/engine.rs +242 -0
- package/rust/core/audio/evaluator.rs +31 -0
- package/rust/core/audio/interpreter/arrow_call.rs +142 -0
- package/rust/core/audio/interpreter/call.rs +70 -0
- package/rust/core/audio/interpreter/condition.rs +69 -0
- package/rust/core/audio/interpreter/driver.rs +236 -0
- package/rust/core/audio/interpreter/let_.rs +19 -0
- package/rust/core/audio/interpreter/load.rs +18 -0
- package/rust/core/audio/interpreter/loop_.rs +67 -0
- package/rust/core/audio/interpreter/mod.rs +12 -0
- package/rust/core/audio/interpreter/sleep.rs +36 -0
- package/rust/core/audio/interpreter/spawn.rs +84 -0
- package/rust/core/audio/interpreter/tempo.rs +16 -0
- package/rust/core/audio/interpreter/trigger.rs +102 -0
- package/rust/core/audio/loader/mod.rs +1 -0
- package/rust/core/audio/loader/trigger.rs +64 -0
- package/rust/core/audio/mod.rs +6 -0
- package/rust/core/audio/player.rs +54 -0
- package/rust/core/audio/renderer.rs +54 -0
- package/rust/core/builder/mod.rs +70 -27
- package/rust/core/debugger/lexer.rs +27 -0
- package/rust/core/debugger/mod.rs +13 -49
- package/rust/core/debugger/preprocessor.rs +27 -0
- package/rust/core/debugger/store.rs +25 -0
- package/rust/core/error/mod.rs +60 -0
- package/rust/core/lexer/handler/arrow.rs +31 -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/driver.rs +241 -0
- package/rust/core/lexer/handler/identifier.rs +41 -0
- package/rust/core/lexer/handler/indent.rs +52 -0
- package/rust/core/lexer/handler/mod.rs +15 -0
- package/rust/core/lexer/handler/newline.rs +23 -0
- package/rust/core/lexer/handler/number.rs +31 -0
- package/rust/core/lexer/handler/operator.rs +44 -0
- package/rust/core/lexer/handler/slash.rs +21 -0
- package/rust/core/lexer/handler/string.rs +63 -0
- package/rust/core/lexer/mod.rs +37 -319
- package/rust/core/lexer/token.rs +87 -0
- package/rust/core/mod.rs +6 -2
- package/rust/core/parser/driver.rs +339 -0
- package/rust/core/parser/handler/arrow_call.rs +151 -0
- package/rust/core/parser/handler/at.rs +162 -0
- package/rust/core/parser/handler/bank.rs +41 -0
- package/rust/core/parser/handler/condition.rs +74 -0
- package/rust/core/parser/handler/dot.rs +178 -0
- package/rust/core/parser/handler/identifier/call.rs +41 -0
- package/rust/core/parser/handler/identifier/group.rs +75 -0
- package/rust/core/parser/handler/identifier/let_.rs +133 -0
- package/rust/core/parser/handler/identifier/mod.rs +51 -0
- package/rust/core/parser/handler/identifier/sleep.rs +33 -0
- package/rust/core/parser/handler/identifier/spawn.rs +41 -0
- package/rust/core/parser/handler/identifier/synth.rs +65 -0
- package/rust/core/parser/handler/loop_.rs +72 -0
- package/rust/core/parser/handler/mod.rs +8 -0
- package/rust/core/parser/handler/tempo.rs +47 -0
- package/rust/core/parser/mod.rs +3 -200
- package/rust/core/parser/statement.rs +96 -0
- package/rust/core/preprocessor/loader.rs +308 -0
- package/rust/core/preprocessor/mod.rs +2 -24
- package/rust/core/preprocessor/module.rs +42 -56
- package/rust/core/preprocessor/processor.rs +76 -0
- package/rust/core/preprocessor/resolver/bank.rs +41 -51
- package/rust/core/preprocessor/resolver/call.rs +123 -0
- package/rust/core/preprocessor/resolver/condition.rs +92 -0
- package/rust/core/preprocessor/resolver/driver.rs +232 -0
- package/rust/core/preprocessor/resolver/group.rs +61 -0
- package/rust/core/preprocessor/resolver/let_.rs +31 -0
- package/rust/core/preprocessor/resolver/loop_.rs +76 -67
- package/rust/core/preprocessor/resolver/mod.rs +12 -111
- package/rust/core/preprocessor/resolver/spawn.rs +58 -0
- package/rust/core/preprocessor/resolver/synth.rs +50 -0
- package/rust/core/preprocessor/resolver/tempo.rs +40 -61
- package/rust/core/preprocessor/resolver/trigger.rs +90 -154
- package/rust/core/preprocessor/resolver/value.rs +78 -0
- package/rust/core/shared/bank.rs +21 -0
- package/rust/core/shared/duration.rs +9 -0
- package/rust/core/shared/mod.rs +3 -0
- package/rust/core/shared/value.rs +29 -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/path.rs +31 -0
- package/rust/core/utils/validation.rs +37 -0
- package/rust/installer/bank.rs +55 -0
- package/rust/installer/mod.rs +1 -0
- package/rust/lib.rs +162 -1
- package/rust/main.rs +104 -31
- package/rust/utils/file.rs +35 -0
- package/rust/utils/installer.rs +56 -0
- package/rust/utils/logger.rs +108 -34
- package/rust/utils/mod.rs +5 -3
- package/rust/utils/{loader.rs → spinner.rs} +2 -0
- package/rust/utils/watcher.rs +33 -0
- package/templates/minimal/.devalang +5 -0
- package/templates/minimal/README.md +202 -0
- package/templates/minimal/src/index.deva +2 -0
- package/templates/welcome/.devalang +5 -0
- package/templates/welcome/README.md +202 -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/typescript/scripts/version/fetch.ts +1 -6
- package/docs/COMMANDS.md +0 -31
- package/docs/SYNTAX.md +0 -148
- package/examples/exported.deva +0 -7
- 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/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/path.rs +0 -46
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
use crate::core::lexer::{
|
|
2
|
+
handler::{
|
|
3
|
+
arrow::handle_arrow_lexer, at::handle_at_lexer, brace::{ handle_lbrace_lexer, handle_rbrace_lexer }, colon::handle_colon_lexer, comment::handle_comment_lexer, dot::handle_dot_lexer, identifier::handle_identifier_lexer, indent::handle_indent_lexer, newline::handle_newline_lexer, number::handle_number_lexer, operator::handle_operator_lexer, slash::handle_slash_lexer, string::handle_string_lexer
|
|
4
|
+
},
|
|
5
|
+
token::{ Token, TokenKind },
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
fn advance_char<I: Iterator<Item = char>>(
|
|
9
|
+
chars: &mut std::iter::Peekable<I>,
|
|
10
|
+
line: &mut usize,
|
|
11
|
+
column: &mut usize
|
|
12
|
+
) -> Option<char> {
|
|
13
|
+
while let Some(c) = chars.next() {
|
|
14
|
+
if c == '\r' {
|
|
15
|
+
continue;
|
|
16
|
+
} else if c == '\n' {
|
|
17
|
+
// newline: don't increment column
|
|
18
|
+
} else {
|
|
19
|
+
*column += 1;
|
|
20
|
+
}
|
|
21
|
+
return Some(c);
|
|
22
|
+
}
|
|
23
|
+
None
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
pub fn handle_content_lexing(content: String) -> Result<Vec<Token>, String> {
|
|
27
|
+
let mut tokens = Vec::new();
|
|
28
|
+
|
|
29
|
+
let mut line = 1;
|
|
30
|
+
let mut column = 1;
|
|
31
|
+
|
|
32
|
+
let mut indent_stack: Vec<usize> = vec![0];
|
|
33
|
+
let mut current_indent = 0;
|
|
34
|
+
let mut at_line_start = true;
|
|
35
|
+
|
|
36
|
+
let mut chars = content.chars().peekable();
|
|
37
|
+
|
|
38
|
+
while chars.peek().is_some() {
|
|
39
|
+
if at_line_start {
|
|
40
|
+
handle_indent_lexer(
|
|
41
|
+
&mut chars,
|
|
42
|
+
&mut current_indent,
|
|
43
|
+
&mut indent_stack,
|
|
44
|
+
&mut tokens,
|
|
45
|
+
&mut line,
|
|
46
|
+
&mut column
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
at_line_start = false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let Some(ch) = advance_char(&mut chars, &mut line, &mut column) else {
|
|
53
|
+
break;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
match ch {
|
|
57
|
+
'\n' => {
|
|
58
|
+
handle_newline_lexer(
|
|
59
|
+
ch,
|
|
60
|
+
&mut chars,
|
|
61
|
+
&mut tokens,
|
|
62
|
+
&mut line,
|
|
63
|
+
&mut column,
|
|
64
|
+
&mut at_line_start,
|
|
65
|
+
&mut current_indent
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
' ' | '\t' => {
|
|
69
|
+
// Already handled by indent_lexer
|
|
70
|
+
}
|
|
71
|
+
'#' => {
|
|
72
|
+
handle_comment_lexer(
|
|
73
|
+
ch,
|
|
74
|
+
&mut chars,
|
|
75
|
+
&mut current_indent,
|
|
76
|
+
&mut indent_stack,
|
|
77
|
+
&mut tokens,
|
|
78
|
+
&mut line,
|
|
79
|
+
&mut column
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
':' => {
|
|
83
|
+
handle_colon_lexer(
|
|
84
|
+
ch,
|
|
85
|
+
&mut chars,
|
|
86
|
+
&mut current_indent,
|
|
87
|
+
&mut indent_stack,
|
|
88
|
+
&mut tokens,
|
|
89
|
+
&mut line,
|
|
90
|
+
&mut column
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
'=' | '!' | '<' | '>' => {
|
|
94
|
+
handle_operator_lexer(
|
|
95
|
+
ch,
|
|
96
|
+
&mut chars,
|
|
97
|
+
&mut current_indent,
|
|
98
|
+
&mut indent_stack,
|
|
99
|
+
&mut tokens,
|
|
100
|
+
&mut line,
|
|
101
|
+
&mut column
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
'/' => {
|
|
105
|
+
handle_slash_lexer(
|
|
106
|
+
ch,
|
|
107
|
+
&mut chars,
|
|
108
|
+
&mut current_indent,
|
|
109
|
+
&mut indent_stack,
|
|
110
|
+
&mut tokens,
|
|
111
|
+
&mut line,
|
|
112
|
+
&mut column
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
'-' => {
|
|
116
|
+
handle_arrow_lexer(
|
|
117
|
+
ch,
|
|
118
|
+
&mut chars,
|
|
119
|
+
&mut current_indent,
|
|
120
|
+
&mut indent_stack,
|
|
121
|
+
&mut tokens,
|
|
122
|
+
&mut line,
|
|
123
|
+
&mut column
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
'{' => {
|
|
127
|
+
handle_lbrace_lexer(
|
|
128
|
+
ch,
|
|
129
|
+
&mut chars,
|
|
130
|
+
&mut current_indent,
|
|
131
|
+
&mut indent_stack,
|
|
132
|
+
&mut tokens,
|
|
133
|
+
&mut line,
|
|
134
|
+
&mut column
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
'}' => {
|
|
138
|
+
handle_rbrace_lexer(
|
|
139
|
+
ch,
|
|
140
|
+
&mut chars,
|
|
141
|
+
&mut current_indent,
|
|
142
|
+
&mut indent_stack,
|
|
143
|
+
&mut tokens,
|
|
144
|
+
&mut line,
|
|
145
|
+
&mut column
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
'.' => {
|
|
149
|
+
handle_dot_lexer(
|
|
150
|
+
ch,
|
|
151
|
+
&mut chars,
|
|
152
|
+
&mut current_indent,
|
|
153
|
+
&mut indent_stack,
|
|
154
|
+
&mut tokens,
|
|
155
|
+
&mut line,
|
|
156
|
+
&mut column
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
'@' => {
|
|
160
|
+
handle_at_lexer(
|
|
161
|
+
ch,
|
|
162
|
+
&mut chars,
|
|
163
|
+
&mut current_indent,
|
|
164
|
+
&mut indent_stack,
|
|
165
|
+
&mut tokens,
|
|
166
|
+
&mut line,
|
|
167
|
+
&mut column
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
'\"' | '\'' => {
|
|
171
|
+
handle_string_lexer(
|
|
172
|
+
ch,
|
|
173
|
+
&mut chars,
|
|
174
|
+
&mut current_indent,
|
|
175
|
+
&mut indent_stack,
|
|
176
|
+
&mut tokens,
|
|
177
|
+
&mut line,
|
|
178
|
+
&mut column
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
c if c.is_ascii_digit() => {
|
|
182
|
+
handle_number_lexer(
|
|
183
|
+
c,
|
|
184
|
+
&mut chars,
|
|
185
|
+
&mut current_indent,
|
|
186
|
+
&mut indent_stack,
|
|
187
|
+
&mut tokens,
|
|
188
|
+
&mut line,
|
|
189
|
+
&mut column
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
c if c.is_ascii_alphabetic() => {
|
|
193
|
+
handle_identifier_lexer(
|
|
194
|
+
c,
|
|
195
|
+
&mut chars,
|
|
196
|
+
&mut current_indent,
|
|
197
|
+
&mut indent_stack,
|
|
198
|
+
&mut tokens,
|
|
199
|
+
&mut line,
|
|
200
|
+
&mut column
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
_ => {
|
|
204
|
+
// Ignore unknown char
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
while indent_stack.len() > 1 {
|
|
210
|
+
indent_stack.pop();
|
|
211
|
+
current_indent = *indent_stack.last().unwrap();
|
|
212
|
+
tokens.push(Token {
|
|
213
|
+
kind: TokenKind::Dedent,
|
|
214
|
+
lexeme: String::new(),
|
|
215
|
+
line,
|
|
216
|
+
column,
|
|
217
|
+
indent: current_indent,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
tokens.push(Token {
|
|
222
|
+
kind: TokenKind::EOF,
|
|
223
|
+
lexeme: String::new(),
|
|
224
|
+
line: line + 1,
|
|
225
|
+
column: 0,
|
|
226
|
+
indent: 0,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// NOTE: Debug only
|
|
230
|
+
// for token in &tokens {
|
|
231
|
+
// println!(
|
|
232
|
+
// "{:?} @ line {}, col {}, indent {}",
|
|
233
|
+
// token.kind,
|
|
234
|
+
// token.line,
|
|
235
|
+
// token.column,
|
|
236
|
+
// token.indent
|
|
237
|
+
// );
|
|
238
|
+
// }
|
|
239
|
+
|
|
240
|
+
Ok(tokens)
|
|
241
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
use crate::core::lexer::token::{ Token, TokenKind };
|
|
2
|
+
|
|
3
|
+
pub fn handle_identifier_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 ident = char.to_string();
|
|
13
|
+
|
|
14
|
+
while let Some(&c) = chars.peek() {
|
|
15
|
+
if c.is_ascii_alphanumeric() || c == '_' {
|
|
16
|
+
ident.push(c);
|
|
17
|
+
chars.next();
|
|
18
|
+
*column += 1;
|
|
19
|
+
} else {
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let kind = match ident.as_str() {
|
|
25
|
+
"if" => TokenKind::If,
|
|
26
|
+
"else" => TokenKind::Else,
|
|
27
|
+
"bank" => TokenKind::Bank,
|
|
28
|
+
"bpm" => TokenKind::Tempo,
|
|
29
|
+
"loop" => TokenKind::Loop,
|
|
30
|
+
"synth" => TokenKind::Synth,
|
|
31
|
+
_ => TokenKind::Identifier,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
tokens.push(Token {
|
|
35
|
+
kind: kind.clone(),
|
|
36
|
+
lexeme: ident,
|
|
37
|
+
line: *line,
|
|
38
|
+
column: *column,
|
|
39
|
+
indent: *current_indent,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
use crate::core::lexer::token::{ Token, TokenKind };
|
|
2
|
+
|
|
3
|
+
pub fn handle_indent_lexer(
|
|
4
|
+
chars: &mut std::iter::Peekable<std::str::Chars>,
|
|
5
|
+
current_indent: &mut usize,
|
|
6
|
+
indent_stack: &mut Vec<usize>,
|
|
7
|
+
tokens: &mut Vec<Token>,
|
|
8
|
+
line: &mut usize,
|
|
9
|
+
column: &mut usize
|
|
10
|
+
) {
|
|
11
|
+
*current_indent = 0;
|
|
12
|
+
let mut col = *column;
|
|
13
|
+
|
|
14
|
+
while let Some(&c) = chars.peek() {
|
|
15
|
+
if c == ' ' {
|
|
16
|
+
*current_indent += 1;
|
|
17
|
+
chars.next();
|
|
18
|
+
col += 1;
|
|
19
|
+
} else if c == '\t' {
|
|
20
|
+
*current_indent += 4;
|
|
21
|
+
chars.next();
|
|
22
|
+
col += 4;
|
|
23
|
+
} else {
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
*column = col;
|
|
29
|
+
|
|
30
|
+
let last_indent = *indent_stack.last().unwrap();
|
|
31
|
+
if *current_indent > last_indent {
|
|
32
|
+
indent_stack.push(*current_indent);
|
|
33
|
+
tokens.push(Token {
|
|
34
|
+
kind: TokenKind::Indent,
|
|
35
|
+
lexeme: String::new(),
|
|
36
|
+
line: *line,
|
|
37
|
+
column: *column,
|
|
38
|
+
indent: *current_indent,
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
while *current_indent < *indent_stack.last().unwrap() {
|
|
42
|
+
indent_stack.pop();
|
|
43
|
+
tokens.push(Token {
|
|
44
|
+
kind: TokenKind::Dedent,
|
|
45
|
+
lexeme: String::new(),
|
|
46
|
+
line: *line,
|
|
47
|
+
column: *column,
|
|
48
|
+
indent: *current_indent,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
pub mod driver;
|
|
2
|
+
|
|
3
|
+
pub mod at;
|
|
4
|
+
pub mod brace;
|
|
5
|
+
pub mod colon;
|
|
6
|
+
pub mod comment;
|
|
7
|
+
pub mod dot;
|
|
8
|
+
pub mod operator;
|
|
9
|
+
pub mod identifier;
|
|
10
|
+
pub mod newline;
|
|
11
|
+
pub mod number;
|
|
12
|
+
pub mod indent;
|
|
13
|
+
pub mod string;
|
|
14
|
+
pub mod arrow;
|
|
15
|
+
pub mod slash;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
use crate::core::lexer::token::{ Token, TokenKind };
|
|
2
|
+
|
|
3
|
+
pub fn handle_newline_lexer(
|
|
4
|
+
ch: char,
|
|
5
|
+
chars: &mut std::iter::Peekable<std::str::Chars>,
|
|
6
|
+
tokens: &mut Vec<Token>,
|
|
7
|
+
line: &mut usize,
|
|
8
|
+
column: &mut usize,
|
|
9
|
+
at_line_start: &mut bool,
|
|
10
|
+
current_indent: &mut usize
|
|
11
|
+
) {
|
|
12
|
+
tokens.push(Token {
|
|
13
|
+
kind: TokenKind::Newline,
|
|
14
|
+
lexeme: ch.to_string(),
|
|
15
|
+
line: *line,
|
|
16
|
+
column: 0,
|
|
17
|
+
indent: *current_indent,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
*line += 1;
|
|
21
|
+
*column = 1;
|
|
22
|
+
*at_line_start = true;
|
|
23
|
+
}
|
|
@@ -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,44 @@
|
|
|
1
|
+
use crate::core::lexer::token::{ Token, TokenKind };
|
|
2
|
+
|
|
3
|
+
pub fn handle_operator_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 next = chars.peek().copied();
|
|
13
|
+
|
|
14
|
+
let (kind, len) = match (ch, next) {
|
|
15
|
+
('=', Some('=')) => (TokenKind::DoubleEquals, 2),
|
|
16
|
+
('!', Some('=')) => (TokenKind::NotEquals, 2),
|
|
17
|
+
('>', Some('=')) => (TokenKind::GreaterEqual, 2),
|
|
18
|
+
('<', Some('=')) => (TokenKind::LessEqual, 2),
|
|
19
|
+
('=', _) => (TokenKind::Equals, 1),
|
|
20
|
+
('>', _) => (TokenKind::Greater, 1),
|
|
21
|
+
('<', _) => (TokenKind::Less, 1),
|
|
22
|
+
_ => {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if len == 2 {
|
|
28
|
+
chars.next(); // consume second char
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
tokens.push(Token {
|
|
32
|
+
kind,
|
|
33
|
+
lexeme: if len == 2 {
|
|
34
|
+
format!("{}{}", ch, next.unwrap())
|
|
35
|
+
} else {
|
|
36
|
+
ch.to_string()
|
|
37
|
+
},
|
|
38
|
+
line: *line,
|
|
39
|
+
column: *column,
|
|
40
|
+
indent: *current_indent,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
*column += len;
|
|
44
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
use crate::core::lexer::token::{ Token, TokenKind };
|
|
2
|
+
|
|
3
|
+
pub fn handle_slash_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 slash = char.to_string();
|
|
13
|
+
|
|
14
|
+
tokens.push(Token {
|
|
15
|
+
kind: TokenKind::Slash,
|
|
16
|
+
lexeme: slash,
|
|
17
|
+
line: *line,
|
|
18
|
+
column: *column,
|
|
19
|
+
indent: *current_indent,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
let start_column = *column;
|
|
15
|
+
let start_line = *line;
|
|
16
|
+
|
|
17
|
+
*column += 1;
|
|
18
|
+
|
|
19
|
+
let mut string_content = String::new();
|
|
20
|
+
|
|
21
|
+
while let Some(&next_ch) = chars.peek() {
|
|
22
|
+
if next_ch == quote_char {
|
|
23
|
+
chars.next();
|
|
24
|
+
*column += 1;
|
|
25
|
+
break;
|
|
26
|
+
} else if next_ch == '\\' {
|
|
27
|
+
chars.next();
|
|
28
|
+
*column += 1;
|
|
29
|
+
|
|
30
|
+
if let Some(escaped) = chars.next() {
|
|
31
|
+
match escaped {
|
|
32
|
+
'n' => string_content.push('\n'),
|
|
33
|
+
't' => string_content.push('\t'),
|
|
34
|
+
'\\' => string_content.push('\\'),
|
|
35
|
+
'"' => string_content.push('"'),
|
|
36
|
+
'\'' => string_content.push('\''),
|
|
37
|
+
other => {
|
|
38
|
+
string_content.push('\\');
|
|
39
|
+
string_content.push(other);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
*column += 1;
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
chars.next();
|
|
46
|
+
if next_ch == '\n' {
|
|
47
|
+
*line += 1;
|
|
48
|
+
*column = 1;
|
|
49
|
+
} else {
|
|
50
|
+
*column += 1;
|
|
51
|
+
}
|
|
52
|
+
string_content.push(next_ch);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
tokens.push(Token {
|
|
57
|
+
kind: TokenKind::String,
|
|
58
|
+
lexeme: string_content,
|
|
59
|
+
indent: *current_indent,
|
|
60
|
+
line: start_line,
|
|
61
|
+
column: start_column,
|
|
62
|
+
});
|
|
63
|
+
}
|