Skip to content

Commit df83ab7

Browse files
author
梶塚太智
committed
Add string escape
1 parent 41c84cb commit df83ab7

File tree

1 file changed

+99
-19
lines changed

1 file changed

+99
-19
lines changed

src/main.rs

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -244,44 +244,65 @@ impl Executor {
244244

245245
let mut syntax = Vec::new(); // Token string
246246
let mut buffer = String::new(); // Temporary storage
247-
let mut in_brackets = 0; // String's nest structure
248-
let mut in_parentheses = 0; // List's nest structure
249-
let mut in_hash = false; // Is it Comment
247+
let mut brackets = 0; // String's nest structure
248+
let mut parentheses = 0; // List's nest structure
249+
let mut hash = false; // Is it Comment
250+
let mut escape = false; // Flag to indicate next character is escaped
250251

251252
for c in code.chars() {
252253
match c {
253-
'(' => {
254-
in_brackets += 1;
254+
'\\' if !escape => {
255+
escape = true;
256+
}
257+
'(' if !hash && !escape => {
258+
brackets += 1;
255259
buffer.push('(');
256260
}
257-
')' => {
258-
in_brackets -= 1;
261+
')' if !hash && !escape => {
262+
brackets -= 1;
259263
buffer.push(')');
260264
}
261-
'#' if !in_hash => {
262-
in_hash = true;
265+
'#' if !hash && !escape => {
266+
hash = true;
263267
buffer.push('#');
264268
}
265-
'#' if in_hash => {
266-
in_hash = false;
269+
'#' if hash && !escape => {
270+
hash = false;
267271
buffer.push('#');
268272
}
269-
'[' if in_brackets == 0 => {
270-
in_parentheses += 1;
273+
'[' if !hash && brackets == 0 && !escape => {
274+
parentheses += 1;
271275
buffer.push('[');
272276
}
273-
']' if in_brackets == 0 => {
274-
in_parentheses -= 1;
277+
']' if !hash && brackets == 0 && !escape => {
278+
parentheses -= 1;
275279
buffer.push(']');
276280
}
277-
' ' if !in_hash && in_parentheses == 0 && in_brackets == 0 => {
281+
' ' if !hash && parentheses == 0 && brackets == 0 && !escape => {
278282
if !buffer.is_empty() {
279283
syntax.push(buffer.clone());
280284
buffer.clear();
281285
}
282286
}
283287
_ => {
284-
buffer.push(c);
288+
if parentheses == 0 && brackets == 0 && !hash {
289+
if escape {
290+
buffer.push(match c {
291+
'n' => '\n',
292+
't' => '\t',
293+
'r' => '\r',
294+
_ => c,
295+
})
296+
} else {
297+
buffer.push(c);
298+
}
299+
} else {
300+
if escape {
301+
buffer.push('\\');
302+
}
303+
buffer.push(c);
304+
}
305+
escape = false; // Reset escape flag for non-escape characters
285306
}
286307
}
287308
}
@@ -314,8 +335,67 @@ impl Executor {
314335
self.stack.push(Type::Bool(token.parse().unwrap_or(true)));
315336
} else if chars[0] == '(' && chars[chars.len() - 1] == ')' {
316337
// Push string value on the stack
317-
self.stack
318-
.push(Type::String(token[1..token.len() - 1].to_string()));
338+
let string = {
339+
let mut buffer = String::new(); // Temporary storage
340+
let mut brackets = 0; // String's nest structure
341+
let mut parentheses = 0; // List's nest structure
342+
let mut hash = false; // Is it Comment
343+
let mut escape = false; // Flag to indicate next character is escaped
344+
345+
for c in token[1..token.len() - 1].to_string().chars() {
346+
match c {
347+
'\\' if !escape => {
348+
escape = true;
349+
}
350+
'(' if !hash && !escape => {
351+
brackets += 1;
352+
buffer.push('(');
353+
}
354+
')' if !hash && !escape => {
355+
brackets -= 1;
356+
buffer.push(')');
357+
}
358+
'#' if !hash && !escape => {
359+
hash = true;
360+
buffer.push('#');
361+
}
362+
'#' if hash && !escape => {
363+
hash = false;
364+
buffer.push('#');
365+
}
366+
'[' if !hash && brackets == 0 && !escape => {
367+
parentheses += 1;
368+
buffer.push('[');
369+
}
370+
']' if !hash && brackets == 0 && !escape => {
371+
parentheses -= 1;
372+
buffer.push(']');
373+
}
374+
_ => {
375+
if parentheses == 0 && brackets == 0 && !hash {
376+
if escape {
377+
buffer.push(match c {
378+
'n' => '\n',
379+
't' => '\t',
380+
'r' => '\r',
381+
_ => c,
382+
})
383+
} else {
384+
buffer.push(c);
385+
}
386+
} else {
387+
if escape {
388+
buffer.push('\\');
389+
}
390+
buffer.push(c);
391+
}
392+
escape = false; // Reset escape flag for non-escape characters
393+
}
394+
}
395+
}
396+
buffer
397+
};
398+
self.stack.push(Type::String(string));
319399
} else if chars[0] == '[' && chars[chars.len() - 1] == ']' {
320400
// Push list value on the stack
321401
let old_len = self.stack.len(); // length of old stack

0 commit comments

Comments
 (0)